1/* 2 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 * 25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. 26 */ 27 28package com.sun.xml.internal.fastinfoset.tools; 29 30import com.sun.xml.internal.fastinfoset.QualifiedName; 31import com.sun.xml.internal.fastinfoset.util.CharArray; 32import com.sun.xml.internal.fastinfoset.util.DuplicateAttributeVerifier; 33import com.sun.xml.internal.fastinfoset.util.KeyIntMap; 34import com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap; 35import com.sun.xml.internal.fastinfoset.util.PrefixArray; 36import com.sun.xml.internal.fastinfoset.util.QualifiedNameArray; 37import com.sun.xml.internal.fastinfoset.util.StringArray; 38import com.sun.xml.internal.fastinfoset.util.StringIntMap; 39import com.sun.xml.internal.fastinfoset.vocab.ParserVocabulary; 40import com.sun.xml.internal.fastinfoset.vocab.SerializerVocabulary; 41 42import org.xml.sax.Attributes; 43import org.xml.sax.SAXException; 44import org.xml.sax.ext.LexicalHandler; 45import org.xml.sax.helpers.DefaultHandler; 46import com.sun.xml.internal.fastinfoset.CommonResourceBundle; 47import java.util.Set; 48import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSerializer; 49 50public class VocabularyGenerator extends DefaultHandler implements LexicalHandler { 51 52 protected SerializerVocabulary _serializerVocabulary; 53 protected ParserVocabulary _parserVocabulary; 54 protected com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary _v; 55 56 protected int attributeValueSizeConstraint = FastInfosetSerializer.MAX_ATTRIBUTE_VALUE_SIZE; 57 58 protected int characterContentChunkSizeContraint = FastInfosetSerializer.MAX_CHARACTER_CONTENT_CHUNK_SIZE; 59 60 /** Creates a new instance of VocabularyGenerator */ 61 public VocabularyGenerator() { 62 _serializerVocabulary = new SerializerVocabulary(); 63 _parserVocabulary = new ParserVocabulary(); 64 65 _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary(); 66 } 67 68 public VocabularyGenerator(SerializerVocabulary serializerVocabulary) { 69 _serializerVocabulary = serializerVocabulary; 70 _parserVocabulary = new ParserVocabulary(); 71 72 _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary(); 73 } 74 75 public VocabularyGenerator(ParserVocabulary parserVocabulary) { 76 _serializerVocabulary = new SerializerVocabulary(); 77 _parserVocabulary = parserVocabulary; 78 79 _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary(); 80 } 81 82 /** Creates a new instance of VocabularyGenerator */ 83 public VocabularyGenerator(SerializerVocabulary serializerVocabulary, ParserVocabulary parserVocabulary) { 84 _serializerVocabulary = serializerVocabulary; 85 _parserVocabulary = parserVocabulary; 86 87 _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary(); 88 } 89 90 public com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary getVocabulary() { 91 return _v; 92 } 93 94 public void setCharacterContentChunkSizeLimit(int size) { 95 if (size < 0 ) { 96 size = 0; 97 } 98 99 characterContentChunkSizeContraint = size; 100 } 101 102 public int getCharacterContentChunkSizeLimit() { 103 return characterContentChunkSizeContraint; 104 } 105 106 public void setAttributeValueSizeLimit(int size) { 107 if (size < 0 ) { 108 size = 0; 109 } 110 111 attributeValueSizeConstraint = size; 112 } 113 114 public int getAttributeValueSizeLimit() { 115 return attributeValueSizeConstraint; 116 } 117 118 // ContentHandler 119 120 public void startDocument() throws SAXException { 121 } 122 123 public void endDocument() throws SAXException { 124 } 125 126 public void startPrefixMapping(String prefix, String uri) throws SAXException { 127 addToTable(prefix, _v.prefixes, _serializerVocabulary.prefix, _parserVocabulary.prefix); 128 addToTable(uri, _v.namespaceNames, _serializerVocabulary.namespaceName, _parserVocabulary.namespaceName); 129 } 130 131 public void endPrefixMapping(String prefix) throws SAXException { 132 } 133 134 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 135 addToNameTable(namespaceURI, qName, localName, 136 _v.elements, _serializerVocabulary.elementName, _parserVocabulary.elementName, false); 137 138 for (int a = 0; a < atts.getLength(); a++) { 139 addToNameTable(atts.getURI(a), atts.getQName(a), atts.getLocalName(a), 140 _v.attributes, _serializerVocabulary.attributeName, _parserVocabulary.attributeName, true); 141 142 String value = atts.getValue(a); 143 if (value.length() < attributeValueSizeConstraint) { 144 addToTable(value, _v.attributeValues, _serializerVocabulary.attributeValue, _parserVocabulary.attributeValue); 145 } 146 } 147 } 148 149 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 150 } 151 152 public void characters(char[] ch, int start, int length) throws SAXException { 153 if (length < characterContentChunkSizeContraint) { 154 addToCharArrayTable(new CharArray(ch, start, length, true)); 155 } 156 } 157 158 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 159 } 160 161 public void processingInstruction(String target, String data) throws SAXException { 162 } 163 164 public void setDocumentLocator(org.xml.sax.Locator locator) { 165 } 166 167 public void skippedEntity(String name) throws SAXException { 168 } 169 170 171 172 // LexicalHandler 173 174 public void comment(char[] ch, int start, int length) throws SAXException { 175 } 176 177 public void startCDATA() throws SAXException { 178 } 179 180 public void endCDATA() throws SAXException { 181 } 182 183 public void startDTD(String name, String publicId, String systemId) throws SAXException { 184 } 185 186 public void endDTD() throws SAXException { 187 } 188 189 public void startEntity(String name) throws SAXException { 190 } 191 192 public void endEntity(String name) throws SAXException { 193 } 194 195 196 public void addToTable(String s, Set v, StringIntMap m, StringArray a) { 197 if (s.length() == 0) { 198 return; 199 } 200 201 if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) { 202 a.add(s); 203 } 204 205 v.add(s); 206 } 207 208 public void addToTable(String s, Set v, StringIntMap m, PrefixArray a) { 209 if (s.length() == 0) { 210 return; 211 } 212 213 if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) { 214 a.add(s); 215 } 216 217 v.add(s); 218 } 219 220 public void addToCharArrayTable(CharArray c) { 221 if (_serializerVocabulary.characterContentChunk.obtainIndex(c.ch, c.start, c.length, false) == KeyIntMap.NOT_PRESENT) { 222 _parserVocabulary.characterContentChunk.add(c.ch, c.length); 223 } 224 225 _v.characterContentChunks.add(c.toString()); 226 } 227 228 public void addToNameTable(String namespaceURI, String qName, String localName, 229 Set v, LocalNameQualifiedNamesMap m, QualifiedNameArray a, 230 boolean isAttribute) throws SAXException { 231 LocalNameQualifiedNamesMap.Entry entry = m.obtainEntry(qName); 232 if (entry._valueIndex > 0) { 233 QualifiedName[] names = entry._value; 234 for (int i = 0; i < entry._valueIndex; i++) { 235 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) { 236 return; 237 } 238 } 239 } 240 241 String prefix = getPrefixFromQualifiedName(qName); 242 243 int namespaceURIIndex = -1; 244 int prefixIndex = -1; 245 int localNameIndex = -1; 246 if (namespaceURI.length() > 0) { 247 namespaceURIIndex = _serializerVocabulary.namespaceName.get(namespaceURI); 248 if (namespaceURIIndex == KeyIntMap.NOT_PRESENT) { 249 throw new SAXException(CommonResourceBundle.getInstance(). 250 getString("message.namespaceURINotIndexed", new Object[]{Integer.valueOf(namespaceURIIndex)})); 251 } 252 253 if (prefix.length() > 0) { 254 prefixIndex = _serializerVocabulary.prefix.get(prefix); 255 if (prefixIndex == KeyIntMap.NOT_PRESENT) { 256 throw new SAXException(CommonResourceBundle.getInstance(). 257 getString("message.prefixNotIndexed", new Object[]{Integer.valueOf(prefixIndex)})); 258 } 259 } 260 } 261 262 localNameIndex = _serializerVocabulary.localName.obtainIndex(localName); 263 if (localNameIndex == KeyIntMap.NOT_PRESENT) { 264 _parserVocabulary.localName.add(localName); 265 localNameIndex = _parserVocabulary.localName.getSize() - 1; 266 } 267 QualifiedName name = new QualifiedName(prefix, namespaceURI, localName, m.getNextIndex(), 268 prefixIndex, namespaceURIIndex, localNameIndex); 269 if (isAttribute) { 270 name.createAttributeValues(DuplicateAttributeVerifier.MAP_SIZE); 271 } 272 entry.addQualifiedName(name); 273 a.add(name); 274 275 v.add(name.getQName()); 276 } 277 278 public static String getPrefixFromQualifiedName(String qName) { 279 int i = qName.indexOf(':'); 280 String prefix = ""; 281 if (i != -1) { 282 prefix = qName.substring(0, i); 283 } 284 return prefix; 285 } 286 287} 288