@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (!(obj instanceof CSSStyleSheet)) { return false; } final CSSStyleSheet css = (CSSStyleSheet) obj; boolean eq = LangUtils.equals(getCssRules(), css.getCssRules()); eq = eq && (getDisabled() == css.getDisabled()); eq = eq && LangUtils.equals(getHref(), css.getHref()); eq = eq && LangUtils.equals(getMedia(), css.getMedia()); // TODO implement some reasonful equals method for ownerNode // eq = eq && Utils.equals(getOwnerNode(), css.getOwnerNode()); // don't use ownerNode and parentStyleSheet in equals() // recursive loop -> stack overflow! eq = eq && LangUtils.equals(getTitle(), css.getTitle()); return eq; }
/** * is affected by pseudo name in ancestor * * @param elementRules * @param ancestor * @param element * @param pseudoName * @return */ private boolean isAffectedByPseudoNameInAncestor(Collection<StyleRuleInfo> elementRules, HTMLElementImpl ancestor, HTMLElementImpl element, String pseudoName) { if (elementRules != null) { Iterator<StyleRuleInfo> i = elementRules.iterator(); while (i.hasNext()) { StyleRuleInfo styleRuleInfo = i.next(); CSSStyleSheet styleSheet = styleRuleInfo.getStyleRule().getParentStyleSheet(); if (styleSheet != null && styleSheet.getDisabled()) { continue; } if (styleRuleInfo.affectedByPseudoNameInAncestor(element, ancestor, pseudoName)) { return true; } } } return false; }
/** * put style declarations * * @param elementRules * @param styleDeclarations * @param element * @param pseudoNames * @return */ private Collection<CSSStyleDeclaration> putStyleDeclarations(Collection<StyleRuleInfo> elementRules, Collection<CSSStyleDeclaration> styleDeclarations, HTMLElementImpl element, Set pseudoNames) { Iterator<StyleRuleInfo> i = elementRules.iterator(); while (i.hasNext()) { StyleRuleInfo styleRuleInfo = i.next(); if (styleRuleInfo.isSelectorMatch(element, pseudoNames)) { CSSStyleRule styleRule = styleRuleInfo.getStyleRule(); CSSStyleSheet styleSheet = styleRule.getParentStyleSheet(); if (styleSheet != null && styleSheet.getDisabled()) { continue; } if (styleDeclarations == null) { styleDeclarations = new LinkedList<CSSStyleDeclaration>(); } styleDeclarations.add(styleRule.getStyle()); } } return styleDeclarations; }
/** * Adds the style sheet. * * @param ss * the ss */ public final void addStyleSheet(CSSStyleSheet ss) { synchronized (this.getTreeLock()) { this.styleSheets.add(ss); this.styleSheetAggregator = null; this.forgetRenderState(); ArrayList<?> nl = this.nodeList; if (nl != null) { Iterator<?> i = nl.iterator(); while (i.hasNext()) { Object node = i.next(); if (node instanceof HTMLElementImpl) { ((HTMLElementImpl) node).forgetStyle(true); } } } } this.allInvalidated(); }
/** * 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); }
/** * Parses a CSS resource and get the CSSStyleSheet as the output. * * @param source * the source of the CSS resource * @return the CSSStyleSheet if succeed * @throws IOException * if the resource is not well-located */ public CSSStyleSheet parseStyleSheet( InputSource source ) throws IOException { CssHandler handler = new CssHandler( ); parser.setDocumentHandler( handler ); parser.setErrorHandler( errorHandler ); try { parser.parseStyleSheet( source ); } catch ( StringIndexOutOfBoundsException e ) { throw new CSSException( CSSException.SAC_SYNTAX_ERR ); } return (StyleSheet) handler.getRoot( ); }
/** * Merges all StyleSheets in this list into one. * * @return the new (merged) StyleSheet */ public StyleSheet merge() { final CSSStyleSheetImpl merged = new CSSStyleSheetImpl(); final CSSRuleListImpl cssRuleList = new CSSRuleListImpl(); final Iterator<CSSStyleSheet> it = getCSSStyleSheets().iterator(); while (it.hasNext()) { final CSSStyleSheetImpl cssStyleSheet = (CSSStyleSheetImpl) it.next(); final CSSMediaRuleImpl cssMediaRule = new CSSMediaRuleImpl(merged, null, cssStyleSheet.getMedia()); cssMediaRule.setRuleList((CSSRuleListImpl) cssStyleSheet.getCssRules()); cssRuleList.add(cssMediaRule); } merged.setCssRules(cssRuleList); merged.setMediaText("all"); return merged; }
/** * Adds the style sheet. * * @param styleSheet * the style sheet * @throws MalformedURLException * the malformed url exception * @throws UnsupportedEncodingException */ private final void addStyleSheet(CSSStyleSheet styleSheet) throws MalformedURLException, UnsupportedEncodingException { CSSRuleList ruleList = styleSheet.getCssRules(); int length = ruleList.getLength(); for (int i = 0; i < length; i++) { CSSRule rule = ruleList.item(i); this.addRule(styleSheet, rule); } }
@Override public void setDisabled(boolean disabled) { this.disabled = disabled; CSSStyleSheet sheet = this.styleSheet; if (sheet != null) { sheet.setDisabled(disabled); } }
/** * Process style. */ protected void processStyle() { this.styleSheet = null; UserAgentContext uacontext = this.getUserAgentContext(); if (uacontext.isInternalCSSEnabled() && CSSUtilities.matchesMedia(this.getMedia(), this.getUserAgentContext())) { String text = this.getRawInnerText(true); if (text != null && !"".equals(text)) { HTMLDocumentImpl doc = (HTMLDocumentImpl) this.getOwnerDocument(); try { CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); InputSource is = CSSUtilities.getCssInputSourceForStyleSheet(text, doc.getBaseURI()); CSSStyleSheet sheet = parser.parseStyleSheet(is, null, null); if (sheet != null) { doc.addStyleSheet(sheet); this.styleSheet = sheet; if (sheet instanceof CSSStyleSheetImpl) { CSSStyleSheetImpl sheetImpl = (CSSStyleSheetImpl) sheet; sheetImpl.setDisabled(disabled); } else { sheet.setDisabled(this.disabled); } } } catch (Throwable err) { logger.error("Unable to parse style sheet", err); } } } }
JStyleSheetWrapper(final cz.vutbr.web.css.StyleSheet jStyleSheet, final String mediaStr, final String href, final Node ownerNode, final CSSStyleSheet parentStyleSheet, final String type, final String title, final StyleSheetBridge bridge) { this.jStyleSheet = jStyleSheet; this.mediaStr = mediaStr; this.href = href; this.bridge = bridge; this.ownerNode = ownerNode; this.type = type; this.title = title; this.parentStyleSheet = parentStyleSheet; }
public HashMap<String, CSSStyleRule> extractCssStyleRules(String cssFile) throws IOException { TEST_FILE_SYSTEM.filesExists(cssFile); CSSOMParser cssParser = new CSSOMParser(); CSSStyleSheet css = cssParser.parseStyleSheet(new InputSource(new FileReader(TEST_FILE_SYSTEM.file(cssFile))), null, null); CSSRuleList cssRules = css.getCssRules(); HashMap<String, CSSStyleRule> rules = new HashMap<String, CSSStyleRule>(); for (int i = 0; i < cssRules.getLength(); i++) { CSSRule rule = cssRules.item(i); if (rule instanceof CSSStyleRule) { rules.put(((CSSStyleRule) rule).getSelectorText(), (CSSStyleRule) rule); } } return rules; }
AttributeRuleList getRuleList(InputStream stream) throws IOException { InputSource source = new InputSource(new InputStreamReader(stream)); CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); parser.setErrorHandler(ThrowCssExceptionErrorHandler.INSTANCE); CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null); CSSRuleList ruleList = stylesheet.getCssRules(); return new AttributeRuleList(ruleList); }
/** * <b>DOM</b>: Implements {@link * org.w3c.dom.css.DOMImplementationCSS#createCSSStyleSheet(String,String)}. */ public CSSStyleSheet createCSSStyleSheet(String title, String media) { throw new UnsupportedOperationException ("DOMImplementationCSS.createCSSStyleSheet is not implemented"); // XXX }
/** * Returns the user-agent stylesheet. */ public CSSStyleSheet getUserAgentStyleSheet() { throw new UnsupportedOperationException ("StyleSheetFactory.getUserAgentStyleSheet is not implemented"); // XXX }
protected CSSStyleSheetAssert(CSSStyleSheet actual) { super(actual, CSSStyleSheetAssert.class); }
public static CSSStyleSheetAssert assertThat(CSSStyleSheet actual) { return new CSSStyleSheetAssert(actual); }
public CSSStyleSheet getParentStyleSheet() { return parentStyleSheet_; }
public List<CSSStyleSheet> getCSSStyleSheets() { if (cssStyleSheets_ == null) { cssStyleSheets_ = new ArrayList<CSSStyleSheet>(); } return cssStyleSheets_; }
public void setCSSStyleSheets(final List<CSSStyleSheet> cssStyleSheets) { cssStyleSheets_ = cssStyleSheets; }
@Override public CSSStyleSheet getStyleSheet() { return null; }
/** * @param href * @param href * @param doc * @return * @throws Exception */ public static CSSStyleSheet parse(String href, HTMLDocumentImpl doc) throws Exception { URL url = null; CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); URL baseURL = new URL(doc.getBaseURI()); URL scriptURL = Urls.createURL(baseURL, href); String scriptURI = scriptURL == null ? href : scriptURL.toExternalForm(); try { if (scriptURI.startsWith("//")) { scriptURI = "http:" + scriptURI; } url = new URL(scriptURI); } catch (MalformedURLException mfu) { int idx = scriptURI.indexOf(':'); if (idx == -1 || idx == 1) { // try file url = new URL("file:" + scriptURI); } else { throw mfu; } } logger.info("process(): Loading URI=[" + scriptURI + "]."); SSLCertificate.setCertificate(); URLConnection connection = url.openConnection(); connection.setRequestProperty("User-Agent", UserAgentContext.DEFAULT_USER_AGENT); 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"); InputSource is = getCssInputSourceForStyleSheet(source, doc.getBaseURI()); return parser.parseStyleSheet(is, null, null); }
/** * 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.ERROR, "Error trying to load URI=[" + uri + "].", err); this.clearCssOutput(); } }
public CSSStyleSheet getParentStyleSheet( ) { return null; }
public CSSStyleSheet getStyleSheet() { // TODO implement this method throw new UnsupportedOperationException(); }
/** * @return The style sheet that contains this rule. */ public CSSStyleSheet getParentStyleSheet() { return containingStyleSheet; }
/** * Parses a SAC input source into a CSSOM style sheet. * * @param source * the SAC input source * @param ownerNode * the owner node (see the definition of <code>ownerNode</code> in * org.w3c.dom.css.StyleSheet) * @param href * the href (see the definition of <code>href</code> in * org.w3c.dom.css.StyleSheet) * @return the CSSOM style sheet * @throws IOException * if the underlying SAC parser throws an IOException */ public CSSStyleSheet parseStyleSheet(final InputSource source, final Node ownerNode, final String href) throws IOException { final CSSOMHandler handler = new CSSOMHandler(); handler.setOwnerNode(ownerNode); handler.setHref(href); parser_.setDocumentHandler(handler); parser_.parseStyleSheet(source); final Object o = handler.getRoot(); if (o instanceof CSSStyleSheet) { return (CSSStyleSheet) o; } return null; }
/** * Adds the style sheets. * * @param styleSheets * the style sheets * @throws MalformedURLException * the malformed url exception * @throws UnsupportedEncodingException */ public final void addStyleSheets(List<CSSStyleSheet> styleSheets) throws MalformedURLException, UnsupportedEncodingException { Iterator<CSSStyleSheet> i = styleSheets.iterator(); while (i.hasNext()) { CSSStyleSheet sheet = i.next(); this.addStyleSheet(sheet); } }
/** * Adds a CSSStyleSheet. * * @param cssStyleSheet * the CSSStyleSheet */ public void add(final CSSStyleSheet cssStyleSheet) { getCSSStyleSheets().add(cssStyleSheet); }
/** * Gets the sheet. * * @return the sheet */ public CSSStyleSheet getSheet() { return this.styleSheet; }
/** * Notifies the listener that the style sheet has been changed. * * @param styleSheet * The style sheet that has changed */ public void notifyStyleSheetChanged(final CSSStyleSheet styleSheet);
/** * @param jStyleSheet * parsed style sheet from jStyleParser * @param mediaStr * The intended destination media for style information. The media is * often specified in the <code>ownerNode</code>. If no media has * been specified, the <code>MediaList</code> will be empty. See the * media attribute definition for the <code>LINK</code> element in * HTML 4.0, and the media pseudo-attribute for the XML style sheet * processing instruction . Modifying the media list may cause a * change to the attribute <code>disabled</code>. * @param href * If the style sheet is a linked style sheet, the value of its * attribute is its location. For inline style sheets, the value of * this attribute is <code>null</code>. See the href attribute * definition for the <code>LINK</code> element in HTML 4.0, and the * href pseudo-attribute for the XML style sheet processing * instruction. * @param parentStyleSheet * For style sheet languages that support the concept of style sheet * inclusion, this attribute represents the including style sheet, if * one exists. If the style sheet is a top-level style sheet, or the * style sheet language does not support inclusion, the value of this * attribute is <code>null</code>. * @param bridge * callback to notify any changes in the style sheet or to * dynamically get data from the caller. */ public JStyleSheetWrapper(final cz.vutbr.web.css.StyleSheet jStyleSheet, final String mediaStr, final String href, final CSSStyleSheet parentStyleSheet, final StyleSheetBridge bridge) { this(jStyleSheet, mediaStr, href, null, parentStyleSheet, null, null, bridge); }
/** * Used to retrieve a style sheet by ordinal index. If index is greater than * or equal to the number of style sheets in the list, this returns * <code>null</code>. * * @param index * Index into the collection * @return The style sheet at the <code>index</code> position in the * <code>StyleSheetList</code>, or <code>null</code> if that is not a * valid index. */ public CSSStyleSheet item(final int index) { return this.bridge.getDocStyleSheets().get(index); }