001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: *
025: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
026: */
027:
028: /*
029: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
030: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
031: *
032: * This code is free software; you can redistribute it and/or modify it
033: * under the terms of the GNU General Public License version 2 only, as
034: * published by the Free Software Foundation. Sun designates this
035: * particular file as subject to the "Classpath" exception as provided
036: * by Sun in the LICENSE file that accompanied this code.
037: *
038: * This code is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
040: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
041: * version 2 for more details (a copy is included in the LICENSE file that
042: * accompanied this code).
043: *
044: * You should have received a copy of the GNU General Public License version
045: * 2 along with this work; if not, write to the Free Software Foundation,
046: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
047: *
048: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
049: * CA 95054 USA or visit www.sun.com if you need additional information or
050: * have any questions.
051: *
052: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
053: *
054: */
055:
056: package com.sun.xml.internal.fastinfoset.vocab;
057:
058: import com.sun.xml.internal.fastinfoset.EncodingConstants;
059: import com.sun.xml.internal.fastinfoset.QualifiedName;
060: import com.sun.xml.internal.fastinfoset.util.CharArrayIntMap;
061: import com.sun.xml.internal.fastinfoset.util.FixedEntryStringIntMap;
062: import com.sun.xml.internal.fastinfoset.util.KeyIntMap;
063: import com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap;
064: import com.sun.xml.internal.fastinfoset.util.StringIntMap;
065: import java.util.Iterator;
066: import javax.xml.namespace.QName;
067:
068: public class SerializerVocabulary extends Vocabulary {
069: public final StringIntMap restrictedAlphabet;
070: public final StringIntMap encodingAlgorithm;
071:
072: public final StringIntMap namespaceName;
073: public final StringIntMap prefix;
074: public final StringIntMap localName;
075: public final StringIntMap otherNCName;
076: public final StringIntMap otherURI;
077: public final StringIntMap attributeValue;
078: public final CharArrayIntMap otherString;
079:
080: public final CharArrayIntMap characterContentChunk;
081:
082: public final LocalNameQualifiedNamesMap elementName;
083: public final LocalNameQualifiedNamesMap attributeName;
084:
085: public final KeyIntMap[] tables = new KeyIntMap[12];
086:
087: protected boolean _useLocalNameAsKey;
088:
089: protected SerializerVocabulary _readOnlyVocabulary;
090:
091: public SerializerVocabulary() {
092: tables[RESTRICTED_ALPHABET] = restrictedAlphabet = new StringIntMap(
093: 4);
094: tables[ENCODING_ALGORITHM] = encodingAlgorithm = new StringIntMap(
095: 4);
096: tables[PREFIX] = prefix = new FixedEntryStringIntMap(
097: EncodingConstants.XML_NAMESPACE_PREFIX, 8);
098: tables[NAMESPACE_NAME] = namespaceName = new FixedEntryStringIntMap(
099: EncodingConstants.XML_NAMESPACE_NAME, 8);
100: tables[LOCAL_NAME] = localName = new StringIntMap();
101: tables[OTHER_NCNAME] = otherNCName = new StringIntMap(4);
102: tables[OTHER_URI] = otherURI = new StringIntMap(4);
103: tables[ATTRIBUTE_VALUE] = attributeValue = new StringIntMap();
104: tables[OTHER_STRING] = otherString = new CharArrayIntMap(4);
105: tables[CHARACTER_CONTENT_CHUNK] = characterContentChunk = new CharArrayIntMap();
106: tables[ELEMENT_NAME] = elementName = new LocalNameQualifiedNamesMap();
107: tables[ATTRIBUTE_NAME] = attributeName = new LocalNameQualifiedNamesMap();
108: }
109:
110: public SerializerVocabulary(
111: com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary v,
112: boolean useLocalNameAsKey) {
113: this ();
114:
115: _useLocalNameAsKey = useLocalNameAsKey;
116: convertVocabulary(v);
117: }
118:
119: public SerializerVocabulary getReadOnlyVocabulary() {
120: return _readOnlyVocabulary;
121: }
122:
123: protected void setReadOnlyVocabulary(
124: SerializerVocabulary readOnlyVocabulary, boolean clear) {
125: for (int i = 0; i < tables.length; i++) {
126: tables[i].setReadOnlyMap(readOnlyVocabulary.tables[i],
127: clear);
128: }
129: }
130:
131: public void setInitialVocabulary(
132: SerializerVocabulary initialVocabulary, boolean clear) {
133: setExternalVocabularyURI(null);
134: setInitialReadOnlyVocabulary(true);
135: setReadOnlyVocabulary(initialVocabulary, clear);
136: }
137:
138: public void setExternalVocabulary(String externalVocabularyURI,
139: SerializerVocabulary externalVocabulary, boolean clear) {
140: setInitialReadOnlyVocabulary(false);
141: setExternalVocabularyURI(externalVocabularyURI);
142: setReadOnlyVocabulary(externalVocabulary, clear);
143: }
144:
145: public void clear() {
146: for (int i = 0; i < tables.length; i++) {
147: tables[i].clear();
148: }
149: }
150:
151: private void convertVocabulary(
152: com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary v) {
153: addToTable(v.restrictedAlphabets.iterator(), restrictedAlphabet);
154: addToTable(v.encodingAlgorithms.iterator(), encodingAlgorithm);
155: addToTable(v.prefixes.iterator(), prefix);
156: addToTable(v.namespaceNames.iterator(), namespaceName);
157: addToTable(v.localNames.iterator(), localName);
158: addToTable(v.otherNCNames.iterator(), otherNCName);
159: addToTable(v.otherURIs.iterator(), otherURI);
160: addToTable(v.attributeValues.iterator(), attributeValue);
161: addToTable(v.otherStrings.iterator(), otherString);
162: addToTable(v.characterContentChunks.iterator(),
163: characterContentChunk);
164: addToTable(v.elements.iterator(), elementName);
165: addToTable(v.attributes.iterator(), attributeName);
166: }
167:
168: private void addToTable(Iterator i, StringIntMap m) {
169: while (i.hasNext()) {
170: addToTable((String) i.next(), m);
171: }
172: }
173:
174: private void addToTable(String s, StringIntMap m) {
175: if (s.length() == 0) {
176: return;
177: }
178:
179: m.obtainIndex(s);
180: }
181:
182: private void addToTable(Iterator i, CharArrayIntMap m) {
183: while (i.hasNext()) {
184: addToTable((String) i.next(), m);
185: }
186: }
187:
188: private void addToTable(String s, CharArrayIntMap m) {
189: if (s.length() == 0) {
190: return;
191: }
192:
193: char[] c = s.toCharArray();
194: m.obtainIndex(c, 0, c.length, false);
195: }
196:
197: private void addToTable(Iterator i, LocalNameQualifiedNamesMap m) {
198: while (i.hasNext()) {
199: addToNameTable((QName) i.next(), m);
200: }
201: }
202:
203: private void addToNameTable(QName n, LocalNameQualifiedNamesMap m) {
204: int namespaceURIIndex = -1;
205: int prefixIndex = -1;
206: if (n.getNamespaceURI().length() > 0) {
207: namespaceURIIndex = namespaceName.obtainIndex(n
208: .getNamespaceURI());
209: if (namespaceURIIndex == KeyIntMap.NOT_PRESENT) {
210: namespaceURIIndex = namespaceName.get(n
211: .getNamespaceURI());
212: }
213:
214: if (n.getPrefix().length() > 0) {
215: prefixIndex = prefix.obtainIndex(n.getPrefix());
216: if (prefixIndex == KeyIntMap.NOT_PRESENT) {
217: prefixIndex = prefix.get(n.getPrefix());
218: }
219: }
220: }
221:
222: int localNameIndex = localName.obtainIndex(n.getLocalPart());
223: if (localNameIndex == KeyIntMap.NOT_PRESENT) {
224: localNameIndex = localName.get(n.getLocalPart());
225: }
226:
227: QualifiedName name = new QualifiedName(n.getPrefix(), n
228: .getNamespaceURI(), n.getLocalPart(), m.getNextIndex(),
229: prefixIndex, namespaceURIIndex, localNameIndex);
230:
231: LocalNameQualifiedNamesMap.Entry entry = null;
232: if (_useLocalNameAsKey) {
233: entry = m.obtainEntry(n.getLocalPart());
234: } else {
235: String qName = (prefixIndex == -1) ? n.getLocalPart() : n
236: .getPrefix()
237: + ":" + n.getLocalPart();
238: entry = m.obtainEntry(qName);
239: }
240:
241: entry.addQualifiedName(name);
242: }
243: }
|