1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/*
* 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);
}
}
|