325N/A/*
325N/A * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A *
325N/A * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.fastinfoset.tools;
325N/A
325N/Aimport com.sun.xml.internal.fastinfoset.QualifiedName;
325N/Aimport com.sun.xml.internal.fastinfoset.util.CharArray;
325N/Aimport com.sun.xml.internal.fastinfoset.util.DuplicateAttributeVerifier;
325N/Aimport com.sun.xml.internal.fastinfoset.util.KeyIntMap;
325N/Aimport com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap;
325N/Aimport com.sun.xml.internal.fastinfoset.util.PrefixArray;
325N/Aimport com.sun.xml.internal.fastinfoset.util.QualifiedNameArray;
325N/Aimport com.sun.xml.internal.fastinfoset.util.StringArray;
325N/Aimport com.sun.xml.internal.fastinfoset.util.StringIntMap;
325N/Aimport com.sun.xml.internal.fastinfoset.vocab.ParserVocabulary;
325N/Aimport com.sun.xml.internal.fastinfoset.vocab.SerializerVocabulary;
325N/A
325N/Aimport org.xml.sax.Attributes;
325N/Aimport org.xml.sax.SAXException;
325N/Aimport org.xml.sax.ext.LexicalHandler;
325N/Aimport org.xml.sax.helpers.DefaultHandler;
325N/Aimport com.sun.xml.internal.fastinfoset.CommonResourceBundle;
325N/Aimport java.util.Set;
325N/Aimport com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSerializer;
325N/A
325N/Apublic class VocabularyGenerator extends DefaultHandler implements LexicalHandler {
325N/A
325N/A protected SerializerVocabulary _serializerVocabulary;
325N/A protected ParserVocabulary _parserVocabulary;
325N/A protected com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary _v;
325N/A
325N/A protected int attributeValueSizeConstraint = FastInfosetSerializer.MAX_ATTRIBUTE_VALUE_SIZE;
325N/A
325N/A protected int characterContentChunkSizeContraint = FastInfosetSerializer.MAX_CHARACTER_CONTENT_CHUNK_SIZE;
325N/A
325N/A /** Creates a new instance of VocabularyGenerator */
325N/A public VocabularyGenerator() {
325N/A _serializerVocabulary = new SerializerVocabulary();
325N/A _parserVocabulary = new ParserVocabulary();
325N/A
325N/A _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
325N/A }
325N/A
325N/A public VocabularyGenerator(SerializerVocabulary serializerVocabulary) {
325N/A _serializerVocabulary = serializerVocabulary;
325N/A _parserVocabulary = new ParserVocabulary();
325N/A
325N/A _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
325N/A }
325N/A
325N/A public VocabularyGenerator(ParserVocabulary parserVocabulary) {
325N/A _serializerVocabulary = new SerializerVocabulary();
325N/A _parserVocabulary = parserVocabulary;
325N/A
325N/A _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
325N/A }
325N/A
325N/A /** Creates a new instance of VocabularyGenerator */
325N/A public VocabularyGenerator(SerializerVocabulary serializerVocabulary, ParserVocabulary parserVocabulary) {
325N/A _serializerVocabulary = serializerVocabulary;
325N/A _parserVocabulary = parserVocabulary;
325N/A
325N/A _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
325N/A }
325N/A
325N/A public com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary getVocabulary() {
325N/A return _v;
325N/A }
325N/A
325N/A public void setCharacterContentChunkSizeLimit(int size) {
325N/A if (size < 0 ) {
325N/A size = 0;
325N/A }
325N/A
325N/A characterContentChunkSizeContraint = size;
325N/A }
325N/A
325N/A public int getCharacterContentChunkSizeLimit() {
325N/A return characterContentChunkSizeContraint;
325N/A }
325N/A
325N/A public void setAttributeValueSizeLimit(int size) {
325N/A if (size < 0 ) {
325N/A size = 0;
325N/A }
325N/A
325N/A attributeValueSizeConstraint = size;
325N/A }
325N/A
325N/A public int getAttributeValueSizeLimit() {
325N/A return attributeValueSizeConstraint;
325N/A }
325N/A
325N/A // ContentHandler
325N/A
325N/A public void startDocument() throws SAXException {
325N/A }
325N/A
325N/A public void endDocument() throws SAXException {
325N/A }
325N/A
325N/A public void startPrefixMapping(String prefix, String uri) throws SAXException {
325N/A addToTable(prefix, _v.prefixes, _serializerVocabulary.prefix, _parserVocabulary.prefix);
325N/A addToTable(uri, _v.namespaceNames, _serializerVocabulary.namespaceName, _parserVocabulary.namespaceName);
325N/A }
325N/A
325N/A public void endPrefixMapping(String prefix) throws SAXException {
325N/A }
325N/A
325N/A public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
325N/A addToNameTable(namespaceURI, qName, localName,
325N/A _v.elements, _serializerVocabulary.elementName, _parserVocabulary.elementName, false);
325N/A
325N/A for (int a = 0; a < atts.getLength(); a++) {
325N/A addToNameTable(atts.getURI(a), atts.getQName(a), atts.getLocalName(a),
325N/A _v.attributes, _serializerVocabulary.attributeName, _parserVocabulary.attributeName, true);
325N/A
325N/A String value = atts.getValue(a);
325N/A if (value.length() < attributeValueSizeConstraint) {
325N/A addToTable(value, _v.attributeValues, _serializerVocabulary.attributeValue, _parserVocabulary.attributeValue);
325N/A }
325N/A }
325N/A }
325N/A
325N/A public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
325N/A }
325N/A
325N/A public void characters(char[] ch, int start, int length) throws SAXException {
325N/A if (length < characterContentChunkSizeContraint) {
325N/A addToCharArrayTable(new CharArray(ch, start, length, true));
325N/A }
325N/A }
325N/A
325N/A public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
325N/A }
325N/A
325N/A public void processingInstruction(String target, String data) throws SAXException {
325N/A }
325N/A
325N/A public void setDocumentLocator(org.xml.sax.Locator locator) {
325N/A }
325N/A
325N/A public void skippedEntity(String name) throws SAXException {
325N/A }
325N/A
325N/A
325N/A
325N/A // LexicalHandler
325N/A
325N/A public void comment(char[] ch, int start, int length) throws SAXException {
325N/A }
325N/A
325N/A public void startCDATA() throws SAXException {
325N/A }
325N/A
325N/A public void endCDATA() throws SAXException {
325N/A }
325N/A
325N/A public void startDTD(String name, String publicId, String systemId) throws SAXException {
325N/A }
325N/A
325N/A public void endDTD() throws SAXException {
325N/A }
325N/A
325N/A public void startEntity(String name) throws SAXException {
325N/A }
325N/A
325N/A public void endEntity(String name) throws SAXException {
325N/A }
325N/A
325N/A
325N/A public void addToTable(String s, Set v, StringIntMap m, StringArray a) {
325N/A if (s.length() == 0) {
325N/A return;
325N/A }
325N/A
325N/A if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) {
325N/A a.add(s);
325N/A }
325N/A
325N/A v.add(s);
325N/A }
325N/A
325N/A public void addToTable(String s, Set v, StringIntMap m, PrefixArray a) {
325N/A if (s.length() == 0) {
325N/A return;
325N/A }
325N/A
325N/A if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) {
325N/A a.add(s);
325N/A }
325N/A
325N/A v.add(s);
325N/A }
325N/A
325N/A public void addToCharArrayTable(CharArray c) {
325N/A if (_serializerVocabulary.characterContentChunk.obtainIndex(c.ch, c.start, c.length, false) == KeyIntMap.NOT_PRESENT) {
325N/A _parserVocabulary.characterContentChunk.add(c.ch, c.length);
325N/A }
325N/A
325N/A _v.characterContentChunks.add(c.toString());
325N/A }
325N/A
325N/A public void addToNameTable(String namespaceURI, String qName, String localName,
325N/A Set v, LocalNameQualifiedNamesMap m, QualifiedNameArray a,
325N/A boolean isAttribute) throws SAXException {
325N/A LocalNameQualifiedNamesMap.Entry entry = m.obtainEntry(qName);
325N/A if (entry._valueIndex > 0) {
325N/A QualifiedName[] names = entry._value;
325N/A for (int i = 0; i < entry._valueIndex; i++) {
325N/A if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
325N/A return;
325N/A }
325N/A }
325N/A }
325N/A
325N/A String prefix = getPrefixFromQualifiedName(qName);
325N/A
325N/A int namespaceURIIndex = -1;
325N/A int prefixIndex = -1;
325N/A int localNameIndex = -1;
325N/A if (namespaceURI.length() > 0) {
325N/A namespaceURIIndex = _serializerVocabulary.namespaceName.get(namespaceURI);
325N/A if (namespaceURIIndex == KeyIntMap.NOT_PRESENT) {
325N/A throw new SAXException(CommonResourceBundle.getInstance().
325N/A getString("message.namespaceURINotIndexed", new Object[]{Integer.valueOf(namespaceURIIndex)}));
325N/A }
325N/A
325N/A if (prefix.length() > 0) {
325N/A prefixIndex = _serializerVocabulary.prefix.get(prefix);
325N/A if (prefixIndex == KeyIntMap.NOT_PRESENT) {
325N/A throw new SAXException(CommonResourceBundle.getInstance().
325N/A getString("message.prefixNotIndexed", new Object[]{Integer.valueOf(prefixIndex)}));
325N/A }
325N/A }
325N/A }
325N/A
325N/A localNameIndex = _serializerVocabulary.localName.obtainIndex(localName);
325N/A if (localNameIndex == KeyIntMap.NOT_PRESENT) {
325N/A _parserVocabulary.localName.add(localName);
325N/A localNameIndex = _parserVocabulary.localName.getSize() - 1;
325N/A }
325N/A QualifiedName name = new QualifiedName(prefix, namespaceURI, localName, m.getNextIndex(),
325N/A prefixIndex, namespaceURIIndex, localNameIndex);
325N/A if (isAttribute) {
325N/A name.createAttributeValues(DuplicateAttributeVerifier.MAP_SIZE);
325N/A }
325N/A entry.addQualifiedName(name);
325N/A a.add(name);
325N/A
325N/A v.add(name.getQName());
325N/A }
325N/A
325N/A public static String getPrefixFromQualifiedName(String qName) {
325N/A int i = qName.indexOf(':');
325N/A String prefix = "";
325N/A if (i != -1) {
325N/A prefix = qName.substring(0, i);
325N/A }
325N/A return prefix;
325N/A }
325N/A
325N/A}