001 /*
002 * Copyright (c) 2007 Henri Sivonen
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a
005 * copy of this software and associated documentation files (the "Software"),
006 * to deal in the Software without restriction, including without limitation
007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
008 * and/or sell copies of the Software, and to permit persons to whom the
009 * Software is furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020 * DEALINGS IN THE SOFTWARE.
021 */
022
023 package nu.validator.htmlparser.test;
024
025 import java.io.FileInputStream;
026 import java.io.IOException;
027 import java.io.InputStream;
028 import java.nio.charset.Charset;
029
030 import nu.validator.htmlparser.impl.HtmlInputStreamReader;
031
032 import org.xml.sax.SAXException;
033
034 public class EncodingTester {
035
036 private final InputStream aggregateStream;
037
038 private final StringBuilder builder = new StringBuilder();
039
040 /**
041 * @param aggregateStream
042 */
043 public EncodingTester(InputStream aggregateStream) {
044 this.aggregateStream = aggregateStream;
045 }
046
047 private void runTests() throws IOException, SAXException {
048 while (runTest()) {
049 // spin
050 }
051 }
052
053 private boolean runTest() throws IOException, SAXException {
054 if (skipLabel()) {
055 return false;
056 }
057 UntilHashInputStream stream = new UntilHashInputStream(aggregateStream);
058 HtmlInputStreamReader reader = new HtmlInputStreamReader(stream, null,
059 null, null);
060 Charset charset = reader.getCharset();
061 stream.close();
062 if (skipLabel()) {
063 System.err.println("Premature end of test data.");
064 return false;
065 }
066 builder.setLength(0);
067 loop: for (;;) {
068 int b = aggregateStream.read();
069 switch (b) {
070 case '\n':
071 break loop;
072 case -1:
073 System.err.println("Premature end of test data.");
074 return false;
075 default:
076 builder.append(((char) b));
077 }
078 }
079 String sniffed = charset.name();
080 String expected = builder.toString();
081 if (expected.equalsIgnoreCase(sniffed)) {
082 System.err.println("Success.");
083 // System.err.println(stream);
084 } else {
085 System.err.println("Failure. Expected: " + expected + " got "
086 + sniffed + ".");
087 System.err.println(stream);
088 }
089 return true;
090 }
091
092 private boolean skipLabel() throws IOException {
093 int b = aggregateStream.read();
094 if (b == -1) {
095 return true;
096 }
097 for (;;) {
098 b = aggregateStream.read();
099 if (b == -1) {
100 return true;
101 } else if (b == 0x0A) {
102 return false;
103 }
104 }
105 }
106
107 /**
108 * @param args
109 * @throws SAXException
110 * @throws IOException
111 */
112 public static void main(String[] args) throws IOException, SAXException {
113 for (int i = 0; i < args.length; i++) {
114 EncodingTester tester = new EncodingTester(new FileInputStream(
115 args[i]));
116 tester.runTests();
117 }
118 }
119
120 }