/**
   * Gets the validation report from the validator.
   *
   * @param source the source text
   * @param path the source path
   * @return the report
   */
  private static String getReport(String source, URI path) {
    StyleSheetParser parser = new StyleSheetParser();
    ApplContext ac = new ApplContext("en"); // $NON-NLS-1$
    ac.setProfile(APTANA_PROFILE);
    try {
      parser.parseStyleElement(
          ac, new ByteArrayInputStream(source.getBytes(IOUtil.UTF_8)), null, null, path.toURL(), 0);
    } catch (MalformedURLException e) {
      IdeLog.logError(
          CSSPlugin.getDefault(),
          MessageFormat.format(Messages.CSSValidator_ERR_InvalidPath, path),
          e);
    } catch (UnsupportedEncodingException e) {
      IdeLog.logError(CSSPlugin.getDefault(), e);
    }

    StyleSheet stylesheet = parser.getStyleSheet();
    stylesheet.findConflicts(ac);
    StyleReport report =
        StyleReportFactory.getStyleReport(
            ac, "Title", stylesheet, "soap12", 2); // $NON-NLS-1$ //$NON-NLS-2$
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    report.print(new PrintWriter(out));
    return out.toString().replaceAll("m:", ""); // $NON-NLS-1$ //$NON-NLS-2$
  }
 public static CssIdent getAllowedIdent(ApplContext ac, CssIdent ident) {
   if (none.equals(ident)) {
     return none;
   }
   if (all.equals(ident)) {
     return all;
   }
   if (PropertiesLoader.getProfile(ac.getPropertyKey()).getProperty(ident.toString()) == null) {
     ac.getFrame().addWarning("noexproperty", ident.toString());
   }
   return ident;
 }
  /**
   * Create a new CssMaxWidthATSC
   *
   * @param expression The expression for this property
   * @exception InvalidParamException Values are incorrect
   */
  public CssMaxWidthATSC(ApplContext ac, CssExpression expression, boolean check)
      throws InvalidParamException {

    if (check && expression.getCount() > 1) {
      throw new InvalidParamException("unrecognize", ac);
    }

    CssValue val = expression.getValue();

    setByUser();

    ac.getFrame().addWarning("atsc", val.toString());

    if (val.equals(inherit)) {
      value = inherit;
    } else if (val instanceof CssLength || val instanceof CssPercentage) {
      float f = ((Float) val.get()).floatValue();
      if (f < 0) {
        throw new InvalidParamException("negative-value", val.toString(), ac);
      }
      value = val;
    } else if (val.equals(none)) {
      value = none;
    } else if (val instanceof CssNumber) {
      value = ((CssNumber) val).getLength();
    } else {
      throw new InvalidParamException("value", expression.getValue(), getPropertyName(), ac);
    }

    expression.next();
  }
 private void checkExpression(
     ApplContext ac, ArrayList<CssValue> curval, ArrayList<CssIdent> values, boolean check) {
   CssIdent val;
   if (values.size() > 1) {
     // create a value out of that. We could even create
     // a CssString for the output (TODO ?)
     StringBuilder sb = new StringBuilder();
     boolean addSpace = false;
     for (CssIdent id : values) {
       if (addSpace) {
         sb.append(' ');
       } else {
         addSpace = true;
       }
       sb.append(id);
     }
     ac.getFrame().addWarning("with-space", 1);
     val = new CssIdent(sb.toString());
   } else {
     val = values.get(0);
     // could be done in the consistency check, but...
     if (null != getGenericFontName(val)) {
       hasGenericFontFamily = true;
     }
     if (inherit.equals(val)) {
       val = inherit;
     }
   }
   curval.add(val);
 }
 /**
  * check if the value is lower or equal than...
  *
  * @param ac the validation context
  * @param property the property the value is defined in
  * @throws InvalidParamException
  */
 public void warnLowerEqualThan(ApplContext ac, double d, CssProperty property) {
   BigDecimal other = BigDecimal.valueOf(d);
   if (value.compareTo(other) > 0) {
     String[] s = new String[2];
     s[0] = toString();
     s[1] = other.toPlainString() + '%';
     ac.getFrame().addWarning("lowerequal", s);
   }
 }
  /**
   * Creates a new CssFontFamily
   *
   * @param expression The expression for this property
   * @throws org.w3c.css.util.InvalidParamException Expressions are incorrect
   */
  public CssFontFamily(ApplContext ac, CssExpression expression, boolean check)
      throws InvalidParamException {

    ArrayList<CssValue> values = new ArrayList<CssValue>();

    while (!expression.end()) {
      char op = expression.getOperator();
      CssValue val = expression.getValue();
      switch (val.getType()) {
        case CssTypes.CSS_STRING:
          // check it's not a quoted reserved keyword
          String s = val.toString();
          if (s.length() > 2) {
            // we remove quotes and check it's not reserved.
            CssIdent id = new CssIdent(s.substring(1, s.length() - 1));
            if (getGenericFontName(id) != null) {
              ac.getFrame().addWarning("generic-family.quote", 2);
            }
          }
          values.add(val);
          break;
        case CssTypes.CSS_IDENT:
          ArrayList<CssIdent> idval = new ArrayList<CssIdent>();
          idval.add((CssIdent) val);
          // we add idents if separated by spaces...
          while (op == SPACE && expression.getRemainingCount() > 1) {
            expression.next();
            op = expression.getOperator();
            val = expression.getValue();
            if (val.getType() == CssTypes.CSS_IDENT) {
              idval.add((CssIdent) val);
            } else {
              throw new InvalidParamException("value", val, getPropertyName(), ac);
            }
          }
          checkExpression(ac, values, idval, check);
          break;
        default:
          throw new InvalidParamException("value", val, getPropertyName(), ac);
      }
      expression.next();
      if (!expression.end() && (op != COMMA)) {
        throw new InvalidParamException("operator", ((new Character(op)).toString()), ac);
      }
    }
    checkValues(ac, values);
    value = (values.size() > 1) ? new CssLayerList(values) : values.get(0);
  }
 /**
  * warn if the value is not positive or null
  *
  * @param ac the validation context
  * @param property the property the value is defined in
  */
 public void warnPositiveness(ApplContext ac, CssProperty property) {
   if (!isPositive()) {
     ac.getFrame().addWarning("negative", toString());
   }
 }
Beispiel #8
0
  /**
   * Create a new OutlineATSC
   *
   * @param expression The expression for this property
   * @exception InvalidParamException Values are incorrect
   */
  public OutlineATSC(ApplContext ac, CssExpression expression, boolean check)
      throws InvalidParamException {

    if (check && expression.getCount() > 3) {
      throw new InvalidParamException("unrecognize", ac);
    }

    CssValue val = expression.getValue();
    char op = SPACE;
    boolean find = true;
    int max_values = 3;

    ac.getFrame().addWarning("atsc", val.toString());

    if (val.equals(inherit)) {
      if (expression.getCount() > 1) {
        throw new InvalidParamException("unrecognize", ac);
      }
      this.same = true;
      color = new OutlineColorATSC(ac, expression);
      width = new OutlineWidthATSC();
      width.value = inherit;
      style = new OutlineStyleATSC();
      style.value = OutlineStyleATSC.BORDERSTYLE.length - 1;
      return;
    }

    while (find && max_values-- > 0) {
      find = false;
      val = expression.getValue();
      op = expression.getOperator();

      if (val != null && val.equals(inherit)) {
        throw new InvalidParamException("unrecognize", ac);
      }

      if (val == null) {
        break;
      }

      if (op != SPACE) {
        throw new InvalidParamException("operator", ((new Character(op)).toString()), ac);
      }

      if (style == null) {
        try {
          style = new OutlineStyleATSC(ac, expression);
          find = true;
        } catch (InvalidParamException e) {
        }
      }
      if (!find && color == null) {
        try {
          color = new OutlineColorATSC(ac, expression);
          find = true;
        } catch (InvalidParamException e) {
        }
      }
      if (!find && width == null) {
        width = new OutlineWidthATSC(ac, expression);
        find = true;
      }
      if (val != null && !find) {
        throw new InvalidParamException("unrecognize", ac);
      }
    }

    if (max_values >= 2) {
      throw new InvalidParamException("few-value", getPropertyName(), ac);
    }
    /*
    if (color == null) {
        color = new OutlineColorATSC();
    }
    if (width == null) {
        width = new OutlineWidthATSC();
    }
    if (style == null) {
        style = new OutlineStyleATSC();
    }
    */
  }
  /**
   * Create a new DocumentParser
   *
   * @exception Exception An error
   */
  public DocumentParser(ApplContext ac, String urlString) throws Exception {
    this.htmlURL = HTTPURL.getURL(urlString);
    this.ac = ac;
    urlString = htmlURL.toString();
    String urlLower = urlString.toLowerCase();
    String media = ac.getMedium();
    String urlProtocol = htmlURL.getProtocol();

    if (!"http".equals(urlProtocol) && !"https".equals(urlProtocol)) {
      if (urlLower.endsWith(".css")) {
        StyleSheetParser parser = new StyleSheetParser();
        parser.parseURL(ac, htmlURL, null, null, media, StyleSheetOrigin.AUTHOR);
        style = parser.getStyleSheet();
      } else if (urlLower.endsWith(".html")
          || urlLower.endsWith(".shtml")
          || urlLower.endsWith("/")) {
        TagSoupStyleSheetHandler handler = new TagSoupStyleSheetHandler(htmlURL, ac);
        handler.parse(htmlURL);
        style = handler.getStyleSheet();
        if (style != null) {
          style.setType("text/html");
        }
      } else if (urlLower.endsWith(".xhtml") || urlLower.endsWith(".xml")) {
        // Seems like we need to use tagsout in this case as well
        XMLStyleSheetHandler handler = new XMLStyleSheetHandler(htmlURL, ac);
        handler.parse(htmlURL);
        style = handler.getStyleSheet();
        if (style != null) {
          style.setType("text/xml");
        }
      } else {
        throw new Exception("Unknown file");
      }
    } else {
      URLConnection connection = null;

      try {
        boolean isXML = false;
        String cType;

        // @@ hum, maybe? (plh, yes probably :-) )
        String credential = ac.getCredential();

        connection = HTTPURL.getConnection(htmlURL, ac);
        htmlURL = connection.getURL();

        String httpCL = connection.getHeaderField("Content-Location");
        if (httpCL != null) {
          htmlURL = HTTPURL.getURL(htmlURL, httpCL);
        }

        cType = connection.getContentType();
        if (cType == null) {
          cType = "unknown/unknown";
        }
        MimeType contentType = null;
        try {
          contentType = new MimeType(cType);
        } catch (MimeTypeFormatException ex) {
        }

        if (Util.onDebug) {
          System.err.println("[DEBUG] content type is [" + contentType + ']');
        }

        if (contentType.match(MimeType.TEXT_HTML) == MimeType.MATCH_SPECIFIC_SUBTYPE) {
          TagSoupStyleSheetHandler handler;
          handler = new TagSoupStyleSheetHandler(htmlURL, ac);
          handler.parse(urlString, connection);
          style = handler.getStyleSheet();

          if (style != null) {
            style.setType("text/html");
          }
        } else if (contentType.match(MimeType.TEXT_CSS) == MimeType.MATCH_SPECIFIC_SUBTYPE) {
          StyleSheetParser parser = new StyleSheetParser();
          parser.parseURL(ac, htmlURL, null, null, media, StyleSheetOrigin.AUTHOR);
          style = parser.getStyleSheet();
        } else if ((contentType.match(MimeType.TEXT_XML) == MimeType.MATCH_SPECIFIC_SUBTYPE)
            || (contentType.match(MimeType.APPLICATION_XHTML_XML)
                == MimeType.MATCH_SPECIFIC_SUBTYPE)
            || (contentType.match(wap) == MimeType.MATCH_SPECIFIC_SUBTYPE)) {
          // TagSoup ?
          XMLStyleSheetHandler handler = new XMLStyleSheetHandler(htmlURL, ac);
          handler.parse(urlString, connection);
          style = handler.getStyleSheet();

          if (style != null) {
            style.setType("text/xml");
          }
        } else {
          throw new IOException("Unknown mime type : " + contentType);
        }
      } finally {
        try {
          connection.getInputStream().close();
        } catch (Exception e) {
        }
      }
    }
  }