1 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 ; 55 import org.xml.sax.SAXException ; 56 import org.xml.sax.ext.LexicalHandler ; 57 import org.xml.sax.helpers.DefaultHandler ; 58 import com.sun.xml.fastinfoset.CommonResourceBundle; 59 import org.jvnet.fastinfoset.FastInfosetSerializer; 60 61 public class VocabularyGenerator extends DefaultHandler implements LexicalHandler { 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 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 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 113 public void startDocument() throws SAXException { 114 } 115 116 public void endDocument() throws SAXException { 117 } 118 119 public void startPrefixMapping(String prefix, String uri) throws SAXException { 120 addToTable(prefix, _serializerVocabulary.prefix, _parserVocabulary.prefix); 121 addToTable(uri, _serializerVocabulary.namespaceName, _parserVocabulary.namespaceName); 122 } 123 124 public void endPrefixMapping(String prefix) throws SAXException { 125 } 126 127 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 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 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 namespaceURI, String localName, String qName) throws SAXException { 141 } 142 143 public void characters(char[] ch, int start, int length) throws SAXException { 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 { 150 } 151 152 public void processingInstruction(String target, String data) throws SAXException { 153 } 154 155 public void setDocumentLocator(org.xml.sax.Locator locator) { 156 } 157 158 public void skippedEntity(String name) throws SAXException { 159 } 160 161 162 163 165 public void comment(char[] ch, int start, int length) throws SAXException { 166 } 167 168 public void startCDATA() throws SAXException { 169 } 170 171 public void endCDATA() throws SAXException { 172 } 173 174 public void startDTD(String name, String publicId, String systemId) throws SAXException { 175 } 176 177 public void endDTD() throws SAXException { 178 } 179 180 public void startEntity(String name) throws SAXException { 181 } 182 183 public void endEntity(String name) throws SAXException { 184 } 185 186 187 public void addToTable(String 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 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 namespaceURI, String qName, String localName, 214 LocalNameQualifiedNamesMap m, QualifiedNameArray a, 215 boolean isAttribute) throws SAXException { 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 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 (CommonResourceBundle.getInstance(). 235 getString("message.namespaceURINotIndexed", new Object []{new Integer (namespaceURIIndex)})); 236 } 237 238 if (prefix != "") { 239 prefixIndex = _serializerVocabulary.prefix.get(prefix); 240 if (prefixIndex == KeyIntMap.NOT_PRESENT) { 241 throw new SAXException (CommonResourceBundle.getInstance(). 242 getString("message.prefixNotIndexed", new Object []{new Integer (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 getPrefixFromQualifiedName(String qName) { 262 int i = qName.indexOf(':'); 263 String prefix = ""; 264 if (i != -1) { 265 prefix = qName.substring(0, i); 266 } 267 return prefix; 268 } 269 270 } 271 | Popular Tags |