Search in sources :

Example 11 with CSSNode

use of com.helger.css.parser.CSSNode in project ph-css by phax.

the class CSSNodeToDomainObject method _readStyleDeclarationList.

private void _readStyleDeclarationList(@Nonnull final CSSNode aNode, @Nonnull final Consumer<CSSDeclaration> aConsumer) {
    _expectNodeType(aNode, ECSSNodeType.STYLEDECLARATIONLIST);
    // Read all contained declarations
    final int nDecls = aNode.jjtGetNumChildren();
    for (int nDecl = 0; nDecl < nDecls; ++nDecl) {
        final CSSNode aChildNode = aNode.jjtGetChild(nDecl);
        if (ECSSNodeType.STYLEDECLARATION.isNode(aChildNode, m_eVersion)) {
            final CSSDeclaration aDeclaration = _createDeclaration(aChildNode);
            if (aDeclaration != null)
                aConsumer.accept(aDeclaration);
        }
    // else
    // ignore ERROR_SKIP to and all "@" things
    }
}
Also used : CSSNode(com.helger.css.parser.CSSNode)

Example 12 with CSSNode

use of com.helger.css.parser.CSSNode in project ph-css by phax.

the class CSSNodeToDomainObject method _createNamespaceRule.

@Nonnull
private CSSNamespaceRule _createNamespaceRule(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.NAMESPACERULE);
    final int nChildCount = aNode.jjtGetNumChildren();
    if (nChildCount < 1 || nChildCount > 2)
        _throwUnexpectedChildrenCount(aNode, "Expected at least 1 child and at last 2 children but got " + nChildCount + "!");
    String sPrefix = null;
    int nURLIndex = 0;
    if (ECSSNodeType.NAMESPACERULEPREFIX.isNode(aNode.jjtGetChild(0), m_eVersion)) {
        sPrefix = aNode.jjtGetChild(0).getText();
        nURLIndex++;
    }
    final CSSNode aURLNode = aNode.jjtGetChild(nURLIndex);
    _expectNodeType(aURLNode, ECSSNodeType.NAMESPACERULEURL);
    final String sURL = CSSParseHelper.extractStringValue(aURLNode.getText());
    final CSSNamespaceRule ret = new CSSNamespaceRule(sPrefix, sURL);
    ret.setSourceLocation(aNode.getSourceLocation());
    return ret;
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull)

Example 13 with CSSNode

use of com.helger.css.parser.CSSNode in project ph-css by phax.

the class CSSNodeToDomainObject method createCascadingStyleSheetFromNode.

@Nonnull
public CascadingStyleSheet createCascadingStyleSheetFromNode(@Nonnull final CSSNode aNode) {
    _expectNodeType(aNode, ECSSNodeType.ROOT);
    final CascadingStyleSheet ret = new CascadingStyleSheet();
    ret.setSourceLocation(aNode.getSourceLocation());
    for (final CSSNode aChildNode : aNode) {
        if (ECSSNodeType.CHARSET.isNode(aChildNode, m_eVersion)) {
        // Ignore because this was handled when reading!
        } else if (ECSSNodeType.IMPORTRULE.isNode(aChildNode, m_eVersion))
            ret.addImportRule(_createImportRule(aChildNode));
        else if (ECSSNodeType.NAMESPACERULE.isNode(aChildNode, m_eVersion))
            ret.addNamespaceRule(_createNamespaceRule(aChildNode));
        else if (ECSSNodeType.STYLERULE.isNode(aChildNode, m_eVersion)) {
            final CSSStyleRule aStyleRule = _createStyleRule(aChildNode);
            if (aStyleRule != null)
                ret.addRule(aStyleRule);
        } else if (ECSSNodeType.PAGERULE.isNode(aChildNode, m_eVersion))
            ret.addRule(_createPageRule(aChildNode));
        else if (ECSSNodeType.MEDIARULE.isNode(aChildNode, m_eVersion))
            ret.addRule(_createMediaRule(aChildNode));
        else if (ECSSNodeType.FONTFACERULE.isNode(aChildNode, m_eVersion))
            ret.addRule(_createFontFaceRule(aChildNode));
        else if (ECSSNodeType.KEYFRAMESRULE.isNode(aChildNode, m_eVersion))
            ret.addRule(_createKeyframesRule(aChildNode));
        else if (ECSSNodeType.VIEWPORTRULE.isNode(aChildNode, m_eVersion))
            ret.addRule(_createViewportRule(aChildNode));
        else if (ECSSNodeType.SUPPORTSRULE.isNode(aChildNode, m_eVersion))
            ret.addRule(_createSupportsRule(aChildNode));
        else if (ECSSNodeType.UNKNOWNRULE.isNode(aChildNode, m_eVersion)) {
            // Unknown rule indicates either
            // 1. a parsing error
            // 2. a non-standard rule
            ret.addRule(_createUnknownRule(aChildNode));
        } else
            m_aErrorHandler.onCSSInterpretationError("Unsupported child of " + ECSSNodeType.getNodeName(aNode, m_eVersion) + ": " + ECSSNodeType.getNodeName(aChildNode, m_eVersion));
    }
    return ret;
}
Also used : CSSNode(com.helger.css.parser.CSSNode) Nonnull(javax.annotation.Nonnull)

Example 14 with CSSNode

use of com.helger.css.parser.CSSNode in project ph-css by phax.

the class CSSReader method isValidCSS.

/**
 * Check if the passed reader can be resembled to valid CSS content. This is
 * accomplished by fully parsing the CSS each time the method is called. This
 * is similar to calling
 * {@link #readFromStream(IHasInputStream, Charset, ECSSVersion)} and checking
 * for a non-<code>null</code> result.
 *
 * @param aReader
 *        The reader to use. May not be <code>null</code>.
 * @param eVersion
 *        The CSS version to use. May not be <code>null</code>.
 * @return <code>true</code> if the CSS is valid according to the version,
 *         <code>false</code> if not
 */
public static boolean isValidCSS(@Nonnull @WillClose final Reader aReader, @Nonnull final ECSSVersion eVersion) {
    ValueEnforcer.notNull(aReader, "Reader");
    ValueEnforcer.notNull(eVersion, "Version");
    try {
        final CSSCharStream aCharStream = new CSSCharStream(aReader);
        final CSSNode aNode = _readStyleSheet(aCharStream, eVersion, getDefaultParseErrorHandler(), new DoNothingCSSParseExceptionCallback(), false);
        return aNode != null;
    } finally {
        StreamHelper.close(aReader);
    }
}
Also used : CSSCharStream(com.helger.css.parser.CSSCharStream) CSSNode(com.helger.css.parser.CSSNode) DoNothingCSSParseExceptionCallback(com.helger.css.handler.DoNothingCSSParseExceptionCallback)

Example 15 with CSSNode

use of com.helger.css.parser.CSSNode in project ph-css by phax.

the class CSSReader method readFromReader.

/**
 * Read the CSS from the passed {@link IReaderProvider}. If the CSS contains
 * an explicit <code>@charset</code> rule, it is ignored and the charset used
 * to create the reader is used instead! Also the fallback charset from the
 * {@link CSSReaderSettings} is ignored.
 *
 * @param aRP
 *        The reader provider to use. The reader is retrieved exactly once and
 *        closed anyway. May not be <code>null</code>.
 * @param aSettings
 *        The settings to be used for reading the CSS. May not be
 *        <code>null</code>.
 * @return <code>null</code> if reading failed, the CSS declarations
 *         otherwise.
 * @since 3.8.2
 */
@Nullable
public static CascadingStyleSheet readFromReader(@Nonnull final IHasReader aRP, @Nonnull final CSSReaderSettings aSettings) {
    ValueEnforcer.notNull(aRP, "ReaderProvider");
    ValueEnforcer.notNull(aSettings, "Settings");
    // Create the reader
    final Reader aReader = aRP.getReader();
    if (aReader == null) {
        // Failed to open reader
        return null;
    }
    // No charset determination, as the Reader already has an implicit Charset
    final ECSSVersion eVersion = aSettings.getCSSVersion();
    try {
        final CSSCharStream aCharStream = new CSSCharStream(aReader);
        aCharStream.setTabSize(aSettings.getTabSize());
        // Use the default CSS parse error handler if none is provided
        ICSSParseErrorHandler aRealParseErrorHandler = aSettings.getCustomErrorHandler();
        if (aRealParseErrorHandler == null)
            aRealParseErrorHandler = getDefaultParseErrorHandler();
        // Use the default CSS exception handler if none is provided
        ICSSParseExceptionCallback aRealParseExceptionHandler = aSettings.getCustomExceptionHandler();
        if (aRealParseExceptionHandler == null)
            aRealParseExceptionHandler = getDefaultParseExceptionHandler();
        final boolean bBrowserCompliantMode = aSettings.isBrowserCompliantMode();
        final CSSNode aNode = _readStyleSheet(aCharStream, eVersion, aRealParseErrorHandler, aRealParseExceptionHandler, bBrowserCompliantMode);
        // Failed to parse content as CSS?
        if (aNode == null)
            return null;
        // Get the interpret error handler
        ICSSInterpretErrorHandler aRealInterpretErrorHandler = aSettings.getInterpretErrorHandler();
        if (aRealInterpretErrorHandler == null)
            aRealInterpretErrorHandler = getDefaultInterpretErrorHandler();
        // Convert the AST to a domain object
        return CSSHandler.readCascadingStyleSheetFromNode(eVersion, aNode, aRealInterpretErrorHandler);
    } finally {
        StreamHelper.close(aReader);
    }
}
Also used : ICSSParseErrorHandler(com.helger.css.reader.errorhandler.ICSSParseErrorHandler) ICSSInterpretErrorHandler(com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler) ECSSVersion(com.helger.css.ECSSVersion) CSSCharStream(com.helger.css.parser.CSSCharStream) IHasReader(com.helger.commons.io.IHasReader) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) Reader(java.io.Reader) ICSSParseExceptionCallback(com.helger.css.handler.ICSSParseExceptionCallback) CSSNode(com.helger.css.parser.CSSNode) Nullable(javax.annotation.Nullable)

Aggregations

CSSNode (com.helger.css.parser.CSSNode)30 Nonnull (javax.annotation.Nonnull)20 Nullable (javax.annotation.Nullable)9 ECSSVersion (com.helger.css.ECSSVersion)5 CSSCharStream (com.helger.css.parser.CSSCharStream)5 ICSSInterpretErrorHandler (com.helger.css.reader.errorhandler.ICSSInterpretErrorHandler)5 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 EModifier (com.helger.css.decl.CSSMediaQuery.EModifier)3 ICSSParseExceptionCallback (com.helger.css.handler.ICSSParseExceptionCallback)3 ICSSParseErrorHandler (com.helger.css.reader.errorhandler.ICSSParseErrorHandler)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 ValueEnforcer (com.helger.commons.ValueEnforcer)2 Nonempty (com.helger.commons.annotation.Nonempty)2 ICommonsList (com.helger.commons.collection.impl.ICommonsList)2 IHasReader (com.helger.commons.io.IHasReader)2 NonBlockingStringReader (com.helger.commons.io.stream.NonBlockingStringReader)2 com.helger.css.decl (com.helger.css.decl)2 DoNothingCSSParseExceptionCallback (com.helger.css.handler.DoNothingCSSParseExceptionCallback)2 ECSSMediaExpressionFeature (com.helger.css.media.ECSSMediaExpressionFeature)2 ECSSMedium (com.helger.css.media.ECSSMedium)2