public Object convert(CSSValue value, CSSEngine engine, Object context) throws Exception {
   if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
     CSSPrimitiveValue primitiveValue = (CSSPrimitiveValue) value;
     if ("true".equals(primitiveValue.getStringValue())) return Boolean.TRUE;
   }
   return Boolean.FALSE;
 }
Пример #2
0
  public String getCssText() {
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < NUMBER_OF_STYLE; i++) {
      // we don't return the format in css as the format
      // is a complex object which can't be represented as
      // css string.
      if (i == IStyle.STYLE_DATA_FORMAT) continue;
      CSSValue value = getProperty(i);
      if (value != null) {
        sb.append(engine.getPropertyName(i));
        sb.append(": ");
        short type = value.getCssValueType();
        switch (type) {
          case CSSValue.CSS_PRIMITIVE_VALUE:
            {
              CSSPrimitiveValue pv = (CSSPrimitiveValue) value;
              short unitType = pv.getPrimitiveType();
              switch (unitType) {
                case CSSPrimitiveValue.CSS_STRING:
                  sb.append("'");
                  sb.append(pv.getStringValue());
                  sb.append("'");
                  break;
                case CSSPrimitiveValue.CSS_URI:
                  sb.append("url('");
                  sb.append(pv.getStringValue());
                  sb.append("')");
                  break;
                default:
                  sb.append(value.getCssText());
              }
            }
            break;
          default:
            sb.append(value.getCssText());
        }
        sb.append("; ");
      }
    }
    if (sb.length() > 2) {
      sb.setLength(sb.length() - 2);
    }
    return sb.toString();
  }
 /**
  * Return the key of the CSSPrimitiveValue <code>value</code> which is used to cache Resource into
  * {@link IResourcesRegistry}.
  *
  * @param value
  * @return
  */
 public static String getCSSPrimitiveValueKey(CSSPrimitiveValue value) {
   switch (value.getPrimitiveType()) {
     case CSSPrimitiveValue.CSS_IDENT:
     case CSSPrimitiveValue.CSS_URI:
       String s = value.getStringValue();
       // Test if s is Color Name
       if (CSS2ColorHelper.isColorName(s)) {
         RGBColor rgbColor = CSS2ColorHelper.getRGBColor(s);
         if (rgbColor != null) {
           return getCSSRGBColorKey(rgbColor);
         }
       }
       return value.getStringValue();
     case CSSPrimitiveValue.CSS_RGBCOLOR:
       RGBColor rgbColor = value.getRGBColorValue();
       return getCSSRGBColorKey(rgbColor);
   }
   return null;
 }
  public void testInsertRule() {
    final String RULE =
        "@font-face { font-family: \"Swiss 721\"; src: url(swiss721.pfr); /* The expanded Swiss 721 */ font-stretch: expanded; }";
    CSSStyleSheet sheet = getStyleSheet();
    assertEquals(0, sheet.insertRule(RULE, 0));

    CSSRuleList ruleList = sheet.getCssRules();
    CSSRule rule = ruleList.item(0);
    assertTrue(rule instanceof CSSFontFaceRule);

    CSSStyleDeclaration declaration = ((CSSFontFaceRule) rule).getStyle();
    assertEquals(3, declaration.getLength());

    CSSValue value;
    CSSPrimitiveValue primitiveValue;

    value = declaration.getPropertyCSSValue("font-family");
    assertTrue(value instanceof CSSPrimitiveValue);

    primitiveValue = (CSSPrimitiveValue) value;
    assertEquals(CSSPrimitiveValue.CSS_STRING, primitiveValue.getPrimitiveType());
    assertEquals("Swiss 721", primitiveValue.getStringValue());

    value = declaration.getPropertyCSSValue("src");
    assertTrue(value instanceof CSSPrimitiveValue);

    primitiveValue = (CSSPrimitiveValue) value;
    assertEquals(CSSPrimitiveValue.CSS_URI, primitiveValue.getPrimitiveType());
    assertEquals("swiss721.pfr", primitiveValue.getStringValue());

    value = declaration.getPropertyCSSValue("font-stretch");
    assertTrue(value instanceof CSSPrimitiveValue);

    primitiveValue = (CSSPrimitiveValue) value;
    assertEquals(CSSPrimitiveValue.CSS_IDENT, primitiveValue.getPrimitiveType());
    assertEquals("expanded", primitiveValue.getStringValue());
  }