- From: Yves Lafon via cvs-syncmail <cvsmail@w3.org>
- Date: Mon, 26 Mar 2012 12:11:27 +0000
- To: www-validator-cvs@w3.org
Update of /sources/public/2002/css-validator/org/w3c/css/css In directory hutz:/tmp/cvs-serv21683/css Modified Files: CssValidator.java DocumentParser.java HTMLParserStyleSheetHandler.java StyleSheetParser.java TagSoupStyleSheetHandler.java XMLStyleSheetHandler.java Log Message: various changes to allow direct use + some small typos Index: XMLStyleSheetHandler.java =================================================================== RCS file: /sources/public/2002/css-validator/org/w3c/css/css/XMLStyleSheetHandler.java,v retrieving revision 1.35 retrieving revision 1.36 diff -u -d -r1.35 -r1.36 --- XMLStyleSheetHandler.java 9 Feb 2012 17:36:26 -0000 1.35 +++ XMLStyleSheetHandler.java 26 Mar 2012 12:11:25 -0000 1.36 @@ -35,10 +35,7 @@ import org.xml.sax.SAXParseException; import org.xml.sax.ext.LexicalHandler; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; +import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -596,6 +593,64 @@ } } + public void parse(InputSource source, String fileName) throws IOException, SAXException { + org.xml.sax.XMLReader xmlParser = new org.apache.xerces.parsers.SAXParser(); + try { + xmlParser.setProperty("http://xml.org/sax/properties/lexical-handler", + this); + xmlParser.setFeature("http://xml.org/sax/features/namespace-prefixes", true); + xmlParser.setFeature("http://xml.org/sax/features/validation", false); + } catch (Exception ex) { + ex.printStackTrace(); + } + xmlParser.setContentHandler(this); + if (fileName != null) { + baseURI = new URL(fileName); + documentURI = new URL(fileName); + source.setSystemId(fileName); + } + URL ref = ac.getReferrer(); + try { + ac.setReferrer(documentURI); + xmlParser.parse(source); + } finally { + ac.setReferrer(ref); + } + } + + /** + * Parse an HTML document, given as an InputStream + * + * @param is the inputstream of the document + * @param docref the String version of the URI of the document + * @throws IOException + * @throws SAXException + */ + public void parse(InputStream is, String docref) throws IOException, SAXException { + InputSource inputSource = new InputSource(is); + try { + parse(inputSource, docref); + } finally { + is.close(); + } + } + + /** + * Parse an HTML document, given as a Reader + * + * @param reader the Reader of the document + * @throws IOException + * @throws SAXException + */ + public void parse(Reader reader) throws IOException, SAXException { + InputSource inputSource = new InputSource(reader); + try { + parse(inputSource, null); + } finally { + reader.close(); + } + } + HashMap<String, String> getValues(String data) { int length = data.length(); int current = 0; Index: HTMLParserStyleSheetHandler.java =================================================================== RCS file: /sources/public/2002/css-validator/org/w3c/css/css/HTMLParserStyleSheetHandler.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- HTMLParserStyleSheetHandler.java 9 Feb 2012 17:36:26 -0000 1.4 +++ HTMLParserStyleSheetHandler.java 26 Mar 2012 12:11:25 -0000 1.5 @@ -36,10 +36,7 @@ import org.xml.sax.SAXParseException; import org.xml.sax.ext.LexicalHandler; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; +import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -527,8 +524,7 @@ } } - public void parse(InputStream in, String fileName) throws IOException, SAXException { - InputSource source = new InputSource(in); + public void parse(InputSource source, String fileName) throws IOException, SAXException { org.xml.sax.XMLReader xmlParser = new nu.validator.htmlparser.sax.HtmlParser(ALLOW); try { xmlParser.setProperty("http://xml.org/sax/properties/lexical-handler", @@ -548,7 +544,38 @@ xmlParser.parse(source); } finally { ac.setReferrer(ref); - in.close(); + } + } + + /** + * Parse an HTML document, given as an InputStream + * @param is the inputstream of the document + * @param docref the String version of the URI of the document + * @throws IOException + * @throws SAXException + */ + public void parse(InputStream is, String docref) throws IOException, SAXException { + InputSource inputSource = new InputSource(is); + try { + parse(inputSource, docref); + } finally { + is.close(); + } + } + + /** + * Parse an HTML document, given as a Reader + * @param reader the Reader of the document + * @param docref the String version of the URI of the document + * @throws IOException + * @throws SAXException + */ + public void parse(Reader reader, String docref) throws IOException, SAXException { + InputSource inputSource = new InputSource(reader); + try { + parse(inputSource, docref); + } finally { + reader.close(); } } Index: DocumentParser.java =================================================================== RCS file: /sources/public/2002/css-validator/org/w3c/css/css/DocumentParser.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- DocumentParser.java 16 Feb 2012 13:29:22 -0000 1.10 +++ DocumentParser.java 26 Mar 2012 12:11:25 -0000 1.11 @@ -14,6 +14,7 @@ import org.w3c.www.mime.MimeTypeFormatException; import java.io.IOException; +import java.io.Reader; import java.net.URL; import java.net.URLConnection; @@ -37,6 +38,41 @@ private Exception exception; private ApplContext ac; + public DocumentParser(ApplContext ac, Reader reader) throws Exception { + this(ac, reader, "urn:unknown", null); + } + + public DocumentParser(ApplContext ac, Reader reader, String urlString, MimeType mediatype) throws Exception { + this.htmlURL = HTTPURL.getURL(urlString); + this.ac = ac; + String media = ac.getMedium(); + + if (mediatype == null) { + mediatype = MimeType.TEXT_CSS; + } + if (mediatype.match(MimeType.TEXT_CSS) == MimeType.MATCH_SPECIFIC_SUBTYPE) { + StyleSheetParser csshandler = new StyleSheetParser(); + csshandler.parseStyleSheet(ac, reader, htmlURL); + style = csshandler.getStyleSheet(); + } else if (mediatype.match(MimeType.TEXT_HTML) == MimeType.MATCH_SPECIFIC_SUBTYPE) { + TagSoupStyleSheetHandler htmlhandler = new TagSoupStyleSheetHandler(htmlURL, ac); + //HTMLParserStyleSheetHandler htmlhandler = new HTMLParserStyleSheetHandler(htmlURL, ac); + htmlhandler.parse(reader); + style = htmlhandler.getStyleSheet(); + if (style != null) { + style.setType("text/html"); + } + } else if (mediatype.toString().endsWith("+xml") || + (mediatype.match(MimeType.APPLICATION_XML) == MimeType.MATCH_SPECIFIC_SUBTYPE)) { + XMLStyleSheetHandler xmlhandler = new XMLStyleSheetHandler(htmlURL, ac); + xmlhandler.parse(reader); + style = xmlhandler.getStyleSheet(); + if (style != null) { + style.setType("text/xml"); + } + } + } + /** * Create a new DocumentParser * Index: TagSoupStyleSheetHandler.java =================================================================== RCS file: /sources/public/2002/css-validator/org/w3c/css/css/TagSoupStyleSheetHandler.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- TagSoupStyleSheetHandler.java 9 Feb 2012 17:36:26 -0000 1.12 +++ TagSoupStyleSheetHandler.java 26 Mar 2012 12:11:25 -0000 1.13 @@ -16,30 +16,14 @@ import org.w3c.css.parser.CssError; import org.w3c.css.parser.Errors; import org.w3c.css.parser.analyzer.TokenMgrError; -import org.w3c.css.util.ApplContext; -import org.w3c.css.util.CssVersion; -import org.w3c.css.util.HTTPURL; -import org.w3c.css.util.InvalidParamException; -import org.w3c.css.util.Util; -import org.w3c.css.util.Warning; -import org.w3c.css.util.Warnings; +import org.w3c.css.util.*; import org.w3c.css.util.xml.XMLCatalog; import org.w3c.www.mime.MimeType; import org.w3c.www.mime.MimeTypeFormatException; -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.EntityResolver; -import org.xml.sax.ErrorHandler; -import org.xml.sax.InputSource; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; +import org.xml.sax.*; import org.xml.sax.ext.LexicalHandler; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; +import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -519,8 +503,7 @@ } } - public void parse(InputStream in, String fileName) throws IOException, SAXException { - InputSource source = new InputSource(in); + public void parse(InputSource source, String fileName) throws IOException, SAXException { org.xml.sax.XMLReader xmlParser = new org.ccil.cowan.tagsoup.Parser(); try { xmlParser.setProperty("http://xml.org/sax/properties/lexical-handler", @@ -531,16 +514,50 @@ ex.printStackTrace(); } xmlParser.setContentHandler(this); - baseURI = new URL(fileName); - documentURI = new URL(fileName); - source.setSystemId(fileName); + if (fileName != null) { + baseURI = new URL(fileName); + documentURI = new URL(fileName); + source.setSystemId(fileName); + } URL ref = ac.getReferrer(); try { ac.setReferrer(documentURI); xmlParser.parse(source); } finally { ac.setReferrer(ref); - in.close(); + } + } + + /** + * Parse an HTML document, given as an InputStream + * + * @param is the inputstream of the document + * @param docref the String version of the URI of the document + * @throws IOException + * @throws SAXException + */ + public void parse(InputStream is, String docref) throws IOException, SAXException { + InputSource inputSource = new InputSource(is); + try { + parse(inputSource, docref); + } finally { + is.close(); + } + } + + /** + * Parse an HTML document, given as a Reader + * + * @param reader the Reader of the document + * @throws IOException + * @throws SAXException + */ + public void parse(Reader reader) throws IOException, SAXException { + InputSource inputSource = new InputSource(reader); + try { + parse(inputSource, null); + } finally { + reader.close(); } } Index: CssValidator.java =================================================================== RCS file: /sources/public/2002/css-validator/org/w3c/css/css/CssValidator.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- CssValidator.java 3 Oct 2011 17:07:08 -0000 1.14 +++ CssValidator.java 26 Mar 2012 12:11:25 -0000 1.15 @@ -9,17 +9,19 @@ package org.w3c.css.css; -import org.w3c.css.parser.CssSelectors; import org.w3c.css.util.ApplContext; import org.w3c.css.util.CssVersion; import org.w3c.css.util.HTTPURL; import org.w3c.css.util.Util; import org.w3c.tools.resources.ProtocolException; +import org.w3c.www.mime.MimeType; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.io.Reader; import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -38,12 +40,11 @@ // @@ HACK static boolean showCSS = false; - private Exception exception; /** * Creates a new instance of CssValidator with the following default values: * <ul> - * <li>profile = css21</li> + * <li>profile = >default<</li> * <li>medium = all</li> * <li>output = text</li> * <li>lang = en</li> @@ -61,11 +62,30 @@ params.put("vextwarning", "false"); } + public CssValidator(String profile, String medium, String lang, int warninglevel, boolean vextwarning, boolean followlinks) { + ac = new ApplContext(lang); + ac.setCssVersionAndProfile(profile); + ac.setMedium(medium); + ac.setTreatVendorExtensionsAsWarnings(vextwarning); + ac.setWarningLevel(warninglevel); + ac.setFollowlinks(followlinks); + } + + public void setOptionsFromParams() { + // CSS version to use + String profile = params.get("profile"); + ac.setCssVersionAndProfile(profile); + + // medium to use + ac.setMedium(params.get("medium")); + + String vextwarn = params.get("vextwarning"); + ac.setTreatVendorExtensionsAsWarnings("true".equalsIgnoreCase(vextwarn)); + } + public static void main(String args[]) throws IOException, MalformedURLException { - CssSelectors selector = null; - CssValidator style = new CssValidator(); // first, we get the parameters and create an application context @@ -113,15 +133,7 @@ System.exit(1); } - // CSS version to use - String profile = (String) style.params.get("profile"); - style.ac.setCssVersionAndProfile(profile); - - // medium to use - style.ac.setMedium((String) style.params.get("medium")); - - String vextwarn = (String) style.params.get("vextwarning"); - style.ac.setTreatVendorExtensionsAsWarnings("true".equalsIgnoreCase(vextwarn)); + style.setOptionsFromParams(); String encoding = style.ac.getMsg().getString("output-encoding-name"); if (encoding != null) { @@ -182,6 +194,42 @@ } + public void handleCSSStyleSheet(ApplContext ac, Reader reader, URL docref) { + StyleSheet sheet; + DocumentParser parser = null; + try { + parser = new DocumentParser(ac, reader, docref.toString(), MimeType.TEXT_CSS); + } catch (Exception ex) { + ex.printStackTrace(); + } + sheet = parser.getStyleSheet(); + sheet.findConflicts(ac); + } + + public void handleHTMLStyleSheet(ApplContext ac, Reader reader, URL docref) { + StyleSheet sheet; + DocumentParser parser = null; + try { + parser = new DocumentParser(ac, reader, docref.toString(), MimeType.TEXT_HTML); + } catch (Exception ex) { + ex.printStackTrace(); + } + sheet = parser.getStyleSheet(); + sheet.findConflicts(ac); + } + + public void handleXMLStyleSheet(ApplContext ac, Reader reader, URL docref) { + StyleSheet sheet; + DocumentParser parser = null; + try { + parser = new DocumentParser(ac, reader, docref.toString(), MimeType.APPLICATION_XML); + } catch (Exception ex) { + ex.printStackTrace(); + } + sheet = parser.getStyleSheet(); + sheet.findConflicts(ac); + } + /** * Analyses the command-line parameters * Index: StyleSheetParser.java =================================================================== RCS file: /sources/public/2002/css-validator/org/w3c/css/css/StyleSheetParser.java,v retrieving revision 1.25 retrieving revision 1.26 diff -u -d -r1.25 -r1.26 --- StyleSheetParser.java 9 Feb 2012 17:36:26 -0000 1.25 +++ StyleSheetParser.java 26 Mar 2012 12:11:25 -0000 1.26 @@ -391,6 +391,16 @@ } /** + * Unify call to the parser for css doc as a reader. + * @param ac + * @param reader + * @param docref + */ + public void parseStyleSheet(ApplContext ac, Reader reader, URL docref) { + parseStyleElement(ac, reader, null, null, (docref == null) ? ac.getFakeURL() : docref, 0); + } + + /** * Parse some declarations. All declarations always comes from the user * * @param input the inputStream containing the style data
Received on Monday, 26 March 2012 12:11:35 UTC