com.gargoylesoftware.css.parser.CSSErrorHandler Java Examples

The following examples show how to use com.gargoylesoftware.css.parser.CSSErrorHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CSSStyleSheet.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the CSS at the specified input source. If anything at all goes wrong, this method
 * returns an empty stylesheet.
 *
 * @param source the source from which to retrieve the CSS to be parsed
 * @param client the client
 * @return the stylesheet parsed from the specified input source
 */
private static CSSStyleSheetImpl parseCSS(final InputSource source, final WebClient client) {
    CSSStyleSheetImpl ss;
    try {
        final CSSErrorHandler errorHandler = client.getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        ss = parser.parseStyleSheet(source, null);
    }
    catch (final Throwable t) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error parsing CSS from '" + toString(source) + "': " + t.getMessage(), t);
        }
        ss = new CSSStyleSheetImpl();
    }
    return ss;
}
 
Example #2
Source File: CSSStyleSheet.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the selectors at the specified input source. If anything at all goes wrong, this
 * method returns an empty selector list.
 *
 * @param source the source from which to retrieve the selectors to be parsed
 * @return the selectors parsed from the specified input source
 */
public SelectorList parseSelectors(final String source) {
    SelectorList selectors;
    try {
        final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        selectors = parser.parseSelectors(source);
        // in case of error parseSelectors returns null
        if (null == selectors) {
            selectors = new SelectorListImpl();
        }
    }
    catch (final Throwable t) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error parsing CSS selectors from '" + source + "': " + t.getMessage(), t);
        }
        selectors = new SelectorListImpl();
    }
    return selectors;
}
 
Example #3
Source File: CSSStyleSheet.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the given media string. If anything at all goes wrong, this
 * method returns an empty MediaList list.
 *
 * @param source the source from which to retrieve the media to be parsed
 * @return the media parsed from the specified input source
 */
static MediaListImpl parseMedia(final CSSErrorHandler errorHandler, final String mediaString) {
    MediaListImpl media = media_.get(mediaString);
    if (media != null) {
        return media;
    }

    try {
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);

        media = new MediaListImpl(parser.parseMedia(mediaString));
        media_.put(mediaString, media);
        return media;
    }
    catch (final Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error parsing CSS media from '" + mediaString + "': " + e.getMessage(), e);
        }
    }

    media = new MediaListImpl(null);
    media_.put(mediaString, media);
    return media;
}
 
Example #4
Source File: CSSStyleSheet.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the selectors at the specified input source. If anything at all goes wrong, this
 * method returns an empty selector list.
 *
 * @param source the source from which to retrieve the selectors to be parsed
 * @return the selectors parsed from the specified input source
 */
public SelectorList parseSelectors(final InputSource source) {
    SelectorList selectors;
    try {
        final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        selectors = parser.parseSelectors(source);
        // in case of error parseSelectors returns null
        if (null == selectors) {
            selectors = new SelectorListImpl();
        }
    }
    catch (final Throwable t) {
        LOG.error("Error parsing CSS selectors from '" + toString(source) + "': " + t.getMessage(), t);
        selectors = new SelectorListImpl();
    }
    return selectors;
}
 
Example #5
Source File: CSSStyleSheet.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the given media string. If anything at all goes wrong, this
 * method returns an empty MediaList list.
 *
 * @param source the source from which to retrieve the media to be parsed
 * @return the media parsed from the specified input source
 */
static MediaList parseMedia(final CSSErrorHandler errorHandler, final String mediaString) {
    MediaList media = media_.get(mediaString);
    if (media != null) {
        return media;
    }

    try {
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);

        final InputSource source = new InputSource(new StringReader(mediaString));
        media = new MediaListImpl(parser.parseMedia(source));
        if (media != null) {
            media_.put(mediaString, media);
            return media;
        }
    }
    catch (final Exception e) {
        LOG.error("Error parsing CSS media from '" + mediaString + "': " + e.getMessage(), e);
    }

    media = new MediaListImpl(null);
    media_.put(mediaString, media);
    return media;
}
 
Example #6
Source File: MediaQueryList.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the document currently matches the media query list or not.
 * @return whether the document currently matches the media query list or not
 */
@JsxGetter
public boolean isMatches() {
    final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
    final MediaListImpl mediaList = CSSStyleSheet.parseMedia(errorHandler, media_);
    return CSSStyleSheet.isActive(this, mediaList);
}
 
Example #7
Source File: CSSStyleDeclaration.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the CSS property value.
 * @param name the name of the property to retrieve
 * @return the value
 */
@JsxFunction(FF60)
public CSSValue getPropertyCSSValue(final String name) {
    if (LOG.isInfoEnabled()) {
        LOG.info("getPropertyCSSValue(" + name + "): getPropertyCSSValue support is experimental");
    }

    // following is a hack, just to have basic support for getPropertyCSSValue
    // TODO: rework the whole CSS processing here! we should *always* parse the style!
    if (styleDeclaration_ == null) {
        final String styleAttribute = jsElement_.getDomNodeOrDie().getAttributeDirect("style");
        final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        try {
            styleDeclaration_ = parser.parseStyleDeclaration(styleAttribute);
        }
        catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }
    CSSValueImpl cssValue = styleDeclaration_.getPropertyCSSValue(name);
    if (cssValue == null) {
        final CSSValueImpl newValue = new CSSValueImpl(null, false);
        newValue.setDoubleValue(0);
        cssValue = newValue;
    }

    // FF has spaces next to ","
    final String cssText = cssValue.getCssText();
    if (cssText.startsWith("rgb(")) {
        final String formatedCssText = StringUtils.replace(cssText, ",", ", ");
        cssValue.setCssText(formatedCssText);
    }

    return new CSSPrimitiveValue(jsElement_, cssValue);
}
 
Example #8
Source File: StyleMedia.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the specified media is supported by the object that displays the document object.
 * @param media the media query
 * @return whether the specified media is supported or not
 */
@JsxFunction
public boolean matchMedium(final String media) {
    final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
    final MediaListImpl mediaList = CSSStyleSheet.parseMedia(errorHandler, media);
    return CSSStyleSheet.isActive(this, mediaList);
}
 
Example #9
Source File: MediaQueryList.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the document currently matches the media query list or not.
 * @return whether the document currently matches the media query list or not
 */
@JsxGetter
public boolean isMatches() {
    final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
    final MediaList mediaList = CSSStyleSheet.parseMedia(errorHandler, media_);
    return CSSStyleSheet.isActive(this, mediaList);
}
 
Example #10
Source File: CSSStyleDeclaration.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the CSS property value.
 * @param name the name of the property to retrieve
 * @return the value
 */
@JsxFunction(FF)
public CSSValue getPropertyCSSValue(final String name) {
    LOG.info("getPropertyCSSValue(" + name + "): getPropertyCSSValue support is experimental");
    // following is a hack, just to have basic support for getPropertyCSSValue
    // TODO: rework the whole CSS processing here! we should *always* parse the style!
    if (styleDeclaration_ == null) {
        final String uri = getDomNodeOrDie().getPage().getWebResponse().getWebRequest()
                .getUrl().toExternalForm();
        final String styleAttribute = jsElement_.getDomNodeOrDie().getAttributeDirect("style");
        final InputSource source = new InputSource(new StringReader(styleAttribute));
        source.setURI(uri);
        final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        try {
            styleDeclaration_ = parser.parseStyleDeclaration(source);
        }
        catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }
    org.w3c.dom.css.CSSValue cssValue = styleDeclaration_.getPropertyCSSValue(name);
    if (cssValue == null) {
        final CSSValueImpl newValue = new CSSValueImpl(null, false);
        newValue.setFloatValue(CSSPrimitiveValue.CSS_PX, 0);
        cssValue = newValue;
    }

    // FF has spaces next to ","
    final String cssText = cssValue.getCssText();
    if (cssText.startsWith("rgb(")) {
        final String formatedCssText = StringUtils.replace(cssText, ",", ", ");
        cssValue.setCssText(formatedCssText);
    }

    return new CSSPrimitiveValue(jsElement_, (org.w3c.dom.css.CSSPrimitiveValue) cssValue);
}
 
Example #11
Source File: CSSStyleSheet.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the CSS at the specified input source. If anything at all goes wrong, this method
 * returns an empty stylesheet.
 *
 * @param source the source from which to retrieve the CSS to be parsed
 * @return the stylesheet parsed from the specified input source
 */
private org.w3c.dom.css.CSSStyleSheet parseCSS(final InputSource source) {
    org.w3c.dom.css.CSSStyleSheet ss;
    try {
        final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        ss = parser.parseStyleSheet(source, null);
    }
    catch (final Throwable t) {
        LOG.error("Error parsing CSS from '" + toString(source) + "': " + t.getMessage(), t);
        ss = new CSSStyleSheetImpl();
    }
    return ss;
}
 
Example #12
Source File: StyleMedia.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the specified media is supported by the object that displays the document object.
 * @param media the media query
 * @return whether the specified media is supported or not
 */
@JsxFunction
public boolean matchMedium(final String media) {
    final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
    final MediaList mediaList = CSSStyleSheet.parseMedia(errorHandler, media);
    return CSSStyleSheet.isActive(this, mediaList);
}
 
Example #13
Source File: WebClient.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the CSS error handler used by this web client when CSS problems are encountered.
 * @return the CSS error handler used by this web client when CSS problems are encountered
 * @see DefaultCssErrorHandler
 * @see SilentCssErrorHandler
 */
public CSSErrorHandler getCssErrorHandler() {
    return cssErrorHandler_;
}
 
Example #14
Source File: WebClient.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the CSS error handler used by this web client when CSS problems are encountered.
 * @param cssErrorHandler the CSS error handler used by this web client when CSS problems are encountered
 * @see DefaultCssErrorHandler
 * @see SilentCssErrorHandler
 */
public void setCssErrorHandler(final CSSErrorHandler cssErrorHandler) {
    WebAssert.notNull("cssErrorHandler", cssErrorHandler);
    cssErrorHandler_ = cssErrorHandler;
}
 
Example #15
Source File: WebClient.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the CSS error handler used by this web client when CSS problems are encountered.
 * @return the CSS error handler used by this web client when CSS problems are encountered
 * @see DefaultCssErrorHandler
 * @see SilentCssErrorHandler
 */
public CSSErrorHandler getCssErrorHandler() {
    return cssErrorHandler_;
}
 
Example #16
Source File: WebClient.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the CSS error handler used by this web client when CSS problems are encountered.
 * @param cssErrorHandler the CSS error handler used by this web client when CSS problems are encountered
 * @see DefaultCssErrorHandler
 * @see SilentCssErrorHandler
 */
public void setCssErrorHandler(final CSSErrorHandler cssErrorHandler) {
    WebAssert.notNull("cssErrorHandler", cssErrorHandler);
    cssErrorHandler_ = cssErrorHandler;
}