Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 2004, 2011, 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 */
28 package com.sun.xml.internal.fastinfoset.tools;
30 import com.sun.xml.internal.fastinfoset.QualifiedName;
31 import com.sun.xml.internal.fastinfoset.util.CharArray;
32 import com.sun.xml.internal.fastinfoset.util.DuplicateAttributeVerifier;
33 import com.sun.xml.internal.fastinfoset.util.KeyIntMap;
34 import com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap;
35 import com.sun.xml.internal.fastinfoset.util.PrefixArray;
36 import com.sun.xml.internal.fastinfoset.util.QualifiedNameArray;
37 import com.sun.xml.internal.fastinfoset.util.StringArray;
38 import com.sun.xml.internal.fastinfoset.util.StringIntMap;
39 import com.sun.xml.internal.fastinfoset.vocab.ParserVocabulary;
40 import com.sun.xml.internal.fastinfoset.vocab.SerializerVocabulary;
42 import org.xml.sax.Attributes;
43 import org.xml.sax.SAXException;
44 import org.xml.sax.ext.LexicalHandler;
45 import org.xml.sax.helpers.DefaultHandler;
46 import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
47 import java.util.Set;
48 import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSerializer;
50 public class VocabularyGenerator extends DefaultHandler implements LexicalHandler {
52 protected SerializerVocabulary _serializerVocabulary;
53 protected ParserVocabulary _parserVocabulary;
54 protected com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary _v;
56 protected int attributeValueSizeConstraint = FastInfosetSerializer.MAX_ATTRIBUTE_VALUE_SIZE;
58 protected int characterContentChunkSizeContraint = FastInfosetSerializer.MAX_CHARACTER_CONTENT_CHUNK_SIZE;
60 /** Creates a new instance of VocabularyGenerator */
61 public VocabularyGenerator() {
62 _serializerVocabulary = new SerializerVocabulary();
63 _parserVocabulary = new ParserVocabulary();
65 _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
66 }
68 public VocabularyGenerator(SerializerVocabulary serializerVocabulary) {
69 _serializerVocabulary = serializerVocabulary;
70 _parserVocabulary = new ParserVocabulary();
72 _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
73 }
75 public VocabularyGenerator(ParserVocabulary parserVocabulary) {
76 _serializerVocabulary = new SerializerVocabulary();
77 _parserVocabulary = parserVocabulary;
79 _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
80 }
82 /** Creates a new instance of VocabularyGenerator */
83 public VocabularyGenerator(SerializerVocabulary serializerVocabulary, ParserVocabulary parserVocabulary) {
84 _serializerVocabulary = serializerVocabulary;
85 _parserVocabulary = parserVocabulary;
87 _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
88 }
90 public com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary getVocabulary() {
91 return _v;
92 }
94 public void setCharacterContentChunkSizeLimit(int size) {
95 if (size < 0 ) {
96 size = 0;
97 }
99 characterContentChunkSizeContraint = size;
100 }
102 public int getCharacterContentChunkSizeLimit() {
103 return characterContentChunkSizeContraint;
104 }
106 public void setAttributeValueSizeLimit(int size) {
107 if (size < 0 ) {
108 size = 0;
109 }
111 attributeValueSizeConstraint = size;
112 }
114 public int getAttributeValueSizeLimit() {
115 return attributeValueSizeConstraint;
116 }
118 // ContentHandler
120 public void startDocument() throws SAXException {
121 }
123 public void endDocument() throws SAXException {
124 }
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 }
131 public void endPrefixMapping(String prefix) throws SAXException {
132 }
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);
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);
142 String value = atts.getValue(a);
143 if (value.length() < attributeValueSizeConstraint) {
144 addToTable(value, _v.attributeValues, _serializerVocabulary.attributeValue, _parserVocabulary.attributeValue);
145 }
146 }
147 }
149 public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
150 }
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 }
158 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
159 }
161 public void processingInstruction(String target, String data) throws SAXException {
162 }
164 public void setDocumentLocator(org.xml.sax.Locator locator) {
165 }
167 public void skippedEntity(String name) throws SAXException {
168 }
172 // LexicalHandler
174 public void comment(char[] ch, int start, int length) throws SAXException {
175 }
177 public void startCDATA() throws SAXException {
178 }
180 public void endCDATA() throws SAXException {
181 }
183 public void startDTD(String name, String publicId, String systemId) throws SAXException {
184 }
186 public void endDTD() throws SAXException {
187 }
189 public void startEntity(String name) throws SAXException {
190 }
192 public void endEntity(String name) throws SAXException {
193 }
196 public void addToTable(String s, Set v, StringIntMap m, StringArray a) {
197 if (s.length() == 0) {
198 return;
199 }
201 if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) {
202 a.add(s);
203 }
205 v.add(s);
206 }
208 public void addToTable(String s, Set v, StringIntMap m, PrefixArray a) {
209 if (s.length() == 0) {
210 return;
211 }
213 if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) {
214 a.add(s);
215 }
217 v.add(s);
218 }
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 }
225 _v.characterContentChunks.add(c.toString());
226 }
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 }
241 String prefix = getPrefixFromQualifiedName(qName);
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 }
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 }
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);
275 v.add(name.getQName());
276 }
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 }
287 }