View Javadoc
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   */
27  
28  package com.sun.xml.internal.fastinfoset.vocab;
29  
30  import com.sun.xml.internal.fastinfoset.EncodingConstants;
31  import com.sun.xml.internal.fastinfoset.QualifiedName;
32  import com.sun.xml.internal.fastinfoset.util.CharArrayIntMap;
33  import com.sun.xml.internal.fastinfoset.util.FixedEntryStringIntMap;
34  import com.sun.xml.internal.fastinfoset.util.KeyIntMap;
35  import com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap;
36  import com.sun.xml.internal.fastinfoset.util.StringIntMap;
37  import java.util.Iterator;
38  import javax.xml.namespace.QName;
39  
40  public class SerializerVocabulary extends Vocabulary {
41      public final StringIntMap restrictedAlphabet;
42      public final StringIntMap encodingAlgorithm;
43  
44      public final StringIntMap namespaceName;
45      public final StringIntMap prefix;
46      public final StringIntMap localName;
47      public final StringIntMap otherNCName;
48      public final StringIntMap otherURI;
49      public final StringIntMap attributeValue;
50      public final CharArrayIntMap otherString;
51  
52      public final CharArrayIntMap characterContentChunk;
53  
54      public final LocalNameQualifiedNamesMap elementName;
55      public final LocalNameQualifiedNamesMap attributeName;
56  
57      public final KeyIntMap[] tables = new KeyIntMap[12];
58  
59      protected boolean _useLocalNameAsKey;
60  
61      protected SerializerVocabulary _readOnlyVocabulary;
62  
63      public SerializerVocabulary() {
64          tables[RESTRICTED_ALPHABET] = restrictedAlphabet = new StringIntMap(4);
65          tables[ENCODING_ALGORITHM] = encodingAlgorithm = new StringIntMap(4);
66          tables[PREFIX] = prefix = new FixedEntryStringIntMap(EncodingConstants.XML_NAMESPACE_PREFIX, 8);
67          tables[NAMESPACE_NAME] = namespaceName = new FixedEntryStringIntMap(EncodingConstants.XML_NAMESPACE_NAME, 8);
68          tables[LOCAL_NAME] = localName = new StringIntMap();
69          tables[OTHER_NCNAME] = otherNCName = new StringIntMap(4);
70          tables[OTHER_URI] = otherURI = new StringIntMap(4);
71          tables[ATTRIBUTE_VALUE] = attributeValue = new StringIntMap();
72          tables[OTHER_STRING] = otherString = new CharArrayIntMap(4);
73          tables[CHARACTER_CONTENT_CHUNK] = characterContentChunk = new CharArrayIntMap();
74          tables[ELEMENT_NAME] = elementName = new LocalNameQualifiedNamesMap();
75          tables[ATTRIBUTE_NAME] = attributeName = new LocalNameQualifiedNamesMap();
76      }
77  
78      public SerializerVocabulary(com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary v,
79              boolean useLocalNameAsKey) {
80          this();
81  
82          _useLocalNameAsKey = useLocalNameAsKey;
83          convertVocabulary(v);
84      }
85  
86      public SerializerVocabulary getReadOnlyVocabulary() {
87          return _readOnlyVocabulary;
88      }
89  
90      protected void setReadOnlyVocabulary(SerializerVocabulary readOnlyVocabulary,
91              boolean clear) {
92          for (int i = 0; i < tables.length; i++) {
93              tables[i].setReadOnlyMap(readOnlyVocabulary.tables[i], clear);
94          }
95      }
96  
97      public void setInitialVocabulary(SerializerVocabulary initialVocabulary,
98              boolean clear) {
99          setExternalVocabularyURI(null);
100         setInitialReadOnlyVocabulary(true);
101         setReadOnlyVocabulary(initialVocabulary, clear);
102     }
103 
104     public void setExternalVocabulary(String externalVocabularyURI,
105             SerializerVocabulary externalVocabulary, boolean clear) {
106         setInitialReadOnlyVocabulary(false);
107         setExternalVocabularyURI(externalVocabularyURI);
108         setReadOnlyVocabulary(externalVocabulary, clear);
109     }
110 
111     public void clear() {
112         for (int i = 0; i < tables.length; i++) {
113             tables[i].clear();
114         }
115     }
116 
117     private void convertVocabulary(com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary v) {
118         addToTable(v.restrictedAlphabets.iterator(), restrictedAlphabet);
119         addToTable(v.encodingAlgorithms.iterator(), encodingAlgorithm);
120         addToTable(v.prefixes.iterator(), prefix);
121         addToTable(v.namespaceNames.iterator(), namespaceName);
122         addToTable(v.localNames.iterator(), localName);
123         addToTable(v.otherNCNames.iterator(), otherNCName);
124         addToTable(v.otherURIs.iterator(), otherURI);
125         addToTable(v.attributeValues.iterator(), attributeValue);
126         addToTable(v.otherStrings.iterator(), otherString);
127         addToTable(v.characterContentChunks.iterator(), characterContentChunk);
128         addToTable(v.elements.iterator(), elementName);
129         addToTable(v.attributes.iterator(), attributeName);
130     }
131 
132     private void addToTable(Iterator i, StringIntMap m) {
133         while (i.hasNext()) {
134             addToTable((String)i.next(), m);
135         }
136     }
137 
138     private void addToTable(String s, StringIntMap m) {
139         if (s.length() == 0) {
140             return;
141         }
142 
143         m.obtainIndex(s);
144     }
145 
146     private void addToTable(Iterator i, CharArrayIntMap m) {
147         while (i.hasNext()) {
148             addToTable((String)i.next(), m);
149         }
150     }
151 
152     private void addToTable(String s, CharArrayIntMap m) {
153         if (s.length() == 0) {
154             return;
155         }
156 
157         char[] c = s.toCharArray();
158         m.obtainIndex(c, 0, c.length, false);
159     }
160 
161     private void addToTable(Iterator i, LocalNameQualifiedNamesMap m) {
162         while (i.hasNext()) {
163             addToNameTable((QName)i.next(), m);
164         }
165     }
166 
167     private void addToNameTable(QName n, LocalNameQualifiedNamesMap m) {
168         int namespaceURIIndex = -1;
169         int prefixIndex = -1;
170         if (n.getNamespaceURI().length() > 0) {
171             namespaceURIIndex = namespaceName.obtainIndex(n.getNamespaceURI());
172             if (namespaceURIIndex == KeyIntMap.NOT_PRESENT) {
173                 namespaceURIIndex = namespaceName.get(n.getNamespaceURI());
174             }
175 
176             if (n.getPrefix().length() > 0) {
177                 prefixIndex = prefix.obtainIndex(n.getPrefix());
178                 if (prefixIndex == KeyIntMap.NOT_PRESENT) {
179                     prefixIndex = prefix.get(n.getPrefix());
180                 }
181             }
182         }
183 
184         int localNameIndex = localName.obtainIndex(n.getLocalPart());
185         if (localNameIndex == KeyIntMap.NOT_PRESENT) {
186             localNameIndex = localName.get(n.getLocalPart());
187         }
188 
189         QualifiedName name = new QualifiedName(n.getPrefix(), n.getNamespaceURI(), n.getLocalPart(),
190                 m.getNextIndex(),
191                 prefixIndex, namespaceURIIndex, localNameIndex);
192 
193         LocalNameQualifiedNamesMap.Entry entry = null;
194         if (_useLocalNameAsKey) {
195             entry = m.obtainEntry(n.getLocalPart());
196         } else {
197             String qName = (prefixIndex == -1)
198                 ? n.getLocalPart()
199                 : n.getPrefix() + ":" + n.getLocalPart();
200             entry = m.obtainEntry(qName);
201         }
202 
203         entry.addQualifiedName(name);
204     }
205 }