示例#1
0
  /*
      public CSS2Parser(
              InputStream stream,
              StyleSheet parentStyleSheet,
              CSSRule ownerRule,
              String href,
              String title,
              String media) {
          _parentStyleSheet = parentStyleSheet;
          _ownerRule = ownerRule;
          _href = href;
          _title = title;
          _media = media;
      }

      public CSS2Parser(
              Reader stream,
              StyleSheet parentStyleSheet,
              CSSRule ownerRule,
              String href,
              String title,
              String media) {
          _parser = new CSSOMParser();
          _is = new InputSource(stream);
      }
  */
  public CSSStyleSheet styleSheet() {
    try {
      return _parser.parseStyleSheet(_is);
    } catch (IOException e) {
      return null;
    }
  }
示例#2
0
 /**
  * Process.
  *
  * @param uri the uri
  */
 private void process(String uri) {
   try {
     URL url;
     try {
       url = new URL(uri);
     } catch (MalformedURLException mfu) {
       int idx = uri.indexOf(':');
       if ((idx == -1) || (idx == 1)) {
         // try file
         url = new URL("file:" + uri);
       } else {
         throw mfu;
       }
     }
     logger.info("process(): Loading URI=[" + uri + "].");
     long time0 = System.currentTimeMillis();
     SSLCertificate.setCertificate();
     URLConnection connection = url.openConnection();
     connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+");
     connection.setRequestProperty("Cookie", "");
     if (connection instanceof HttpURLConnection) {
       HttpURLConnection hc = (HttpURLConnection) connection;
       hc.setInstanceFollowRedirects(true);
       int responseCode = hc.getResponseCode();
       logger.info("process(): HTTP response code: " + responseCode);
     }
     InputStream in = connection.getInputStream();
     byte[] content;
     try {
       content = IORoutines.load(in, 8192);
     } finally {
       in.close();
     }
     String source = new String(content, "UTF-8");
     this.textArea.setText(source);
     long time1 = System.currentTimeMillis();
     CSSOMParser parser = new CSSOMParser();
     InputSource is = CSSUtilities.getCssInputSourceForStyleSheet(source, uri);
     CSSStyleSheet styleSheet = parser.parseStyleSheet(is, null, null);
     long time2 = System.currentTimeMillis();
     logger.info(
         "Parsed URI=["
             + uri
             + "]: Parse elapsed: "
             + (time2 - time1)
             + " ms. Load elapsed: "
             + (time1 - time0)
             + " ms.");
     this.showStyleSheet(styleSheet);
   } catch (Exception err) {
     logger.log(Level.SEVERE, "Error trying to load URI=[" + uri + "].", err);
     this.clearCssOutput();
   }
 }
  /**
   * Parse CSS and create completion informations.
   *
   * @param css CSS
   */
  private void processStylesheet(String css) {
    try {
      CSSOMParser parser = new CSSOMParser();
      InputSource is = new InputSource(new StringReader(css));
      CSSStyleSheet stylesheet = parser.parseStyleSheet(is);
      CSSRuleList list = stylesheet.getCssRules();
      //			ArrayList assists = new ArrayList();
      for (int i = 0; i < list.getLength(); i++) {
        CSSRule rule = list.item(i);
        if (rule instanceof CSSStyleRule) {
          CSSStyleRule styleRule = (CSSStyleRule) rule;
          String selector = styleRule.getSelectorText();
          SelectorList selectors =
              parser.parseSelectors(new InputSource(new StringReader(selector)));
          for (int j = 0; j < selectors.getLength(); j++) {
            Selector sel = selectors.item(j);
            if (sel instanceof ConditionalSelector) {
              Condition cond = ((ConditionalSelector) sel).getCondition();
              SimpleSelector simple = ((ConditionalSelector) sel).getSimpleSelector();

              if (simple instanceof ElementSelector) {
                String tagName = ((ElementSelector) simple).getLocalName();
                if (tagName == null) {
                  tagName = "*";
                } else {
                  tagName = tagName.toLowerCase();
                }
                if (cond instanceof AttributeCondition) {
                  AttributeCondition attrCond = (AttributeCondition) cond;
                  if (rules.get(tagName) == null) {
                    List<String> classes = new ArrayList<String>();
                    classes.add(attrCond.getValue());
                    rules.put(tagName, classes);
                    //									} else {
                    //										ArrayList classes = (ArrayList)rules.get(tagName);
                    ////										classes.add(new AssistInfo(attrCond.getValue()));
                    //										classes.add(attrCond.getValue());
                  }
                }
              }
            }
          }
        }
      }
    } catch (Throwable ex) {
      // java.lang.Error: Missing return statement in function
    }
  }
示例#4
0
文件: CssTest.java 项目: ulfjack/cqs
 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;
     }
   }
 }