public void setCssText(String cssText) throws DOMException {
    if (_parentStyleSheet != null && _parentStyleSheet.isReadOnly()) {
      throw new DOMExceptionImpl(
          DOMException.NO_MODIFICATION_ALLOWED_ERR, DOMExceptionImpl.READ_ONLY_STYLE_SHEET);
    }

    try {
      InputSource is = new InputSource(new StringReader(cssText));
      CSSOMParser parser = new CSSOMParser();
      CSSRule r = parser.parseRule(is);

      // The rule must be a page rule
      if (r.getType() == CSSRule.PAGE_RULE) {
        _ident = ((CSSPageRuleImpl) r)._ident;
        _pseudoPage = ((CSSPageRuleImpl) r)._pseudoPage;
        _style = ((CSSPageRuleImpl) r)._style;
      } else {
        throw new DOMExceptionImpl(
            DOMException.INVALID_MODIFICATION_ERR, DOMExceptionImpl.EXPECTING_PAGE_RULE);
      }
    } catch (CSSException e) {
      throw new DOMExceptionImpl(
          DOMException.SYNTAX_ERR, DOMExceptionImpl.SYNTAX_ERROR, e.getMessage());
    } catch (IOException e) {
      throw new DOMExceptionImpl(
          DOMException.SYNTAX_ERR, DOMExceptionImpl.SYNTAX_ERROR, e.getMessage());
    }
  }
  public void setCssText(final String cssText) throws DOMException {
    final CSSStyleSheetImpl parentStyleSheet = getParentStyleSheetImpl();
    if (parentStyleSheet != null && parentStyleSheet.isReadOnly()) {
      throw new DOMExceptionImpl(
          DOMException.NO_MODIFICATION_ALLOWED_ERR, DOMExceptionImpl.READ_ONLY_STYLE_SHEET);
    }

    try {
      final InputSource is = new InputSource(new StringReader(cssText));
      final CSSOMParser parser = new CSSOMParser();
      final CSSRule r = parser.parseRule(is);

      // The rule must be a media rule
      if (r.getType() == CSSRule.MEDIA_RULE) {
        media_ = ((CSSMediaRuleImpl) r).media_;
        cssRules_ = ((CSSMediaRuleImpl) r).cssRules_;
      } else {
        throw new DOMExceptionImpl(
            DOMException.INVALID_MODIFICATION_ERR, DOMExceptionImpl.EXPECTING_MEDIA_RULE);
      }
    } catch (final CSSException e) {
      throw new DOMExceptionImpl(
          DOMException.SYNTAX_ERR, DOMExceptionImpl.SYNTAX_ERROR, e.getMessage());
    } catch (final IOException e) {
      throw new DOMExceptionImpl(
          DOMException.SYNTAX_ERR, DOMExceptionImpl.SYNTAX_ERROR, e.getMessage());
    }
  }
 /**
  * Show style sheet.
  *
  * @param styleSheet the style sheet
  */
 private void showStyleSheet(CSSStyleSheet styleSheet) {
   StringWriter stringWriter = new StringWriter();
   PrintWriter writer = new PrintWriter(stringWriter);
   writer.println("<DL>");
   CSSRuleList ruleList = styleSheet.getCssRules();
   int length = ruleList.getLength();
   for (int i = 0; i < length; i++) {
     CSSRule rule = ruleList.item(i);
     writer.println(
         "<DT><strong>Rule: type="
             + rule.getType()
             + ",class="
             + rule.getClass().getName()
             + "</strong></DT>");
     writer.println("<DD>");
     this.writeRuleInfo(writer, rule);
     writer.println("</DD>");
   }
   writer.println("</DL>");
   writer.flush();
   String html = stringWriter.toString();
   HtmlRendererContext rcontext =
       new SimpleHtmlRendererContext(this.cssOutput, (UserAgentContext) null);
   this.cssOutput.setHtml(html, "about:css", rcontext);
 }
  public void _testInsertText1() throws IOException {
    ICSSModel model = getModel();
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    structuredDocument.set(
        FileUtil.createString(
            "src/org/eclipse/wst/css/core/tests/testfiles", "CSSFontFaceRuleTest.css"));

    CSSStyleSheet sheet = (CSSStyleSheet) model.getDocument();
    CSSRuleList ruleList = sheet.getCssRules();
    assertEquals(3, ruleList.getLength());

    CSSRule rule;
    CSSStyleDeclaration declaration;
    CSSValue value;
    CSSValueList valueList;

    // rule 1

    rule = ruleList.item(0);
    assertEquals(CSSRule.FONT_FACE_RULE, rule.getType());
    assertTrue(rule instanceof CSSFontFaceRule);

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

    value = declaration.getPropertyCSSValue("font-family");
    checkPrimitiveString(value, new PrimitiveString(CSSPrimitiveValue.CSS_STRING, "Swiss 721"));

    value = declaration.getPropertyCSSValue("src");
    checkPrimitiveString(value, new PrimitiveString(CSSPrimitiveValue.CSS_URI, "swiss721blk.pfr"));

    value = declaration.getPropertyCSSValue("font-style");
    assertTrue(value instanceof CSSValueList);

    valueList = (CSSValueList) value;
    assertEquals(3, valueList.getLength());

    checkPrimitiveString(
        valueList.item(0), new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "normal"));
    checkPrimitiveString(valueList.item(1), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveString(
        valueList.item(2), new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "italic"));

    value = declaration.getPropertyCSSValue("font-weight");
    assertTrue(value instanceof CSSValueList);

    valueList = (CSSValueList) value;
    assertEquals(3, valueList.getLength());

    checkPrimitiveNumber(
        valueList.item(0), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 800));
    checkPrimitiveString(valueList.item(1), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveNumber(
        valueList.item(2), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 900));
  }
  /** {@inheritDoc} */
  @Override
  public String getCssText(final CSSFormat format) {
    final StringBuilder sb = new StringBuilder("@media ");

    sb.append(((MediaListImpl) getMedia()).getMediaText(format));
    sb.append(" {");
    for (int i = 0; i < getCssRules().getLength(); i++) {
      final CSSRule rule = getCssRules().item(i);
      sb.append(rule.getCssText()).append(" ");
    }
    sb.append("}");
    return sb.toString();
  }
 /**
  * Creates a CSSRule according to the specified rule type.
  *
  * @param stylesheet the Stylesheet of this rule
  * @param rule the wrapped rule
  * @return a CSSRule subclass according to the rule type
  */
 public static CSSRule create(final CSSStyleSheet stylesheet, final org.w3c.dom.css.CSSRule rule) {
   switch (rule.getType()) {
     case STYLE_RULE:
       return new CSSStyleRule(stylesheet, (org.w3c.dom.css.CSSStyleRule) rule);
     case IMPORT_RULE:
       return new CSSImportRule(stylesheet, (org.w3c.dom.css.CSSImportRule) rule);
     case CHARSET_RULE:
       return new CSSCharsetRule(stylesheet, (org.w3c.dom.css.CSSCharsetRule) rule);
     case MEDIA_RULE:
       return new CSSMediaRule(stylesheet, (org.w3c.dom.css.CSSMediaRule) rule);
     case FONT_FACE_RULE:
       return new CSSFontFaceRule(stylesheet, (org.w3c.dom.css.CSSFontFaceRule) rule);
     default:
       throw new UnsupportedOperationException(
           "CSSRule " + rule.getClass().getName() + " is not yet supported:" + rule.getCssText());
   }
 }
 /**
  * If this rule is contained inside another rule (e.g. a style rule inside an @media block), this
  * is the containing rule. If this rule is not nested inside any other rules, this returns <code>
  * null</code>.
  *
  * @return the parent rule
  */
 @JsxGetter({@WebBrowser(FF), @WebBrowser(value = IE, minVersion = 11)})
 public CSSRule getParentRule() {
   final org.w3c.dom.css.CSSRule parentRule = rule_.getParentRule();
   if (parentRule != null) {
     return CSSRule.create(stylesheet_, parentRule);
   }
   return null;
 }
Beispiel #8
0
 private void parseFile(String filename) throws IOException {
   Reader r = new FileReader(filename);
   InputSource is = new InputSource(r);
   CSSOMParser parser = new CSSOMParser();
   CSSStyleSheet styleSheet = parser.parseStyleSheet(is);
   CSSRuleList list = styleSheet.getCssRules();
   for (int i = 0; i < list.getLength(); i++) {
     CSSRule rule = list.item(i);
     switch (rule.getType()) {
       case CSSRule.UNKNOWN_RULE:
         throw new IOException("Unknown rule in css file");
       case CSSRule.STYLE_RULE:
         //				CSSStyleRule srule = (CSSStyleRule) rule;
         //				System.out.println(srule.getSelectorText());
         break;
       default:
         System.out.println(rule);
         break;
     }
   }
 }
  public void _testInsertText3() throws IOException {
    ICSSModel model = getModel();
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    structuredDocument.set(
        FileUtil.createString(
            "src/org/eclipse/wst/css/core/tests/testfiles", "CSSFontFaceRuleTest.css"));

    CSSStyleSheet sheet = (CSSStyleSheet) model.getDocument();
    CSSRuleList ruleList = sheet.getCssRules();
    assertEquals(3, ruleList.getLength());

    CSSRule rule;
    CSSStyleDeclaration declaration;
    CSSValue value;
    CSSValueList valueList;

    // rule 3

    rule = ruleList.item(2);
    assertEquals(CSSRule.FONT_FACE_RULE, rule.getType());
    assertTrue(rule instanceof CSSFontFaceRule);

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

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

    valueList = (CSSValueList) value;
    assertEquals(4, valueList.getLength());

    checkPrimitiveString(
        valueList.item(0), new PrimitiveString(ICSSPrimitiveValue.CSS_LOCAL, "Alabama Italic"));
    checkPrimitiveString(valueList.item(1), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveString(
        valueList.item(2),
        new PrimitiveString(CSSPrimitiveValue.CSS_URI, "http://www.fonts.org/A/alabama-italic"));
    checkPrimitiveString(
        valueList.item(3), new PrimitiveString(ICSSPrimitiveValue.CSS_FORMAT, "truetype"));

    value = declaration.getPropertyCSSValue("panose-1");
    assertTrue(value instanceof CSSValueList);

    valueList = (CSSValueList) value;
    assertEquals(10, valueList.getLength());

    checkPrimitiveNumber(valueList.item(0), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 2));
    checkPrimitiveNumber(valueList.item(1), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 4));
    checkPrimitiveNumber(valueList.item(2), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 5));
    checkPrimitiveNumber(valueList.item(3), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 2));
    checkPrimitiveNumber(valueList.item(4), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 5));
    checkPrimitiveNumber(valueList.item(5), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 4));
    checkPrimitiveNumber(valueList.item(6), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 5));
    checkPrimitiveNumber(valueList.item(7), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 9));
    checkPrimitiveNumber(valueList.item(8), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 3));
    checkPrimitiveNumber(valueList.item(9), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 3));

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

    valueList = (CSSValueList) value;
    assertEquals(3, valueList.getLength());

    checkPrimitiveString(
        valueList.item(0), new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "Alabama"));
    checkPrimitiveString(valueList.item(1), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveString(
        valueList.item(2), new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "serif"));

    value = declaration.getPropertyCSSValue("font-weight");
    assertTrue(value instanceof CSSValueList);

    valueList = (CSSValueList) value;
    assertEquals(5, valueList.getLength());

    checkPrimitiveNumber(
        valueList.item(0), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 300));
    checkPrimitiveString(valueList.item(1), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveNumber(
        valueList.item(2), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 400));
    checkPrimitiveString(valueList.item(3), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveNumber(
        valueList.item(4), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 500));

    value = declaration.getPropertyCSSValue("font-style");
    assertTrue(value instanceof CSSValueList);

    valueList = (CSSValueList) value;
    assertEquals(3, valueList.getLength());

    checkPrimitiveString(
        valueList.item(0), new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "italic"));
    checkPrimitiveString(valueList.item(1), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveString(
        valueList.item(2), new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "oblique"));
  }
  public void _testInsertText2() throws IOException {
    ICSSModel model = getModel();
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    structuredDocument.set(
        FileUtil.createString(
            "src/org/eclipse/wst/css/core/tests/testfiles", "CSSFontFaceRuleTest.css"));

    CSSStyleSheet sheet = (CSSStyleSheet) model.getDocument();
    CSSRuleList ruleList = sheet.getCssRules();
    assertEquals(3, ruleList.getLength());

    CSSRule rule;
    CSSStyleDeclaration declaration;
    CSSValue value;
    CSSValueList valueList;

    // rule 2

    rule = ruleList.item(1);
    assertEquals(CSSRule.FONT_FACE_RULE, rule.getType());
    assertTrue(rule instanceof CSSFontFaceRule);

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

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

    valueList = (CSSValueList) value;
    assertEquals(9, valueList.getLength());

    checkPrimitiveString(
        valueList.item(0), new PrimitiveString(ICSSPrimitiveValue.CSS_LOCAL, "Palatino"));
    checkPrimitiveString(valueList.item(1), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveString(
        valueList.item(2), new PrimitiveString(ICSSPrimitiveValue.CSS_LOCAL, "Times New Roman"));
    checkPrimitiveString(valueList.item(3), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveString(
        valueList.item(4), new PrimitiveString(ICSSPrimitiveValue.CSS_LOCAL, "New York"));
    checkPrimitiveString(valueList.item(5), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveString(
        valueList.item(6), new PrimitiveString(ICSSPrimitiveValue.CSS_LOCAL, "Utopia"));
    checkPrimitiveString(valueList.item(7), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveString(
        valueList.item(8),
        new PrimitiveString(CSSPrimitiveValue.CSS_URI, "http://somewhere/free/font"));

    value = declaration.getPropertyCSSValue("font-family");
    checkPrimitiveString(value, new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "serif"));

    value = declaration.getPropertyCSSValue("font-weight");
    assertTrue(value instanceof CSSValueList);

    valueList = (CSSValueList) value;
    assertEquals(9, valueList.getLength());

    checkPrimitiveNumber(
        valueList.item(0), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 100));
    checkPrimitiveString(valueList.item(1), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveNumber(
        valueList.item(2), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 200));
    checkPrimitiveString(valueList.item(3), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveNumber(
        valueList.item(4), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 300));
    checkPrimitiveString(valueList.item(5), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveNumber(
        valueList.item(6), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 400));
    checkPrimitiveString(valueList.item(7), new PrimitiveString(ICSSPrimitiveValue.CSS_COMMA, ","));
    checkPrimitiveNumber(
        valueList.item(8), new PrimitiveNumber(ICSSPrimitiveValue.CSS_INTEGER, 500));

    value = declaration.getPropertyCSSValue("font-style");
    checkPrimitiveString(value, new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "normal"));

    value = declaration.getPropertyCSSValue("font-variant");
    checkPrimitiveString(value, new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "normal"));

    value = declaration.getPropertyCSSValue("font-size");
    checkPrimitiveString(value, new PrimitiveString(CSSPrimitiveValue.CSS_IDENT, "all"));
  }
 /**
  * Sets the parsable textual representation of the rule.
  *
  * @param cssText the parsable textual representation of the rule
  */
 @JsxSetter({@WebBrowser(FF), @WebBrowser(value = IE, minVersion = 11)})
 public void setCssText(final String cssText) {
   rule_.setCssText(cssText);
 }
 /**
  * Returns the parsable textual representation of the rule. This reflects the current state of the
  * rule and not its initial value.
  *
  * @return the parsable textual representation of the rule.
  */
 @JsxGetter({@WebBrowser(FF), @WebBrowser(value = IE, minVersion = 11)})
 public String getCssText() {
   return rule_.getCssText();
 }
 /**
  * Returns the type of the rule.
  *
  * @return the type of the rule.
  */
 @JsxGetter({@WebBrowser(FF), @WebBrowser(value = IE, minVersion = 11)})
 public short getType() {
   return rule_.getType();
 }