001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.vocab;
040:
041: import com.sun.xml.fastinfoset.EncodingConstants;
042: import com.sun.xml.fastinfoset.QualifiedName;
043: import com.sun.xml.fastinfoset.util.CharArray;
044: import com.sun.xml.fastinfoset.util.CharArrayArray;
045: import com.sun.xml.fastinfoset.util.ContiguousCharArrayArray;
046: import com.sun.xml.fastinfoset.util.DuplicateAttributeVerifier;
047: import com.sun.xml.fastinfoset.util.FixedEntryStringIntMap;
048: import com.sun.xml.fastinfoset.util.KeyIntMap;
049: import com.sun.xml.fastinfoset.util.PrefixArray;
050: import com.sun.xml.fastinfoset.util.QualifiedNameArray;
051: import com.sun.xml.fastinfoset.util.StringArray;
052: import com.sun.xml.fastinfoset.util.StringIntMap;
053: import com.sun.xml.fastinfoset.util.ValueArray;
054: import java.util.Iterator;
055: import javax.xml.namespace.QName;
056:
057: public class ParserVocabulary extends Vocabulary {
058: public static final String IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS_PEOPERTY = "com.sun.xml.fastinfoset.vocab.ParserVocabulary.IdentifyingStringTable.maximumItems";
059: public static final String NON_IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS_PEOPERTY = "com.sun.xml.fastinfoset.vocab.ParserVocabulary.NonIdentifyingStringTable.maximumItems";
060: public static final String NON_IDENTIFYING_STRING_TABLE_MAXIMUM_CHARACTERS_PEOPERTY = "com.sun.xml.fastinfoset.vocab.ParserVocabulary.NonIdentifyingStringTable.maximumCharacters";
061:
062: protected static final int IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS = getIntegerValueFromProperty(IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS_PEOPERTY);
063: protected static final int NON_IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS = getIntegerValueFromProperty(NON_IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS_PEOPERTY);
064: protected static final int NON_IDENTIFYING_STRING_TABLE_MAXIMUM_CHARACTERS = getIntegerValueFromProperty(NON_IDENTIFYING_STRING_TABLE_MAXIMUM_CHARACTERS_PEOPERTY);
065:
066: private static int getIntegerValueFromProperty(String property) {
067: String value = System.getProperty(property);
068: if (value == null) {
069: return Integer.MAX_VALUE;
070: }
071:
072: try {
073: return Math.max(Integer.parseInt(value),
074: ValueArray.DEFAULT_CAPACITY);
075: } catch (NumberFormatException e) {
076: return Integer.MAX_VALUE;
077: }
078: }
079:
080: public final CharArrayArray restrictedAlphabet = new CharArrayArray(
081: ValueArray.DEFAULT_CAPACITY, 256);
082: public final StringArray encodingAlgorithm = new StringArray(
083: ValueArray.DEFAULT_CAPACITY, 256, true);
084:
085: public final StringArray namespaceName;
086: public final PrefixArray prefix;
087: public final StringArray localName;
088: public final StringArray otherNCName;
089: public final StringArray otherURI;
090: public final StringArray attributeValue;
091: public final CharArrayArray otherString;
092:
093: public final ContiguousCharArrayArray characterContentChunk;
094:
095: public final QualifiedNameArray elementName;
096: public final QualifiedNameArray attributeName;
097:
098: public final ValueArray[] tables = new ValueArray[12];
099:
100: protected SerializerVocabulary _readOnlyVocabulary;
101:
102: /** Creates a new instance of ParserVocabulary */
103: public ParserVocabulary() {
104: namespaceName = new StringArray(ValueArray.DEFAULT_CAPACITY,
105: IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS, false);
106: prefix = new PrefixArray(ValueArray.DEFAULT_CAPACITY,
107: IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS);
108: localName = new StringArray(ValueArray.DEFAULT_CAPACITY,
109: IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS, false);
110: otherNCName = new StringArray(ValueArray.DEFAULT_CAPACITY,
111: IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS, false);
112: otherURI = new StringArray(ValueArray.DEFAULT_CAPACITY,
113: IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS, true);
114: attributeValue = new StringArray(ValueArray.DEFAULT_CAPACITY,
115: NON_IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS, true);
116: otherString = new CharArrayArray(ValueArray.DEFAULT_CAPACITY,
117: NON_IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS);
118:
119: characterContentChunk = new ContiguousCharArrayArray(
120: ValueArray.DEFAULT_CAPACITY,
121: NON_IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS,
122: ContiguousCharArrayArray.INITIAL_CHARACTER_SIZE,
123: NON_IDENTIFYING_STRING_TABLE_MAXIMUM_CHARACTERS);
124:
125: elementName = new QualifiedNameArray(
126: ValueArray.DEFAULT_CAPACITY,
127: IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS);
128: attributeName = new QualifiedNameArray(
129: ValueArray.DEFAULT_CAPACITY,
130: IDENTIFYING_STRING_TABLE_MAXIMUM_ITEMS);
131:
132: tables[RESTRICTED_ALPHABET] = restrictedAlphabet;
133: tables[ENCODING_ALGORITHM] = encodingAlgorithm;
134: tables[PREFIX] = prefix;
135: tables[NAMESPACE_NAME] = namespaceName;
136: tables[LOCAL_NAME] = localName;
137: tables[OTHER_NCNAME] = otherNCName;
138: tables[OTHER_URI] = otherURI;
139: tables[ATTRIBUTE_VALUE] = attributeValue;
140: tables[OTHER_STRING] = otherString;
141: tables[CHARACTER_CONTENT_CHUNK] = characterContentChunk;
142: tables[ELEMENT_NAME] = elementName;
143: tables[ATTRIBUTE_NAME] = attributeName;
144: }
145:
146: public ParserVocabulary(org.jvnet.fastinfoset.Vocabulary v) {
147: this ();
148:
149: convertVocabulary(v);
150: }
151:
152: void setReadOnlyVocabulary(ParserVocabulary readOnlyVocabulary,
153: boolean clear) {
154: for (int i = 0; i < tables.length; i++) {
155: tables[i].setReadOnlyArray(readOnlyVocabulary.tables[i],
156: clear);
157: }
158: }
159:
160: public void setInitialVocabulary(
161: ParserVocabulary initialVocabulary, boolean clear) {
162: setExternalVocabularyURI(null);
163: setInitialReadOnlyVocabulary(true);
164: setReadOnlyVocabulary(initialVocabulary, clear);
165: }
166:
167: public void setReferencedVocabulary(String referencedVocabularyURI,
168: ParserVocabulary referencedVocabulary, boolean clear) {
169: if (!referencedVocabularyURI.equals(getExternalVocabularyURI())) {
170: setInitialReadOnlyVocabulary(false);
171: setExternalVocabularyURI(referencedVocabularyURI);
172: setReadOnlyVocabulary(referencedVocabulary, clear);
173: }
174: }
175:
176: public void clear() {
177: for (int i = 0; i < tables.length; i++) {
178: tables[i].clear();
179: }
180: }
181:
182: private void convertVocabulary(org.jvnet.fastinfoset.Vocabulary v) {
183: final StringIntMap prefixMap = new FixedEntryStringIntMap(
184: EncodingConstants.XML_NAMESPACE_PREFIX, 8);
185: final StringIntMap namespaceNameMap = new FixedEntryStringIntMap(
186: EncodingConstants.XML_NAMESPACE_NAME, 8);
187: final StringIntMap localNameMap = new StringIntMap();
188:
189: addToTable(v.restrictedAlphabets.iterator(), restrictedAlphabet);
190: addToTable(v.encodingAlgorithms.iterator(), encodingAlgorithm);
191: addToTable(v.prefixes.iterator(), prefix, prefixMap);
192: addToTable(v.namespaceNames.iterator(), namespaceName,
193: namespaceNameMap);
194: addToTable(v.localNames.iterator(), localName, localNameMap);
195: addToTable(v.otherNCNames.iterator(), otherNCName);
196: addToTable(v.otherURIs.iterator(), otherURI);
197: addToTable(v.attributeValues.iterator(), attributeValue);
198: addToTable(v.otherStrings.iterator(), otherString);
199: addToTable(v.characterContentChunks.iterator(),
200: characterContentChunk);
201: addToTable(v.elements.iterator(), elementName, false,
202: prefixMap, namespaceNameMap, localNameMap);
203: addToTable(v.attributes.iterator(), attributeName, true,
204: prefixMap, namespaceNameMap, localNameMap);
205: }
206:
207: private void addToTable(Iterator i, StringArray a) {
208: while (i.hasNext()) {
209: addToTable((String) i.next(), a, null);
210: }
211: }
212:
213: private void addToTable(Iterator i, StringArray a, StringIntMap m) {
214: while (i.hasNext()) {
215: addToTable((String) i.next(), a, m);
216: }
217: }
218:
219: private void addToTable(String s, StringArray a, StringIntMap m) {
220: if (s.length() == 0) {
221: return;
222: }
223:
224: if (m != null)
225: m.obtainIndex(s);
226: a.add(s);
227: }
228:
229: private void addToTable(Iterator i, PrefixArray a, StringIntMap m) {
230: while (i.hasNext()) {
231: addToTable((String) i.next(), a, m);
232: }
233: }
234:
235: private void addToTable(String s, PrefixArray a, StringIntMap m) {
236: if (s.length() == 0) {
237: return;
238: }
239:
240: if (m != null)
241: m.obtainIndex(s);
242: a.add(s);
243: }
244:
245: private void addToTable(Iterator i, ContiguousCharArrayArray a) {
246: while (i.hasNext()) {
247: addToTable((String) i.next(), a);
248: }
249: }
250:
251: private void addToTable(String s, ContiguousCharArrayArray a) {
252: if (s.length() == 0) {
253: return;
254: }
255:
256: char[] c = s.toCharArray();
257: a.add(c, c.length);
258: }
259:
260: private void addToTable(Iterator i, CharArrayArray a) {
261: while (i.hasNext()) {
262: addToTable((String) i.next(), a);
263: }
264: }
265:
266: private void addToTable(String s, CharArrayArray a) {
267: if (s.length() == 0) {
268: return;
269: }
270:
271: char[] c = s.toCharArray();
272: a.add(new CharArray(c, 0, c.length, false));
273: }
274:
275: private void addToTable(Iterator i, QualifiedNameArray a,
276: boolean isAttribute, StringIntMap prefixMap,
277: StringIntMap namespaceNameMap, StringIntMap localNameMap) {
278: while (i.hasNext()) {
279: addToNameTable((QName) i.next(), a, isAttribute, prefixMap,
280: namespaceNameMap, localNameMap);
281: }
282: }
283:
284: private void addToNameTable(QName n, QualifiedNameArray a,
285: boolean isAttribute, StringIntMap prefixMap,
286: StringIntMap namespaceNameMap, StringIntMap localNameMap) {
287: int namespaceURIIndex = -1;
288: int prefixIndex = -1;
289: if (n.getNamespaceURI().length() > 0) {
290: namespaceURIIndex = namespaceNameMap.obtainIndex(n
291: .getNamespaceURI());
292: if (namespaceURIIndex == KeyIntMap.NOT_PRESENT) {
293: namespaceURIIndex = namespaceName.getSize();
294: namespaceName.add(n.getNamespaceURI());
295: }
296:
297: if (n.getPrefix().length() > 0) {
298: prefixIndex = prefixMap.obtainIndex(n.getPrefix());
299: if (prefixIndex == KeyIntMap.NOT_PRESENT) {
300: prefixIndex = prefix.getSize();
301: prefix.add(n.getPrefix());
302: }
303: }
304: }
305:
306: int localNameIndex = localNameMap.obtainIndex(n.getLocalPart());
307: if (localNameIndex == KeyIntMap.NOT_PRESENT) {
308: localNameIndex = localName.getSize();
309: localName.add(n.getLocalPart());
310: }
311:
312: QualifiedName name = new QualifiedName(n.getPrefix(), n
313: .getNamespaceURI(), n.getLocalPart(), a.getSize(),
314: prefixIndex, namespaceURIIndex, localNameIndex);
315: if (isAttribute) {
316: name
317: .createAttributeValues(DuplicateAttributeVerifier.MAP_SIZE);
318: }
319: a.add(name);
320: }
321: }
|