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 nu.validator.htmlparser.impl.ContentModelFlag; 026 import nu.validator.htmlparser.impl.TokenHandler; 027 import nu.validator.htmlparser.impl.Tokenizer; 028 029 import org.xml.sax.Attributes; 030 import org.xml.sax.ErrorHandler; 031 import org.xml.sax.SAXException; 032 import org.xml.sax.SAXParseException; 033 034 import com.sdicons.json.model.JSONArray; 035 import com.sdicons.json.model.JSONBoolean; 036 import com.sdicons.json.model.JSONNull; 037 import com.sdicons.json.model.JSONObject; 038 import com.sdicons.json.model.JSONString; 039 040 public class JSONArrayTokenHandler implements TokenHandler, ErrorHandler { 041 042 private static final JSONString DOCTYPE = new JSONString("DOCTYPE"); 043 044 private static final JSONString START_TAG = new JSONString("StartTag"); 045 046 private static final JSONString END_TAG = new JSONString("EndTag"); 047 048 private static final JSONString COMMENT = new JSONString("Comment"); 049 050 private static final JSONString CHARACTER = new JSONString("Character"); 051 052 private static final JSONString PARSE_ERROR = new JSONString("ParseError"); 053 054 private final StringBuilder builder = new StringBuilder(); 055 056 private JSONArray array = null; 057 058 private ContentModelFlag contentModelFlag; 059 060 private String contentModelElement; 061 062 public void setContentModelFlag(ContentModelFlag contentModelFlag, String contentModelElement) { 063 this.contentModelFlag = contentModelFlag; 064 this.contentModelElement = contentModelElement; 065 } 066 067 public void characters(char[] buf, int start, int length) 068 throws SAXException { 069 builder.append(buf, start, length); 070 } 071 072 private void flushCharacters() { 073 if (builder.length() > 0) { 074 JSONArray token = new JSONArray(); 075 token.getValue().add(CHARACTER); 076 token.getValue().add(new JSONString(builder.toString())); 077 array.getValue().add(token); 078 builder.setLength(0); 079 } 080 } 081 082 public void comment(char[] buf, int length) throws SAXException { 083 flushCharacters(); 084 JSONArray token = new JSONArray(); 085 token.getValue().add(COMMENT); 086 token.getValue().add(new JSONString(new String(buf, 0, length))); 087 array.getValue().add(token); 088 } 089 090 public void doctype(String name, String publicIdentifier, String systemIdentifier, boolean correct) throws SAXException { 091 flushCharacters(); 092 JSONArray token = new JSONArray(); 093 token.getValue().add(DOCTYPE); 094 token.getValue().add(new JSONString(name)); 095 token.getValue().add(publicIdentifier == null ? JSONNull.NULL : new JSONString(publicIdentifier)); 096 token.getValue().add(systemIdentifier == null ? JSONNull.NULL : new JSONString(systemIdentifier)); 097 token.getValue().add(new JSONBoolean(correct)); 098 array.getValue().add(token); 099 } 100 101 public void endTag(String name, Attributes attributes) throws SAXException { 102 flushCharacters(); 103 JSONArray token = new JSONArray(); 104 token.getValue().add(END_TAG); 105 token.getValue().add(new JSONString(name)); 106 array.getValue().add(token); 107 } 108 109 public void eof() throws SAXException { 110 flushCharacters(); 111 } 112 113 public void start(Tokenizer self) throws SAXException { 114 array = new JSONArray(); 115 self.setContentModelFlag(contentModelFlag, contentModelElement); 116 } 117 118 public void startTag(String name, Attributes attributes) 119 throws SAXException { 120 flushCharacters(); 121 JSONArray token = new JSONArray(); 122 token.getValue().add(START_TAG); 123 token.getValue().add(new JSONString(name)); 124 JSONObject attrs = new JSONObject(); 125 for (int i = 0; i < attributes.getLength(); i++) { 126 attrs.getValue().put(attributes.getQName(i), new JSONString(attributes.getValue(i))); 127 } 128 token.getValue().add(attrs); 129 array.getValue().add(token); 130 } 131 132 public boolean wantsComments() throws SAXException { 133 return true; 134 } 135 136 public void error(SAXParseException exception) throws SAXException { 137 flushCharacters(); 138 array.getValue().add(PARSE_ERROR); 139 } 140 141 public void fatalError(SAXParseException exception) throws SAXException { 142 throw new RuntimeException("Should never happen."); 143 } 144 145 public void warning(SAXParseException exception) throws SAXException { 146 } 147 148 /** 149 * Returns the array. 150 * 151 * @return the array 152 */ 153 public JSONArray getArray() { 154 return array; 155 } 156 157 }