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