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