Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* CSS Parser Project
*
* Copyright (C) 1999-2011 David Schweinsberg. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* To contact the authors of the library:
*
* http://cssparser.sourceforge.net/
* mailto:[email protected]
*
*/
package com.steadystate.css.parser;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Stack;
import org.w3c.css.sac.CSSException;
import org.w3c.css.sac.ErrorHandler;
import org.w3c.css.sac.InputSource;
import org.w3c.css.sac.LexicalUnit;
import org.w3c.css.sac.Locator;
import org.w3c.css.sac.Parser;
import org.w3c.css.sac.SACMediaList;
import org.w3c.css.sac.SelectorList;
import org.w3c.css.sac.helpers.ParserFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSStyleDeclaration;
import org.w3c.dom.css.CSSStyleSheet;
import org.w3c.dom.css.CSSValue;
import com.steadystate.css.dom.CSSCharsetRuleImpl;
import com.steadystate.css.dom.CSSFontFaceRuleImpl;
import com.steadystate.css.dom.CSSImportRuleImpl;
import com.steadystate.css.dom.CSSMediaRuleImpl;
import com.steadystate.css.dom.CSSOMObject;
import com.steadystate.css.dom.CSSPageRuleImpl;
import com.steadystate.css.dom.CSSRuleListImpl;
import com.steadystate.css.dom.CSSStyleDeclarationImpl;
import com.steadystate.css.dom.CSSStyleRuleImpl;
import com.steadystate.css.dom.CSSStyleSheetImpl;
import com.steadystate.css.dom.CSSUnknownRuleImpl;
import com.steadystate.css.dom.CSSValueImpl;
import com.steadystate.css.dom.MediaListImpl;
import com.steadystate.css.dom.Property;
import com.steadystate.css.sac.DocumentHandlerExt;
import com.steadystate.css.userdata.UserDataConstants;
/**
* @author David Schweinsberg
*/
public class CSSOMParser {
private static final String DEFAULT_PARSER = "com.steadystate.css.parser.SACParserCSS21";
private static String LastFailed_;
private Parser parser_;
private CSSStyleSheetImpl parentStyleSheet_;
/** Creates new CSSOMParser */
public CSSOMParser() {
this (null);
}
/**
* Creates new CSSOMParser.
*
* @param parser the SAC Parser
*/
public CSSOMParser(final Parser parser) {
synchronized (DEFAULT_PARSER) {
if (null != parser) {
System.setProperty("org.w3c.css.sac.parser", parser.getClass().getCanonicalName());
parser_ = parser;
return;
}
// no parser provided, determine the correct one
String currentParser = System.getProperty("org.w3c.css.sac.parser");
try {
// use the direct method if we already failed once before
if (null != LastFailed_ && LastFailed_.equals(currentParser)) {
parser_ = new SACParserCSS21();
}
else {
if (null == currentParser) {
System.setProperty("org.w3c.css.sac.parser", DEFAULT_PARSER);
currentParser = DEFAULT_PARSER;
}
final ParserFactory factory = new ParserFactory();
parser_ = factory.makeParser();
}
}
catch (final Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
System.err.println("using the default parser instead");
LastFailed_ = currentParser;
parser_ = new SACParserCSS21();
}
}
}
public void setErrorHandler(final ErrorHandler eh) {
parser_.setErrorHandler(eh);
}
/**
* 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
* ownerNode in org.w3c.dom.css.StyleSheet)
* @param href the href (see the definition of href 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);
this.parser_.setDocumentHandler(handler);
this.parser_.parseStyleSheet(source);
final Object o = handler.getRoot();
if (o instanceof CSSStyleSheet) {
return (CSSStyleSheet) handler.getRoot();
}
return null;
}
/**
* Parses a SAC input source into a CSSOM style declaration.
*
* @param source the SAC input source
* @return the CSSOM style declaration
* @throws IOException if the underlying SAC parser throws an IOException
*/
public CSSStyleDeclaration parseStyleDeclaration(final InputSource source) throws IOException {
final CSSStyleDeclarationImpl sd = new CSSStyleDeclarationImpl(null);
parseStyleDeclaration(sd, source);
return sd;
}
public void parseStyleDeclaration(final CSSStyleDeclaration sd, final InputSource source)
throws IOException {
final Stack