Search in sources :

Example 6 with CSSParseException

use of com.gargoylesoftware.css.parser.CSSParseException in project LoboEvolution by LoboEvolution.

the class CSS3Parser method mediaRule.

// 
// media
// : MEDIA_SYM S* medium [ COMMA S* medium ]* '{' S* ruleset* '}' S*
// ;
// 
public final void mediaRule() throws ParseException {
    boolean start = false;
    MediaQueryList ml = new MediaQueryList();
    Locator locator;
    try {
        jj_consume_token(MEDIA_SYM);
        locator = createLocator(token);
        label_9: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[17] = jj_gen;
                    break label_9;
            }
            jj_consume_token(S);
        }
        mediaList(ml);
        start = true;
        handleStartMedia(ml, locator);
        jj_consume_token(LBRACE);
        label_10: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[18] = jj_gen;
                    break label_10;
            }
            jj_consume_token(S);
        }
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case IDENT:
            case DOT:
            case COLON:
            case ASTERISK:
            case LSQUARE:
            case HASH:
            case IMPORT_SYM:
            case PAGE_SYM:
            case MEDIA_SYM:
            case ATKEYWORD:
                {
                    mediaRuleList();
                    break;
                }
            default:
                jj_la1[19] = jj_gen;
                ;
        }
        jj_consume_token(RBRACE);
    } catch (CSSParseException e) {
        getErrorHandler().error(e);
        error_skipblock("ignoringRule", e);
    } catch (ParseException e) {
        CSSParseException cpe = toCSSParseException("invalidMediaRule", e);
        getErrorHandler().error(cpe);
        error_skipblock("ignoringRule", cpe);
    } finally {
        if (start) {
            handleEndMedia(ml);
        }
    }
}
Also used : Locator(com.gargoylesoftware.css.parser.Locator) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) MediaQueryList(com.gargoylesoftware.css.parser.media.MediaQueryList) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException)

Example 7 with CSSParseException

use of com.gargoylesoftware.css.parser.CSSParseException in project LoboEvolution by LoboEvolution.

the class CSS3Parser method declaration.

// 
// declaration
// : property ':' S* expr prio?
// |
// ;
// 
public final void declaration() throws ParseException {
    String p;
    LexicalUnit e;
    Token t;
    boolean priority = false;
    Locator starHack = null;
    Locator locator = null;
    try {
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case ASTERISK:
                {
                    jj_consume_token(ASTERISK);
                    starHack = createLocator(token);
                    break;
                }
            default:
                jj_la1[95] = jj_gen;
                ;
        }
        p = property();
        locator = createLocator(token);
        jj_consume_token(COLON);
        label_61: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[96] = jj_gen;
                    break label_61;
            }
            jj_consume_token(S);
        }
        e = expr();
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case IMPORTANT_SYM:
                {
                    priority = prio();
                    break;
                }
            default:
                jj_la1[97] = jj_gen;
                ;
        }
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case UNKNOWN:
                {
                    t = jj_consume_token(UNKNOWN);
                    locator = createLocator(t);
                    CSSParseException cpe = toCSSParseException("invalidDeclarationInvalidChar", new String[] { t.image }, locator);
                    getErrorHandler().error(cpe);
                    error_skipdecl();
                    break;
                }
            default:
                jj_la1[98] = jj_gen;
                ;
        }
        if (starHack != null) {
            CSSParseException cpe = toCSSParseException("invalidDeclarationStarHack", new Object[0], starHack);
            getErrorHandler().error(cpe);
            return;
        }
        handleProperty(p, e, priority, locator);
    } catch (CSSParseException ex) {
        getErrorHandler().error(ex);
        error_skipdecl();
    } catch (ParseException ex) {
        CSSParseException cpe = toCSSParseException("invalidDeclaration", ex);
        getErrorHandler().error(cpe);
        error_skipdecl();
    }
}
Also used : Locator(com.gargoylesoftware.css.parser.Locator) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) LexicalUnit(com.gargoylesoftware.css.parser.LexicalUnit)

Example 8 with CSSParseException

use of com.gargoylesoftware.css.parser.CSSParseException in project yacy_grid_loader by yacy.

the class HtmlUnitLoader method getClient.

public static WebClient getClient(String userAgent) {
    WebClient webClient = new WebClient(getBrowser(userAgent));
    WebClientOptions options = webClient.getOptions();
    options.setJavaScriptEnabled(true);
    options.setCssEnabled(false);
    options.setPopupBlockerEnabled(true);
    options.setRedirectEnabled(true);
    options.setDownloadImages(false);
    options.setGeolocationEnabled(false);
    options.setPrintContentOnFailingStatusCode(false);
    options.setThrowExceptionOnScriptError(false);
    options.setMaxInMemory(0);
    options.setHistoryPageCacheLimit(0);
    options.setHistorySizeLimit(0);
    // ProxyConfig proxyConfig = new ProxyConfig();
    // proxyConfig.setProxyHost("127.0.0.1");
    // proxyConfig.setProxyPort(Service.getPort());
    // options.setProxyConfig(proxyConfig);
    // this might be a bit large, is regulated with throttling and client cache clear in short memory status
    webClient.getCache().setMaxSize(10000);
    webClient.setIncorrectnessListener(new IncorrectnessListener() {

        @Override
        public void notify(String arg0, Object arg1) {
        }
    });
    webClient.setCssErrorHandler(new CSSErrorHandler() {

        @Override
        public void warning(CSSParseException exception) throws CSSException {
        }

        @Override
        public void error(CSSParseException exception) throws CSSException {
        }

        @Override
        public void fatalError(CSSParseException exception) throws CSSException {
        }
    });
    webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {

        @Override
        public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
        }

        @Override
        public void scriptException(HtmlPage arg0, ScriptException arg1) {
        }

        @Override
        public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
        }

        @Override
        public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
        }

        @Override
        public void warn(String message, String sourceName, int line, String lineSource, int lineOffset) {
        }
    });
    webClient.setHTMLParserListener(new HTMLParserListener() {

        @Override
        public void error(String message, URL url, String html, int line, int column, String key) {
        }

        @Override
        public void warning(String message, URL url, String html, int line, int column, String key) {
        }
    });
    return webClient;
}
Also used : HTMLParserListener(com.gargoylesoftware.htmlunit.html.parser.HTMLParserListener) WebClientOptions(com.gargoylesoftware.htmlunit.WebClientOptions) MalformedURLException(java.net.MalformedURLException) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient) IncorrectnessListener(com.gargoylesoftware.htmlunit.IncorrectnessListener) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSException(com.gargoylesoftware.css.parser.CSSException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) JavaScriptErrorListener(com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) CSSException(com.gargoylesoftware.css.parser.CSSException)

Example 9 with CSSParseException

use of com.gargoylesoftware.css.parser.CSSParseException in project LoboEvolution by LoboEvolution.

the class StyleSheetAggregator method selectsPseudoClass.

private boolean selectsPseudoClass(final Condition condition, final HTMLElement element, final boolean mouseOver) {
    final String value = condition.getValue();
    switch(value) {
        case "hover":
            setMouseOver(true);
            return mouseOver;
        case "root":
            NodeImpl parentDOMNodeImpl = (NodeImpl) element.getParentNode();
            return parentDOMNodeImpl != null && parentDOMNodeImpl.getNodeType() == NodeType.DOCUMENT_TYPE_NODE;
        case "enabled":
            return element.hasAttribute("enabled") || (!element.hasAttribute("enabled") && !element.hasAttribute("disabled"));
        case "disabled":
            return element.hasAttribute("disabled");
        case "placeholder":
            return element.hasAttribute("placeholder");
        case "read-only":
            return element.hasAttribute("readonly");
        case "read-write":
            return !element.hasAttribute("readonly");
        case "out-of-range":
            if (element instanceof HTMLInputElement) {
                HTMLInputElementImpl input = (HTMLInputElementImpl) element;
                if ("number".equals(input.getType())) {
                    String minTxt = input.getAttribute("min");
                    String maxTxt = input.getAttribute("max");
                    int min = minTxt == null ? 0 : Integer.parseInt(input.getAttribute("min"));
                    int max = maxTxt == null ? Integer.MAX_VALUE : Integer.parseInt(input.getAttribute("max"));
                    int valueNumber = Integer.parseInt(input.getValue());
                    return (valueNumber < min || valueNumber > max);
                }
            }
        case "checked":
            return (element instanceof HTMLInputElement && ((HTMLInputElement) element).isChecked()) || (element instanceof HTMLOptionElement && ((HTMLOptionElement) element).isSelected());
        case "required":
            return (element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement) && element.hasAttribute("required");
        case "optional":
            return (element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement) && !element.hasAttribute("required");
        case "link":
            return (element instanceof HTMLLinkElement);
        case "visited":
            if (element instanceof HTMLLinkElement) {
                HTMLLinkElement elem = (HTMLLinkElement) element;
                return LinkStore.isVisited(elem.getHref());
            } else {
                return false;
            }
        case "first-child":
            for (Node n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof HTMLElement) {
                    return false;
                }
            }
            return true;
        case "last-child":
            for (Node n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof HTMLElement) {
                    return false;
                }
            }
            return true;
        case "first-of-type":
            final String firstType = element.getNodeName();
            for (Node n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof HTMLElement && n.getNodeName().equals(firstType)) {
                    return false;
                }
            }
            return true;
        case "last-of-type":
            final String lastType = element.getNodeName();
            for (Node n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof HTMLElement && n.getNodeName().equals(lastType)) {
                    return false;
                }
            }
            return true;
        case "only-child":
            for (Node n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof HTMLElement) {
                    return false;
                }
            }
            for (Node n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof HTMLElement) {
                    return false;
                }
            }
            return true;
        case "only-of-type":
            final String type = element.getNodeName();
            for (Node n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof HTMLElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            for (Node n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof HTMLElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            return true;
        case "empty":
            return isEmpty(element);
        default:
            if (value.startsWith("nth-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (Node n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof HTMLElement) {
                        index++;
                    }
                }
                return getNth(element, nth, index);
            } else if (value.startsWith("nth-last-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (Node n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof HTMLElement) {
                        index++;
                    }
                }
                return getNth(element, nth, index);
            } else if (value.startsWith("nth-of-type(")) {
                final String nthType = element.getNodeName();
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (Node n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof HTMLElement && n.getNodeName().equals(nthType)) {
                        index++;
                    }
                }
                return getNth(element, nth, index);
            } else if (value.startsWith("nth-last-of-type(")) {
                final String nthLastType = element.getNodeName();
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (Node n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof HTMLElement && n.getNodeName().equals(nthLastType)) {
                        index++;
                    }
                }
                return getNth(element, nth, index);
            } else if (value.startsWith("not(")) {
                final String selectors = value.substring(value.indexOf('(') + 1, value.length() - 1);
                final AtomicBoolean errorOccured = new AtomicBoolean(false);
                final CSSErrorHandler errorHandler = new CSSErrorHandler() {

                    @Override
                    public void warning(final CSSParseException exception) throws CSSException {
                    // ignore
                    }

                    @Override
                    public void fatalError(final CSSParseException exception) throws CSSException {
                        errorOccured.set(true);
                    }

                    @Override
                    public void error(final CSSParseException exception) throws CSSException {
                        errorOccured.set(true);
                    }
                };
                final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
                parser.setErrorHandler(errorHandler);
                try {
                    final SelectorList selectorList = parser.parseSelectors(selectors);
                    if (errorOccured.get() || selectorList == null || selectorList.size() != 1) {
                        throw new CSSException("Invalid selectors: " + selectors);
                    }
                    return !selects(selectorList.get(0), element, null, mouseOver);
                } catch (final IOException e) {
                    throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage());
                }
            }
            return false;
    }
}
Also used : NodeImpl(org.loboevolution.html.dom.nodeimpl.NodeImpl) Node(org.loboevolution.html.node.Node) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) HTMLInputElementImpl(org.loboevolution.html.dom.domimpl.HTMLInputElementImpl) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) CSSException(com.gargoylesoftware.css.parser.CSSException)

Example 10 with CSSParseException

use of com.gargoylesoftware.css.parser.CSSParseException in project LoboEvolution by LoboEvolution.

the class CSS3Parser method importRule.

// 
// import
// : IMPORT_SYM S*
// [STRING|URI] S* [ medium [ COMMA S* medium]* ]? ';' S*
// ;
// 
public final void importRule(final boolean nonImportRuleFoundBefore) throws ParseException {
    Token t;
    MediaQueryList ml = new MediaQueryList();
    Locator locator;
    try {
        ParseException e = null;
        if (nonImportRuleFoundBefore) {
            e = generateParseException();
        }
        jj_consume_token(IMPORT_SYM);
        locator = createLocator(token);
        label_7: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[13] = jj_gen;
                    break label_7;
            }
            jj_consume_token(S);
        }
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case STRING:
                {
                    t = jj_consume_token(STRING);
                    break;
                }
            case URI:
                {
                    t = jj_consume_token(URI);
                    break;
                }
            default:
                jj_la1[14] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        label_8: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[15] = jj_gen;
                    break label_8;
            }
            jj_consume_token(S);
        }
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case NOT:
            case ONLY:
            case IDENT:
            case LROUND:
                {
                    mediaList(ml);
                    break;
                }
            default:
                jj_la1[16] = jj_gen;
                ;
        }
        jj_consume_token(SEMICOLON);
        if (nonImportRuleFoundBefore) {
            getErrorHandler().error(toCSSParseException("invalidImportRuleIgnored2", e));
        } else {
            handleImportStyle(unescape(t.image, false), ml, null, locator);
        }
    } catch (CSSParseException e) {
        getErrorHandler().error(e);
        error_skipAtRule();
    } catch (ParseException e) {
        getErrorHandler().error(toCSSParseException("invalidImportRule", e));
        error_skipAtRule();
    }
}
Also used : Locator(com.gargoylesoftware.css.parser.Locator) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) MediaQueryList(com.gargoylesoftware.css.parser.media.MediaQueryList) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException)

Aggregations

CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)11 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)4 Locator (com.gargoylesoftware.css.parser.Locator)4 IOException (java.io.IOException)4 CSSException (com.gargoylesoftware.css.parser.CSSException)3 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)3 MediaQueryList (com.gargoylesoftware.css.parser.media.MediaQueryList)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)2 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 LexicalUnit (com.gargoylesoftware.css.parser.LexicalUnit)1 IncorrectnessListener (com.gargoylesoftware.htmlunit.IncorrectnessListener)1 ScriptException (com.gargoylesoftware.htmlunit.ScriptException)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 WebClientOptions (com.gargoylesoftware.htmlunit.WebClientOptions)1 DisabledElement (com.gargoylesoftware.htmlunit.html.DisabledElement)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlCheckBoxInput (com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput)1