public int insertRule(final String rule, final int index) 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(rule)); final CSSOMParser parser = new CSSOMParser(); parser.setParentStyleSheet(parentStyleSheet); parser.setErrorHandler(ThrowCssExceptionErrorHandler.INSTANCE); // parser._parentRule is never read // parser.setParentRule(_parentRule); final CSSRule r = parser.parseRule(is); // Insert the rule into the list of rules ((CSSRuleListImpl) getCssRules()).insert(r, index); } catch (final IndexOutOfBoundsException e) { throw new DOMExceptionImpl( DOMException.INDEX_SIZE_ERR, DOMExceptionImpl.INDEX_OUT_OF_BOUNDS, e.getMessage()); } 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()); } return index; }
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()); } }
/** * 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(); } }
public void setMediaText(final String mediaText) throws DOMException { final InputSource source = new InputSource(new StringReader(mediaText)); try { final CSSOMParser parser = new CSSOMParser(); parser.setErrorHandler(ThrowCssExceptionErrorHandler.INSTANCE); final SACMediaList sml = parser.parseMedia(source); setMediaList(sml); } catch (final CSSParseException e) { throw new DOMException(DOMException.SYNTAX_ERR, e.getLocalizedMessage()); } catch (final IOException e) { throw new DOMException(DOMException.NOT_FOUND_ERR, e.getLocalizedMessage()); } }
/** * 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 } }
/* 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; } }
public CSSValue expr() { try { return _parser.parsePropertyValue(_is); } catch (IOException e) { return null; } }
public CSSStyleDeclaration styleDeclaration() { try { return _parser.parseStyleDeclaration(_is); } catch (IOException e) { return null; } }
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; } } }