diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index c558b1e1..00000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "test-src/test/resources/html5lib-tests"] - path = test-src/test/resources/html5lib-tests - url = https://github.com/html5lib/html5lib-tests.git diff --git a/HtmlParser-compile b/HtmlParser-compile deleted file mode 100755 index 3e867827..00000000 --- a/HtmlParser-compile +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -APPDIR=`dirname $0`; -java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/gwt-src:$APPDIR/super:/Developer/gwt-mac-1.5.1/gwt-user.jar:/Developer/gwt-mac-1.5.1/gwt-dev-mac.jar" com.google.gwt.dev.GWTCompiler -out "$APPDIR/www" "$@" nu.validator.htmlparser.HtmlParser; diff --git a/HtmlParser-compile-detailed b/HtmlParser-compile-detailed deleted file mode 100755 index a4102d64..00000000 --- a/HtmlParser-compile-detailed +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -APPDIR=`dirname $0`; -java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/gwt-src:$APPDIR/super:/Developer/gwt-mac-1.5.1/gwt-user.jar:/Developer/gwt-mac-1.5.1/gwt-dev-mac.jar" com.google.gwt.dev.GWTCompiler -style DETAILED -out "$APPDIR/www" "$@" nu.validator.htmlparser.HtmlParser; diff --git a/HtmlParser-compile-detailed.launch b/HtmlParser-compile-detailed.launch deleted file mode 100644 index 0347fd6c..00000000 --- a/HtmlParser-compile-detailed.launch +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/HtmlParser-compile.launch b/HtmlParser-compile.launch deleted file mode 100644 index 54e7bc33..00000000 --- a/HtmlParser-compile.launch +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/HtmlParser-linux b/HtmlParser-linux deleted file mode 100755 index 0a9e9def..00000000 --- a/HtmlParser-linux +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -APPDIR=`dirname $0`; -java -Xmx256M -cp "$APPDIR/src:$APPDIR/gwt-src:$APPDIR/super:$APPDIR/bin:/home/hsivonen/gwt-linux-1.5.1/gwt-user.jar:/home/hsivonen/gwt-linux-1.5.1/gwt-dev-linux.jar" com.google.gwt.dev.GWTShell -out "$APPDIR/www" "$@" nu.validator.htmlparser.HtmlParser/HtmlParser.html; diff --git a/HtmlParser-shell b/HtmlParser-shell deleted file mode 100755 index ffcf2e29..00000000 --- a/HtmlParser-shell +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -APPDIR=`dirname $0`; -java -XstartOnFirstThread -Xmx256M -cp "$APPDIR/src:$APPDIR/gwt-src:$APPDIR/super:$APPDIR/bin:/Developer/gwt-mac-1.5.1/gwt-user.jar:/Developer/gwt-mac-1.5.1/gwt-dev-mac.jar" com.google.gwt.dev.GWTShell -out "$APPDIR/www" "$@" nu.validator.htmlparser.HtmlParser/HtmlParser.html; diff --git a/HtmlParser.launch b/HtmlParser.launch deleted file mode 100644 index 9335abf6..00000000 --- a/HtmlParser.launch +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gwt-src/nu/validator/htmlparser/HtmlParser.gwt.xml b/gwt-src/nu/validator/htmlparser/HtmlParser.gwt.xml deleted file mode 100644 index 1eab09c2..00000000 --- a/gwt-src/nu/validator/htmlparser/HtmlParser.gwt.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/gwt-src/nu/validator/htmlparser/gwt/BrowserTreeBuilder.java b/gwt-src/nu/validator/htmlparser/gwt/BrowserTreeBuilder.java deleted file mode 100644 index 2a3c5370..00000000 --- a/gwt-src/nu/validator/htmlparser/gwt/BrowserTreeBuilder.java +++ /dev/null @@ -1,580 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008-2017 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.gwt; - -import java.util.LinkedList; - -import nu.validator.htmlparser.common.DocumentMode; -import nu.validator.htmlparser.impl.CoalescingTreeBuilder; -import nu.validator.htmlparser.impl.HtmlAttributes; - -import org.xml.sax.SAXException; - -import com.google.gwt.core.client.JavaScriptException; -import com.google.gwt.core.client.JavaScriptObject; - -class BrowserTreeBuilder extends CoalescingTreeBuilder { - - private JavaScriptObject document; - - private JavaScriptObject script; - - private JavaScriptObject placeholder; - - private boolean readyToRun; - - private final LinkedList scriptStack = new LinkedList(); - - private class ScriptHolder { - private final JavaScriptObject script; - - private final JavaScriptObject placeholder; - - /** - * @param script - * @param placeholder - */ - public ScriptHolder(JavaScriptObject script, - JavaScriptObject placeholder) { - this.script = script; - this.placeholder = placeholder; - } - - /** - * Returns the script. - * - * @return the script - */ - public JavaScriptObject getScript() { - return script; - } - - /** - * Returns the placeholder. - * - * @return the placeholder - */ - public JavaScriptObject getPlaceholder() { - return placeholder; - } - } - - protected BrowserTreeBuilder(JavaScriptObject document) { - super(); - this.document = document; - installExplorerCreateElementNS(document); - } - - private static native boolean installExplorerCreateElementNS( - JavaScriptObject doc) /*-{ - if (!doc.createElementNS) { - doc.createElementNS = function (uri, local) { - if ("http://www.w3.org/1999/xhtml" == uri) { - return doc.createElement(local); - } else if ("http://www.w3.org/1998/Math/MathML" == uri) { - if (!doc.mathplayerinitialized) { - var obj = document.createElement("object"); - obj.setAttribute("id", "mathplayer"); - obj.setAttribute("classid", "clsid:32F66A20-7614-11D4-BD11-00104BD3F987"); - document.getElementsByTagName("head")[0].appendChild(obj); - document.namespaces.add("m", "http://www.w3.org/1998/Math/MathML", "#mathplayer"); - doc.mathplayerinitialized = true; - } - return doc.createElement("m:" + local); - } else if ("http://www.w3.org/2000/svg" == uri) { - if (!doc.renesisinitialized) { - var obj = document.createElement("object"); - obj.setAttribute("id", "renesis"); - obj.setAttribute("classid", "clsid:AC159093-1683-4BA2-9DCF-0C350141D7F2"); - document.getElementsByTagName("head")[0].appendChild(obj); - document.namespaces.add("s", "http://www.w3.org/2000/svg", "#renesis"); - doc.renesisinitialized = true; - } - return doc.createElement("s:" + local); - } else { - // throw - } - } - } - }-*/; - - private static native boolean hasAttributeNS(JavaScriptObject element, - String uri, String localName) /*-{ - return element.hasAttributeNS(uri, localName); - }-*/; - - private static native void setAttributeNS(JavaScriptObject element, - String uri, String localName, String value) /*-{ - element.setAttributeNS(uri, localName, value); - }-*/; - - @Override protected void addAttributesToElement(JavaScriptObject element, - HtmlAttributes attributes) throws SAXException { - try { - for (int i = 0; i < attributes.getLength(); i++) { - String localName = attributes.getLocalNameNoBoundsCheck(i); - String uri = attributes.getURINoBoundsCheck(i); - if (!hasAttributeNS(element, uri, localName)) { - setAttributeNS(element, uri, localName, - attributes.getValueNoBoundsCheck(i)); - } - } - } catch (JavaScriptException e) { - fatal(e); - } - } - - private static native void appendChild(JavaScriptObject parent, - JavaScriptObject child) /*-{ - parent.appendChild(child); - }-*/; - - private static native JavaScriptObject createTextNode(JavaScriptObject doc, - String text) /*-{ - return doc.createTextNode(text); - }-*/; - - private static native JavaScriptObject getLastChild(JavaScriptObject node) /*-{ - return node.lastChild; - }-*/; - - private static native void extendTextNode(JavaScriptObject node, String text) /*-{ - node.data += text; - }-*/; - - @Override protected void appendCharacters(JavaScriptObject parent, - String text) throws SAXException { - try { - if (parent == placeholder) { - appendChild(script, createTextNode(document, text)); - - } - JavaScriptObject lastChild = getLastChild(parent); - if (lastChild != null && getNodeType(lastChild) == 3) { - extendTextNode(lastChild, text); - return; - } - appendChild(parent, createTextNode(document, text)); - } catch (JavaScriptException e) { - fatal(e); - } - } - - private static native boolean hasChildNodes(JavaScriptObject element) /*-{ - return element.hasChildNodes(); - }-*/; - - private static native JavaScriptObject getFirstChild( - JavaScriptObject element) /*-{ - return element.firstChild; - }-*/; - - @Override protected void appendChildrenToNewParent( - JavaScriptObject oldParent, JavaScriptObject newParent) - throws SAXException { - try { - while (hasChildNodes(oldParent)) { - appendChild(newParent, getFirstChild(oldParent)); - } - } catch (JavaScriptException e) { - fatal(e); - } - } - - private static native JavaScriptObject createComment(JavaScriptObject doc, - String text) /*-{ - return doc.createComment(text); - }-*/; - - @Override protected void appendComment(JavaScriptObject parent, - String comment) throws SAXException { - try { - if (parent == placeholder) { - appendChild(script, createComment(document, comment)); - } - appendChild(parent, createComment(document, comment)); - } catch (JavaScriptException e) { - fatal(e); - } - } - - @Override protected void appendCommentToDocument(String comment) - throws SAXException { - try { - appendChild(document, createComment(document, comment)); - } catch (JavaScriptException e) { - fatal(e); - } - } - - private static native JavaScriptObject createElementNS( - JavaScriptObject doc, String ns, String local) /*-{ - return doc.createElementNS(ns, local); - }-*/; - - @Override protected JavaScriptObject createElement(String ns, String name, - HtmlAttributes attributes) throws SAXException { - try { - JavaScriptObject rv = createElementNS(document, ns, name); - for (int i = 0; i < attributes.getLength(); i++) { - setAttributeNS(rv, attributes.getURINoBoundsCheck(i), - attributes.getLocalNameNoBoundsCheck(i), - attributes.getValueNoBoundsCheck(i)); - } - - if ("script" == name) { - if (placeholder != null) { - scriptStack.addLast(new ScriptHolder(script, placeholder)); - } - script = rv; - placeholder = createElementNS(document, - "http://n.validator.nu/placeholder/", "script"); - rv = placeholder; - for (int i = 0; i < attributes.getLength(); i++) { - setAttributeNS(rv, attributes.getURINoBoundsCheck(i), - attributes.getLocalNameNoBoundsCheck(i), - attributes.getValueNoBoundsCheck(i)); - } - } - - return rv; - } catch (JavaScriptException e) { - fatal(e); - throw new RuntimeException("Unreachable"); - } - } - - @Override protected JavaScriptObject createHtmlElementSetAsRoot( - HtmlAttributes attributes) throws SAXException { - try { - JavaScriptObject rv = createElementNS(document, - "http://www.w3.org/1999/xhtml", "html"); - for (int i = 0; i < attributes.getLength(); i++) { - setAttributeNS(rv, attributes.getURINoBoundsCheck(i), - attributes.getLocalNameNoBoundsCheck(i), - attributes.getValueNoBoundsCheck(i)); - } - appendChild(document, rv); - return rv; - } catch (JavaScriptException e) { - fatal(e); - throw new RuntimeException("Unreachable"); - } - } - - private static native JavaScriptObject getParentNode( - JavaScriptObject element) /*-{ - return element.parentNode; - }-*/; - - @Override protected void appendElement(JavaScriptObject child, - JavaScriptObject newParent) throws SAXException { - try { - if (newParent == placeholder) { - appendChild(script, cloneNodeDeep(child)); - } - appendChild(newParent, child); - } catch (JavaScriptException e) { - fatal(e); - } - } - - @Override protected boolean hasChildren(JavaScriptObject element) - throws SAXException { - try { - return hasChildNodes(element); - } catch (JavaScriptException e) { - fatal(e); - throw new RuntimeException("Unreachable"); - } - } - - private static native void insertBeforeNative(JavaScriptObject parent, - JavaScriptObject child, JavaScriptObject sibling) /*-{ - parent.insertBefore(child, sibling); - }-*/; - - private static native int getNodeType(JavaScriptObject node) /*-{ - return node.nodeType; - }-*/; - - private static native JavaScriptObject cloneNodeDeep(JavaScriptObject node) /*-{ - return node.cloneNode(true); - }-*/; - - /** - * Returns the document. - * - * @return the document - */ - JavaScriptObject getDocument() { - JavaScriptObject rv = document; - document = null; - return rv; - } - - private static native JavaScriptObject createDocumentFragment( - JavaScriptObject doc) /*-{ - return doc.createDocumentFragment(); - }-*/; - - JavaScriptObject getDocumentFragment() { - JavaScriptObject rv = createDocumentFragment(document); - JavaScriptObject rootElt = getFirstChild(document); - while (hasChildNodes(rootElt)) { - appendChild(rv, getFirstChild(rootElt)); - } - document = null; - return rv; - } - - /** - * @see nu.validator.htmlparser.impl.TreeBuilder#createJavaScriptObject(String, - * java.lang.String, org.xml.sax.Attributes, java.lang.Object) - */ - @Override protected JavaScriptObject createElement(String ns, String name, - HtmlAttributes attributes, JavaScriptObject form) - throws SAXException { - try { - JavaScriptObject rv = createElement(ns, name, attributes); - // rv.setUserData("nu.validator.form-pointer", form, null); - return rv; - } catch (JavaScriptException e) { - fatal(e); - return null; - } - } - - /** - * @see nu.validator.htmlparser.impl.TreeBuilder#start() - */ - @Override protected void start(boolean fragment) throws SAXException { - script = null; - placeholder = null; - readyToRun = false; - } - - protected void documentMode(DocumentMode mode, String publicIdentifier, - String systemIdentifier) - throws SAXException { - // document.setUserData("nu.validator.document-mode", mode, null); - } - - /** - * @see nu.validator.htmlparser.impl.TreeBuilder#elementPopped(java.lang.String, - * java.lang.String, java.lang.Object) - */ - @Override protected void elementPopped(String ns, String name, - JavaScriptObject node) throws SAXException { - if (node == placeholder) { - readyToRun = true; - requestSuspension(); - } - } - - private static native void replace(JavaScriptObject oldNode, - JavaScriptObject newNode) /*-{ - oldNode.parentNode.replaceChild(newNode, oldNode); - }-*/; - - private static native JavaScriptObject getPreviousSibling(JavaScriptObject node) /*-{ - return node.previousSibling; - }-*/; - - void maybeRunScript() { - if (readyToRun) { - readyToRun = false; - replace(placeholder, script); - if (scriptStack.isEmpty()) { - script = null; - placeholder = null; - } else { - ScriptHolder scriptHolder = scriptStack.removeLast(); - script = scriptHolder.getScript(); - placeholder = scriptHolder.getPlaceholder(); - } - } - } - - @Override protected void insertFosterParentedCharacters(String text, - JavaScriptObject table, JavaScriptObject stackParent) - throws SAXException { - try { - JavaScriptObject parent = getParentNode(table); - if (parent != null) { // always an element if not null - JavaScriptObject previousSibling = getPreviousSibling(table); - if (previousSibling != null - && getNodeType(previousSibling) == 3) { - extendTextNode(previousSibling, text); - return; - } - insertBeforeNative(parent, createTextNode(document, text), table); - return; - } - JavaScriptObject lastChild = getLastChild(stackParent); - if (lastChild != null && getNodeType(lastChild) == 3) { - extendTextNode(lastChild, text); - return; - } - appendChild(stackParent, createTextNode(document, text)); - } catch (JavaScriptException e) { - fatal(e); - } - } - - @Override protected void insertFosterParentedChild(JavaScriptObject child, - JavaScriptObject table, JavaScriptObject stackParent) - throws SAXException { - JavaScriptObject parent = getParentNode(table); - try { - if (parent != null && getNodeType(parent) == 1) { - insertBeforeNative(parent, child, table); - } else { - appendChild(stackParent, child); - } - } catch (JavaScriptException e) { - fatal(e); - } - } - - private static native void removeChild(JavaScriptObject parent, - JavaScriptObject child) /*-{ - parent.removeChild(child); - }-*/; - - @Override protected void detachFromParent(JavaScriptObject element) - throws SAXException { - try { - JavaScriptObject parent = getParentNode(element); - if (parent != null) { - removeChild(parent, element); - } - } catch (JavaScriptException e) { - fatal(e); - } - } - - private static native JavaScriptObject getNextSibling( - JavaScriptObject node) /*-{ - return node.nextSibling; - }-*/; - - private static native String getLocalName( - JavaScriptObject node) /*-{ - return node.localName; - }-*/; - - private static native String getNamespaceURI( - JavaScriptObject node) /*-{ - return node.namespaceURI; - }-*/; - - private static native boolean hasAttribute( - JavaScriptObject node, String name) /*-{ - return node.hasAttribute(name); - }-*/; - - @Override - // https://html.spec.whatwg.org/multipage/form-elements.html#maybe-clone-an-option-into-selectedcontent - // Implements "maybe clone an option into selectedcontent" - protected void optionElementPopped(JavaScriptObject option) - throws SAXException { - try { - // Find the nearest ancestor element - JavaScriptObject ancestor = getParentNode(option); - JavaScriptObject select = null; - while (ancestor != null && getNodeType(ancestor) == 1) { - if ("select".equals(getLocalName(ancestor)) - && "http://www.w3.org/1999/xhtml".equals( - getNamespaceURI(ancestor))) { - select = ancestor; - break; - } - ancestor = getParentNode(ancestor); - } - if (select == null) { - return; - } - if (hasAttribute(select, "multiple")) { - return; - } - - // Find the first descendant of - JavaScriptObject selectedContent = findSelectedContent( - select); - if (selectedContent == null) { - return; - } - - // Check option selectedness - boolean hasSelectedAttr = hasAttribute(option, "selected"); - if (!hasSelectedAttr && hasChildNodes(selectedContent)) { - // Not the first option and no explicit selected attr - return; - } - - // Clear selectedcontent children and deep-clone option children - while (hasChildNodes(selectedContent)) { - removeChild(selectedContent, getFirstChild(selectedContent)); - } - for (JavaScriptObject child = getFirstChild(option); - child != null; child = getNextSibling(child)) { - appendChild(selectedContent, cloneNodeDeep(child)); - } - } catch (JavaScriptException e) { - fatal(e); - } - } - - private JavaScriptObject findSelectedContent( - JavaScriptObject root) { - JavaScriptObject current = getFirstChild(root); - if (current == null) { - return null; - } - JavaScriptObject next; - for (;;) { - if (getNodeType(current) == 1 - && "selectedcontent".equals(getLocalName(current)) - && "http://www.w3.org/1999/xhtml".equals( - getNamespaceURI(current))) { - return current; - } - if ((next = getFirstChild(current)) != null) { - current = next; - continue; - } - for (;;) { - if (current == root) { - return null; - } - if ((next = getNextSibling(current)) != null) { - current = next; - break; - } - current = getParentNode(current); - } - } - } -} diff --git a/gwt-src/nu/validator/htmlparser/gwt/HtmlParser.java b/gwt-src/nu/validator/htmlparser/gwt/HtmlParser.java deleted file mode 100644 index 1d71cdfd..00000000 --- a/gwt-src/nu/validator/htmlparser/gwt/HtmlParser.java +++ /dev/null @@ -1,265 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2007-2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.gwt; - -import java.util.LinkedList; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.impl.ErrorReportingTokenizer; -import nu.validator.htmlparser.impl.Tokenizer; -import nu.validator.htmlparser.impl.UTF16Buffer; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -import com.google.gwt.core.client.JavaScriptObject; -import com.google.gwt.user.client.Timer; - -/** - * This class implements an HTML5 parser that exposes data through the DOM - * interface. - * - * By default, when using the constructor without arguments, the - * this parser treats XML 1.0-incompatible infosets as fatal errors. - * This corresponds to - * FATAL as the general XML violation policy. To make the parser - * support non-conforming HTML fully per the HTML 5 spec while on the other - * hand potentially violating the DOM API contract, set the general XML - * violation policy to ALLOW. This does not work with a standard - * DOM implementation. Handling all input without fatal errors and without - * violating the DOM API contract is possible by setting - * the general XML violation policy to ALTER_INFOSET. This - * makes the parser non-conforming but is probably the most useful - * setting for most applications. - * - * The doctype is not represented in the tree. - * - * The document mode is represented as user data DocumentMode - * object with the key nu.validator.document-mode on the document - * node. - * - * The form pointer is also stored as user data with the key - * nu.validator.form-pointer. - * - * @version $Id: HtmlDocumentBuilder.java 255 2008-05-29 08:57:38Z hsivonen $ - * @author hsivonen - */ -public class HtmlParser { - - private static final int CHUNK_SIZE = 512; - - private final Tokenizer tokenizer; - - private final BrowserTreeBuilder domTreeBuilder; - - private final StringBuilder documentWriteBuffer = new StringBuilder(); - - private ErrorHandler errorHandler; - - private UTF16Buffer stream; - - private int streamLength; - - private boolean lastWasCR; - - private boolean ending; - - private ParseEndListener parseEndListener; - - private final LinkedList bufferStack = new LinkedList(); - - /** - * Instantiates the parser - * - * @param implementation - * the DOM implementation - * @param xmlPolicy the policy - */ - public HtmlParser(JavaScriptObject document) { - this.domTreeBuilder = new BrowserTreeBuilder(document); - this.tokenizer = new ErrorReportingTokenizer(domTreeBuilder); - this.domTreeBuilder.setNamePolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setCommentPolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setContentNonXmlCharPolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setContentSpacePolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setNamePolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setXmlnsPolicy(XmlViolationPolicy.ALTER_INFOSET); - } - - /** - * Parses a document from a SAX InputSource. - * @param is the source - * @return the doc - * @see javax.xml.parsers.DocumentBuilder#parse(org.xml.sax.InputSource) - */ - public void parse(String source, ParseEndListener callback) throws SAXException { - parseEndListener = callback; - domTreeBuilder.setFragmentContext(null); - tokenize(source, null); - } - - /** - * @param is - * @throws SAXException - * @throws IOException - * @throws MalformedURLException - */ - private void tokenize(String source, String context) throws SAXException { - lastWasCR = false; - ending = false; - documentWriteBuffer.setLength(0); - streamLength = source.length(); - stream = new UTF16Buffer(source.toCharArray(), 0, - (streamLength < CHUNK_SIZE ? streamLength : CHUNK_SIZE)); - bufferStack.clear(); - push(stream); - domTreeBuilder.setFragmentContext(context == null ? null : context.intern()); - tokenizer.start(); - pump(); - } - - private void pump() throws SAXException { - if (ending) { - tokenizer.end(); - domTreeBuilder.getDocument(); // drops the internal reference - parseEndListener.parseComplete(); - // Don't schedule timeout - return; - } - - int docWriteLen = documentWriteBuffer.length(); - if (docWriteLen > 0) { - char[] newBuf = new char[docWriteLen]; - documentWriteBuffer.getChars(0, docWriteLen, newBuf, 0); - push(new UTF16Buffer(newBuf, 0, docWriteLen)); - documentWriteBuffer.setLength(0); - } - - for (;;) { - UTF16Buffer buffer = peek(); - if (!buffer.hasMore()) { - if (buffer == stream) { - if (buffer.getEnd() == streamLength) { - // Stop parsing - tokenizer.eof(); - ending = true; - break; - } else { - int newEnd = buffer.getStart() + CHUNK_SIZE; - buffer.setEnd(newEnd < streamLength ? newEnd - : streamLength); - continue; - } - } else { - pop(); - continue; - } - } - // now we have a non-empty buffer - buffer.adjust(lastWasCR); - lastWasCR = false; - if (buffer.hasMore()) { - lastWasCR = tokenizer.tokenizeBuffer(buffer); - domTreeBuilder.maybeRunScript(); - break; - } else { - continue; - } - } - - // schedule - Timer timer = new Timer() { - - @Override public void run() { - try { - pump(); - } catch (SAXException e) { - ending = true; - if (errorHandler != null) { - try { - errorHandler.fatalError(new SAXParseException( - e.getMessage(), null, null, -1, -1, e)); - } catch (SAXException e1) { - } - } - } - } - - }; - timer.schedule(1); - } - - private void push(UTF16Buffer buffer) { - bufferStack.addLast(buffer); - } - - private UTF16Buffer peek() { - return bufferStack.getLast(); - } - - private void pop() { - bufferStack.removeLast(); - } - - public void documentWrite(String text) throws SAXException { - UTF16Buffer buffer = new UTF16Buffer(text.toCharArray(), 0, text.length()); - while (buffer.hasMore()) { - buffer.adjust(lastWasCR); - lastWasCR = false; - if (buffer.hasMore()) { - lastWasCR = tokenizer.tokenizeBuffer(buffer); - domTreeBuilder.maybeRunScript(); - } - } - } - - /** - * @see javax.xml.parsers.DocumentBuilder#setErrorHandler(org.xml.sax.ErrorHandler) - */ - public void setErrorHandler(ErrorHandler errorHandler) { - this.errorHandler = errorHandler; - domTreeBuilder.setErrorHandler(errorHandler); - tokenizer.setErrorHandler(errorHandler); - } - - /** - * Sets whether comment nodes appear in the tree. - * @param ignoreComments true to ignore comments - * @see nu.validator.htmlparser.impl.TreeBuilder#setIgnoringComments(boolean) - */ - public void setIgnoringComments(boolean ignoreComments) { - domTreeBuilder.setIgnoringComments(ignoreComments); - } - - /** - * Sets whether the parser considers scripting to be enabled for noscript treatment. - * @param scriptingEnabled true to enable - * @see nu.validator.htmlparser.impl.TreeBuilder#setScriptingEnabled(boolean) - */ - public void setScriptingEnabled(boolean scriptingEnabled) { - domTreeBuilder.setScriptingEnabled(scriptingEnabled); - } - -} diff --git a/gwt-src/nu/validator/htmlparser/gwt/HtmlParserModule.java b/gwt-src/nu/validator/htmlparser/gwt/HtmlParserModule.java deleted file mode 100644 index 255a02d1..00000000 --- a/gwt-src/nu/validator/htmlparser/gwt/HtmlParserModule.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.gwt; - -import org.xml.sax.SAXException; - -import com.google.gwt.core.client.EntryPoint; -import com.google.gwt.core.client.JavaScriptObject; - -public class HtmlParserModule implements EntryPoint { - - private static native void zapChildren(JavaScriptObject node) /*-{ - while (node.hasChildNodes()) { - node.removeChild(node.lastChild); - } - }-*/; - - private static native void installDocWrite(JavaScriptObject doc, HtmlParser parser) /*-{ - doc.write = function() { - if (arguments.length == 0) { - return; - } - var text = arguments[0]; - for (var i = 1; i < arguments.length; i++) { - text += arguments[i]; - } - parser.@nu.validator.htmlparser.gwt.HtmlParser::documentWrite(Ljava/lang/String;)(text); - } - doc.writeln = function() { - if (arguments.length == 0) { - parser.@nu.validator.htmlparser.gwt.HtmlParser::documentWrite(Ljava/lang/String;)("\n"); - return; - } - var text = arguments[0]; - for (var i = 1; i < arguments.length; i++) { - text += arguments[i]; - } - text += "\n"; - parser.@nu.validator.htmlparser.gwt.HtmlParser::documentWrite(Ljava/lang/String;)(text); - } - }-*/; - - @SuppressWarnings("unused") - private static void parseHtmlDocument(String source, JavaScriptObject document, JavaScriptObject readyCallback, JavaScriptObject errorHandler) throws SAXException { - if (readyCallback == null) { - readyCallback = JavaScriptObject.createFunction(); - } - zapChildren(document); - HtmlParser parser = new HtmlParser(document); - parser.setScriptingEnabled(true); - // XXX error handler - - installDocWrite(document, parser); - - parser.parse(source, new ParseEndListener(readyCallback)); - } - - private static native void exportEntryPoints() /*-{ - $wnd.parseHtmlDocument = @nu.validator.htmlparser.gwt.HtmlParserModule::parseHtmlDocument(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;Lcom/google/gwt/core/client/JavaScriptObject;Lcom/google/gwt/core/client/JavaScriptObject;); - }-*/; - - - public void onModuleLoad() { - exportEntryPoints(); - } - -} diff --git a/gwt-src/nu/validator/htmlparser/gwt/ParseEndListener.java b/gwt-src/nu/validator/htmlparser/gwt/ParseEndListener.java deleted file mode 100644 index 43235c5b..00000000 --- a/gwt-src/nu/validator/htmlparser/gwt/ParseEndListener.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.gwt; - -import com.google.gwt.core.client.JavaScriptObject; - -public class ParseEndListener { - - private final JavaScriptObject callback; - - /** - * @param callback - */ - public ParseEndListener(JavaScriptObject callback) { - this.callback = callback; - } - - public void parseComplete() { - call(callback); - } - - private static native void call(JavaScriptObject callback) /*-{ - callback(); - }-*/; - -} diff --git a/gwt-src/nu/validator/htmlparser/public/HtmlParser.html b/gwt-src/nu/validator/htmlparser/public/HtmlParser.html deleted file mode 100644 index 4d9cde81..00000000 --- a/gwt-src/nu/validator/htmlparser/public/HtmlParser.html +++ /dev/null @@ -1,225 +0,0 @@ - - - - Live DOM Viewer - - - - - - - Live DOM Viewer - Markup to test (permalink, upload, download, hide): - <!DOCTYPE html> -... - DOM view (hide, refresh): - - Rendered view: (hide): - - innerHTML view: (show, refresh): - <!DOCTYPE HTML><html></html> - Log: (hide): - Script not loaded. - - This script puts a function w(s) into the - global scope of the test page, where s is a string to - output to the log. Also, five files are accessible in the current - directory for test purposes: image (a GIF image), - flash (a Flash file), script (a JS file), - style (a CSS file), and document (an HTML - file). - - \ No newline at end of file diff --git a/gwt-src/nu/validator/htmlparser/public/LICENSE.Live-DOM-viewer.txt b/gwt-src/nu/validator/htmlparser/public/LICENSE.Live-DOM-viewer.txt deleted file mode 100644 index bd2f4fcf..00000000 --- a/gwt-src/nu/validator/htmlparser/public/LICENSE.Live-DOM-viewer.txt +++ /dev/null @@ -1,25 +0,0 @@ -From: -http://software.hixie.ch/utilities/js/live-dom-viewer/LICENSE -regarding the upstream of HtmlParser.html: - -The MIT License - -Copyright (c) 2000, 2006, 2008 Ian Hickson and various contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/gwt-src/nu/validator/htmlparser/public/blank.html b/gwt-src/nu/validator/htmlparser/public/blank.html deleted file mode 100644 index a8756c9f..00000000 --- a/gwt-src/nu/validator/htmlparser/public/blank.html +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/mozilla-export-scripts/README.txt b/mozilla-export-scripts/README.txt deleted file mode 100644 index 3567b846..00000000 --- a/mozilla-export-scripts/README.txt +++ /dev/null @@ -1,25 +0,0 @@ -These scripts export the Java-to-C++ translator and the java source files that -implement the HTML5 parser. The exported translator may be used (with no -external dependencies) to translate the exported java source files into Gecko- -compatible C++. - -Hacking the translator itself still requires a working copy of the Java HTML5 -parser repository, but hacking the parser (modifying the Java source files and -performing the translation) should now be possible using only files committed -to the mozilla source tree. - -Run any of these scripts without arguments to receive usage instructions. - - make-translator-jar.sh: compiles the Java-to-C++ translator into a .jar file - export-java-srcs.sh: exports minimal java source files implementing the - HTML5 parser - export-translator.sh: exports the compiled translator and javaparser.jar - export-all.sh: runs the previous two scripts - util.sh: provides various shell utility functions to the - scripts listed above (does nothing if run directly) - -All path arguments may be either absolute or relative. This includes the path -to the script itself ($0), so the directory from which you run these scripts -doesn't matter. - -Ben Newman (7 July 2009) diff --git a/mozilla-export-scripts/export-all.sh b/mozilla-export-scripts/export-all.sh deleted file mode 100644 index 9ae07d33..00000000 --- a/mozilla-export-scripts/export-all.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env sh - -SCRIPT_DIR=`dirname $0` -source $SCRIPT_DIR/util.sh -SCRIPT_DIR=`abs $SCRIPT_DIR` - -if [ $# -eq 1 ] -then - MOZ_PARSER_PATH=`abs $1` -else - echo - echo "Usage: sh `basename $0` /path/to/mozilla-central/parser/html" - echo "Note that relative paths will work just fine." - echo - exit 1 -fi - -$SCRIPT_DIR/export-translator.sh $MOZ_PARSER_PATH -$SCRIPT_DIR/export-java-srcs.sh $MOZ_PARSER_PATH - -echo -echo "Now go to $MOZ_PARSER_PATH and run" -echo " java -jar javalib/translator.jar javasrc . nsHtml5AtomList.h" -echo diff --git a/mozilla-export-scripts/export-java-srcs.sh b/mozilla-export-scripts/export-java-srcs.sh deleted file mode 100644 index 6d32b07d..00000000 --- a/mozilla-export-scripts/export-java-srcs.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env sh - -SCRIPT_DIR=`dirname $0` -source $SCRIPT_DIR/util.sh -SCRIPT_DIR=`abs $SCRIPT_DIR` - -SRCDIR=`abs $SCRIPT_DIR/../src/nu/validator/htmlparser/impl` - -if [ $# -eq 1 ] -then - MOZ_PARSER_PATH=`abs $1` -else - echo - echo "Usage: sh `basename $0` /path/to/mozilla-central/parser/html" - echo "Note that relative paths will work just fine." - echo - exit 1 -fi - -SRCTARGET=$MOZ_PARSER_PATH/javasrc - -rm -rf $SRCTARGET -mkdir $SRCTARGET -# Avoid copying the .svn directory: -cp -rv $SRCDIR/*.java $SRCTARGET diff --git a/mozilla-export-scripts/export-translator.sh b/mozilla-export-scripts/export-translator.sh deleted file mode 100644 index d1f4f1c3..00000000 --- a/mozilla-export-scripts/export-translator.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env sh - -SCRIPT_DIR=`dirname $0` -source $SCRIPT_DIR/util.sh -SCRIPT_DIR=`abs $SCRIPT_DIR` - -LIBDIR=`abs $SCRIPT_DIR/../translator-lib` - -if [ $# -eq 1 ] -then - MOZ_PARSER_PATH=`abs $1` -else - echo - echo "Usage: sh `basename $0` /path/to/mozilla-central/parser/html" - echo "Note that relative paths will work just fine." - echo "Be sure that you have run `dirname $0`/make-translator-jar.sh before running this script." - echo - exit 1 -fi - -LIBTARGET=$MOZ_PARSER_PATH/javalib - -rm -rf $LIBTARGET -cp -rv $LIBDIR $LIBTARGET diff --git a/mozilla-export-scripts/make-translator-jar.sh b/mozilla-export-scripts/make-translator-jar.sh deleted file mode 100644 index 4f21ae66..00000000 --- a/mozilla-export-scripts/make-translator-jar.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env sh - -SCRIPT_DIR=`dirname $0` -source $SCRIPT_DIR/util.sh -SCRIPT_DIR=`abs $SCRIPT_DIR` - -SRCDIR=`abs $SCRIPT_DIR/../translator-src` -BINDIR=`abs $SCRIPT_DIR/../translator-bin` -LIBDIR=`abs $SCRIPT_DIR/../translator-lib` - -if [ $# -eq 1 ] -then - JAVAPARSER_JAR_PATH=`abs $1` -else - echo - echo "Usage: sh `basename $0` /path/to/javaparser-1.0.7.jar" - echo "Note that relative paths will work just fine." - echo "Obtain javaparser-1.0.7.jar from http://code.google.com/p/javaparser" - echo - exit 1 -fi - -set_up() { - rm -rf $BINDIR; mkdir $BINDIR - rm -rf $LIBDIR; mkdir $LIBDIR - cp $JAVAPARSER_JAR_PATH $LIBDIR/javaparser.jar -} - -write_manifest() { - rm -f $LIBDIR/manifest - echo "Main-Class: nu.validator.htmlparser.cpptranslate.Main" > $LIBDIR/manifest - echo "Class-Path: javaparser.jar" >> $LIBDIR/manifest -} - -compile_translator() { - find $SRCDIR -name "*.java" | \ - xargs javac -cp $LIBDIR/javaparser.jar -g -d $BINDIR -} - -generate_jar() { - jar cvfm $LIBDIR/translator.jar $LIBDIR/manifest -C $BINDIR . -} - -clean_up() { - rm -f $LIBDIR/manifest -} - -success_message() { - echo - echo "Successfully generated directory \"$LIBDIR\" with contents:" - echo - ls -al $LIBDIR - echo - echo "Now run `dirname $0`/export-all.sh with no arguments and follow the usage instructions." - echo -} - -set_up && \ - compile_translator && \ - write_manifest && \ - generate_jar && \ - clean_up && \ - success_message diff --git a/mozilla-export-scripts/util.sh b/mozilla-export-scripts/util.sh deleted file mode 100644 index 348ca14f..00000000 --- a/mozilla-export-scripts/util.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env sh - -abs() { - local rel - local p - if [ $# -ne 1 ] - then - rel=. - else - rel=$1 - fi - if [ -d $rel ] - then - pushd $rel > /dev/null - p=`pwd` - popd > /dev/null - else - pushd `dirname $rel` > /dev/null - p=`pwd`/`basename $rel` - popd > /dev/null - fi - echo $p -} diff --git a/test-src/nu/validator/htmlparser/test/DecoderLoopTester.java b/test-src/nu/validator/htmlparser/test/DecoderLoopTester.java deleted file mode 100644 index 3337a655..00000000 --- a/test-src/nu/validator/htmlparser/test/DecoderLoopTester.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.CharsetEncoder; -import java.nio.charset.CodingErrorAction; - -import nu.validator.htmlparser.common.Heuristics; -import nu.validator.htmlparser.io.Encoding; -import nu.validator.htmlparser.io.HtmlInputStreamReader; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; - -public class DecoderLoopTester { - - private static final int LEAD_OFFSET = 0xD800 - (0x10000 >> 10); - - private static final int NUMBER_OR_ASTRAL_CHARS = 24500; - - private void runTest(int padding) throws SAXException, IOException { - Encoding utf8 = Encoding.forName("UTF-8"); - char[] charArr = new char[1 + padding + 2 * NUMBER_OR_ASTRAL_CHARS]; - byte[] byteArr; - int i = 0; - charArr[i++] = '\uFEFF'; - for (int j = 0; j < padding; j++) { - charArr[i++] = 'x'; - } - for (int j = 0; j < NUMBER_OR_ASTRAL_CHARS; j++) { - int value = 0x10000 + j; - charArr[i++] = (char) (LEAD_OFFSET + (value >> 10)); - charArr[i++] = (char) (0xDC00 + (value & 0x3FF)); -// charArr[i++] = 'y'; -// charArr[i++] = 'z'; - - } - CharBuffer charBuffer = CharBuffer.wrap(charArr); - CharsetEncoder enc = utf8.newEncoder(); - enc.onMalformedInput(CodingErrorAction.REPORT); - enc.onUnmappableCharacter(CodingErrorAction.REPORT); - ByteBuffer byteBuffer = enc.encode(charBuffer); - byteArr = new byte[byteBuffer.limit()]; - byteBuffer.get(byteArr); - - ErrorHandler eh = new SystemErrErrorHandler(); - compare(new HtmlInputStreamReader(new ByteArrayInputStream(byteArr), eh, null, null, Heuristics.NONE), padding, charArr, byteArr); - compare(new HtmlInputStreamReader(new ByteArrayInputStream(byteArr), eh, null, null, utf8), padding, charArr, byteArr); - } - - /** - * @param padding - * @param charArr - * @param byteArr - * @throws SAXException - * @throws IOException - */ - private void compare(HtmlInputStreamReader reader, int padding, char[] charArr, byte[] byteArr) throws SAXException, IOException { - char[] readBuffer = new char[2048]; - int offset = 0; - int num = 0; - int readNum = 0; - while ((num = reader.read(readBuffer)) != -1) { - for (int j = 0; j < num; j++) { - System.out.println(offset + j); - if (readBuffer[j] != charArr[offset + j]) { - throw new RuntimeException("Test failed. Char: " + Integer.toHexString(readBuffer[j]) + " j: " + j + " readNum: " + readNum); - } - } - offset += num; - readNum++; - } - } - - void runTests() throws SAXException, IOException { - for (int i = 0; i < 4; i++) { - runTest(i); - } - } - - /** - * @param args - * @throws IOException - * @throws SAXException - */ - public static void main(String[] args) throws IOException, SAXException { - new DecoderLoopTester().runTests(); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/DomIdTester.java b/test-src/nu/validator/htmlparser/test/DomIdTester.java deleted file mode 100644 index a3866f5d..00000000 --- a/test-src/nu/validator/htmlparser/test/DomIdTester.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.IOException; -import java.io.StringReader; - -import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import nu.validator.htmlparser.dom.HtmlDocumentBuilder; - -public class DomIdTester { - - private static final String testSrc = "buoeoauoeuo"; - - /** - * @param args - * @throws IOException - * @throws SAXException - */ - public static void main(String[] args) throws SAXException, IOException { - HtmlDocumentBuilder builder = new HtmlDocumentBuilder(); - Document doc = builder.parse(new InputSource(new StringReader(testSrc))); - System.out.println(doc.getElementById("foo").getLocalName()); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/DomTest.java b/test-src/nu/validator/htmlparser/test/DomTest.java deleted file mode 100644 index 07d054b9..00000000 --- a/test-src/nu/validator/htmlparser/test/DomTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2009 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -public class DomTest { - public static void main(String[] args) throws Exception { - DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); - f.setNamespaceAware(true); // not setting this causes pain and suffering with SVG - DocumentBuilder b = f.newDocumentBuilder(); - Document d = b.newDocument(); - Element e = d.createElementNS("http://www.w3.org/1999/xhtml", "html"); - e.setAttribute("xmlns:foo", "bar"); - } -} diff --git a/test-src/nu/validator/htmlparser/test/EncodingTester.java b/test-src/nu/validator/htmlparser/test/EncodingTester.java deleted file mode 100644 index f0dba50a..00000000 --- a/test-src/nu/validator/htmlparser/test/EncodingTester.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -import nu.validator.htmlparser.common.Heuristics; -import nu.validator.htmlparser.io.Encoding; -import nu.validator.htmlparser.io.HtmlInputStreamReader; - -import org.xml.sax.SAXException; - -public class EncodingTester { - - static int exitStatus = 0; - - protected static int SNIFFING_LIMIT = 16384; - - private final InputStream aggregateStream; - - private final StringBuilder builder = new StringBuilder(); - - /** - * @param aggregateStream - */ - public EncodingTester(InputStream aggregateStream) { - this.aggregateStream = aggregateStream; - } - - void runTests() throws IOException, SAXException { - while (runTest()) { - // spin - } - } - - @SuppressWarnings("resource") - private boolean runTest() throws IOException, SAXException { - if (skipLabel()) { - return false; - } - UntilHashInputStream stream = new UntilHashInputStream(aggregateStream); - HtmlInputStreamReader reader = new HtmlInputStreamReader(stream, null, - null, null, Heuristics.NONE, SNIFFING_LIMIT); - Charset charset = reader.getCharset(); - stream.close(); - if (skipLabel()) { - exitStatus = 1; - System.err.println("Premature end of test data."); - return false; - } - builder.setLength(0); - loop: for (;;) { - int b = aggregateStream.read(); - switch (b) { - case '\n': - break loop; - case -1: - exitStatus = 1; - System.err.println("Premature end of test data."); - return false; - default: - builder.append(((char) b)); - } - } - String sniffed = charset.name(); - String expected = Encoding.forName(builder.toString()).newDecoder().charset().name(); - if (expected.equalsIgnoreCase(sniffed)) { - // System.err.println(stream); - } else { - exitStatus = 1; - System.err.println("Failure. Expected: " + expected + " got " - + sniffed + "."); - System.err.println(stream); - } - return true; - } - - private boolean skipLabel() throws IOException { - int b = aggregateStream.read(); - if (b == -1) { - return true; - } - for (;;) { - b = aggregateStream.read(); - if (b == -1) { - return true; - } else if (b == 0x0A) { - return false; - } - } - } - - /** - * @param args - * @throws SAXException - * @throws IOException - */ - public static void main(String[] args) throws IOException, SAXException { - for (int i = 0; i < args.length; i++) { - EncodingTester tester = new EncodingTester(new FileInputStream( - args[i])); - tester.runTests(); - } - System.exit(exitStatus); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/Html5libTest.java b/test-src/nu/validator/htmlparser/test/Html5libTest.java deleted file mode 100644 index 724062e2..00000000 --- a/test-src/nu/validator/htmlparser/test/Html5libTest.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (c) 2020 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.function.Consumer; - -public class Html5libTest { - - private final Path testDir; - - public Html5libTest() throws URISyntaxException { - this.testDir = Paths.get( - Html5libTest.class.getResource("/html5lib-tests").toURI()); - } - - public void testEncoding() throws Exception { - Files.walkFileTree(testDir.resolve("encoding"), // - new TestVisitor(true, ".dat", file -> // - new EncodingTester(Files.newInputStream(file)).runTests())); - if (EncodingTester.exitStatus != 0) { - assert false : "Encoding test failed"; - } - } - - public void testTokenizer() throws Exception { - Files.walkFileTree(testDir.resolve("tokenizer"), - new TestVisitor(true, ".test", file -> // - new TokenizerTester(getDoubleEscapedInput(file)).runTests())); - if (TokenizerTester.exitStatus != 0) { - assert false : "Tokenizer test failed"; - } - } - - public void testTree() throws Exception { - Files.walkFileTree(testDir.resolve("tree-construction"), - new TestVisitor(true, ".dat", file -> // - new TreeTester(Files.newInputStream(file)).runTests())); - if (TreeTester.exitStatus != 0) { - assert false : "Tree test failed"; - } - } - - private ByteArrayInputStream getDoubleEscapedInput(Path file) - throws IOException { - byte[] fileBytes = Files.readAllBytes(file); - String fileContent = new String(fileBytes, StandardCharsets.UTF_8); - String unescapedContent = fileContent.replace("\\\\u", "\\u"); - byte[] newBytes = unescapedContent.getBytes(StandardCharsets.UTF_8); - return new ByteArrayInputStream(newBytes); - } - - private interface TestConsumer extends Consumer { - - @Override - default void accept(Path t) { - try { - acceptTest(t); - } catch (Throwable e) { - throw new AssertionError(e); - } - } - - void acceptTest(Path t) throws Throwable; - - } - - private static class TestVisitor extends SimpleFileVisitor { - - private final boolean skipScripted; - - private final String requiredTestExtension; - - private final TestConsumer runner; - - private TestVisitor(boolean skipScripted, String requiredTestExtension, - TestConsumer runner) { - this.skipScripted = skipScripted; - this.requiredTestExtension = requiredTestExtension; - this.runner = runner; - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, - BasicFileAttributes attrs) throws IOException { - if (skipScripted - && dir.getFileName().equals(Paths.get("scripted"))) { - return FileVisitResult.SKIP_SUBTREE; - } - - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) - throws IOException { - if (file.getFileName().toString().endsWith(requiredTestExtension)) { - runner.accept(file); - } - return FileVisitResult.CONTINUE; - } - } - -} diff --git a/test-src/nu/validator/htmlparser/test/JSONArrayTokenHandler.java b/test-src/nu/validator/htmlparser/test/JSONArrayTokenHandler.java deleted file mode 100644 index 298f406a..00000000 --- a/test-src/nu/validator/htmlparser/test/JSONArrayTokenHandler.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -import com.sdicons.json.model.JSONArray; -import com.sdicons.json.model.JSONBoolean; -import com.sdicons.json.model.JSONNull; -import com.sdicons.json.model.JSONObject; -import com.sdicons.json.model.JSONString; - -import nu.validator.htmlparser.common.TokenHandler; -import nu.validator.htmlparser.impl.ElementName; -import nu.validator.htmlparser.impl.HtmlAttributes; -import nu.validator.htmlparser.impl.Tokenizer; - -public class JSONArrayTokenHandler implements TokenHandler, ErrorHandler { - - private static final JSONString DOCTYPE = new JSONString("DOCTYPE"); - - private static final JSONString START_TAG = new JSONString("StartTag"); - - private static final JSONString END_TAG = new JSONString("EndTag"); - - private static final JSONString COMMENT = new JSONString("Comment"); - - private static final JSONString CHARACTER = new JSONString("Character"); - - private static final JSONString PARSE_ERROR = new JSONString("ParseError"); - - private static final char[] REPLACEMENT_CHARACTER = { '\uFFFD' }; - - private static final char[] NULL = { '\u0000' }; - - private final StringBuilder builder = new StringBuilder(); - - private JSONArray array = null; - - private int contentModelFlag; - - private String contentModelElement; - - public void setContentModelFlag(int contentModelFlag, String contentModelElement) { - this.contentModelFlag = contentModelFlag; - this.contentModelElement = contentModelElement; - } - - public void characters(char[] buf, int start, int length) - throws SAXException { - builder.append(buf, start, length); - } - - private void flushCharacters() { - if (builder.length() > 0) { - JSONArray token = new JSONArray(); - token.getValue().add(CHARACTER); - token.getValue().add(new JSONString(builder.toString())); - array.getValue().add(token); - builder.setLength(0); - } - } - - public void comment(char[] buf, int start, int length) throws SAXException { - flushCharacters(); - JSONArray token = new JSONArray(); - token.getValue().add(COMMENT); - token.getValue().add(new JSONString(new String(buf, start, length))); - array.getValue().add(token); - } - - public void doctype(String name, String publicIdentifier, String systemIdentifier, boolean forceQuirks) throws SAXException { - flushCharacters(); - JSONArray token = new JSONArray(); - token.getValue().add(DOCTYPE); - token.getValue().add(name == null ? JSONNull.NULL : new JSONString(name)); - token.getValue().add(publicIdentifier == null ? JSONNull.NULL : new JSONString(publicIdentifier)); - token.getValue().add(systemIdentifier == null ? JSONNull.NULL : new JSONString(systemIdentifier)); - token.getValue().add(new JSONBoolean(!forceQuirks)); - array.getValue().add(token); - } - - public void endTag(ElementName eltName) throws SAXException { - String name = eltName.getName(); - flushCharacters(); - JSONArray token = new JSONArray(); - token.getValue().add(END_TAG); - token.getValue().add(new JSONString(name)); - array.getValue().add(token); - } - - public void eof() throws SAXException { - flushCharacters(); - } - - public void startTokenization(Tokenizer self) throws SAXException { - array = new JSONArray(); - if (contentModelElement != null) { - self.setStateAndEndTagExpectation(contentModelFlag, contentModelElement); - } - } - - public void startTag(ElementName eltName, HtmlAttributes attributes, - boolean selfClosing) throws SAXException { - String name = eltName.getName(); - flushCharacters(); - JSONArray token = new JSONArray(); - token.getValue().add(START_TAG); - token.getValue().add(new JSONString(name)); - JSONObject attrs = new JSONObject(); - for (int i = 0; i < attributes.getLength(); i++) { - attrs.getValue().put(attributes.getQNameNoBoundsCheck(i), - new JSONString(attributes.getValueNoBoundsCheck(i))); - } - token.getValue().add(attrs); - if (selfClosing) { - token.getValue().add(JSONBoolean.TRUE); - } - array.getValue().add(token); - } - - public boolean wantsComments() throws SAXException { - return true; - } - - public void error(SAXParseException exception) throws SAXException { -// flushCharacters(); -// array.getValue().add(PARSE_ERROR); - } - - public void fatalError(SAXParseException exception) throws SAXException { - throw new RuntimeException("Should never happen."); - } - - public void warning(SAXParseException exception) throws SAXException { - } - - /** - * Returns the array. - * - * @return the array - */ - public JSONArray getArray() { - return array; - } - - public void endTokenization() throws SAXException { - - } - - @Override public void zeroOriginatingReplacementCharacter() - throws SAXException { - builder.append(REPLACEMENT_CHARACTER, 0, 1); - } - - @Override public void zeroOrReplacementCharacter() - throws SAXException { - builder.append(NULL, 0, 1); - } - - @Override public boolean cdataSectionAllowed() throws SAXException { - return false; - } - - @Override public void ensureBufferSpace(int inputLength) - throws SAXException { - } - -} diff --git a/test-src/nu/validator/htmlparser/test/ListErrorHandler.java b/test-src/nu/validator/htmlparser/test/ListErrorHandler.java deleted file mode 100644 index 9a207f27..00000000 --- a/test-src/nu/validator/htmlparser/test/ListErrorHandler.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.util.LinkedList; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -public class ListErrorHandler implements ErrorHandler { - - private boolean fatal = false; - - private LinkedList errors = new LinkedList(); - - public void error(SAXParseException spe) throws SAXException { - errors.add(Integer.toString(spe.getColumnNumber()) + ": " + spe.getMessage()); - } - - public void fatalError(SAXParseException arg0) throws SAXException { - fatal = true; - } - - public void warning(SAXParseException arg0) throws SAXException { - } - - /** - * Returns the errors. - * - * @return the errors - */ - public LinkedList getErrors() { - return errors; - } - - /** - * Returns the fatal. - * - * @return the fatal - */ - public boolean isFatal() { - return fatal; - } - -} diff --git a/test-src/nu/validator/htmlparser/test/SystemErrErrorHandler.java b/test-src/nu/validator/htmlparser/test/SystemErrErrorHandler.java deleted file mode 100644 index 9ee490b9..00000000 --- a/test-src/nu/validator/htmlparser/test/SystemErrErrorHandler.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (c) 2005, 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.UnsupportedEncodingException; -import java.io.Writer; - -import javax.xml.transform.ErrorListener; -import javax.xml.transform.SourceLocator; -import javax.xml.transform.TransformerException; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -/** - * @version $Id$ - * @author hsivonen - */ -public class SystemErrErrorHandler implements ErrorHandler, ErrorListener { - - private Writer out; - - private boolean inError = false; - - public SystemErrErrorHandler() { - try { - out = new OutputStreamWriter(System.err, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - - /** - * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException) - */ - public void warning(SAXParseException e) throws SAXException { - try { - out.write("Warning:\n"); - out.write(e.getMessage()); - out.write("\nFile: "); - String systemId = e.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(e.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(e.getColumnNumber())); - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new SAXException(e1); - } - } - - /** - * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException) - */ - public void error(SAXParseException e) throws SAXException { - inError = true; - try { - out.write("Error:\n"); - out.write(e.getMessage()); - out.write("\nFile: "); - String systemId = e.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(e.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(e.getColumnNumber())); - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new SAXException(e1); - } - } - - /** - * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException) - */ - public void fatalError(SAXParseException e) throws SAXException { - inError = true; - try { - out.write("Fatal Error:\n"); - out.write(e.getMessage()); - out.write("\nFile: "); - String systemId = e.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(e.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(e.getColumnNumber())); - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new SAXException(e1); - } - } - - /** - * Returns the inError. - * - * @return the inError - */ - public boolean isInError() { - return inError; - } - - public void reset() { - inError = false; - } - - public void error(TransformerException e) throws TransformerException { - inError = true; - try { - out.write("Error:\n"); - out.write(e.getMessage()); - SourceLocator sourceLocator = e.getLocator(); - if (sourceLocator != null) { - out.write("\nFile: "); - String systemId = sourceLocator.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(sourceLocator.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(sourceLocator.getColumnNumber())); - } - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new TransformerException(e1); - } - } - - public void fatalError(TransformerException e) - throws TransformerException { - inError = true; - try { - out.write("Fatal Error:\n"); - out.write(e.getMessage()); - SourceLocator sourceLocator = e.getLocator(); - if (sourceLocator != null) { - out.write("\nFile: "); - String systemId = sourceLocator.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(sourceLocator.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(sourceLocator.getColumnNumber())); - } - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new TransformerException(e1); - } - } - - public void warning(TransformerException e) - throws TransformerException { - try { - out.write("Warning:\n"); - out.write(e.getMessage()); - SourceLocator sourceLocator = e.getLocator(); - if (sourceLocator != null) { - out.write("\nFile: "); - String systemId = sourceLocator.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(sourceLocator.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(sourceLocator.getColumnNumber())); - } - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new TransformerException(e1); - } - } - -} diff --git a/test-src/nu/validator/htmlparser/test/TokenPrinter.java b/test-src/nu/validator/htmlparser/test/TokenPrinter.java deleted file mode 100755 index 5fb16ce6..00000000 --- a/test-src/nu/validator/htmlparser/test/TokenPrinter.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -import nu.validator.htmlparser.common.TokenHandler; -import nu.validator.htmlparser.impl.ElementName; -import nu.validator.htmlparser.impl.ErrorReportingTokenizer; -import nu.validator.htmlparser.impl.HtmlAttributes; -import nu.validator.htmlparser.impl.Tokenizer; -import nu.validator.htmlparser.io.Driver; - -public class TokenPrinter implements TokenHandler, ErrorHandler { - - private final Writer writer; - - public void characters(char[] buf, int start, int length) - throws SAXException { - try { - boolean lineStarted = true; - writer.write('-'); - for (int i = start; i < start + length; i++) { - if (!lineStarted) { - writer.write("\n-"); - lineStarted = true; - } - char c = buf[i]; - if (c == '\n') { - writer.write("\\n"); - lineStarted = false; - } else { - writer.write(c); - } - } - writer.write('\n'); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void comment(char[] buf, int start, int length) throws SAXException { - try { - writer.write('!'); - writer.write(buf, start, length); - writer.write('\n'); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void doctype(String name, String publicIdentifier, String systemIdentifier, boolean forceQuirks) throws SAXException { - try { - writer.write('D'); - writer.write(name); - writer.write(' '); - writer.write("" + forceQuirks); - writer.write('\n'); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void endTag(ElementName eltName) throws SAXException { - try { - writer.write(')'); - writer.write(eltName.getName()); - writer.write('\n'); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void eof() throws SAXException { - try { - writer.write("E\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void startTokenization(Tokenizer self) throws SAXException { - - } - - public void startTag(ElementName eltName, HtmlAttributes attributes, boolean selfClosing) - throws SAXException { - try { - writer.write('('); - writer.write(eltName.getName()); - writer.write('\n'); - for (int i = 0; i < attributes.getLength(); i++) { - writer.write('A'); - writer.write(attributes.getQNameNoBoundsCheck(i)); - writer.write(' '); - writer.write(attributes.getValueNoBoundsCheck(i)); - writer.write('\n'); - } - } catch (IOException e) { - throw new SAXException(e); - } - } - - public boolean wantsComments() throws SAXException { - return true; - } - - public static void main(String[] args) throws SAXException, IOException { - TokenPrinter printer = new TokenPrinter(new OutputStreamWriter(System.out, "UTF-8")); - Driver tokenizer = new Driver(new ErrorReportingTokenizer(printer)); - tokenizer.setErrorHandler(printer); - File file = new File(args[0]); - InputSource is = new InputSource(new FileInputStream(file)); - is.setSystemId(file.toURI().toASCIIString()); - tokenizer.tokenize(is); - } - - /** - * @param writer - */ - public TokenPrinter(final Writer writer) { - this.writer = writer; - } - - public void error(SAXParseException exception) throws SAXException { - try { - writer.write("R "); - writer.write(exception.getMessage()); - writer.write("\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void fatalError(SAXParseException exception) throws SAXException { - try { - writer.write("F "); - writer.write(exception.getMessage()); - writer.write("\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void warning(SAXParseException exception) throws SAXException { - try { - writer.write("W "); - writer.write(exception.getMessage()); - writer.write("\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void endTokenization() throws SAXException { - try { - writer.flush(); - writer.close(); - } catch (IOException e) { - throw new SAXException(e); - } - } - - @Override public void zeroOriginatingReplacementCharacter() - throws SAXException { - try { - writer.write("0\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - @Override public void zeroOrReplacementCharacter() - throws SAXException { - zeroOriginatingReplacementCharacter(); - } - - @Override public boolean cdataSectionAllowed() throws SAXException { - return false; - } - - @Override public void ensureBufferSpace(int inputLength) - throws SAXException { - } -} diff --git a/test-src/nu/validator/htmlparser/test/TokenizerTester.java b/test-src/nu/validator/htmlparser/test/TokenizerTester.java deleted file mode 100644 index 7048dca6..00000000 --- a/test-src/nu/validator/htmlparser/test/TokenizerTester.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.ByteArrayInputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.StringReader; -import java.io.UnsupportedEncodingException; -import java.io.Writer; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.impl.ErrorReportingTokenizer; -import nu.validator.htmlparser.impl.Tokenizer; -import nu.validator.htmlparser.io.Driver; - -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import antlr.RecognitionException; -import antlr.TokenStreamException; - -import com.sdicons.json.model.JSONArray; -import com.sdicons.json.model.JSONObject; -import com.sdicons.json.model.JSONString; -import com.sdicons.json.model.JSONValue; -import com.sdicons.json.parser.JSONParser; - -public class TokenizerTester { - - static int exitStatus = 0; - - private static JSONString PLAINTEXT = new JSONString("PLAINTEXT state"); - - private static JSONString PCDATA = new JSONString("Data state"); - - private static JSONString RCDATA = new JSONString("RCDATA state"); - - private static JSONString CDATA = new JSONString("CDATA section state"); - - private static JSONString RAWTEXT = new JSONString("RAWTEXT state"); - - private static JSONString SCRIPT_DATA = new JSONString("Script data state"); - - private static boolean jsonDeepEquals(JSONValue one, JSONValue other) { - if (one.isSimple()) { - return one.equals(other); - } else if (one.isArray()) { - if (other.isArray()) { - JSONArray oneArr = (JSONArray) one; - JSONArray otherArr = (JSONArray) other; - return oneArr.getValue().equals(otherArr.getValue()); - } else { - return false; - } - } else if (one.isObject()) { - if (other.isObject()) { - JSONObject oneObject = (JSONObject) one; - JSONObject otherObject = (JSONObject) other; - return oneObject.getValue().equals(otherObject.getValue()); - } else { - return false; - } - } else { - throw new RuntimeException("Should never happen."); - } - } - - private JSONArray tests; - - private final JSONArrayTokenHandler tokenHandler; - - private final Driver driver; - - private final Writer writer; - - public TokenizerTester(InputStream stream) throws TokenStreamException, - RecognitionException, UnsupportedEncodingException { - tokenHandler = new JSONArrayTokenHandler(); - driver = new Driver(new ErrorReportingTokenizer(tokenHandler)); - driver.setCommentPolicy(XmlViolationPolicy.ALLOW); - driver.setContentNonXmlCharPolicy(XmlViolationPolicy.ALLOW); - driver.setContentSpacePolicy(XmlViolationPolicy.ALLOW); - driver.setNamePolicy(XmlViolationPolicy.ALLOW); - driver.setXmlnsPolicy(XmlViolationPolicy.ALLOW); - driver.setErrorHandler(tokenHandler); - driver.dontSwallowBom(); - writer = new OutputStreamWriter(System.out, "UTF-8"); - JSONParser jsonParser = new JSONParser(new InputStreamReader(stream, - "UTF-8")); - JSONObject obj = (JSONObject) jsonParser.nextValue(); - tests = (JSONArray) obj.get("tests"); - if (tests == null) { - tests = (JSONArray) obj.get("xmlViolationTests"); - driver.setCommentPolicy(XmlViolationPolicy.ALTER_INFOSET); - driver.setContentNonXmlCharPolicy(XmlViolationPolicy.ALTER_INFOSET); - driver.setNamePolicy(XmlViolationPolicy.ALTER_INFOSET); - driver.setXmlnsPolicy(XmlViolationPolicy.ALTER_INFOSET); - } - } - - void runTests() throws SAXException, IOException { - for (JSONValue val : tests.getValue()) { - runTest((JSONObject) val); - } - writer.flush(); - } - - private void runTest(JSONObject test) throws SAXException, IOException { - String inputString = ((JSONString) test.get("input")).getValue(); - JSONArray expectedTokens = (JSONArray) test.get("output"); - String description = ((JSONString) test.get("description")).getValue(); - JSONString lastStartTagJSON = ((JSONString) test.get("lastStartTag")); - String lastStartTag = lastStartTagJSON == null ? null - : lastStartTagJSON.getValue(); - JSONArray contentModelFlags = (JSONArray) test.get("initialStates"); - if (contentModelFlags == null) { - runTestInner(inputString, expectedTokens, description, - Tokenizer.DATA, null); - } else { - for (JSONValue value : contentModelFlags.getValue()) { - if (PCDATA.equals(value)) { - lastStartTag = lastStartTag == null ? "xmp" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.DATA, lastStartTag); - } else if (RAWTEXT.equals(value)) { - lastStartTag = lastStartTag == null ? "xmp" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.RAWTEXT, lastStartTag); - } else if (RCDATA.equals(value)) { - lastStartTag = lastStartTag == null ? "xmp" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.RCDATA, lastStartTag); - } else if (CDATA.equals(value)) { - lastStartTag = lastStartTag == null ? "xmp" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.CDATA_SECTION, lastStartTag); - } else if (PLAINTEXT.equals(value)) { - lastStartTag = lastStartTag == null ? "plaintext" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.PLAINTEXT, lastStartTag); - } else if (SCRIPT_DATA.equals(value)) { - lastStartTag = lastStartTag == null ? "script" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.SCRIPT_DATA, lastStartTag); - } else { - throw new RuntimeException("Broken test data."); - } - } - } - } - - /** - * @param contentModelElement - * @param contentModelFlag - * @param test - * @throws SAXException - * @throws IOException - */ - private void runTestInner(String inputString, JSONArray expectedTokens, - String description, int contentModelFlag, - String contentModelElement) throws SAXException, IOException { - tokenHandler.setContentModelFlag(contentModelFlag, contentModelElement); - InputSource is = new InputSource(new StringReader(inputString)); - try { - driver.tokenize(is); - JSONArray actualTokens = tokenHandler.getArray(); - if (!jsonDeepEquals(actualTokens, expectedTokens)) { - exitStatus = 1; - writer.write("Failure\n"); - writer.write(description); - writer.write("\nInput:\n"); - writer.write(inputString); - writer.write("\nExpected tokens:\n"); - writer.write(expectedTokens.render(false)); - writer.write("\nActual tokens:\n"); - writer.write(actualTokens.render(false)); - writer.write("\n"); - } - } catch (Throwable t) { - exitStatus = 1; - writer.write("Failure\n"); - writer.write(description); - writer.write("\nInput:\n"); - writer.write(inputString); - writer.write("\n"); - t.printStackTrace(new PrintWriter(writer, false)); - } - } - - /** - * @param args - * @throws RecognitionException - * @throws TokenStreamException - * @throws IOException - * @throws SAXException - */ - public static void main(String[] args) throws TokenStreamException, - RecognitionException, SAXException, IOException { - for (int i = 0; i < args.length; i++) { - byte[] fileBytes = Files.readAllBytes(Paths.get(args[i])); - String fileContent = new String(fileBytes, StandardCharsets.UTF_8); - String unescapedContent = fileContent.replace("\\\\u", "\\u"); - byte[] newBytes = unescapedContent.getBytes(StandardCharsets.UTF_8); - ByteArrayInputStream bais = new ByteArrayInputStream(newBytes); - TokenizerTester tester = new TokenizerTester(bais); - tester.runTests(); - } - System.exit(exitStatus); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/TreeDumpContentHandler.java b/test-src/nu/validator/htmlparser/test/TreeDumpContentHandler.java deleted file mode 100644 index 9b95b763..00000000 --- a/test-src/nu/validator/htmlparser/test/TreeDumpContentHandler.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.IOException; -import java.io.Writer; -import java.util.Map; -import java.util.TreeMap; - -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.ext.LexicalHandler; - -public class TreeDumpContentHandler implements ContentHandler, LexicalHandler { - - private final Writer writer; - - private int level = 0; - - private boolean inCharacters = false; - - private boolean close; - - /** - * @param writer - */ - public TreeDumpContentHandler(final Writer writer, boolean close) { - this.writer = writer; - this.close = close; - } - - public TreeDumpContentHandler(final Writer writer) { - this(writer, true); - } - - private void printLead() throws IOException { - if (inCharacters) { - writer.write("\"\n"); - inCharacters = false; - } - writer.write("| "); - for (int i = 0; i < level; i++) { - writer.write(" "); - } - } - - public void characters(char[] ch, int start, int length) - throws SAXException { - try { - if (!inCharacters) { - printLead(); - writer.write('"'); - inCharacters = true; - } - writer.write(ch, start, length); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void endElement(String uri, String localName, String qName) - throws SAXException { - try { - if (inCharacters) { - writer.write("\"\n"); - inCharacters = false; - } - level--; - if ("http://www.w3.org/1999/xhtml" == uri && - "template" == localName) { - // decrement level for the "content" - level--; - } - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void startElement(String uri, String localName, String qName, - Attributes atts) throws SAXException { - try { - printLead(); - writer.write('<'); - if ("http://www.w3.org/1998/Math/MathML" == uri) { - writer.write("math "); - } else if ("http://www.w3.org/2000/svg" == uri) { - writer.write("svg "); - } else if ("http://www.w3.org/1999/xhtml" != uri) { - writer.write("otherns "); - } - writer.write(localName); - writer.write(">\n"); - level++; - TreeMap map = new TreeMap(); - for (int i = 0; i < atts.getLength(); i++) { - String ns = atts.getURI(i); - String name; - if ("http://www.w3.org/1999/xlink" == ns) { - name = "xlink " + atts.getLocalName(i); - } else if ("http://www.w3.org/XML/1998/namespace" == ns) { - name = "xml " + atts.getLocalName(i); - } else if ("http://www.w3.org/2000/xmlns/" == ns) { - name = "xmlns " + atts.getLocalName(i); - } else if ("" != uri) { - name = atts.getLocalName(i); - } else { - name = "otherns " + atts.getLocalName(i); - } - map.put(name, atts.getValue(i)); - } - for (Map.Entry entry : map.entrySet()) { - printLead(); - writer.write(entry.getKey()); - writer.write("=\""); - writer.write(entry.getValue()); - writer.write("\"\n"); - } - if ("http://www.w3.org/1999/xhtml" == uri && - "template" == localName) { - printLead(); - level++; - writer.write("content\n"); - } - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void comment(char[] ch, int offset, int len) throws SAXException { - try { - printLead(); - writer.write("\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void startDTD(String name, String publicIdentifier, - String systemIdentifier) throws SAXException { - try { - printLead(); - writer.write(" 0 || systemIdentifier.length() > 0) { - writer.write(' '); - writer.write('\"'); - writer.write(publicIdentifier); - writer.write('\"'); - writer.write(' '); - writer.write('\"'); - writer.write(systemIdentifier); - writer.write('\"'); - } - writer.write(">\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void endDocument() throws SAXException { - try { - if (inCharacters) { - writer.write("\"\n"); - inCharacters = false; - } - if (close) { - writer.flush(); - writer.close(); - } - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void startPrefixMapping(String prefix, String uri) - throws SAXException { - } - - public void startEntity(String arg0) throws SAXException { - } - - public void endCDATA() throws SAXException { - } - - public void endDTD() throws SAXException { - } - - public void endEntity(String arg0) throws SAXException { - } - - public void startCDATA() throws SAXException { - } - - public void endPrefixMapping(String prefix) throws SAXException { - } - - public void ignorableWhitespace(char[] ch, int start, int length) - throws SAXException { - } - - public void processingInstruction(String target, String data) - throws SAXException { - } - - public void setDocumentLocator(Locator locator) { - } - - public void skippedEntity(String name) throws SAXException { - } - - public void startDocument() throws SAXException { - } - -} diff --git a/test-src/nu/validator/htmlparser/test/TreePrinter.java b/test-src/nu/validator/htmlparser/test/TreePrinter.java deleted file mode 100644 index c0916938..00000000 --- a/test-src/nu/validator/htmlparser/test/TreePrinter.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; - -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlParser; - -public class TreePrinter { - - public static void main(String[] args) throws SAXException, IOException { - TreeDumpContentHandler treeDumpContentHandler = new TreeDumpContentHandler(new OutputStreamWriter(System.out, "UTF-8")); - HtmlParser htmlParser = new HtmlParser(); - htmlParser.setContentHandler(treeDumpContentHandler); - htmlParser.setLexicalHandler(treeDumpContentHandler); - htmlParser.setErrorHandler(new SystemErrErrorHandler()); - htmlParser.setXmlPolicy(XmlViolationPolicy.ALLOW); - File file = new File(args[0]); - InputSource is = new InputSource(new FileInputStream(file)); - is.setSystemId(file.toURI().toASCIIString()); - htmlParser.parse(is); - } -} diff --git a/test-src/nu/validator/htmlparser/test/TreeTester.java b/test-src/nu/validator/htmlparser/test/TreeTester.java deleted file mode 100644 index b19cbe96..00000000 --- a/test-src/nu/validator/htmlparser/test/TreeTester.java +++ /dev/null @@ -1,279 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.BufferedInputStream; -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.StringWriter; -import java.util.LinkedList; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlParser; - -import org.xml.sax.InputSource; -import org.xml.sax.SAXParseException; - -public class TreeTester { - - private final BufferedInputStream aggregateStream; - - private boolean streaming = false; - - static int exitStatus = 0; - - /** - * @param aggregateStream - */ - public TreeTester(InputStream aggregateStream) { - this.aggregateStream = new BufferedInputStream(aggregateStream); - } - - void runTests() throws Throwable { - if (aggregateStream.read() != '#') { - System.err.println("No hash at start!"); - return; - } - while (runTest()) { - // spin - } - } - - @SuppressWarnings("resource") - private boolean runTest() throws Throwable { - UntilHashInputStream stream = null; - try { - String context = null; - boolean scriptingEnabled = true; - boolean hadScriptingDirective = false; - aggregateStream.mark(12288); - if (skipLabel()) { // #data - return false; - } - stream = new UntilHashInputStream(aggregateStream); - while (stream.read() != -1) { - // spin - } - if (skipLabel()) { // #errors - System.err.println("Premature end of test data."); - return false; - } - stream = new UntilHashInputStream(aggregateStream); - while (stream.read() != -1) { - // spin - } - - boolean newErrors = false; - StringBuilder sb = new StringBuilder(); - int c; - while ((c = aggregateStream.read()) != '\n') { - sb.append((char) c); - } - String label = sb.toString(); - if ("new-errors".equals(label)) { - newErrors = true; - stream = new UntilHashInputStream(aggregateStream); - while (stream.read() != -1) { - // spin - } - sb.setLength(0); - while ((c = aggregateStream.read()) != '\n') { - sb.append((char) c); - } - label = sb.toString(); - } - if ("document-fragment".equals(label)) { - sb.setLength(0); - while ((c = aggregateStream.read()) != '\n') { - sb.append((char) c); - } - context = sb.toString(); - // Now potentially gather #script-on/off - sb.setLength(0); - while ((c = aggregateStream.read()) != '\n') { - sb.append((char) c); - } - label = sb.toString(); - } - if ("script-on".equals(label)) { - hadScriptingDirective = true; - } else if ("script-off".equals(label)) { - hadScriptingDirective = true; - scriptingEnabled = false; - } - aggregateStream.reset(); - if (skipLabel()) { // #data - System.err.println("Premature end of test data."); - return false; - } - stream = new UntilHashInputStream(aggregateStream); - InputSource is = new InputSource(stream); - is.setEncoding("UTF-8"); - StringWriter sw = new StringWriter(); - ListErrorHandler leh = new ListErrorHandler(); - TreeDumpContentHandler treeDumpContentHandler = new TreeDumpContentHandler( - sw); - HtmlParser htmlParser = new HtmlParser(XmlViolationPolicy.ALLOW); - if (streaming) { - htmlParser.setStreamabilityViolationPolicy(XmlViolationPolicy.FATAL); - } - htmlParser.setContentHandler(treeDumpContentHandler); - htmlParser.setLexicalHandler(treeDumpContentHandler); - htmlParser.setErrorHandler(leh); - htmlParser.setScriptingEnabled(scriptingEnabled); - try { - if (context == null) { - htmlParser.parse(is); - } else { - String ns = "http://www.w3.org/1999/xhtml"; - if (context.startsWith("svg ")) { - ns = "http://www.w3.org/2000/svg"; - context = context.substring(4); - } else if (context.startsWith("math ")) { - ns = "http://www.w3.org/1998/Math/MathML"; - context = context.substring(5); - } - htmlParser.parseFragment(is, context, ns); - treeDumpContentHandler.endDocument(); - } - } catch (SAXParseException e) { - // ignore - } - stream.close(); - - if (skipLabel()) { // #errors - System.err.println("Premature end of test data."); - return false; - } - LinkedList expectedErrors = new LinkedList(); - BufferedReader br = new BufferedReader(new InputStreamReader( - new UntilHashInputStream(aggregateStream), "UTF-8")); - String line = null; - while ((line = br.readLine()) != null) { - expectedErrors.add(line); - } - - if (newErrors) { - if (skipLabel()) { // #new-errors - System.err.println("Premature end of test data."); - return false; - } - br = new BufferedReader(new InputStreamReader( - new UntilHashInputStream(aggregateStream), "UTF-8")); - while ((line = br.readLine()) != null) { - expectedErrors.add(line); - } - } - - if (context != null) { - if (skipLabel()) { // #document-fragment - System.err.println("Premature end of test data."); - return false; - } - UntilHashInputStream stream2 = new UntilHashInputStream(aggregateStream); - while (stream2.read() != -1) { - // spin - } - } - if (hadScriptingDirective && skipLabel()) { // #script-on/off - System.err.println("Premature end of test data."); - return false; - } - - if (skipLabel()) { // #document - System.err.println("Premature end of test data."); - return false; - } - - StringBuilder expectedBuilder = new StringBuilder(); - br = new BufferedReader(new InputStreamReader( - new UntilHashInputStream(aggregateStream), "UTF-8")); - int ch; - while ((ch = br.read()) != -1) { - expectedBuilder.append((char)ch); - } - String expected = expectedBuilder.toString(); - if (expected.contains("")) { - return true; - } - String actual = sw.toString(); - - LinkedList actualErrors = leh.getErrors(); - - if (expected.equals(actual) || (streaming && leh.isFatal()) /* - * && expectedErrors.size() == - * actualErrors.size() - */) { - // System.err.println(stream); - } else { - exitStatus = 1; - System.err.print("Failure.\nData:\n" + stream + "\nExpected:\n" - + expected + "Got: \n" + actual); - System.err.println("Expected errors:"); - for (String err : expectedErrors) { - System.err.println(err); - } - System.err.println("Actual errors:"); - for (String err : actualErrors) { - System.err.println(err); - } - } - } catch (Throwable t) { - exitStatus = 1; - System.err.println("Failure.\nData:\n" + stream); - throw t; - } - return true; - } - - private boolean skipLabel() throws IOException { - int b = aggregateStream.read(); - if (b == -1) { - return true; - } - for (;;) { - b = aggregateStream.read(); - if (b == -1) { - return true; - } else if (b == 0x0A) { - return false; - } - } - } - - /** - * @param args - * @throws Throwable - */ - public static void main(String[] args) throws Throwable { - for (int i = 0; i < args.length; i++) { - TreeTester tester = new TreeTester(new FileInputStream(args[i])); - tester.runTests(); - } - System.exit(exitStatus); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/UntilHashInputStream.java b/test-src/nu/validator/htmlparser/test/UntilHashInputStream.java deleted file mode 100644 index 473a9f7f..00000000 --- a/test-src/nu/validator/htmlparser/test/UntilHashInputStream.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.IOException; -import java.io.InputStream; - -public class UntilHashInputStream extends InputStream { - - private final StringBuilder builder = new StringBuilder(); - - private final InputStream delegate; - - private int buffer = -1; - - private boolean closed = false; - - /** - * @param delegate - * @throws IOException - */ - public UntilHashInputStream(final InputStream delegate) throws IOException { - this.delegate = delegate; - this.buffer = delegate.read(); - if (buffer == '#') { - closed = true; - } - } - - public int read() throws IOException { - if (closed) { - return -1; - } - int rv = buffer; - buffer = delegate.read(); - if (buffer == '#' && rv == '\n') { - // end of stream - closed = true; - return -1; - } else { - if (rv >= 0x20 && rv < 0x80) { - builder.append(((char)rv)); - } else { - builder.append("0x"); - builder.append(Integer.toHexString(rv)); - } - return rv; - } - } - - /** - * @see java.io.InputStream#close() - */ - @Override - public void close() throws IOException { - super.close(); - if (closed) { - return; - } - for (;;) { - int b = delegate.read(); - if (b == 0x23 || b == -1) { - break; - } - } - closed = true; - } - - /** - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return builder.toString(); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/XmlSerializerTester.java b/test-src/nu/validator/htmlparser/test/XmlSerializerTester.java deleted file mode 100644 index 0d23fda3..00000000 --- a/test-src/nu/validator/htmlparser/test/XmlSerializerTester.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; - -import nu.validator.htmlparser.sax.XmlSerializer; - -public class XmlSerializerTester { - - - - /** - * @param args - * @throws SAXException - */ - public static void main(String[] args) throws SAXException { - AttributesImpl attrs = new AttributesImpl(); - XmlSerializer serializer = new XmlSerializer(System.out); - serializer.startDocument(); - serializer.startElement("1", "a", null, attrs); - serializer.startElement("1", "b", null, attrs); - serializer.endElement("1", "b", null); - serializer.startElement("2", "c", null, attrs); - serializer.endElement("2", "c", null); - attrs.addAttribute("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "about", null, "CDATA", ""); - serializer.startElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "d", null, attrs); - serializer.endElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "d", null); - serializer.startPrefixMapping("rdf", "foo"); - serializer.startElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "e", null, attrs); - serializer.startPrefixMapping("p0", "bar"); - serializer.startElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "f", null, attrs); - serializer.characters("a\uD834\uDD21a\uD834a\uDD21a".toCharArray(), 0, 8); - serializer.endElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "f", null); - serializer.endElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "e", null); - - serializer.endPrefixMapping("rdf"); - serializer.endElement("1", "a", null); - serializer.endDocument(); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/XomTest.java b/test-src/nu/validator/htmlparser/test/XomTest.java deleted file mode 100644 index 66d706ae..00000000 --- a/test-src/nu/validator/htmlparser/test/XomTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2009 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import nu.xom.Attribute; -import nu.xom.Element; - -public class XomTest { - public static void main(String[] args) { - Element elt = new Element("html", "http://www.w3.org/1999/xhtml"); - elt.addAttribute(new Attribute("xmlns:foo", "bar")); - } -} diff --git a/test-src/nu/validator/htmlparser/test/package.html b/test-src/nu/validator/htmlparser/test/package.html deleted file mode 100644 index 57809b84..00000000 --- a/test-src/nu/validator/htmlparser/test/package.html +++ /dev/null @@ -1,29 +0,0 @@ - - -Package Overview - - - -Test drivers. - - \ No newline at end of file diff --git a/test-src/nu/validator/htmlparser/tools/HTML2HTML.java b/test-src/nu/validator/htmlparser/tools/HTML2HTML.java deleted file mode 100644 index 5e2cf1f5..00000000 --- a/test-src/nu/validator/htmlparser/tools/HTML2HTML.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.MalformedURLException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlParser; -import nu.validator.htmlparser.sax.HtmlSerializer; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -public class HTML2HTML { - - /** - * @param args - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, - TransformerException { - InputStream in; - OutputStream out; - - switch (args.length) { - case 0: - in = System.in; - out = System.out; - break; - case 1: - in = new FileInputStream(args[0]); - out = System.out; - break; - case 2: - in = new FileInputStream(args[0]); - out = new FileOutputStream(args[1]); - break; - default: - System.err.println("Too many arguments. No arguments to use stdin/stdout. One argument to reading from file and write to stdout. Two arguments to read from first file and write to second."); - System.exit(1); - return; - } - - ContentHandler serializer = new HtmlSerializer(out); - - HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALLOW); - - parser.setErrorHandler(new SystemErrErrorHandler()); - parser.setContentHandler(serializer); - parser.setProperty("http://xml.org/sax/properties/lexical-handler", - serializer); - parser.parse(new InputSource(in)); - out.flush(); - out.close(); - } -} diff --git a/test-src/nu/validator/htmlparser/tools/HTML2XML.java b/test-src/nu/validator/htmlparser/tools/HTML2XML.java deleted file mode 100644 index 57666f93..00000000 --- a/test-src/nu/validator/htmlparser/tools/HTML2XML.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.MalformedURLException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlParser; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -public class HTML2XML { - - /** - * @param args - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, - TransformerException { - InputStream in; - OutputStream out; - - switch (args.length) { - case 0: - in = System.in; - out = System.out; - break; - case 1: - in = new FileInputStream(args[0]); - out = System.out; - break; - case 2: - in = new FileInputStream(args[0]); - out = new FileOutputStream(args[1]); - break; - default: - System.err.println("Too many arguments. No arguments to use stdin/stdout. One argument to reading from file and write to stdout. Two arguments to read from first file and write to second."); - System.exit(1); - return; - } - - ContentHandler serializer = new XmlSerializer(out); - - HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALTER_INFOSET); - - parser.setErrorHandler(new SystemErrErrorHandler()); - parser.setContentHandler(serializer); - parser.setProperty("http://xml.org/sax/properties/lexical-handler", - serializer); - parser.parse(new InputSource(in)); - out.flush(); - out.close(); - } -} diff --git a/test-src/nu/validator/htmlparser/tools/XML2HTML.java b/test-src/nu/validator/htmlparser/tools/XML2HTML.java deleted file mode 100644 index dad89a5b..00000000 --- a/test-src/nu/validator/htmlparser/tools/XML2HTML.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.MalformedURLException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.transform.TransformerException; - -import nu.validator.htmlparser.sax.HtmlSerializer; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; - -public class XML2HTML { - - /** - * @param args - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, - TransformerException { - InputStream in; - OutputStream out; - - switch (args.length) { - case 0: - in = System.in; - out = System.out; - break; - case 1: - in = new FileInputStream(args[0]); - out = System.out; - break; - case 2: - in = new FileInputStream(args[0]); - out = new FileOutputStream(args[1]); - break; - default: - System.err.println("Too many arguments. No arguments to use stdin/stdout. One argument to reading from file and write to stdout. Two arguments to read from first file and write to second."); - System.exit(1); - return; - } - - ContentHandler serializer = new HtmlSerializer(out); - - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(false); - XMLReader parser = factory.newSAXParser().getXMLReader(); - parser.setErrorHandler(new SystemErrErrorHandler()); - parser.setContentHandler(serializer); - parser.setProperty("http://xml.org/sax/properties/lexical-handler", - serializer); - parser.parse(new InputSource(in)); - out.flush(); - out.close(); - } -} diff --git a/test-src/nu/validator/htmlparser/tools/XML2XML.java b/test-src/nu/validator/htmlparser/tools/XML2XML.java deleted file mode 100644 index 2f6aa24d..00000000 --- a/test-src/nu/validator/htmlparser/tools/XML2XML.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.MalformedURLException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.transform.TransformerException; - -import nu.validator.htmlparser.sax.NameCheckingXmlSerializer; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; - -public class XML2XML { - - /** - * @param args - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, - TransformerException { - InputStream in; - OutputStream out; - - switch (args.length) { - case 0: - in = System.in; - out = System.out; - break; - case 1: - in = new FileInputStream(args[0]); - out = System.out; - break; - case 2: - in = new FileInputStream(args[0]); - out = new FileOutputStream(args[1]); - break; - default: - System.err.println("Too many arguments. No arguments to use stdin/stdout. One argument to reading from file and write to stdout. Two arguments to read from first file and write to second."); - System.exit(1); - return; - } - - ContentHandler serializer = new NameCheckingXmlSerializer(out); - - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(false); - XMLReader parser = factory.newSAXParser().getXMLReader(); - parser.setErrorHandler(new SystemErrErrorHandler()); - parser.setContentHandler(serializer); - parser.setProperty("http://xml.org/sax/properties/lexical-handler", - serializer); - parser.parse(new InputSource(in)); - out.flush(); - out.close(); - } -} diff --git a/test-src/nu/validator/htmlparser/tools/XSLT4HTML5.java b/test-src/nu/validator/htmlparser/tools/XSLT4HTML5.java deleted file mode 100644 index 05d8193c..00000000 --- a/test-src/nu/validator/htmlparser/tools/XSLT4HTML5.java +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2007 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.MalformedURLException; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.transform.Templates; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.sax.SAXResult; -import javax.xml.transform.sax.SAXTransformerFactory; -import javax.xml.transform.sax.TemplatesHandler; -import javax.xml.transform.sax.TransformerHandler; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.dom.HtmlDocumentBuilder; -import nu.validator.htmlparser.sax.HtmlParser; -import nu.validator.htmlparser.sax.HtmlSerializer; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.w3c.dom.Document; -import org.xml.sax.ContentHandler; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.ext.LexicalHandler; - -public class XSLT4HTML5 { - - private enum Mode { - STREAMING_SAX, BUFFERED_SAX, DOM, - } - - private static final String TEMPLATE = "--template="; - - private static final String INPUT_HTML = "--input-html="; - - private static final String INPUT_XML = "--input-xml="; - - private static final String OUTPUT_HTML = "--output-html="; - - private static final String OUTPUT_XML = "--output-xml="; - - private static final String MODE = "--mode="; - - /** - * @param args - * @throws ParserConfigurationException - * @throws SAXException - * @throws IOException - * @throws MalformedURLException - * @throws TransformerException - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, TransformerException { - if (args.length == 0) { - System.out.println("--template=file --input-[html|xml]=file --output-[html|xml]=file --mode=[sax-streaming|sax-buffered|dom]"); - System.exit(0); - } - String template = null; - String input = null; - boolean inputHtml = false; - String output = null; - boolean outputHtml = false; - Mode mode = null; - for (int i = 0; i < args.length; i++) { - String arg = args[i]; - if (arg.startsWith(TEMPLATE)) { - if (template == null) { - template = arg.substring(TEMPLATE.length()); - } else { - System.err.println("Tried to set template twice."); - System.exit(1); - } - } else if (arg.startsWith(INPUT_HTML)) { - if (input == null) { - input = arg.substring(INPUT_HTML.length()); - inputHtml = true; - } else { - System.err.println("Tried to set input twice."); - System.exit(2); - } - } else if (arg.startsWith(INPUT_XML)) { - if (input == null) { - input = arg.substring(INPUT_XML.length()); - inputHtml = false; - } else { - System.err.println("Tried to set input twice."); - System.exit(2); - } - } else if (arg.startsWith(OUTPUT_HTML)) { - if (output == null) { - output = arg.substring(OUTPUT_HTML.length()); - outputHtml = true; - } else { - System.err.println("Tried to set output twice."); - System.exit(3); - } - } else if (arg.startsWith(OUTPUT_XML)) { - if (output == null) { - output = arg.substring(OUTPUT_XML.length()); - outputHtml = false; - } else { - System.err.println("Tried to set output twice."); - System.exit(3); - } - } else if (arg.startsWith(MODE)) { - if (mode == null) { - String modeStr = arg.substring(MODE.length()); - if ("dom".equals(modeStr)) { - mode = Mode.DOM; - } else if ("sax-buffered".equals(modeStr)) { - mode = Mode.BUFFERED_SAX; - } else if ("sax-streaming".equals(modeStr)) { - mode = Mode.STREAMING_SAX; - } else { - System.err.println("Unrecognized mode."); - System.exit(5); - } - } else { - System.err.println("Tried to set mode twice."); - System.exit(4); - } - } - } - - if (template == null) { - System.err.println("No template specified."); - System.exit(6); - } - if (input == null) { - System.err.println("No input specified."); - System.exit(7); - } - if (output == null) { - System.err.println("No output specified."); - System.exit(8); - } - if (mode == null) { - mode = Mode.BUFFERED_SAX; - } - - SystemErrErrorHandler errorHandler = new SystemErrErrorHandler(); - - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(false); - XMLReader reader = factory.newSAXParser().getXMLReader(); - reader.setErrorHandler(errorHandler); - - SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); - transformerFactory.setErrorListener(errorHandler); - TemplatesHandler templatesHandler = transformerFactory.newTemplatesHandler(); - reader.setContentHandler(templatesHandler); - reader.parse(new File(template).toURI().toASCIIString()); - - Templates templates = templatesHandler.getTemplates(); - - FileOutputStream outputStream = new FileOutputStream(output); - ContentHandler serializer; - if (outputHtml) { - serializer = new HtmlSerializer(outputStream); - } else { - serializer = new XmlSerializer(outputStream); - } - SAXResult result = new SAXResult(new XmlnsDropper(serializer)); - result.setLexicalHandler((LexicalHandler) serializer); - - if (mode == Mode.DOM) { - Document inputDoc; - DocumentBuilder builder; - if (inputHtml) { - builder = new HtmlDocumentBuilder(XmlViolationPolicy.ALTER_INFOSET); - } else { - DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - try { - builder = builderFactory.newDocumentBuilder(); - } catch (ParserConfigurationException e) { - throw new RuntimeException(e); - } - } - inputDoc = builder.parse(new File(input)); - DOMSource inputSource = new DOMSource(inputDoc, - new File(input).toURI().toASCIIString()); - Transformer transformer = templates.newTransformer(); - transformer.setErrorListener(errorHandler); - transformer.transform(inputSource, result); - } else { - if (inputHtml) { - reader = new HtmlParser(XmlViolationPolicy.ALTER_INFOSET); - if (mode == Mode.STREAMING_SAX) { - reader.setProperty("http://validator.nu/properties/streamability-violation-policy", XmlViolationPolicy.FATAL); - } - } - TransformerHandler transformerHandler = transformerFactory.newTransformerHandler(templates); - transformerHandler.setResult(result); - reader.setErrorHandler(errorHandler); - reader.setContentHandler(transformerHandler); - reader.setProperty("http://xml.org/sax/properties/lexical-handler", transformerHandler); - reader.parse(new File(input).toURI().toASCIIString()); - } - outputStream.flush(); - outputStream.close(); - } - -} diff --git a/test-src/nu/validator/htmlparser/tools/XSLT4HTML5XOM.java b/test-src/nu/validator/htmlparser/tools/XSLT4HTML5XOM.java deleted file mode 100644 index b364cc52..00000000 --- a/test-src/nu/validator/htmlparser/tools/XSLT4HTML5XOM.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2007 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlSerializer; -import nu.validator.htmlparser.xom.HtmlBuilder; -import nu.xom.Builder; -import nu.xom.Document; -import nu.xom.Element; -import nu.xom.Nodes; -import nu.xom.ParsingException; -import nu.xom.Serializer; -import nu.xom.ValidityException; -import nu.xom.converters.SAXConverter; -import nu.xom.xslt.XSLException; -import nu.xom.xslt.XSLTransform; - -import org.xml.sax.SAXException; - -public class XSLT4HTML5XOM { - - private static final String TEMPLATE = "--template="; - - private static final String INPUT_HTML = "--input-html="; - - private static final String INPUT_XML = "--input-xml="; - - private static final String OUTPUT_HTML = "--output-html="; - - private static final String OUTPUT_XML = "--output-xml="; - - /** - * @param args - * @throws IOException - * @throws ParsingException - * @throws ValidityException - * @throws XSLException - * @throws SAXException - */ - public static void main(String[] args) throws ValidityException, - ParsingException, IOException, XSLException, SAXException { - if (args.length == 0) { - System.out.println("--template=file --input-[html|xml]=file --output-[html|xml]=file --mode=[sax-streaming|sax-buffered|dom]"); - System.exit(0); - } - String template = null; - String input = null; - boolean inputHtml = false; - String output = null; - boolean outputHtml = false; - for (int i = 0; i < args.length; i++) { - String arg = args[i]; - if (arg.startsWith(TEMPLATE)) { - if (template == null) { - template = arg.substring(TEMPLATE.length()); - } else { - System.err.println("Tried to set template twice."); - System.exit(1); - } - } else if (arg.startsWith(INPUT_HTML)) { - if (input == null) { - input = arg.substring(INPUT_HTML.length()); - inputHtml = true; - } else { - System.err.println("Tried to set input twice."); - System.exit(2); - } - } else if (arg.startsWith(INPUT_XML)) { - if (input == null) { - input = arg.substring(INPUT_XML.length()); - inputHtml = false; - } else { - System.err.println("Tried to set input twice."); - System.exit(2); - } - } else if (arg.startsWith(OUTPUT_HTML)) { - if (output == null) { - output = arg.substring(OUTPUT_HTML.length()); - outputHtml = true; - } else { - System.err.println("Tried to set output twice."); - System.exit(3); - } - } else if (arg.startsWith(OUTPUT_XML)) { - if (output == null) { - output = arg.substring(OUTPUT_XML.length()); - outputHtml = false; - } else { - System.err.println("Tried to set output twice."); - System.exit(3); - } - } - } - - if (template == null) { - System.err.println("No template specified."); - System.exit(6); - } - if (input == null) { - System.err.println("No input specified."); - System.exit(7); - } - if (output == null) { - System.err.println("No output specified."); - System.exit(8); - } - - Builder builder = new Builder(); - - Document transformationDoc = builder.build(new File(template)); - - XSLTransform transform = new XSLTransform(transformationDoc); - - FileOutputStream outputStream = new FileOutputStream(output); - - Document inputDoc; - if (inputHtml) { - builder = new HtmlBuilder(XmlViolationPolicy.ALTER_INFOSET); - } - inputDoc = builder.build(new File(input)); - Nodes result = transform.transform(inputDoc); - Document outputDoc = new Document((Element) result.get(0)); - if (outputHtml) { - HtmlSerializer htmlSerializer = new HtmlSerializer(outputStream); - SAXConverter converter = new SAXConverter(htmlSerializer); - converter.setLexicalHandler(htmlSerializer); - converter.convert(outputDoc); - } else { - Serializer serializer = new Serializer(outputStream); - serializer.write(outputDoc); - } - outputStream.flush(); - outputStream.close(); - } - -} diff --git a/test-src/nu/validator/htmlparser/tools/XmlnsDropper.java b/test-src/nu/validator/htmlparser/tools/XmlnsDropper.java deleted file mode 100644 index 0e6d4b1c..00000000 --- a/test-src/nu/validator/htmlparser/tools/XmlnsDropper.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; - -/** - * Quick and dirty hack to work around Xalan xmlns weirdness. - * - * @version $Id$ - * @author hsivonen - */ -class XmlnsDropper implements ContentHandler { - - private final ContentHandler delegate; - - /** - * @param delegate - */ - public XmlnsDropper(final ContentHandler delegate) { - this.delegate = delegate; - } - - /** - * @param ch - * @param start - * @param length - * @throws SAXException - * @see org.xml.sax.ContentHandler#characters(char[], int, int) - */ - public void characters(char[] ch, int start, int length) throws SAXException { - delegate.characters(ch, start, length); - } - - /** - * @throws SAXException - * @see org.xml.sax.ContentHandler#endDocument() - */ - public void endDocument() throws SAXException { - delegate.endDocument(); - } - - /** - * @param uri - * @param localName - * @param qName - * @throws SAXException - * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String) - */ - public void endElement(String uri, String localName, String qName) throws SAXException { - delegate.endElement(uri, localName, qName); - } - - /** - * @param prefix - * @throws SAXException - * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String) - */ - public void endPrefixMapping(String prefix) throws SAXException { - delegate.endPrefixMapping(prefix); - } - - /** - * @param ch - * @param start - * @param length - * @throws SAXException - * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int) - */ - public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { - delegate.ignorableWhitespace(ch, start, length); - } - - /** - * @param target - * @param data - * @throws SAXException - * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String) - */ - public void processingInstruction(String target, String data) throws SAXException { - delegate.processingInstruction(target, data); - } - - /** - * @param locator - * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator) - */ - public void setDocumentLocator(Locator locator) { - delegate.setDocumentLocator(locator); - } - - /** - * @param name - * @throws SAXException - * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String) - */ - public void skippedEntity(String name) throws SAXException { - delegate.skippedEntity(name); - } - - /** - * @throws SAXException - * @see org.xml.sax.ContentHandler#startDocument() - */ - public void startDocument() throws SAXException { - delegate.startDocument(); - } - - /** - * @param uri - * @param localName - * @param qName - * @param atts - * @throws SAXException - * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) - */ - public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { - AttributesImpl ai = new AttributesImpl(); - for (int i = 0; i < atts.getLength(); i++) { - String u = atts.getURI(i); - String t = atts.getType(i); - String v = atts.getValue(i); - String n = atts.getLocalName(i); - String q = atts.getQName(i); - if (q != null) { - if ("xmlns".equals(q) || q.startsWith("xmlns:")) { - continue; - } - } - ai.addAttribute(u, n, q, t, v); - } - delegate.startElement(uri, localName, qName, ai); - } - - /** - * @param prefix - * @param uri - * @throws SAXException - * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String) - */ - public void startPrefixMapping(String prefix, String uri) throws SAXException { - delegate.startPrefixMapping(prefix, uri); - } - -} diff --git a/test-src/nu/validator/htmlparser/tools/package.html b/test-src/nu/validator/htmlparser/tools/package.html deleted file mode 100644 index a04bf3cd..00000000 --- a/test-src/nu/validator/htmlparser/tools/package.html +++ /dev/null @@ -1,29 +0,0 @@ - - -Package Overview - - - -Demo apps. - - \ No newline at end of file diff --git a/test-src/nu/validator/saxtree/test/PassThruPrinter.java b/test-src/nu/validator/saxtree/test/PassThruPrinter.java deleted file mode 100644 index df391d4b..00000000 --- a/test-src/nu/validator/saxtree/test/PassThruPrinter.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.saxtree.test; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParserFactory; - -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.saxtree.Node; -import nu.validator.saxtree.TreeBuilder; -import nu.validator.saxtree.TreeParser; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.ext.LexicalHandler; - -public class PassThruPrinter { - public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException { - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(false); - XMLReader reader = factory.newSAXParser().getXMLReader(); - - TreeBuilder treeBuilder = new TreeBuilder(); - reader.setContentHandler(treeBuilder); - reader.setProperty("http://xml.org/sax/properties/lexical-handler", treeBuilder); - - File file = new File(args[0]); - InputSource is = new InputSource(new FileInputStream(file)); - is.setSystemId(file.toURI().toASCIIString()); - reader.parse(is); - - Node doc = treeBuilder.getRoot(); - - ContentHandler xmlSerializer = new XmlSerializer(System.out); - - TreeParser treeParser = new TreeParser(xmlSerializer, (LexicalHandler) xmlSerializer); - treeParser.parse(doc); - } - -} diff --git a/test-src/nu/validator/saxtree/test/package.html b/test-src/nu/validator/saxtree/test/package.html deleted file mode 100644 index 57809b84..00000000 --- a/test-src/nu/validator/saxtree/test/package.html +++ /dev/null @@ -1,29 +0,0 @@ - - -Package Overview - - - -Test drivers. - - \ No newline at end of file diff --git a/test-src/test/resources/html5lib-tests b/test-src/test/resources/html5lib-tests deleted file mode 160000 index e4463205..00000000 --- a/test-src/test/resources/html5lib-tests +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e4463205ac3c4500e1379103daadfdcfe5e33af5
By default, when using the constructor without arguments, the - * this parser treats XML 1.0-incompatible infosets as fatal errors. - * This corresponds to - * FATAL as the general XML violation policy. To make the parser - * support non-conforming HTML fully per the HTML 5 spec while on the other - * hand potentially violating the DOM API contract, set the general XML - * violation policy to ALLOW. This does not work with a standard - * DOM implementation. Handling all input without fatal errors and without - * violating the DOM API contract is possible by setting - * the general XML violation policy to ALTER_INFOSET. This - * makes the parser non-conforming but is probably the most useful - * setting for most applications. - * - *
FATAL
ALLOW
ALTER_INFOSET
The doctype is not represented in the tree. - * - *
The document mode is represented as user data DocumentMode - * object with the key nu.validator.document-mode on the document - * node. - * - *
DocumentMode
nu.validator.document-mode
The form pointer is also stored as user data with the key - * nu.validator.form-pointer. - * - * @version $Id: HtmlDocumentBuilder.java 255 2008-05-29 08:57:38Z hsivonen $ - * @author hsivonen - */ -public class HtmlParser { - - private static final int CHUNK_SIZE = 512; - - private final Tokenizer tokenizer; - - private final BrowserTreeBuilder domTreeBuilder; - - private final StringBuilder documentWriteBuffer = new StringBuilder(); - - private ErrorHandler errorHandler; - - private UTF16Buffer stream; - - private int streamLength; - - private boolean lastWasCR; - - private boolean ending; - - private ParseEndListener parseEndListener; - - private final LinkedList bufferStack = new LinkedList(); - - /** - * Instantiates the parser - * - * @param implementation - * the DOM implementation - * @param xmlPolicy the policy - */ - public HtmlParser(JavaScriptObject document) { - this.domTreeBuilder = new BrowserTreeBuilder(document); - this.tokenizer = new ErrorReportingTokenizer(domTreeBuilder); - this.domTreeBuilder.setNamePolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setCommentPolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setContentNonXmlCharPolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setContentSpacePolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setNamePolicy(XmlViolationPolicy.ALTER_INFOSET); - this.tokenizer.setXmlnsPolicy(XmlViolationPolicy.ALTER_INFOSET); - } - - /** - * Parses a document from a SAX InputSource. - * @param is the source - * @return the doc - * @see javax.xml.parsers.DocumentBuilder#parse(org.xml.sax.InputSource) - */ - public void parse(String source, ParseEndListener callback) throws SAXException { - parseEndListener = callback; - domTreeBuilder.setFragmentContext(null); - tokenize(source, null); - } - - /** - * @param is - * @throws SAXException - * @throws IOException - * @throws MalformedURLException - */ - private void tokenize(String source, String context) throws SAXException { - lastWasCR = false; - ending = false; - documentWriteBuffer.setLength(0); - streamLength = source.length(); - stream = new UTF16Buffer(source.toCharArray(), 0, - (streamLength < CHUNK_SIZE ? streamLength : CHUNK_SIZE)); - bufferStack.clear(); - push(stream); - domTreeBuilder.setFragmentContext(context == null ? null : context.intern()); - tokenizer.start(); - pump(); - } - - private void pump() throws SAXException { - if (ending) { - tokenizer.end(); - domTreeBuilder.getDocument(); // drops the internal reference - parseEndListener.parseComplete(); - // Don't schedule timeout - return; - } - - int docWriteLen = documentWriteBuffer.length(); - if (docWriteLen > 0) { - char[] newBuf = new char[docWriteLen]; - documentWriteBuffer.getChars(0, docWriteLen, newBuf, 0); - push(new UTF16Buffer(newBuf, 0, docWriteLen)); - documentWriteBuffer.setLength(0); - } - - for (;;) { - UTF16Buffer buffer = peek(); - if (!buffer.hasMore()) { - if (buffer == stream) { - if (buffer.getEnd() == streamLength) { - // Stop parsing - tokenizer.eof(); - ending = true; - break; - } else { - int newEnd = buffer.getStart() + CHUNK_SIZE; - buffer.setEnd(newEnd < streamLength ? newEnd - : streamLength); - continue; - } - } else { - pop(); - continue; - } - } - // now we have a non-empty buffer - buffer.adjust(lastWasCR); - lastWasCR = false; - if (buffer.hasMore()) { - lastWasCR = tokenizer.tokenizeBuffer(buffer); - domTreeBuilder.maybeRunScript(); - break; - } else { - continue; - } - } - - // schedule - Timer timer = new Timer() { - - @Override public void run() { - try { - pump(); - } catch (SAXException e) { - ending = true; - if (errorHandler != null) { - try { - errorHandler.fatalError(new SAXParseException( - e.getMessage(), null, null, -1, -1, e)); - } catch (SAXException e1) { - } - } - } - } - - }; - timer.schedule(1); - } - - private void push(UTF16Buffer buffer) { - bufferStack.addLast(buffer); - } - - private UTF16Buffer peek() { - return bufferStack.getLast(); - } - - private void pop() { - bufferStack.removeLast(); - } - - public void documentWrite(String text) throws SAXException { - UTF16Buffer buffer = new UTF16Buffer(text.toCharArray(), 0, text.length()); - while (buffer.hasMore()) { - buffer.adjust(lastWasCR); - lastWasCR = false; - if (buffer.hasMore()) { - lastWasCR = tokenizer.tokenizeBuffer(buffer); - domTreeBuilder.maybeRunScript(); - } - } - } - - /** - * @see javax.xml.parsers.DocumentBuilder#setErrorHandler(org.xml.sax.ErrorHandler) - */ - public void setErrorHandler(ErrorHandler errorHandler) { - this.errorHandler = errorHandler; - domTreeBuilder.setErrorHandler(errorHandler); - tokenizer.setErrorHandler(errorHandler); - } - - /** - * Sets whether comment nodes appear in the tree. - * @param ignoreComments true to ignore comments - * @see nu.validator.htmlparser.impl.TreeBuilder#setIgnoringComments(boolean) - */ - public void setIgnoringComments(boolean ignoreComments) { - domTreeBuilder.setIgnoringComments(ignoreComments); - } - - /** - * Sets whether the parser considers scripting to be enabled for noscript treatment. - * @param scriptingEnabled true to enable - * @see nu.validator.htmlparser.impl.TreeBuilder#setScriptingEnabled(boolean) - */ - public void setScriptingEnabled(boolean scriptingEnabled) { - domTreeBuilder.setScriptingEnabled(scriptingEnabled); - } - -} diff --git a/gwt-src/nu/validator/htmlparser/gwt/HtmlParserModule.java b/gwt-src/nu/validator/htmlparser/gwt/HtmlParserModule.java deleted file mode 100644 index 255a02d1..00000000 --- a/gwt-src/nu/validator/htmlparser/gwt/HtmlParserModule.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.gwt; - -import org.xml.sax.SAXException; - -import com.google.gwt.core.client.EntryPoint; -import com.google.gwt.core.client.JavaScriptObject; - -public class HtmlParserModule implements EntryPoint { - - private static native void zapChildren(JavaScriptObject node) /*-{ - while (node.hasChildNodes()) { - node.removeChild(node.lastChild); - } - }-*/; - - private static native void installDocWrite(JavaScriptObject doc, HtmlParser parser) /*-{ - doc.write = function() { - if (arguments.length == 0) { - return; - } - var text = arguments[0]; - for (var i = 1; i < arguments.length; i++) { - text += arguments[i]; - } - parser.@nu.validator.htmlparser.gwt.HtmlParser::documentWrite(Ljava/lang/String;)(text); - } - doc.writeln = function() { - if (arguments.length == 0) { - parser.@nu.validator.htmlparser.gwt.HtmlParser::documentWrite(Ljava/lang/String;)("\n"); - return; - } - var text = arguments[0]; - for (var i = 1; i < arguments.length; i++) { - text += arguments[i]; - } - text += "\n"; - parser.@nu.validator.htmlparser.gwt.HtmlParser::documentWrite(Ljava/lang/String;)(text); - } - }-*/; - - @SuppressWarnings("unused") - private static void parseHtmlDocument(String source, JavaScriptObject document, JavaScriptObject readyCallback, JavaScriptObject errorHandler) throws SAXException { - if (readyCallback == null) { - readyCallback = JavaScriptObject.createFunction(); - } - zapChildren(document); - HtmlParser parser = new HtmlParser(document); - parser.setScriptingEnabled(true); - // XXX error handler - - installDocWrite(document, parser); - - parser.parse(source, new ParseEndListener(readyCallback)); - } - - private static native void exportEntryPoints() /*-{ - $wnd.parseHtmlDocument = @nu.validator.htmlparser.gwt.HtmlParserModule::parseHtmlDocument(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;Lcom/google/gwt/core/client/JavaScriptObject;Lcom/google/gwt/core/client/JavaScriptObject;); - }-*/; - - - public void onModuleLoad() { - exportEntryPoints(); - } - -} diff --git a/gwt-src/nu/validator/htmlparser/gwt/ParseEndListener.java b/gwt-src/nu/validator/htmlparser/gwt/ParseEndListener.java deleted file mode 100644 index 43235c5b..00000000 --- a/gwt-src/nu/validator/htmlparser/gwt/ParseEndListener.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.gwt; - -import com.google.gwt.core.client.JavaScriptObject; - -public class ParseEndListener { - - private final JavaScriptObject callback; - - /** - * @param callback - */ - public ParseEndListener(JavaScriptObject callback) { - this.callback = callback; - } - - public void parseComplete() { - call(callback); - } - - private static native void call(JavaScriptObject callback) /*-{ - callback(); - }-*/; - -} diff --git a/gwt-src/nu/validator/htmlparser/public/HtmlParser.html b/gwt-src/nu/validator/htmlparser/public/HtmlParser.html deleted file mode 100644 index 4d9cde81..00000000 --- a/gwt-src/nu/validator/htmlparser/public/HtmlParser.html +++ /dev/null @@ -1,225 +0,0 @@ - - - - Live DOM Viewer - - - - - - - Live DOM Viewer - Markup to test (permalink, upload, download, hide): - <!DOCTYPE html> -... - DOM view (hide, refresh): - - Rendered view: (hide): - - innerHTML view: (show, refresh): - <!DOCTYPE HTML><html></html> - Log: (hide): - Script not loaded. - - This script puts a function w(s) into the - global scope of the test page, where s is a string to - output to the log. Also, five files are accessible in the current - directory for test purposes: image (a GIF image), - flash (a Flash file), script (a JS file), - style (a CSS file), and document (an HTML - file). - - \ No newline at end of file diff --git a/gwt-src/nu/validator/htmlparser/public/LICENSE.Live-DOM-viewer.txt b/gwt-src/nu/validator/htmlparser/public/LICENSE.Live-DOM-viewer.txt deleted file mode 100644 index bd2f4fcf..00000000 --- a/gwt-src/nu/validator/htmlparser/public/LICENSE.Live-DOM-viewer.txt +++ /dev/null @@ -1,25 +0,0 @@ -From: -http://software.hixie.ch/utilities/js/live-dom-viewer/LICENSE -regarding the upstream of HtmlParser.html: - -The MIT License - -Copyright (c) 2000, 2006, 2008 Ian Hickson and various contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/gwt-src/nu/validator/htmlparser/public/blank.html b/gwt-src/nu/validator/htmlparser/public/blank.html deleted file mode 100644 index a8756c9f..00000000 --- a/gwt-src/nu/validator/htmlparser/public/blank.html +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/mozilla-export-scripts/README.txt b/mozilla-export-scripts/README.txt deleted file mode 100644 index 3567b846..00000000 --- a/mozilla-export-scripts/README.txt +++ /dev/null @@ -1,25 +0,0 @@ -These scripts export the Java-to-C++ translator and the java source files that -implement the HTML5 parser. The exported translator may be used (with no -external dependencies) to translate the exported java source files into Gecko- -compatible C++. - -Hacking the translator itself still requires a working copy of the Java HTML5 -parser repository, but hacking the parser (modifying the Java source files and -performing the translation) should now be possible using only files committed -to the mozilla source tree. - -Run any of these scripts without arguments to receive usage instructions. - - make-translator-jar.sh: compiles the Java-to-C++ translator into a .jar file - export-java-srcs.sh: exports minimal java source files implementing the - HTML5 parser - export-translator.sh: exports the compiled translator and javaparser.jar - export-all.sh: runs the previous two scripts - util.sh: provides various shell utility functions to the - scripts listed above (does nothing if run directly) - -All path arguments may be either absolute or relative. This includes the path -to the script itself ($0), so the directory from which you run these scripts -doesn't matter. - -Ben Newman (7 July 2009) diff --git a/mozilla-export-scripts/export-all.sh b/mozilla-export-scripts/export-all.sh deleted file mode 100644 index 9ae07d33..00000000 --- a/mozilla-export-scripts/export-all.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env sh - -SCRIPT_DIR=`dirname $0` -source $SCRIPT_DIR/util.sh -SCRIPT_DIR=`abs $SCRIPT_DIR` - -if [ $# -eq 1 ] -then - MOZ_PARSER_PATH=`abs $1` -else - echo - echo "Usage: sh `basename $0` /path/to/mozilla-central/parser/html" - echo "Note that relative paths will work just fine." - echo - exit 1 -fi - -$SCRIPT_DIR/export-translator.sh $MOZ_PARSER_PATH -$SCRIPT_DIR/export-java-srcs.sh $MOZ_PARSER_PATH - -echo -echo "Now go to $MOZ_PARSER_PATH and run" -echo " java -jar javalib/translator.jar javasrc . nsHtml5AtomList.h" -echo diff --git a/mozilla-export-scripts/export-java-srcs.sh b/mozilla-export-scripts/export-java-srcs.sh deleted file mode 100644 index 6d32b07d..00000000 --- a/mozilla-export-scripts/export-java-srcs.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env sh - -SCRIPT_DIR=`dirname $0` -source $SCRIPT_DIR/util.sh -SCRIPT_DIR=`abs $SCRIPT_DIR` - -SRCDIR=`abs $SCRIPT_DIR/../src/nu/validator/htmlparser/impl` - -if [ $# -eq 1 ] -then - MOZ_PARSER_PATH=`abs $1` -else - echo - echo "Usage: sh `basename $0` /path/to/mozilla-central/parser/html" - echo "Note that relative paths will work just fine." - echo - exit 1 -fi - -SRCTARGET=$MOZ_PARSER_PATH/javasrc - -rm -rf $SRCTARGET -mkdir $SRCTARGET -# Avoid copying the .svn directory: -cp -rv $SRCDIR/*.java $SRCTARGET diff --git a/mozilla-export-scripts/export-translator.sh b/mozilla-export-scripts/export-translator.sh deleted file mode 100644 index d1f4f1c3..00000000 --- a/mozilla-export-scripts/export-translator.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env sh - -SCRIPT_DIR=`dirname $0` -source $SCRIPT_DIR/util.sh -SCRIPT_DIR=`abs $SCRIPT_DIR` - -LIBDIR=`abs $SCRIPT_DIR/../translator-lib` - -if [ $# -eq 1 ] -then - MOZ_PARSER_PATH=`abs $1` -else - echo - echo "Usage: sh `basename $0` /path/to/mozilla-central/parser/html" - echo "Note that relative paths will work just fine." - echo "Be sure that you have run `dirname $0`/make-translator-jar.sh before running this script." - echo - exit 1 -fi - -LIBTARGET=$MOZ_PARSER_PATH/javalib - -rm -rf $LIBTARGET -cp -rv $LIBDIR $LIBTARGET diff --git a/mozilla-export-scripts/make-translator-jar.sh b/mozilla-export-scripts/make-translator-jar.sh deleted file mode 100644 index 4f21ae66..00000000 --- a/mozilla-export-scripts/make-translator-jar.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env sh - -SCRIPT_DIR=`dirname $0` -source $SCRIPT_DIR/util.sh -SCRIPT_DIR=`abs $SCRIPT_DIR` - -SRCDIR=`abs $SCRIPT_DIR/../translator-src` -BINDIR=`abs $SCRIPT_DIR/../translator-bin` -LIBDIR=`abs $SCRIPT_DIR/../translator-lib` - -if [ $# -eq 1 ] -then - JAVAPARSER_JAR_PATH=`abs $1` -else - echo - echo "Usage: sh `basename $0` /path/to/javaparser-1.0.7.jar" - echo "Note that relative paths will work just fine." - echo "Obtain javaparser-1.0.7.jar from http://code.google.com/p/javaparser" - echo - exit 1 -fi - -set_up() { - rm -rf $BINDIR; mkdir $BINDIR - rm -rf $LIBDIR; mkdir $LIBDIR - cp $JAVAPARSER_JAR_PATH $LIBDIR/javaparser.jar -} - -write_manifest() { - rm -f $LIBDIR/manifest - echo "Main-Class: nu.validator.htmlparser.cpptranslate.Main" > $LIBDIR/manifest - echo "Class-Path: javaparser.jar" >> $LIBDIR/manifest -} - -compile_translator() { - find $SRCDIR -name "*.java" | \ - xargs javac -cp $LIBDIR/javaparser.jar -g -d $BINDIR -} - -generate_jar() { - jar cvfm $LIBDIR/translator.jar $LIBDIR/manifest -C $BINDIR . -} - -clean_up() { - rm -f $LIBDIR/manifest -} - -success_message() { - echo - echo "Successfully generated directory \"$LIBDIR\" with contents:" - echo - ls -al $LIBDIR - echo - echo "Now run `dirname $0`/export-all.sh with no arguments and follow the usage instructions." - echo -} - -set_up && \ - compile_translator && \ - write_manifest && \ - generate_jar && \ - clean_up && \ - success_message diff --git a/mozilla-export-scripts/util.sh b/mozilla-export-scripts/util.sh deleted file mode 100644 index 348ca14f..00000000 --- a/mozilla-export-scripts/util.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env sh - -abs() { - local rel - local p - if [ $# -ne 1 ] - then - rel=. - else - rel=$1 - fi - if [ -d $rel ] - then - pushd $rel > /dev/null - p=`pwd` - popd > /dev/null - else - pushd `dirname $rel` > /dev/null - p=`pwd`/`basename $rel` - popd > /dev/null - fi - echo $p -} diff --git a/test-src/nu/validator/htmlparser/test/DecoderLoopTester.java b/test-src/nu/validator/htmlparser/test/DecoderLoopTester.java deleted file mode 100644 index 3337a655..00000000 --- a/test-src/nu/validator/htmlparser/test/DecoderLoopTester.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.CharsetEncoder; -import java.nio.charset.CodingErrorAction; - -import nu.validator.htmlparser.common.Heuristics; -import nu.validator.htmlparser.io.Encoding; -import nu.validator.htmlparser.io.HtmlInputStreamReader; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; - -public class DecoderLoopTester { - - private static final int LEAD_OFFSET = 0xD800 - (0x10000 >> 10); - - private static final int NUMBER_OR_ASTRAL_CHARS = 24500; - - private void runTest(int padding) throws SAXException, IOException { - Encoding utf8 = Encoding.forName("UTF-8"); - char[] charArr = new char[1 + padding + 2 * NUMBER_OR_ASTRAL_CHARS]; - byte[] byteArr; - int i = 0; - charArr[i++] = '\uFEFF'; - for (int j = 0; j < padding; j++) { - charArr[i++] = 'x'; - } - for (int j = 0; j < NUMBER_OR_ASTRAL_CHARS; j++) { - int value = 0x10000 + j; - charArr[i++] = (char) (LEAD_OFFSET + (value >> 10)); - charArr[i++] = (char) (0xDC00 + (value & 0x3FF)); -// charArr[i++] = 'y'; -// charArr[i++] = 'z'; - - } - CharBuffer charBuffer = CharBuffer.wrap(charArr); - CharsetEncoder enc = utf8.newEncoder(); - enc.onMalformedInput(CodingErrorAction.REPORT); - enc.onUnmappableCharacter(CodingErrorAction.REPORT); - ByteBuffer byteBuffer = enc.encode(charBuffer); - byteArr = new byte[byteBuffer.limit()]; - byteBuffer.get(byteArr); - - ErrorHandler eh = new SystemErrErrorHandler(); - compare(new HtmlInputStreamReader(new ByteArrayInputStream(byteArr), eh, null, null, Heuristics.NONE), padding, charArr, byteArr); - compare(new HtmlInputStreamReader(new ByteArrayInputStream(byteArr), eh, null, null, utf8), padding, charArr, byteArr); - } - - /** - * @param padding - * @param charArr - * @param byteArr - * @throws SAXException - * @throws IOException - */ - private void compare(HtmlInputStreamReader reader, int padding, char[] charArr, byte[] byteArr) throws SAXException, IOException { - char[] readBuffer = new char[2048]; - int offset = 0; - int num = 0; - int readNum = 0; - while ((num = reader.read(readBuffer)) != -1) { - for (int j = 0; j < num; j++) { - System.out.println(offset + j); - if (readBuffer[j] != charArr[offset + j]) { - throw new RuntimeException("Test failed. Char: " + Integer.toHexString(readBuffer[j]) + " j: " + j + " readNum: " + readNum); - } - } - offset += num; - readNum++; - } - } - - void runTests() throws SAXException, IOException { - for (int i = 0; i < 4; i++) { - runTest(i); - } - } - - /** - * @param args - * @throws IOException - * @throws SAXException - */ - public static void main(String[] args) throws IOException, SAXException { - new DecoderLoopTester().runTests(); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/DomIdTester.java b/test-src/nu/validator/htmlparser/test/DomIdTester.java deleted file mode 100644 index a3866f5d..00000000 --- a/test-src/nu/validator/htmlparser/test/DomIdTester.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.IOException; -import java.io.StringReader; - -import org.w3c.dom.Document; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import nu.validator.htmlparser.dom.HtmlDocumentBuilder; - -public class DomIdTester { - - private static final String testSrc = "buoeoauoeuo"; - - /** - * @param args - * @throws IOException - * @throws SAXException - */ - public static void main(String[] args) throws SAXException, IOException { - HtmlDocumentBuilder builder = new HtmlDocumentBuilder(); - Document doc = builder.parse(new InputSource(new StringReader(testSrc))); - System.out.println(doc.getElementById("foo").getLocalName()); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/DomTest.java b/test-src/nu/validator/htmlparser/test/DomTest.java deleted file mode 100644 index 07d054b9..00000000 --- a/test-src/nu/validator/htmlparser/test/DomTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2009 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -public class DomTest { - public static void main(String[] args) throws Exception { - DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); - f.setNamespaceAware(true); // not setting this causes pain and suffering with SVG - DocumentBuilder b = f.newDocumentBuilder(); - Document d = b.newDocument(); - Element e = d.createElementNS("http://www.w3.org/1999/xhtml", "html"); - e.setAttribute("xmlns:foo", "bar"); - } -} diff --git a/test-src/nu/validator/htmlparser/test/EncodingTester.java b/test-src/nu/validator/htmlparser/test/EncodingTester.java deleted file mode 100644 index f0dba50a..00000000 --- a/test-src/nu/validator/htmlparser/test/EncodingTester.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.Charset; - -import nu.validator.htmlparser.common.Heuristics; -import nu.validator.htmlparser.io.Encoding; -import nu.validator.htmlparser.io.HtmlInputStreamReader; - -import org.xml.sax.SAXException; - -public class EncodingTester { - - static int exitStatus = 0; - - protected static int SNIFFING_LIMIT = 16384; - - private final InputStream aggregateStream; - - private final StringBuilder builder = new StringBuilder(); - - /** - * @param aggregateStream - */ - public EncodingTester(InputStream aggregateStream) { - this.aggregateStream = aggregateStream; - } - - void runTests() throws IOException, SAXException { - while (runTest()) { - // spin - } - } - - @SuppressWarnings("resource") - private boolean runTest() throws IOException, SAXException { - if (skipLabel()) { - return false; - } - UntilHashInputStream stream = new UntilHashInputStream(aggregateStream); - HtmlInputStreamReader reader = new HtmlInputStreamReader(stream, null, - null, null, Heuristics.NONE, SNIFFING_LIMIT); - Charset charset = reader.getCharset(); - stream.close(); - if (skipLabel()) { - exitStatus = 1; - System.err.println("Premature end of test data."); - return false; - } - builder.setLength(0); - loop: for (;;) { - int b = aggregateStream.read(); - switch (b) { - case '\n': - break loop; - case -1: - exitStatus = 1; - System.err.println("Premature end of test data."); - return false; - default: - builder.append(((char) b)); - } - } - String sniffed = charset.name(); - String expected = Encoding.forName(builder.toString()).newDecoder().charset().name(); - if (expected.equalsIgnoreCase(sniffed)) { - // System.err.println(stream); - } else { - exitStatus = 1; - System.err.println("Failure. Expected: " + expected + " got " - + sniffed + "."); - System.err.println(stream); - } - return true; - } - - private boolean skipLabel() throws IOException { - int b = aggregateStream.read(); - if (b == -1) { - return true; - } - for (;;) { - b = aggregateStream.read(); - if (b == -1) { - return true; - } else if (b == 0x0A) { - return false; - } - } - } - - /** - * @param args - * @throws SAXException - * @throws IOException - */ - public static void main(String[] args) throws IOException, SAXException { - for (int i = 0; i < args.length; i++) { - EncodingTester tester = new EncodingTester(new FileInputStream( - args[i])); - tester.runTests(); - } - System.exit(exitStatus); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/Html5libTest.java b/test-src/nu/validator/htmlparser/test/Html5libTest.java deleted file mode 100644 index 724062e2..00000000 --- a/test-src/nu/validator/htmlparser/test/Html5libTest.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (c) 2020 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.function.Consumer; - -public class Html5libTest { - - private final Path testDir; - - public Html5libTest() throws URISyntaxException { - this.testDir = Paths.get( - Html5libTest.class.getResource("/html5lib-tests").toURI()); - } - - public void testEncoding() throws Exception { - Files.walkFileTree(testDir.resolve("encoding"), // - new TestVisitor(true, ".dat", file -> // - new EncodingTester(Files.newInputStream(file)).runTests())); - if (EncodingTester.exitStatus != 0) { - assert false : "Encoding test failed"; - } - } - - public void testTokenizer() throws Exception { - Files.walkFileTree(testDir.resolve("tokenizer"), - new TestVisitor(true, ".test", file -> // - new TokenizerTester(getDoubleEscapedInput(file)).runTests())); - if (TokenizerTester.exitStatus != 0) { - assert false : "Tokenizer test failed"; - } - } - - public void testTree() throws Exception { - Files.walkFileTree(testDir.resolve("tree-construction"), - new TestVisitor(true, ".dat", file -> // - new TreeTester(Files.newInputStream(file)).runTests())); - if (TreeTester.exitStatus != 0) { - assert false : "Tree test failed"; - } - } - - private ByteArrayInputStream getDoubleEscapedInput(Path file) - throws IOException { - byte[] fileBytes = Files.readAllBytes(file); - String fileContent = new String(fileBytes, StandardCharsets.UTF_8); - String unescapedContent = fileContent.replace("\\\\u", "\\u"); - byte[] newBytes = unescapedContent.getBytes(StandardCharsets.UTF_8); - return new ByteArrayInputStream(newBytes); - } - - private interface TestConsumer extends Consumer { - - @Override - default void accept(Path t) { - try { - acceptTest(t); - } catch (Throwable e) { - throw new AssertionError(e); - } - } - - void acceptTest(Path t) throws Throwable; - - } - - private static class TestVisitor extends SimpleFileVisitor { - - private final boolean skipScripted; - - private final String requiredTestExtension; - - private final TestConsumer runner; - - private TestVisitor(boolean skipScripted, String requiredTestExtension, - TestConsumer runner) { - this.skipScripted = skipScripted; - this.requiredTestExtension = requiredTestExtension; - this.runner = runner; - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, - BasicFileAttributes attrs) throws IOException { - if (skipScripted - && dir.getFileName().equals(Paths.get("scripted"))) { - return FileVisitResult.SKIP_SUBTREE; - } - - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) - throws IOException { - if (file.getFileName().toString().endsWith(requiredTestExtension)) { - runner.accept(file); - } - return FileVisitResult.CONTINUE; - } - } - -} diff --git a/test-src/nu/validator/htmlparser/test/JSONArrayTokenHandler.java b/test-src/nu/validator/htmlparser/test/JSONArrayTokenHandler.java deleted file mode 100644 index 298f406a..00000000 --- a/test-src/nu/validator/htmlparser/test/JSONArrayTokenHandler.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -import com.sdicons.json.model.JSONArray; -import com.sdicons.json.model.JSONBoolean; -import com.sdicons.json.model.JSONNull; -import com.sdicons.json.model.JSONObject; -import com.sdicons.json.model.JSONString; - -import nu.validator.htmlparser.common.TokenHandler; -import nu.validator.htmlparser.impl.ElementName; -import nu.validator.htmlparser.impl.HtmlAttributes; -import nu.validator.htmlparser.impl.Tokenizer; - -public class JSONArrayTokenHandler implements TokenHandler, ErrorHandler { - - private static final JSONString DOCTYPE = new JSONString("DOCTYPE"); - - private static final JSONString START_TAG = new JSONString("StartTag"); - - private static final JSONString END_TAG = new JSONString("EndTag"); - - private static final JSONString COMMENT = new JSONString("Comment"); - - private static final JSONString CHARACTER = new JSONString("Character"); - - private static final JSONString PARSE_ERROR = new JSONString("ParseError"); - - private static final char[] REPLACEMENT_CHARACTER = { '\uFFFD' }; - - private static final char[] NULL = { '\u0000' }; - - private final StringBuilder builder = new StringBuilder(); - - private JSONArray array = null; - - private int contentModelFlag; - - private String contentModelElement; - - public void setContentModelFlag(int contentModelFlag, String contentModelElement) { - this.contentModelFlag = contentModelFlag; - this.contentModelElement = contentModelElement; - } - - public void characters(char[] buf, int start, int length) - throws SAXException { - builder.append(buf, start, length); - } - - private void flushCharacters() { - if (builder.length() > 0) { - JSONArray token = new JSONArray(); - token.getValue().add(CHARACTER); - token.getValue().add(new JSONString(builder.toString())); - array.getValue().add(token); - builder.setLength(0); - } - } - - public void comment(char[] buf, int start, int length) throws SAXException { - flushCharacters(); - JSONArray token = new JSONArray(); - token.getValue().add(COMMENT); - token.getValue().add(new JSONString(new String(buf, start, length))); - array.getValue().add(token); - } - - public void doctype(String name, String publicIdentifier, String systemIdentifier, boolean forceQuirks) throws SAXException { - flushCharacters(); - JSONArray token = new JSONArray(); - token.getValue().add(DOCTYPE); - token.getValue().add(name == null ? JSONNull.NULL : new JSONString(name)); - token.getValue().add(publicIdentifier == null ? JSONNull.NULL : new JSONString(publicIdentifier)); - token.getValue().add(systemIdentifier == null ? JSONNull.NULL : new JSONString(systemIdentifier)); - token.getValue().add(new JSONBoolean(!forceQuirks)); - array.getValue().add(token); - } - - public void endTag(ElementName eltName) throws SAXException { - String name = eltName.getName(); - flushCharacters(); - JSONArray token = new JSONArray(); - token.getValue().add(END_TAG); - token.getValue().add(new JSONString(name)); - array.getValue().add(token); - } - - public void eof() throws SAXException { - flushCharacters(); - } - - public void startTokenization(Tokenizer self) throws SAXException { - array = new JSONArray(); - if (contentModelElement != null) { - self.setStateAndEndTagExpectation(contentModelFlag, contentModelElement); - } - } - - public void startTag(ElementName eltName, HtmlAttributes attributes, - boolean selfClosing) throws SAXException { - String name = eltName.getName(); - flushCharacters(); - JSONArray token = new JSONArray(); - token.getValue().add(START_TAG); - token.getValue().add(new JSONString(name)); - JSONObject attrs = new JSONObject(); - for (int i = 0; i < attributes.getLength(); i++) { - attrs.getValue().put(attributes.getQNameNoBoundsCheck(i), - new JSONString(attributes.getValueNoBoundsCheck(i))); - } - token.getValue().add(attrs); - if (selfClosing) { - token.getValue().add(JSONBoolean.TRUE); - } - array.getValue().add(token); - } - - public boolean wantsComments() throws SAXException { - return true; - } - - public void error(SAXParseException exception) throws SAXException { -// flushCharacters(); -// array.getValue().add(PARSE_ERROR); - } - - public void fatalError(SAXParseException exception) throws SAXException { - throw new RuntimeException("Should never happen."); - } - - public void warning(SAXParseException exception) throws SAXException { - } - - /** - * Returns the array. - * - * @return the array - */ - public JSONArray getArray() { - return array; - } - - public void endTokenization() throws SAXException { - - } - - @Override public void zeroOriginatingReplacementCharacter() - throws SAXException { - builder.append(REPLACEMENT_CHARACTER, 0, 1); - } - - @Override public void zeroOrReplacementCharacter() - throws SAXException { - builder.append(NULL, 0, 1); - } - - @Override public boolean cdataSectionAllowed() throws SAXException { - return false; - } - - @Override public void ensureBufferSpace(int inputLength) - throws SAXException { - } - -} diff --git a/test-src/nu/validator/htmlparser/test/ListErrorHandler.java b/test-src/nu/validator/htmlparser/test/ListErrorHandler.java deleted file mode 100644 index 9a207f27..00000000 --- a/test-src/nu/validator/htmlparser/test/ListErrorHandler.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.util.LinkedList; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -public class ListErrorHandler implements ErrorHandler { - - private boolean fatal = false; - - private LinkedList errors = new LinkedList(); - - public void error(SAXParseException spe) throws SAXException { - errors.add(Integer.toString(spe.getColumnNumber()) + ": " + spe.getMessage()); - } - - public void fatalError(SAXParseException arg0) throws SAXException { - fatal = true; - } - - public void warning(SAXParseException arg0) throws SAXException { - } - - /** - * Returns the errors. - * - * @return the errors - */ - public LinkedList getErrors() { - return errors; - } - - /** - * Returns the fatal. - * - * @return the fatal - */ - public boolean isFatal() { - return fatal; - } - -} diff --git a/test-src/nu/validator/htmlparser/test/SystemErrErrorHandler.java b/test-src/nu/validator/htmlparser/test/SystemErrErrorHandler.java deleted file mode 100644 index 9ee490b9..00000000 --- a/test-src/nu/validator/htmlparser/test/SystemErrErrorHandler.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (c) 2005, 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.UnsupportedEncodingException; -import java.io.Writer; - -import javax.xml.transform.ErrorListener; -import javax.xml.transform.SourceLocator; -import javax.xml.transform.TransformerException; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -/** - * @version $Id$ - * @author hsivonen - */ -public class SystemErrErrorHandler implements ErrorHandler, ErrorListener { - - private Writer out; - - private boolean inError = false; - - public SystemErrErrorHandler() { - try { - out = new OutputStreamWriter(System.err, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - - /** - * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException) - */ - public void warning(SAXParseException e) throws SAXException { - try { - out.write("Warning:\n"); - out.write(e.getMessage()); - out.write("\nFile: "); - String systemId = e.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(e.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(e.getColumnNumber())); - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new SAXException(e1); - } - } - - /** - * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException) - */ - public void error(SAXParseException e) throws SAXException { - inError = true; - try { - out.write("Error:\n"); - out.write(e.getMessage()); - out.write("\nFile: "); - String systemId = e.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(e.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(e.getColumnNumber())); - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new SAXException(e1); - } - } - - /** - * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException) - */ - public void fatalError(SAXParseException e) throws SAXException { - inError = true; - try { - out.write("Fatal Error:\n"); - out.write(e.getMessage()); - out.write("\nFile: "); - String systemId = e.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(e.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(e.getColumnNumber())); - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new SAXException(e1); - } - } - - /** - * Returns the inError. - * - * @return the inError - */ - public boolean isInError() { - return inError; - } - - public void reset() { - inError = false; - } - - public void error(TransformerException e) throws TransformerException { - inError = true; - try { - out.write("Error:\n"); - out.write(e.getMessage()); - SourceLocator sourceLocator = e.getLocator(); - if (sourceLocator != null) { - out.write("\nFile: "); - String systemId = sourceLocator.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(sourceLocator.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(sourceLocator.getColumnNumber())); - } - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new TransformerException(e1); - } - } - - public void fatalError(TransformerException e) - throws TransformerException { - inError = true; - try { - out.write("Fatal Error:\n"); - out.write(e.getMessage()); - SourceLocator sourceLocator = e.getLocator(); - if (sourceLocator != null) { - out.write("\nFile: "); - String systemId = sourceLocator.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(sourceLocator.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(sourceLocator.getColumnNumber())); - } - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new TransformerException(e1); - } - } - - public void warning(TransformerException e) - throws TransformerException { - try { - out.write("Warning:\n"); - out.write(e.getMessage()); - SourceLocator sourceLocator = e.getLocator(); - if (sourceLocator != null) { - out.write("\nFile: "); - String systemId = sourceLocator.getSystemId(); - out.write((systemId == null) ? "Unknown" : systemId); - out.write("\nLine: "); - out.write(Integer.toString(sourceLocator.getLineNumber())); - out.write(" Col: "); - out.write(Integer.toString(sourceLocator.getColumnNumber())); - } - out.write("\n\n"); - out.flush(); - } catch (IOException e1) { - throw new TransformerException(e1); - } - } - -} diff --git a/test-src/nu/validator/htmlparser/test/TokenPrinter.java b/test-src/nu/validator/htmlparser/test/TokenPrinter.java deleted file mode 100755 index 5fb16ce6..00000000 --- a/test-src/nu/validator/htmlparser/test/TokenPrinter.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; - -import org.xml.sax.ErrorHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.SAXParseException; - -import nu.validator.htmlparser.common.TokenHandler; -import nu.validator.htmlparser.impl.ElementName; -import nu.validator.htmlparser.impl.ErrorReportingTokenizer; -import nu.validator.htmlparser.impl.HtmlAttributes; -import nu.validator.htmlparser.impl.Tokenizer; -import nu.validator.htmlparser.io.Driver; - -public class TokenPrinter implements TokenHandler, ErrorHandler { - - private final Writer writer; - - public void characters(char[] buf, int start, int length) - throws SAXException { - try { - boolean lineStarted = true; - writer.write('-'); - for (int i = start; i < start + length; i++) { - if (!lineStarted) { - writer.write("\n-"); - lineStarted = true; - } - char c = buf[i]; - if (c == '\n') { - writer.write("\\n"); - lineStarted = false; - } else { - writer.write(c); - } - } - writer.write('\n'); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void comment(char[] buf, int start, int length) throws SAXException { - try { - writer.write('!'); - writer.write(buf, start, length); - writer.write('\n'); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void doctype(String name, String publicIdentifier, String systemIdentifier, boolean forceQuirks) throws SAXException { - try { - writer.write('D'); - writer.write(name); - writer.write(' '); - writer.write("" + forceQuirks); - writer.write('\n'); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void endTag(ElementName eltName) throws SAXException { - try { - writer.write(')'); - writer.write(eltName.getName()); - writer.write('\n'); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void eof() throws SAXException { - try { - writer.write("E\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void startTokenization(Tokenizer self) throws SAXException { - - } - - public void startTag(ElementName eltName, HtmlAttributes attributes, boolean selfClosing) - throws SAXException { - try { - writer.write('('); - writer.write(eltName.getName()); - writer.write('\n'); - for (int i = 0; i < attributes.getLength(); i++) { - writer.write('A'); - writer.write(attributes.getQNameNoBoundsCheck(i)); - writer.write(' '); - writer.write(attributes.getValueNoBoundsCheck(i)); - writer.write('\n'); - } - } catch (IOException e) { - throw new SAXException(e); - } - } - - public boolean wantsComments() throws SAXException { - return true; - } - - public static void main(String[] args) throws SAXException, IOException { - TokenPrinter printer = new TokenPrinter(new OutputStreamWriter(System.out, "UTF-8")); - Driver tokenizer = new Driver(new ErrorReportingTokenizer(printer)); - tokenizer.setErrorHandler(printer); - File file = new File(args[0]); - InputSource is = new InputSource(new FileInputStream(file)); - is.setSystemId(file.toURI().toASCIIString()); - tokenizer.tokenize(is); - } - - /** - * @param writer - */ - public TokenPrinter(final Writer writer) { - this.writer = writer; - } - - public void error(SAXParseException exception) throws SAXException { - try { - writer.write("R "); - writer.write(exception.getMessage()); - writer.write("\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void fatalError(SAXParseException exception) throws SAXException { - try { - writer.write("F "); - writer.write(exception.getMessage()); - writer.write("\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void warning(SAXParseException exception) throws SAXException { - try { - writer.write("W "); - writer.write(exception.getMessage()); - writer.write("\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void endTokenization() throws SAXException { - try { - writer.flush(); - writer.close(); - } catch (IOException e) { - throw new SAXException(e); - } - } - - @Override public void zeroOriginatingReplacementCharacter() - throws SAXException { - try { - writer.write("0\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - @Override public void zeroOrReplacementCharacter() - throws SAXException { - zeroOriginatingReplacementCharacter(); - } - - @Override public boolean cdataSectionAllowed() throws SAXException { - return false; - } - - @Override public void ensureBufferSpace(int inputLength) - throws SAXException { - } -} diff --git a/test-src/nu/validator/htmlparser/test/TokenizerTester.java b/test-src/nu/validator/htmlparser/test/TokenizerTester.java deleted file mode 100644 index 7048dca6..00000000 --- a/test-src/nu/validator/htmlparser/test/TokenizerTester.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.ByteArrayInputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.StringReader; -import java.io.UnsupportedEncodingException; -import java.io.Writer; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.impl.ErrorReportingTokenizer; -import nu.validator.htmlparser.impl.Tokenizer; -import nu.validator.htmlparser.io.Driver; - -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import antlr.RecognitionException; -import antlr.TokenStreamException; - -import com.sdicons.json.model.JSONArray; -import com.sdicons.json.model.JSONObject; -import com.sdicons.json.model.JSONString; -import com.sdicons.json.model.JSONValue; -import com.sdicons.json.parser.JSONParser; - -public class TokenizerTester { - - static int exitStatus = 0; - - private static JSONString PLAINTEXT = new JSONString("PLAINTEXT state"); - - private static JSONString PCDATA = new JSONString("Data state"); - - private static JSONString RCDATA = new JSONString("RCDATA state"); - - private static JSONString CDATA = new JSONString("CDATA section state"); - - private static JSONString RAWTEXT = new JSONString("RAWTEXT state"); - - private static JSONString SCRIPT_DATA = new JSONString("Script data state"); - - private static boolean jsonDeepEquals(JSONValue one, JSONValue other) { - if (one.isSimple()) { - return one.equals(other); - } else if (one.isArray()) { - if (other.isArray()) { - JSONArray oneArr = (JSONArray) one; - JSONArray otherArr = (JSONArray) other; - return oneArr.getValue().equals(otherArr.getValue()); - } else { - return false; - } - } else if (one.isObject()) { - if (other.isObject()) { - JSONObject oneObject = (JSONObject) one; - JSONObject otherObject = (JSONObject) other; - return oneObject.getValue().equals(otherObject.getValue()); - } else { - return false; - } - } else { - throw new RuntimeException("Should never happen."); - } - } - - private JSONArray tests; - - private final JSONArrayTokenHandler tokenHandler; - - private final Driver driver; - - private final Writer writer; - - public TokenizerTester(InputStream stream) throws TokenStreamException, - RecognitionException, UnsupportedEncodingException { - tokenHandler = new JSONArrayTokenHandler(); - driver = new Driver(new ErrorReportingTokenizer(tokenHandler)); - driver.setCommentPolicy(XmlViolationPolicy.ALLOW); - driver.setContentNonXmlCharPolicy(XmlViolationPolicy.ALLOW); - driver.setContentSpacePolicy(XmlViolationPolicy.ALLOW); - driver.setNamePolicy(XmlViolationPolicy.ALLOW); - driver.setXmlnsPolicy(XmlViolationPolicy.ALLOW); - driver.setErrorHandler(tokenHandler); - driver.dontSwallowBom(); - writer = new OutputStreamWriter(System.out, "UTF-8"); - JSONParser jsonParser = new JSONParser(new InputStreamReader(stream, - "UTF-8")); - JSONObject obj = (JSONObject) jsonParser.nextValue(); - tests = (JSONArray) obj.get("tests"); - if (tests == null) { - tests = (JSONArray) obj.get("xmlViolationTests"); - driver.setCommentPolicy(XmlViolationPolicy.ALTER_INFOSET); - driver.setContentNonXmlCharPolicy(XmlViolationPolicy.ALTER_INFOSET); - driver.setNamePolicy(XmlViolationPolicy.ALTER_INFOSET); - driver.setXmlnsPolicy(XmlViolationPolicy.ALTER_INFOSET); - } - } - - void runTests() throws SAXException, IOException { - for (JSONValue val : tests.getValue()) { - runTest((JSONObject) val); - } - writer.flush(); - } - - private void runTest(JSONObject test) throws SAXException, IOException { - String inputString = ((JSONString) test.get("input")).getValue(); - JSONArray expectedTokens = (JSONArray) test.get("output"); - String description = ((JSONString) test.get("description")).getValue(); - JSONString lastStartTagJSON = ((JSONString) test.get("lastStartTag")); - String lastStartTag = lastStartTagJSON == null ? null - : lastStartTagJSON.getValue(); - JSONArray contentModelFlags = (JSONArray) test.get("initialStates"); - if (contentModelFlags == null) { - runTestInner(inputString, expectedTokens, description, - Tokenizer.DATA, null); - } else { - for (JSONValue value : contentModelFlags.getValue()) { - if (PCDATA.equals(value)) { - lastStartTag = lastStartTag == null ? "xmp" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.DATA, lastStartTag); - } else if (RAWTEXT.equals(value)) { - lastStartTag = lastStartTag == null ? "xmp" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.RAWTEXT, lastStartTag); - } else if (RCDATA.equals(value)) { - lastStartTag = lastStartTag == null ? "xmp" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.RCDATA, lastStartTag); - } else if (CDATA.equals(value)) { - lastStartTag = lastStartTag == null ? "xmp" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.CDATA_SECTION, lastStartTag); - } else if (PLAINTEXT.equals(value)) { - lastStartTag = lastStartTag == null ? "plaintext" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.PLAINTEXT, lastStartTag); - } else if (SCRIPT_DATA.equals(value)) { - lastStartTag = lastStartTag == null ? "script" : lastStartTag; - runTestInner(inputString, expectedTokens, description, - Tokenizer.SCRIPT_DATA, lastStartTag); - } else { - throw new RuntimeException("Broken test data."); - } - } - } - } - - /** - * @param contentModelElement - * @param contentModelFlag - * @param test - * @throws SAXException - * @throws IOException - */ - private void runTestInner(String inputString, JSONArray expectedTokens, - String description, int contentModelFlag, - String contentModelElement) throws SAXException, IOException { - tokenHandler.setContentModelFlag(contentModelFlag, contentModelElement); - InputSource is = new InputSource(new StringReader(inputString)); - try { - driver.tokenize(is); - JSONArray actualTokens = tokenHandler.getArray(); - if (!jsonDeepEquals(actualTokens, expectedTokens)) { - exitStatus = 1; - writer.write("Failure\n"); - writer.write(description); - writer.write("\nInput:\n"); - writer.write(inputString); - writer.write("\nExpected tokens:\n"); - writer.write(expectedTokens.render(false)); - writer.write("\nActual tokens:\n"); - writer.write(actualTokens.render(false)); - writer.write("\n"); - } - } catch (Throwable t) { - exitStatus = 1; - writer.write("Failure\n"); - writer.write(description); - writer.write("\nInput:\n"); - writer.write(inputString); - writer.write("\n"); - t.printStackTrace(new PrintWriter(writer, false)); - } - } - - /** - * @param args - * @throws RecognitionException - * @throws TokenStreamException - * @throws IOException - * @throws SAXException - */ - public static void main(String[] args) throws TokenStreamException, - RecognitionException, SAXException, IOException { - for (int i = 0; i < args.length; i++) { - byte[] fileBytes = Files.readAllBytes(Paths.get(args[i])); - String fileContent = new String(fileBytes, StandardCharsets.UTF_8); - String unescapedContent = fileContent.replace("\\\\u", "\\u"); - byte[] newBytes = unescapedContent.getBytes(StandardCharsets.UTF_8); - ByteArrayInputStream bais = new ByteArrayInputStream(newBytes); - TokenizerTester tester = new TokenizerTester(bais); - tester.runTests(); - } - System.exit(exitStatus); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/TreeDumpContentHandler.java b/test-src/nu/validator/htmlparser/test/TreeDumpContentHandler.java deleted file mode 100644 index 9b95b763..00000000 --- a/test-src/nu/validator/htmlparser/test/TreeDumpContentHandler.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.IOException; -import java.io.Writer; -import java.util.Map; -import java.util.TreeMap; - -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.ext.LexicalHandler; - -public class TreeDumpContentHandler implements ContentHandler, LexicalHandler { - - private final Writer writer; - - private int level = 0; - - private boolean inCharacters = false; - - private boolean close; - - /** - * @param writer - */ - public TreeDumpContentHandler(final Writer writer, boolean close) { - this.writer = writer; - this.close = close; - } - - public TreeDumpContentHandler(final Writer writer) { - this(writer, true); - } - - private void printLead() throws IOException { - if (inCharacters) { - writer.write("\"\n"); - inCharacters = false; - } - writer.write("| "); - for (int i = 0; i < level; i++) { - writer.write(" "); - } - } - - public void characters(char[] ch, int start, int length) - throws SAXException { - try { - if (!inCharacters) { - printLead(); - writer.write('"'); - inCharacters = true; - } - writer.write(ch, start, length); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void endElement(String uri, String localName, String qName) - throws SAXException { - try { - if (inCharacters) { - writer.write("\"\n"); - inCharacters = false; - } - level--; - if ("http://www.w3.org/1999/xhtml" == uri && - "template" == localName) { - // decrement level for the "content" - level--; - } - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void startElement(String uri, String localName, String qName, - Attributes atts) throws SAXException { - try { - printLead(); - writer.write('<'); - if ("http://www.w3.org/1998/Math/MathML" == uri) { - writer.write("math "); - } else if ("http://www.w3.org/2000/svg" == uri) { - writer.write("svg "); - } else if ("http://www.w3.org/1999/xhtml" != uri) { - writer.write("otherns "); - } - writer.write(localName); - writer.write(">\n"); - level++; - TreeMap map = new TreeMap(); - for (int i = 0; i < atts.getLength(); i++) { - String ns = atts.getURI(i); - String name; - if ("http://www.w3.org/1999/xlink" == ns) { - name = "xlink " + atts.getLocalName(i); - } else if ("http://www.w3.org/XML/1998/namespace" == ns) { - name = "xml " + atts.getLocalName(i); - } else if ("http://www.w3.org/2000/xmlns/" == ns) { - name = "xmlns " + atts.getLocalName(i); - } else if ("" != uri) { - name = atts.getLocalName(i); - } else { - name = "otherns " + atts.getLocalName(i); - } - map.put(name, atts.getValue(i)); - } - for (Map.Entry entry : map.entrySet()) { - printLead(); - writer.write(entry.getKey()); - writer.write("=\""); - writer.write(entry.getValue()); - writer.write("\"\n"); - } - if ("http://www.w3.org/1999/xhtml" == uri && - "template" == localName) { - printLead(); - level++; - writer.write("content\n"); - } - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void comment(char[] ch, int offset, int len) throws SAXException { - try { - printLead(); - writer.write("\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void startDTD(String name, String publicIdentifier, - String systemIdentifier) throws SAXException { - try { - printLead(); - writer.write(" 0 || systemIdentifier.length() > 0) { - writer.write(' '); - writer.write('\"'); - writer.write(publicIdentifier); - writer.write('\"'); - writer.write(' '); - writer.write('\"'); - writer.write(systemIdentifier); - writer.write('\"'); - } - writer.write(">\n"); - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void endDocument() throws SAXException { - try { - if (inCharacters) { - writer.write("\"\n"); - inCharacters = false; - } - if (close) { - writer.flush(); - writer.close(); - } - } catch (IOException e) { - throw new SAXException(e); - } - } - - public void startPrefixMapping(String prefix, String uri) - throws SAXException { - } - - public void startEntity(String arg0) throws SAXException { - } - - public void endCDATA() throws SAXException { - } - - public void endDTD() throws SAXException { - } - - public void endEntity(String arg0) throws SAXException { - } - - public void startCDATA() throws SAXException { - } - - public void endPrefixMapping(String prefix) throws SAXException { - } - - public void ignorableWhitespace(char[] ch, int start, int length) - throws SAXException { - } - - public void processingInstruction(String target, String data) - throws SAXException { - } - - public void setDocumentLocator(Locator locator) { - } - - public void skippedEntity(String name) throws SAXException { - } - - public void startDocument() throws SAXException { - } - -} diff --git a/test-src/nu/validator/htmlparser/test/TreePrinter.java b/test-src/nu/validator/htmlparser/test/TreePrinter.java deleted file mode 100644 index c0916938..00000000 --- a/test-src/nu/validator/htmlparser/test/TreePrinter.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; - -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlParser; - -public class TreePrinter { - - public static void main(String[] args) throws SAXException, IOException { - TreeDumpContentHandler treeDumpContentHandler = new TreeDumpContentHandler(new OutputStreamWriter(System.out, "UTF-8")); - HtmlParser htmlParser = new HtmlParser(); - htmlParser.setContentHandler(treeDumpContentHandler); - htmlParser.setLexicalHandler(treeDumpContentHandler); - htmlParser.setErrorHandler(new SystemErrErrorHandler()); - htmlParser.setXmlPolicy(XmlViolationPolicy.ALLOW); - File file = new File(args[0]); - InputSource is = new InputSource(new FileInputStream(file)); - is.setSystemId(file.toURI().toASCIIString()); - htmlParser.parse(is); - } -} diff --git a/test-src/nu/validator/htmlparser/test/TreeTester.java b/test-src/nu/validator/htmlparser/test/TreeTester.java deleted file mode 100644 index b19cbe96..00000000 --- a/test-src/nu/validator/htmlparser/test/TreeTester.java +++ /dev/null @@ -1,279 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.BufferedInputStream; -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.StringWriter; -import java.util.LinkedList; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlParser; - -import org.xml.sax.InputSource; -import org.xml.sax.SAXParseException; - -public class TreeTester { - - private final BufferedInputStream aggregateStream; - - private boolean streaming = false; - - static int exitStatus = 0; - - /** - * @param aggregateStream - */ - public TreeTester(InputStream aggregateStream) { - this.aggregateStream = new BufferedInputStream(aggregateStream); - } - - void runTests() throws Throwable { - if (aggregateStream.read() != '#') { - System.err.println("No hash at start!"); - return; - } - while (runTest()) { - // spin - } - } - - @SuppressWarnings("resource") - private boolean runTest() throws Throwable { - UntilHashInputStream stream = null; - try { - String context = null; - boolean scriptingEnabled = true; - boolean hadScriptingDirective = false; - aggregateStream.mark(12288); - if (skipLabel()) { // #data - return false; - } - stream = new UntilHashInputStream(aggregateStream); - while (stream.read() != -1) { - // spin - } - if (skipLabel()) { // #errors - System.err.println("Premature end of test data."); - return false; - } - stream = new UntilHashInputStream(aggregateStream); - while (stream.read() != -1) { - // spin - } - - boolean newErrors = false; - StringBuilder sb = new StringBuilder(); - int c; - while ((c = aggregateStream.read()) != '\n') { - sb.append((char) c); - } - String label = sb.toString(); - if ("new-errors".equals(label)) { - newErrors = true; - stream = new UntilHashInputStream(aggregateStream); - while (stream.read() != -1) { - // spin - } - sb.setLength(0); - while ((c = aggregateStream.read()) != '\n') { - sb.append((char) c); - } - label = sb.toString(); - } - if ("document-fragment".equals(label)) { - sb.setLength(0); - while ((c = aggregateStream.read()) != '\n') { - sb.append((char) c); - } - context = sb.toString(); - // Now potentially gather #script-on/off - sb.setLength(0); - while ((c = aggregateStream.read()) != '\n') { - sb.append((char) c); - } - label = sb.toString(); - } - if ("script-on".equals(label)) { - hadScriptingDirective = true; - } else if ("script-off".equals(label)) { - hadScriptingDirective = true; - scriptingEnabled = false; - } - aggregateStream.reset(); - if (skipLabel()) { // #data - System.err.println("Premature end of test data."); - return false; - } - stream = new UntilHashInputStream(aggregateStream); - InputSource is = new InputSource(stream); - is.setEncoding("UTF-8"); - StringWriter sw = new StringWriter(); - ListErrorHandler leh = new ListErrorHandler(); - TreeDumpContentHandler treeDumpContentHandler = new TreeDumpContentHandler( - sw); - HtmlParser htmlParser = new HtmlParser(XmlViolationPolicy.ALLOW); - if (streaming) { - htmlParser.setStreamabilityViolationPolicy(XmlViolationPolicy.FATAL); - } - htmlParser.setContentHandler(treeDumpContentHandler); - htmlParser.setLexicalHandler(treeDumpContentHandler); - htmlParser.setErrorHandler(leh); - htmlParser.setScriptingEnabled(scriptingEnabled); - try { - if (context == null) { - htmlParser.parse(is); - } else { - String ns = "http://www.w3.org/1999/xhtml"; - if (context.startsWith("svg ")) { - ns = "http://www.w3.org/2000/svg"; - context = context.substring(4); - } else if (context.startsWith("math ")) { - ns = "http://www.w3.org/1998/Math/MathML"; - context = context.substring(5); - } - htmlParser.parseFragment(is, context, ns); - treeDumpContentHandler.endDocument(); - } - } catch (SAXParseException e) { - // ignore - } - stream.close(); - - if (skipLabel()) { // #errors - System.err.println("Premature end of test data."); - return false; - } - LinkedList expectedErrors = new LinkedList(); - BufferedReader br = new BufferedReader(new InputStreamReader( - new UntilHashInputStream(aggregateStream), "UTF-8")); - String line = null; - while ((line = br.readLine()) != null) { - expectedErrors.add(line); - } - - if (newErrors) { - if (skipLabel()) { // #new-errors - System.err.println("Premature end of test data."); - return false; - } - br = new BufferedReader(new InputStreamReader( - new UntilHashInputStream(aggregateStream), "UTF-8")); - while ((line = br.readLine()) != null) { - expectedErrors.add(line); - } - } - - if (context != null) { - if (skipLabel()) { // #document-fragment - System.err.println("Premature end of test data."); - return false; - } - UntilHashInputStream stream2 = new UntilHashInputStream(aggregateStream); - while (stream2.read() != -1) { - // spin - } - } - if (hadScriptingDirective && skipLabel()) { // #script-on/off - System.err.println("Premature end of test data."); - return false; - } - - if (skipLabel()) { // #document - System.err.println("Premature end of test data."); - return false; - } - - StringBuilder expectedBuilder = new StringBuilder(); - br = new BufferedReader(new InputStreamReader( - new UntilHashInputStream(aggregateStream), "UTF-8")); - int ch; - while ((ch = br.read()) != -1) { - expectedBuilder.append((char)ch); - } - String expected = expectedBuilder.toString(); - if (expected.contains("")) { - return true; - } - String actual = sw.toString(); - - LinkedList actualErrors = leh.getErrors(); - - if (expected.equals(actual) || (streaming && leh.isFatal()) /* - * && expectedErrors.size() == - * actualErrors.size() - */) { - // System.err.println(stream); - } else { - exitStatus = 1; - System.err.print("Failure.\nData:\n" + stream + "\nExpected:\n" - + expected + "Got: \n" + actual); - System.err.println("Expected errors:"); - for (String err : expectedErrors) { - System.err.println(err); - } - System.err.println("Actual errors:"); - for (String err : actualErrors) { - System.err.println(err); - } - } - } catch (Throwable t) { - exitStatus = 1; - System.err.println("Failure.\nData:\n" + stream); - throw t; - } - return true; - } - - private boolean skipLabel() throws IOException { - int b = aggregateStream.read(); - if (b == -1) { - return true; - } - for (;;) { - b = aggregateStream.read(); - if (b == -1) { - return true; - } else if (b == 0x0A) { - return false; - } - } - } - - /** - * @param args - * @throws Throwable - */ - public static void main(String[] args) throws Throwable { - for (int i = 0; i < args.length; i++) { - TreeTester tester = new TreeTester(new FileInputStream(args[i])); - tester.runTests(); - } - System.exit(exitStatus); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/UntilHashInputStream.java b/test-src/nu/validator/htmlparser/test/UntilHashInputStream.java deleted file mode 100644 index 473a9f7f..00000000 --- a/test-src/nu/validator/htmlparser/test/UntilHashInputStream.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import java.io.IOException; -import java.io.InputStream; - -public class UntilHashInputStream extends InputStream { - - private final StringBuilder builder = new StringBuilder(); - - private final InputStream delegate; - - private int buffer = -1; - - private boolean closed = false; - - /** - * @param delegate - * @throws IOException - */ - public UntilHashInputStream(final InputStream delegate) throws IOException { - this.delegate = delegate; - this.buffer = delegate.read(); - if (buffer == '#') { - closed = true; - } - } - - public int read() throws IOException { - if (closed) { - return -1; - } - int rv = buffer; - buffer = delegate.read(); - if (buffer == '#' && rv == '\n') { - // end of stream - closed = true; - return -1; - } else { - if (rv >= 0x20 && rv < 0x80) { - builder.append(((char)rv)); - } else { - builder.append("0x"); - builder.append(Integer.toHexString(rv)); - } - return rv; - } - } - - /** - * @see java.io.InputStream#close() - */ - @Override - public void close() throws IOException { - super.close(); - if (closed) { - return; - } - for (;;) { - int b = delegate.read(); - if (b == 0x23 || b == -1) { - break; - } - } - closed = true; - } - - /** - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return builder.toString(); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/XmlSerializerTester.java b/test-src/nu/validator/htmlparser/test/XmlSerializerTester.java deleted file mode 100644 index 0d23fda3..00000000 --- a/test-src/nu/validator/htmlparser/test/XmlSerializerTester.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; - -import nu.validator.htmlparser.sax.XmlSerializer; - -public class XmlSerializerTester { - - - - /** - * @param args - * @throws SAXException - */ - public static void main(String[] args) throws SAXException { - AttributesImpl attrs = new AttributesImpl(); - XmlSerializer serializer = new XmlSerializer(System.out); - serializer.startDocument(); - serializer.startElement("1", "a", null, attrs); - serializer.startElement("1", "b", null, attrs); - serializer.endElement("1", "b", null); - serializer.startElement("2", "c", null, attrs); - serializer.endElement("2", "c", null); - attrs.addAttribute("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "about", null, "CDATA", ""); - serializer.startElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "d", null, attrs); - serializer.endElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "d", null); - serializer.startPrefixMapping("rdf", "foo"); - serializer.startElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "e", null, attrs); - serializer.startPrefixMapping("p0", "bar"); - serializer.startElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "f", null, attrs); - serializer.characters("a\uD834\uDD21a\uD834a\uDD21a".toCharArray(), 0, 8); - serializer.endElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "f", null); - serializer.endElement("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "e", null); - - serializer.endPrefixMapping("rdf"); - serializer.endElement("1", "a", null); - serializer.endDocument(); - } - -} diff --git a/test-src/nu/validator/htmlparser/test/XomTest.java b/test-src/nu/validator/htmlparser/test/XomTest.java deleted file mode 100644 index 66d706ae..00000000 --- a/test-src/nu/validator/htmlparser/test/XomTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2009 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.test; - -import nu.xom.Attribute; -import nu.xom.Element; - -public class XomTest { - public static void main(String[] args) { - Element elt = new Element("html", "http://www.w3.org/1999/xhtml"); - elt.addAttribute(new Attribute("xmlns:foo", "bar")); - } -} diff --git a/test-src/nu/validator/htmlparser/test/package.html b/test-src/nu/validator/htmlparser/test/package.html deleted file mode 100644 index 57809b84..00000000 --- a/test-src/nu/validator/htmlparser/test/package.html +++ /dev/null @@ -1,29 +0,0 @@ - - -Package Overview - - - -Test drivers. - - \ No newline at end of file diff --git a/test-src/nu/validator/htmlparser/tools/HTML2HTML.java b/test-src/nu/validator/htmlparser/tools/HTML2HTML.java deleted file mode 100644 index 5e2cf1f5..00000000 --- a/test-src/nu/validator/htmlparser/tools/HTML2HTML.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.MalformedURLException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlParser; -import nu.validator.htmlparser.sax.HtmlSerializer; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -public class HTML2HTML { - - /** - * @param args - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, - TransformerException { - InputStream in; - OutputStream out; - - switch (args.length) { - case 0: - in = System.in; - out = System.out; - break; - case 1: - in = new FileInputStream(args[0]); - out = System.out; - break; - case 2: - in = new FileInputStream(args[0]); - out = new FileOutputStream(args[1]); - break; - default: - System.err.println("Too many arguments. No arguments to use stdin/stdout. One argument to reading from file and write to stdout. Two arguments to read from first file and write to second."); - System.exit(1); - return; - } - - ContentHandler serializer = new HtmlSerializer(out); - - HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALLOW); - - parser.setErrorHandler(new SystemErrErrorHandler()); - parser.setContentHandler(serializer); - parser.setProperty("http://xml.org/sax/properties/lexical-handler", - serializer); - parser.parse(new InputSource(in)); - out.flush(); - out.close(); - } -} diff --git a/test-src/nu/validator/htmlparser/tools/HTML2XML.java b/test-src/nu/validator/htmlparser/tools/HTML2XML.java deleted file mode 100644 index 57666f93..00000000 --- a/test-src/nu/validator/htmlparser/tools/HTML2XML.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.MalformedURLException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.TransformerException; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlParser; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -public class HTML2XML { - - /** - * @param args - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, - TransformerException { - InputStream in; - OutputStream out; - - switch (args.length) { - case 0: - in = System.in; - out = System.out; - break; - case 1: - in = new FileInputStream(args[0]); - out = System.out; - break; - case 2: - in = new FileInputStream(args[0]); - out = new FileOutputStream(args[1]); - break; - default: - System.err.println("Too many arguments. No arguments to use stdin/stdout. One argument to reading from file and write to stdout. Two arguments to read from first file and write to second."); - System.exit(1); - return; - } - - ContentHandler serializer = new XmlSerializer(out); - - HtmlParser parser = new HtmlParser(XmlViolationPolicy.ALTER_INFOSET); - - parser.setErrorHandler(new SystemErrErrorHandler()); - parser.setContentHandler(serializer); - parser.setProperty("http://xml.org/sax/properties/lexical-handler", - serializer); - parser.parse(new InputSource(in)); - out.flush(); - out.close(); - } -} diff --git a/test-src/nu/validator/htmlparser/tools/XML2HTML.java b/test-src/nu/validator/htmlparser/tools/XML2HTML.java deleted file mode 100644 index dad89a5b..00000000 --- a/test-src/nu/validator/htmlparser/tools/XML2HTML.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.MalformedURLException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.transform.TransformerException; - -import nu.validator.htmlparser.sax.HtmlSerializer; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; - -public class XML2HTML { - - /** - * @param args - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, - TransformerException { - InputStream in; - OutputStream out; - - switch (args.length) { - case 0: - in = System.in; - out = System.out; - break; - case 1: - in = new FileInputStream(args[0]); - out = System.out; - break; - case 2: - in = new FileInputStream(args[0]); - out = new FileOutputStream(args[1]); - break; - default: - System.err.println("Too many arguments. No arguments to use stdin/stdout. One argument to reading from file and write to stdout. Two arguments to read from first file and write to second."); - System.exit(1); - return; - } - - ContentHandler serializer = new HtmlSerializer(out); - - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(false); - XMLReader parser = factory.newSAXParser().getXMLReader(); - parser.setErrorHandler(new SystemErrErrorHandler()); - parser.setContentHandler(serializer); - parser.setProperty("http://xml.org/sax/properties/lexical-handler", - serializer); - parser.parse(new InputSource(in)); - out.flush(); - out.close(); - } -} diff --git a/test-src/nu/validator/htmlparser/tools/XML2XML.java b/test-src/nu/validator/htmlparser/tools/XML2XML.java deleted file mode 100644 index 2f6aa24d..00000000 --- a/test-src/nu/validator/htmlparser/tools/XML2XML.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2008 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.MalformedURLException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.transform.TransformerException; - -import nu.validator.htmlparser.sax.NameCheckingXmlSerializer; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; - -public class XML2XML { - - /** - * @param args - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, - TransformerException { - InputStream in; - OutputStream out; - - switch (args.length) { - case 0: - in = System.in; - out = System.out; - break; - case 1: - in = new FileInputStream(args[0]); - out = System.out; - break; - case 2: - in = new FileInputStream(args[0]); - out = new FileOutputStream(args[1]); - break; - default: - System.err.println("Too many arguments. No arguments to use stdin/stdout. One argument to reading from file and write to stdout. Two arguments to read from first file and write to second."); - System.exit(1); - return; - } - - ContentHandler serializer = new NameCheckingXmlSerializer(out); - - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(false); - XMLReader parser = factory.newSAXParser().getXMLReader(); - parser.setErrorHandler(new SystemErrErrorHandler()); - parser.setContentHandler(serializer); - parser.setProperty("http://xml.org/sax/properties/lexical-handler", - serializer); - parser.parse(new InputSource(in)); - out.flush(); - out.close(); - } -} diff --git a/test-src/nu/validator/htmlparser/tools/XSLT4HTML5.java b/test-src/nu/validator/htmlparser/tools/XSLT4HTML5.java deleted file mode 100644 index 05d8193c..00000000 --- a/test-src/nu/validator/htmlparser/tools/XSLT4HTML5.java +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2007 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.MalformedURLException; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.transform.Templates; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.sax.SAXResult; -import javax.xml.transform.sax.SAXTransformerFactory; -import javax.xml.transform.sax.TemplatesHandler; -import javax.xml.transform.sax.TransformerHandler; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.dom.HtmlDocumentBuilder; -import nu.validator.htmlparser.sax.HtmlParser; -import nu.validator.htmlparser.sax.HtmlSerializer; -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.htmlparser.test.SystemErrErrorHandler; - -import org.w3c.dom.Document; -import org.xml.sax.ContentHandler; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.ext.LexicalHandler; - -public class XSLT4HTML5 { - - private enum Mode { - STREAMING_SAX, BUFFERED_SAX, DOM, - } - - private static final String TEMPLATE = "--template="; - - private static final String INPUT_HTML = "--input-html="; - - private static final String INPUT_XML = "--input-xml="; - - private static final String OUTPUT_HTML = "--output-html="; - - private static final String OUTPUT_XML = "--output-xml="; - - private static final String MODE = "--mode="; - - /** - * @param args - * @throws ParserConfigurationException - * @throws SAXException - * @throws IOException - * @throws MalformedURLException - * @throws TransformerException - */ - public static void main(String[] args) throws SAXException, - ParserConfigurationException, MalformedURLException, IOException, TransformerException { - if (args.length == 0) { - System.out.println("--template=file --input-[html|xml]=file --output-[html|xml]=file --mode=[sax-streaming|sax-buffered|dom]"); - System.exit(0); - } - String template = null; - String input = null; - boolean inputHtml = false; - String output = null; - boolean outputHtml = false; - Mode mode = null; - for (int i = 0; i < args.length; i++) { - String arg = args[i]; - if (arg.startsWith(TEMPLATE)) { - if (template == null) { - template = arg.substring(TEMPLATE.length()); - } else { - System.err.println("Tried to set template twice."); - System.exit(1); - } - } else if (arg.startsWith(INPUT_HTML)) { - if (input == null) { - input = arg.substring(INPUT_HTML.length()); - inputHtml = true; - } else { - System.err.println("Tried to set input twice."); - System.exit(2); - } - } else if (arg.startsWith(INPUT_XML)) { - if (input == null) { - input = arg.substring(INPUT_XML.length()); - inputHtml = false; - } else { - System.err.println("Tried to set input twice."); - System.exit(2); - } - } else if (arg.startsWith(OUTPUT_HTML)) { - if (output == null) { - output = arg.substring(OUTPUT_HTML.length()); - outputHtml = true; - } else { - System.err.println("Tried to set output twice."); - System.exit(3); - } - } else if (arg.startsWith(OUTPUT_XML)) { - if (output == null) { - output = arg.substring(OUTPUT_XML.length()); - outputHtml = false; - } else { - System.err.println("Tried to set output twice."); - System.exit(3); - } - } else if (arg.startsWith(MODE)) { - if (mode == null) { - String modeStr = arg.substring(MODE.length()); - if ("dom".equals(modeStr)) { - mode = Mode.DOM; - } else if ("sax-buffered".equals(modeStr)) { - mode = Mode.BUFFERED_SAX; - } else if ("sax-streaming".equals(modeStr)) { - mode = Mode.STREAMING_SAX; - } else { - System.err.println("Unrecognized mode."); - System.exit(5); - } - } else { - System.err.println("Tried to set mode twice."); - System.exit(4); - } - } - } - - if (template == null) { - System.err.println("No template specified."); - System.exit(6); - } - if (input == null) { - System.err.println("No input specified."); - System.exit(7); - } - if (output == null) { - System.err.println("No output specified."); - System.exit(8); - } - if (mode == null) { - mode = Mode.BUFFERED_SAX; - } - - SystemErrErrorHandler errorHandler = new SystemErrErrorHandler(); - - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(false); - XMLReader reader = factory.newSAXParser().getXMLReader(); - reader.setErrorHandler(errorHandler); - - SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance(); - transformerFactory.setErrorListener(errorHandler); - TemplatesHandler templatesHandler = transformerFactory.newTemplatesHandler(); - reader.setContentHandler(templatesHandler); - reader.parse(new File(template).toURI().toASCIIString()); - - Templates templates = templatesHandler.getTemplates(); - - FileOutputStream outputStream = new FileOutputStream(output); - ContentHandler serializer; - if (outputHtml) { - serializer = new HtmlSerializer(outputStream); - } else { - serializer = new XmlSerializer(outputStream); - } - SAXResult result = new SAXResult(new XmlnsDropper(serializer)); - result.setLexicalHandler((LexicalHandler) serializer); - - if (mode == Mode.DOM) { - Document inputDoc; - DocumentBuilder builder; - if (inputHtml) { - builder = new HtmlDocumentBuilder(XmlViolationPolicy.ALTER_INFOSET); - } else { - DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - try { - builder = builderFactory.newDocumentBuilder(); - } catch (ParserConfigurationException e) { - throw new RuntimeException(e); - } - } - inputDoc = builder.parse(new File(input)); - DOMSource inputSource = new DOMSource(inputDoc, - new File(input).toURI().toASCIIString()); - Transformer transformer = templates.newTransformer(); - transformer.setErrorListener(errorHandler); - transformer.transform(inputSource, result); - } else { - if (inputHtml) { - reader = new HtmlParser(XmlViolationPolicy.ALTER_INFOSET); - if (mode == Mode.STREAMING_SAX) { - reader.setProperty("http://validator.nu/properties/streamability-violation-policy", XmlViolationPolicy.FATAL); - } - } - TransformerHandler transformerHandler = transformerFactory.newTransformerHandler(templates); - transformerHandler.setResult(result); - reader.setErrorHandler(errorHandler); - reader.setContentHandler(transformerHandler); - reader.setProperty("http://xml.org/sax/properties/lexical-handler", transformerHandler); - reader.parse(new File(input).toURI().toASCIIString()); - } - outputStream.flush(); - outputStream.close(); - } - -} diff --git a/test-src/nu/validator/htmlparser/tools/XSLT4HTML5XOM.java b/test-src/nu/validator/htmlparser/tools/XSLT4HTML5XOM.java deleted file mode 100644 index b364cc52..00000000 --- a/test-src/nu/validator/htmlparser/tools/XSLT4HTML5XOM.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * Copyright (c) 2007 Mozilla Foundation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; - -import nu.validator.htmlparser.common.XmlViolationPolicy; -import nu.validator.htmlparser.sax.HtmlSerializer; -import nu.validator.htmlparser.xom.HtmlBuilder; -import nu.xom.Builder; -import nu.xom.Document; -import nu.xom.Element; -import nu.xom.Nodes; -import nu.xom.ParsingException; -import nu.xom.Serializer; -import nu.xom.ValidityException; -import nu.xom.converters.SAXConverter; -import nu.xom.xslt.XSLException; -import nu.xom.xslt.XSLTransform; - -import org.xml.sax.SAXException; - -public class XSLT4HTML5XOM { - - private static final String TEMPLATE = "--template="; - - private static final String INPUT_HTML = "--input-html="; - - private static final String INPUT_XML = "--input-xml="; - - private static final String OUTPUT_HTML = "--output-html="; - - private static final String OUTPUT_XML = "--output-xml="; - - /** - * @param args - * @throws IOException - * @throws ParsingException - * @throws ValidityException - * @throws XSLException - * @throws SAXException - */ - public static void main(String[] args) throws ValidityException, - ParsingException, IOException, XSLException, SAXException { - if (args.length == 0) { - System.out.println("--template=file --input-[html|xml]=file --output-[html|xml]=file --mode=[sax-streaming|sax-buffered|dom]"); - System.exit(0); - } - String template = null; - String input = null; - boolean inputHtml = false; - String output = null; - boolean outputHtml = false; - for (int i = 0; i < args.length; i++) { - String arg = args[i]; - if (arg.startsWith(TEMPLATE)) { - if (template == null) { - template = arg.substring(TEMPLATE.length()); - } else { - System.err.println("Tried to set template twice."); - System.exit(1); - } - } else if (arg.startsWith(INPUT_HTML)) { - if (input == null) { - input = arg.substring(INPUT_HTML.length()); - inputHtml = true; - } else { - System.err.println("Tried to set input twice."); - System.exit(2); - } - } else if (arg.startsWith(INPUT_XML)) { - if (input == null) { - input = arg.substring(INPUT_XML.length()); - inputHtml = false; - } else { - System.err.println("Tried to set input twice."); - System.exit(2); - } - } else if (arg.startsWith(OUTPUT_HTML)) { - if (output == null) { - output = arg.substring(OUTPUT_HTML.length()); - outputHtml = true; - } else { - System.err.println("Tried to set output twice."); - System.exit(3); - } - } else if (arg.startsWith(OUTPUT_XML)) { - if (output == null) { - output = arg.substring(OUTPUT_XML.length()); - outputHtml = false; - } else { - System.err.println("Tried to set output twice."); - System.exit(3); - } - } - } - - if (template == null) { - System.err.println("No template specified."); - System.exit(6); - } - if (input == null) { - System.err.println("No input specified."); - System.exit(7); - } - if (output == null) { - System.err.println("No output specified."); - System.exit(8); - } - - Builder builder = new Builder(); - - Document transformationDoc = builder.build(new File(template)); - - XSLTransform transform = new XSLTransform(transformationDoc); - - FileOutputStream outputStream = new FileOutputStream(output); - - Document inputDoc; - if (inputHtml) { - builder = new HtmlBuilder(XmlViolationPolicy.ALTER_INFOSET); - } - inputDoc = builder.build(new File(input)); - Nodes result = transform.transform(inputDoc); - Document outputDoc = new Document((Element) result.get(0)); - if (outputHtml) { - HtmlSerializer htmlSerializer = new HtmlSerializer(outputStream); - SAXConverter converter = new SAXConverter(htmlSerializer); - converter.setLexicalHandler(htmlSerializer); - converter.convert(outputDoc); - } else { - Serializer serializer = new Serializer(outputStream); - serializer.write(outputDoc); - } - outputStream.flush(); - outputStream.close(); - } - -} diff --git a/test-src/nu/validator/htmlparser/tools/XmlnsDropper.java b/test-src/nu/validator/htmlparser/tools/XmlnsDropper.java deleted file mode 100644 index 0e6d4b1c..00000000 --- a/test-src/nu/validator/htmlparser/tools/XmlnsDropper.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.htmlparser.tools; - -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.Locator; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; - -/** - * Quick and dirty hack to work around Xalan xmlns weirdness. - * - * @version $Id$ - * @author hsivonen - */ -class XmlnsDropper implements ContentHandler { - - private final ContentHandler delegate; - - /** - * @param delegate - */ - public XmlnsDropper(final ContentHandler delegate) { - this.delegate = delegate; - } - - /** - * @param ch - * @param start - * @param length - * @throws SAXException - * @see org.xml.sax.ContentHandler#characters(char[], int, int) - */ - public void characters(char[] ch, int start, int length) throws SAXException { - delegate.characters(ch, start, length); - } - - /** - * @throws SAXException - * @see org.xml.sax.ContentHandler#endDocument() - */ - public void endDocument() throws SAXException { - delegate.endDocument(); - } - - /** - * @param uri - * @param localName - * @param qName - * @throws SAXException - * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String) - */ - public void endElement(String uri, String localName, String qName) throws SAXException { - delegate.endElement(uri, localName, qName); - } - - /** - * @param prefix - * @throws SAXException - * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String) - */ - public void endPrefixMapping(String prefix) throws SAXException { - delegate.endPrefixMapping(prefix); - } - - /** - * @param ch - * @param start - * @param length - * @throws SAXException - * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int) - */ - public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { - delegate.ignorableWhitespace(ch, start, length); - } - - /** - * @param target - * @param data - * @throws SAXException - * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String) - */ - public void processingInstruction(String target, String data) throws SAXException { - delegate.processingInstruction(target, data); - } - - /** - * @param locator - * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator) - */ - public void setDocumentLocator(Locator locator) { - delegate.setDocumentLocator(locator); - } - - /** - * @param name - * @throws SAXException - * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String) - */ - public void skippedEntity(String name) throws SAXException { - delegate.skippedEntity(name); - } - - /** - * @throws SAXException - * @see org.xml.sax.ContentHandler#startDocument() - */ - public void startDocument() throws SAXException { - delegate.startDocument(); - } - - /** - * @param uri - * @param localName - * @param qName - * @param atts - * @throws SAXException - * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) - */ - public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { - AttributesImpl ai = new AttributesImpl(); - for (int i = 0; i < atts.getLength(); i++) { - String u = atts.getURI(i); - String t = atts.getType(i); - String v = atts.getValue(i); - String n = atts.getLocalName(i); - String q = atts.getQName(i); - if (q != null) { - if ("xmlns".equals(q) || q.startsWith("xmlns:")) { - continue; - } - } - ai.addAttribute(u, n, q, t, v); - } - delegate.startElement(uri, localName, qName, ai); - } - - /** - * @param prefix - * @param uri - * @throws SAXException - * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String) - */ - public void startPrefixMapping(String prefix, String uri) throws SAXException { - delegate.startPrefixMapping(prefix, uri); - } - -} diff --git a/test-src/nu/validator/htmlparser/tools/package.html b/test-src/nu/validator/htmlparser/tools/package.html deleted file mode 100644 index a04bf3cd..00000000 --- a/test-src/nu/validator/htmlparser/tools/package.html +++ /dev/null @@ -1,29 +0,0 @@ - - -Package Overview - - - -Demo apps. - - \ No newline at end of file diff --git a/test-src/nu/validator/saxtree/test/PassThruPrinter.java b/test-src/nu/validator/saxtree/test/PassThruPrinter.java deleted file mode 100644 index df391d4b..00000000 --- a/test-src/nu/validator/saxtree/test/PassThruPrinter.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2007 Henri Sivonen - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -package nu.validator.saxtree.test; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParserFactory; - -import nu.validator.htmlparser.sax.XmlSerializer; -import nu.validator.saxtree.Node; -import nu.validator.saxtree.TreeBuilder; -import nu.validator.saxtree.TreeParser; - -import org.xml.sax.ContentHandler; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.ext.LexicalHandler; - -public class PassThruPrinter { - public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException { - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setNamespaceAware(true); - factory.setValidating(false); - XMLReader reader = factory.newSAXParser().getXMLReader(); - - TreeBuilder treeBuilder = new TreeBuilder(); - reader.setContentHandler(treeBuilder); - reader.setProperty("http://xml.org/sax/properties/lexical-handler", treeBuilder); - - File file = new File(args[0]); - InputSource is = new InputSource(new FileInputStream(file)); - is.setSystemId(file.toURI().toASCIIString()); - reader.parse(is); - - Node doc = treeBuilder.getRoot(); - - ContentHandler xmlSerializer = new XmlSerializer(System.out); - - TreeParser treeParser = new TreeParser(xmlSerializer, (LexicalHandler) xmlSerializer); - treeParser.parse(doc); - } - -} diff --git a/test-src/nu/validator/saxtree/test/package.html b/test-src/nu/validator/saxtree/test/package.html deleted file mode 100644 index 57809b84..00000000 --- a/test-src/nu/validator/saxtree/test/package.html +++ /dev/null @@ -1,29 +0,0 @@ - - -Package Overview - - - -Test drivers. - - \ No newline at end of file diff --git a/test-src/test/resources/html5lib-tests b/test-src/test/resources/html5lib-tests deleted file mode 160000 index e4463205..00000000 --- a/test-src/test/resources/html5lib-tests +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e4463205ac3c4500e1379103daadfdcfe5e33af5
nu.validator.form-pointer
InputSource
true
<!DOCTYPE html> -...
<!DOCTYPE HTML><html></html>
Script not loaded.
This script puts a function w(s) into the - global scope of the test page, where s is a string to - output to the log. Also, five files are accessible in the current - directory for test purposes: image (a GIF image), - flash (a Flash file), script (a JS file), - style (a CSS file), and document (an HTML - file).
w(s)
image
flash
script
style
document
uoeuo
Test drivers.
Demo apps.