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.tools;
040:
041: import com.sun.xml.fastinfoset.QualifiedName;
042: import com.sun.xml.fastinfoset.util.CharArray;
043: import com.sun.xml.fastinfoset.util.DuplicateAttributeVerifier;
044: import com.sun.xml.fastinfoset.util.KeyIntMap;
045: import com.sun.xml.fastinfoset.util.LocalNameQualifiedNamesMap;
046: import com.sun.xml.fastinfoset.util.PrefixArray;
047: import com.sun.xml.fastinfoset.util.QualifiedNameArray;
048: import com.sun.xml.fastinfoset.util.StringArray;
049: import com.sun.xml.fastinfoset.util.StringIntMap;
050: import com.sun.xml.fastinfoset.vocab.ParserVocabulary;
051: import com.sun.xml.fastinfoset.vocab.SerializerVocabulary;
052:
053: import org.xml.sax.Attributes;
054: import org.xml.sax.SAXException;
055: import org.xml.sax.ext.LexicalHandler;
056: import org.xml.sax.helpers.DefaultHandler;
057: import com.sun.xml.fastinfoset.CommonResourceBundle;
058: import java.util.Set;
059: import org.jvnet.fastinfoset.FastInfosetSerializer;
060:
061: public class VocabularyGenerator extends DefaultHandler implements
062: LexicalHandler {
063:
064: protected SerializerVocabulary _serializerVocabulary;
065: protected ParserVocabulary _parserVocabulary;
066: protected org.jvnet.fastinfoset.Vocabulary _v;
067:
068: protected int attributeValueSizeConstraint = FastInfosetSerializer.ATTRIBUTE_VALUE_SIZE_CONSTRAINT;
069:
070: protected int characterContentChunkSizeContraint = FastInfosetSerializer.CHARACTER_CONTENT_CHUNK_SIZE_CONSTRAINT;
071:
072: /** Creates a new instance of VocabularyGenerator */
073: public VocabularyGenerator() {
074: _serializerVocabulary = new SerializerVocabulary();
075: _parserVocabulary = new ParserVocabulary();
076:
077: _v = new org.jvnet.fastinfoset.Vocabulary();
078: }
079:
080: public VocabularyGenerator(SerializerVocabulary serializerVocabulary) {
081: _serializerVocabulary = serializerVocabulary;
082: _parserVocabulary = new ParserVocabulary();
083:
084: _v = new org.jvnet.fastinfoset.Vocabulary();
085: }
086:
087: public VocabularyGenerator(ParserVocabulary parserVocabulary) {
088: _serializerVocabulary = new SerializerVocabulary();
089: _parserVocabulary = parserVocabulary;
090:
091: _v = new org.jvnet.fastinfoset.Vocabulary();
092: }
093:
094: /** Creates a new instance of VocabularyGenerator */
095: public VocabularyGenerator(
096: SerializerVocabulary serializerVocabulary,
097: ParserVocabulary parserVocabulary) {
098: _serializerVocabulary = serializerVocabulary;
099: _parserVocabulary = parserVocabulary;
100:
101: _v = new org.jvnet.fastinfoset.Vocabulary();
102: }
103:
104: public org.jvnet.fastinfoset.Vocabulary getVocabulary() {
105: return _v;
106: }
107:
108: public void setCharacterContentChunkSizeLimit(int size) {
109: if (size < 0) {
110: size = 0;
111: }
112:
113: characterContentChunkSizeContraint = size;
114: }
115:
116: public int getCharacterContentChunkSizeLimit() {
117: return characterContentChunkSizeContraint;
118: }
119:
120: public void setAttributeValueSizeLimit(int size) {
121: if (size < 0) {
122: size = 0;
123: }
124:
125: attributeValueSizeConstraint = size;
126: }
127:
128: public int getAttributeValueSizeLimit() {
129: return attributeValueSizeConstraint;
130: }
131:
132: // ContentHandler
133:
134: public void startDocument() throws SAXException {
135: }
136:
137: public void endDocument() throws SAXException {
138: }
139:
140: public void startPrefixMapping(String prefix, String uri)
141: throws SAXException {
142: addToTable(prefix, _v.prefixes, _serializerVocabulary.prefix,
143: _parserVocabulary.prefix);
144: addToTable(uri, _v.namespaceNames,
145: _serializerVocabulary.namespaceName,
146: _parserVocabulary.namespaceName);
147: }
148:
149: public void endPrefixMapping(String prefix) throws SAXException {
150: }
151:
152: public void startElement(String namespaceURI, String localName,
153: String qName, Attributes atts) throws SAXException {
154: addToNameTable(namespaceURI, qName, localName, _v.elements,
155: _serializerVocabulary.elementName,
156: _parserVocabulary.elementName, false);
157:
158: for (int a = 0; a < atts.getLength(); a++) {
159: addToNameTable(atts.getURI(a), atts.getQName(a), atts
160: .getLocalName(a), _v.attributes,
161: _serializerVocabulary.attributeName,
162: _parserVocabulary.attributeName, true);
163:
164: String value = atts.getValue(a);
165: if (value.length() < attributeValueSizeConstraint) {
166: addToTable(value, _v.attributeValues,
167: _serializerVocabulary.attributeValue,
168: _parserVocabulary.attributeValue);
169: }
170: }
171: }
172:
173: public void endElement(String namespaceURI, String localName,
174: String qName) throws SAXException {
175: }
176:
177: public void characters(char[] ch, int start, int length)
178: throws SAXException {
179: if (length < characterContentChunkSizeContraint) {
180: addToCharArrayTable(new CharArray(ch, start, length, true));
181: }
182: }
183:
184: public void ignorableWhitespace(char[] ch, int start, int length)
185: throws SAXException {
186: }
187:
188: public void processingInstruction(String target, String data)
189: throws SAXException {
190: }
191:
192: public void setDocumentLocator(org.xml.sax.Locator locator) {
193: }
194:
195: public void skippedEntity(String name) throws SAXException {
196: }
197:
198: // LexicalHandler
199:
200: public void comment(char[] ch, int start, int length)
201: throws SAXException {
202: }
203:
204: public void startCDATA() throws SAXException {
205: }
206:
207: public void endCDATA() throws SAXException {
208: }
209:
210: public void startDTD(String name, String publicId, String systemId)
211: throws SAXException {
212: }
213:
214: public void endDTD() throws SAXException {
215: }
216:
217: public void startEntity(String name) throws SAXException {
218: }
219:
220: public void endEntity(String name) throws SAXException {
221: }
222:
223: public void addToTable(String s, Set v, StringIntMap m,
224: StringArray a) {
225: if (s.length() == 0) {
226: return;
227: }
228:
229: if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) {
230: a.add(s);
231: }
232:
233: v.add(s);
234: }
235:
236: public void addToTable(String s, Set v, StringIntMap m,
237: PrefixArray a) {
238: if (s.length() == 0) {
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(
251: c.ch, c.start, c.length, false) == KeyIntMap.NOT_PRESENT) {
252: _parserVocabulary.characterContentChunk.add(c.ch, c.length);
253: }
254:
255: _v.characterContentChunks.add(c.toString());
256: }
257:
258: public void addToNameTable(String namespaceURI, String qName,
259: String localName, Set v, LocalNameQualifiedNamesMap m,
260: QualifiedNameArray a, boolean isAttribute)
261: throws SAXException {
262: LocalNameQualifiedNamesMap.Entry entry = m.obtainEntry(qName);
263: if (entry._valueIndex > 0) {
264: QualifiedName[] names = entry._value;
265: for (int i = 0; i < entry._valueIndex; i++) {
266: if ((namespaceURI == names[i].namespaceName || namespaceURI
267: .equals(names[i].namespaceName))) {
268: return;
269: }
270: }
271: }
272:
273: String prefix = getPrefixFromQualifiedName(qName);
274:
275: int namespaceURIIndex = -1;
276: int prefixIndex = -1;
277: int localNameIndex = -1;
278: if (namespaceURI.length() > 0) {
279: namespaceURIIndex = _serializerVocabulary.namespaceName
280: .get(namespaceURI);
281: if (namespaceURIIndex == KeyIntMap.NOT_PRESENT) {
282: throw new SAXException(CommonResourceBundle
283: .getInstance().getString(
284: "message.namespaceURINotIndexed",
285: new Object[] { Integer
286: .valueOf(namespaceURIIndex) }));
287: }
288:
289: if (prefix.length() > 0) {
290: prefixIndex = _serializerVocabulary.prefix.get(prefix);
291: if (prefixIndex == KeyIntMap.NOT_PRESENT) {
292: throw new SAXException(CommonResourceBundle
293: .getInstance().getString(
294: "message.prefixNotIndexed",
295: new Object[] { Integer
296: .valueOf(prefixIndex) }));
297: }
298: }
299: }
300:
301: localNameIndex = _serializerVocabulary.localName
302: .obtainIndex(localName);
303: if (localNameIndex == KeyIntMap.NOT_PRESENT) {
304: _parserVocabulary.localName.add(localName);
305: localNameIndex = _parserVocabulary.localName.getSize() - 1;
306: }
307: QualifiedName name = new QualifiedName(prefix, namespaceURI,
308: localName, m.getNextIndex(), prefixIndex,
309: namespaceURIIndex, localNameIndex);
310: if (isAttribute) {
311: name
312: .createAttributeValues(DuplicateAttributeVerifier.MAP_SIZE);
313: }
314: entry.addQualifiedName(name);
315: a.add(name);
316:
317: v.add(name.getQName());
318: }
319:
320: public static String getPrefixFromQualifiedName(String qName) {
321: int i = qName.indexOf(':');
322: String prefix = "";
323: if (i != -1) {
324: prefix = qName.substring(0, i);
325: }
326: return prefix;
327: }
328:
329: }
|