KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > sax > VocabularyGenerator


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.sax;
41
42 import com.sun.xml.fastinfoset.QualifiedName;
43 import com.sun.xml.fastinfoset.util.CharArray;
44 import com.sun.xml.fastinfoset.util.DuplicateAttributeVerifier;
45 import com.sun.xml.fastinfoset.util.KeyIntMap;
46 import com.sun.xml.fastinfoset.util.LocalNameQualifiedNamesMap;
47 import com.sun.xml.fastinfoset.util.PrefixArray;
48 import com.sun.xml.fastinfoset.util.QualifiedNameArray;
49 import com.sun.xml.fastinfoset.util.StringArray;
50 import com.sun.xml.fastinfoset.util.StringIntMap;
51 import com.sun.xml.fastinfoset.vocab.ParserVocabulary;
52 import com.sun.xml.fastinfoset.vocab.SerializerVocabulary;
53
54 import org.xml.sax.Attributes JavaDoc;
55 import org.xml.sax.SAXException JavaDoc;
56 import org.xml.sax.ext.LexicalHandler JavaDoc;
57 import org.xml.sax.helpers.DefaultHandler JavaDoc;
58 import com.sun.xml.fastinfoset.CommonResourceBundle;
59 import org.jvnet.fastinfoset.FastInfosetSerializer;
60
61 public class VocabularyGenerator extends DefaultHandler JavaDoc implements LexicalHandler JavaDoc {
62     
63     protected SerializerVocabulary _serializerVocabulary;
64     protected ParserVocabulary _parserVocabulary;
65     
66     protected int attributeValueSizeConstraint = FastInfosetSerializer.ATTRIBUTE_VALUE_SIZE_CONSTRAINT;
67     
68     protected int characterContentChunkSizeContraint = FastInfosetSerializer.CHARACTER_CONTENT_CHUNK_SIZE_CONSTRAINT;
69     
70     /** Creates a new instance of VocabularyGenerator */
71     public VocabularyGenerator(SerializerVocabulary serializerVocabulary) {
72         _serializerVocabulary = serializerVocabulary;
73         _parserVocabulary = new ParserVocabulary();
74     }
75
76     public VocabularyGenerator(ParserVocabulary parserVocabulary) {
77         _serializerVocabulary = new SerializerVocabulary();
78         _parserVocabulary = parserVocabulary;
79     }
80     
81     /** Creates a new instance of VocabularyGenerator */
82     public VocabularyGenerator(SerializerVocabulary serializerVocabulary, ParserVocabulary parserVocabulary) {
83         _serializerVocabulary = serializerVocabulary;
84         _parserVocabulary = parserVocabulary;
85     }
86     
87     public void setCharacterContentChunkSizeLimit(int size) {
88         if (size < 0 ) {
89             size = 0;
90         }
91                 
92         characterContentChunkSizeContraint = size;
93     }
94     
95     public int getCharacterContentChunkSizeLimit() {
96         return characterContentChunkSizeContraint;
97     }
98
99     public void setAttributeValueSizeLimit(int size) {
100         if (size < 0 ) {
101             size = 0;
102         }
103         
104         attributeValueSizeConstraint = size;
105     }
106     
107     public int getAttributeValueSizeLimit() {
108         return attributeValueSizeConstraint;
109     }
110     
111     // ContentHandler
112

113     public void startDocument() throws SAXException JavaDoc {
114     }
115
116     public void endDocument() throws SAXException JavaDoc {
117     }
118     
119     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
120         addToTable(prefix, _serializerVocabulary.prefix, _parserVocabulary.prefix);
121         addToTable(uri, _serializerVocabulary.namespaceName, _parserVocabulary.namespaceName);
122     }
123
124     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
125     }
126
127     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
128         addToNameTable(namespaceURI, qName, localName, _serializerVocabulary.elementName, _parserVocabulary.elementName, false);
129         
130         for (int a = 0; a < atts.getLength(); a++) {
131             addToNameTable(atts.getURI(a), atts.getQName(a), atts.getLocalName(a), _serializerVocabulary.attributeName, _parserVocabulary.attributeName, true);
132         
133             String JavaDoc value = atts.getValue(a);
134             if (value.length() < attributeValueSizeConstraint) {
135                 addToTable(value, _serializerVocabulary.attributeValue, _parserVocabulary.attributeValue);
136             }
137         }
138     }
139
140     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
141     }
142         
143     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
144         if (length < characterContentChunkSizeContraint) {
145             addToCharArrayTable(new CharArray(ch, start, length, true));
146         }
147     }
148
149     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException JavaDoc {
150     }
151     
152     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
153     }
154     
155     public void setDocumentLocator(org.xml.sax.Locator JavaDoc locator) {
156     }
157     
158     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
159     }
160     
161        
162
163     // LexicalHandler
164

165     public void comment(char[] ch, int start, int length) throws SAXException JavaDoc {
166     }
167   
168     public void startCDATA() throws SAXException JavaDoc {
169     }
170   
171     public void endCDATA() throws SAXException JavaDoc {
172     }
173     
174     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
175     }
176
177     public void endDTD() throws SAXException JavaDoc {
178     }
179     
180     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
181     }
182
183     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
184     }
185
186     
187     public void addToTable(String JavaDoc s, StringIntMap m, StringArray a) {
188         if (s == "") {
189             return;
190         }
191         
192         if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) {
193             a.add(s);
194         }
195     }
196
197     public void addToTable(String JavaDoc s, StringIntMap m, PrefixArray a) {
198         if (s == "") {
199             return;
200         }
201         
202         if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) {
203             a.add(s);
204         }
205     }
206     
207     public void addToCharArrayTable(CharArray c) {
208         if (_serializerVocabulary.characterContentChunk.obtainIndex(c.ch, c.start, c.length, false) == KeyIntMap.NOT_PRESENT) {
209             _parserVocabulary.characterContentChunk.add(c.ch, c.length);
210         }
211     }
212
213     public void addToNameTable(String JavaDoc namespaceURI, String JavaDoc qName, String JavaDoc localName,
214             LocalNameQualifiedNamesMap m, QualifiedNameArray a,
215             boolean isAttribute) throws SAXException JavaDoc {
216         LocalNameQualifiedNamesMap.Entry entry = m.obtainEntry(qName);
217         if (entry._valueIndex > 0) {
218             QualifiedName[] names = entry._value;
219             for (int i = 0; i < entry._valueIndex; i++) {
220                 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
221                     return;
222                 }
223             }
224         }
225         
226         String JavaDoc prefix = getPrefixFromQualifiedName(qName);
227         
228         int namespaceURIIndex = -1;
229         int prefixIndex = -1;
230         int localNameIndex = -1;
231         if (namespaceURI != "") {
232             namespaceURIIndex = _serializerVocabulary.namespaceName.get(namespaceURI);
233             if (namespaceURIIndex == KeyIntMap.NOT_PRESENT) {
234                 throw new SAXException JavaDoc(CommonResourceBundle.getInstance().
235                         getString("message.namespaceURINotIndexed", new Object JavaDoc[]{new Integer JavaDoc(namespaceURIIndex)}));
236             }
237             
238             if (prefix != "") {
239                 prefixIndex = _serializerVocabulary.prefix.get(prefix);
240                 if (prefixIndex == KeyIntMap.NOT_PRESENT) {
241                     throw new SAXException JavaDoc(CommonResourceBundle.getInstance().
242                             getString("message.prefixNotIndexed", new Object JavaDoc[]{new Integer JavaDoc(prefixIndex)}));
243                 }
244             }
245         }
246         
247         localNameIndex = _serializerVocabulary.localName.obtainIndex(localName);
248         if (localNameIndex == KeyIntMap.NOT_PRESENT) {
249             _parserVocabulary.localName.add(localName);
250             localNameIndex = _parserVocabulary.localName.getSize() - 1;
251         }
252         QualifiedName name = new QualifiedName(prefix, namespaceURI, localName, m.getNextIndex(),
253                 prefixIndex, namespaceURIIndex, localNameIndex);
254         if (isAttribute) {
255             name.createAttributeValues(DuplicateAttributeVerifier.MAP_SIZE);
256         }
257         entry.addQualifiedName(name);
258         a.add(name);
259     }
260         
261     public static String JavaDoc getPrefixFromQualifiedName(String JavaDoc qName) {
262         int i = qName.indexOf(':');
263         String JavaDoc prefix = "";
264         if (i != -1) {
265             prefix = qName.substring(0, i);
266         }
267         return prefix;
268     }
269
270 }
271
Popular Tags