com.gargoylesoftware.css.parser.CSSOMParser Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2019-2021 Ronald Brill.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gargoylesoftware.css.parser;
import java.io.IOException;
import java.io.StringReader;
import java.util.Stack;
import org.w3c.dom.DOMException;
import com.gargoylesoftware.css.dom.AbstractCSSRuleImpl;
import com.gargoylesoftware.css.dom.CSSCharsetRuleImpl;
import com.gargoylesoftware.css.dom.CSSFontFaceRuleImpl;
import com.gargoylesoftware.css.dom.CSSImportRuleImpl;
import com.gargoylesoftware.css.dom.CSSMediaRuleImpl;
import com.gargoylesoftware.css.dom.CSSPageRuleImpl;
import com.gargoylesoftware.css.dom.CSSRuleListImpl;
import com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl;
import com.gargoylesoftware.css.dom.CSSStyleRuleImpl;
import com.gargoylesoftware.css.dom.CSSStyleSheetImpl;
import com.gargoylesoftware.css.dom.CSSUnknownRuleImpl;
import com.gargoylesoftware.css.dom.CSSValueImpl;
import com.gargoylesoftware.css.dom.MediaListImpl;
import com.gargoylesoftware.css.dom.Property;
import com.gargoylesoftware.css.parser.javacc.CSS3Parser;
import com.gargoylesoftware.css.parser.media.MediaQueryList;
import com.gargoylesoftware.css.parser.selector.SelectorList;
/**
* @author Ronald Brill
*/
public class CSSOMParser {
private final AbstractCSSParser parser_;
private CSSStyleSheetImpl parentStyleSheet_;
/**
* Creates new CSSOMParser.
* @param parser the parser
*/
public CSSOMParser(final AbstractCSSParser parser) {
parser_ = parser;
}
/**
* Creates new CSSOMParser.
*/
public CSSOMParser() {
parser_ = new CSS3Parser();
}
/**
* @param eh the error handler to be used
*/
public void setErrorHandler(final CSSErrorHandler eh) {
parser_.setErrorHandler(eh);
}
/**
* Parses a SAC input source into a CSSOM style sheet.
*
* @param source the SAC input source
* @param href the href
* @return the CSSOM style sheet
* @throws IOException if the underlying SAC parser throws an IOException
*/
public CSSStyleSheetImpl parseStyleSheet(final InputSource source, final String href) throws IOException {
final CSSOMHandler handler = new CSSOMHandler();
handler.setHref(href);
parser_.setDocumentHandler(handler);
parser_.parseStyleSheet(source);
final Object o = handler.getRoot();
if (o instanceof CSSStyleSheetImpl) {
return (CSSStyleSheetImpl) o;
}
return null;
}
/**
* Parses a input string into a CSSOM style declaration.
*
* @param styleDecl the input string
* @return the CSSOM style declaration
* @throws IOException if the underlying SAC parser throws an IOException
*/
public CSSStyleDeclarationImpl parseStyleDeclaration(final String styleDecl) throws IOException {
final CSSStyleDeclarationImpl sd = new CSSStyleDeclarationImpl(null);
parseStyleDeclaration(sd, styleDecl);
return sd;
}
/**
* Parses a input string into a CSSOM style declaration.
*
* @param styleDecl the input string
* @param sd the CSSOM style declaration
* @throws IOException if the underlying SAC parser throws an IOException
*/
public void parseStyleDeclaration(final CSSStyleDeclarationImpl sd, final String styleDecl) throws IOException {
try (InputSource source = new InputSource(new StringReader(styleDecl))) {
final Stack
© 2015 - 2024 Weber Informatics LLC | Privacy Policy