src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/tools/VocabularyGenerator.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/tools/VocabularyGenerator.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,287 @@
     1.4 +/*
     1.5 + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + *
    1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    1.29 + */
    1.30 +
    1.31 +package com.sun.xml.internal.fastinfoset.tools;
    1.32 +
    1.33 +import com.sun.xml.internal.fastinfoset.QualifiedName;
    1.34 +import com.sun.xml.internal.fastinfoset.util.CharArray;
    1.35 +import com.sun.xml.internal.fastinfoset.util.DuplicateAttributeVerifier;
    1.36 +import com.sun.xml.internal.fastinfoset.util.KeyIntMap;
    1.37 +import com.sun.xml.internal.fastinfoset.util.LocalNameQualifiedNamesMap;
    1.38 +import com.sun.xml.internal.fastinfoset.util.PrefixArray;
    1.39 +import com.sun.xml.internal.fastinfoset.util.QualifiedNameArray;
    1.40 +import com.sun.xml.internal.fastinfoset.util.StringArray;
    1.41 +import com.sun.xml.internal.fastinfoset.util.StringIntMap;
    1.42 +import com.sun.xml.internal.fastinfoset.vocab.ParserVocabulary;
    1.43 +import com.sun.xml.internal.fastinfoset.vocab.SerializerVocabulary;
    1.44 +
    1.45 +import org.xml.sax.Attributes;
    1.46 +import org.xml.sax.SAXException;
    1.47 +import org.xml.sax.ext.LexicalHandler;
    1.48 +import org.xml.sax.helpers.DefaultHandler;
    1.49 +import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
    1.50 +import java.util.Set;
    1.51 +import com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSerializer;
    1.52 +
    1.53 +public class VocabularyGenerator extends DefaultHandler implements LexicalHandler {
    1.54 +
    1.55 +    protected SerializerVocabulary _serializerVocabulary;
    1.56 +    protected ParserVocabulary _parserVocabulary;
    1.57 +    protected com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary _v;
    1.58 +
    1.59 +    protected int attributeValueSizeConstraint = FastInfosetSerializer.MAX_ATTRIBUTE_VALUE_SIZE;
    1.60 +
    1.61 +    protected int characterContentChunkSizeContraint = FastInfosetSerializer.MAX_CHARACTER_CONTENT_CHUNK_SIZE;
    1.62 +
    1.63 +    /** Creates a new instance of VocabularyGenerator */
    1.64 +    public VocabularyGenerator() {
    1.65 +        _serializerVocabulary = new SerializerVocabulary();
    1.66 +        _parserVocabulary = new ParserVocabulary();
    1.67 +
    1.68 +        _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
    1.69 +    }
    1.70 +
    1.71 +    public VocabularyGenerator(SerializerVocabulary serializerVocabulary) {
    1.72 +        _serializerVocabulary = serializerVocabulary;
    1.73 +        _parserVocabulary = new ParserVocabulary();
    1.74 +
    1.75 +        _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
    1.76 +    }
    1.77 +
    1.78 +    public VocabularyGenerator(ParserVocabulary parserVocabulary) {
    1.79 +        _serializerVocabulary = new SerializerVocabulary();
    1.80 +        _parserVocabulary = parserVocabulary;
    1.81 +
    1.82 +        _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
    1.83 +    }
    1.84 +
    1.85 +    /** Creates a new instance of VocabularyGenerator */
    1.86 +    public VocabularyGenerator(SerializerVocabulary serializerVocabulary, ParserVocabulary parserVocabulary) {
    1.87 +        _serializerVocabulary = serializerVocabulary;
    1.88 +        _parserVocabulary = parserVocabulary;
    1.89 +
    1.90 +        _v = new com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary();
    1.91 +    }
    1.92 +
    1.93 +    public com.sun.xml.internal.org.jvnet.fastinfoset.Vocabulary getVocabulary() {
    1.94 +        return _v;
    1.95 +    }
    1.96 +
    1.97 +    public void setCharacterContentChunkSizeLimit(int size) {
    1.98 +        if (size < 0 ) {
    1.99 +            size = 0;
   1.100 +        }
   1.101 +
   1.102 +        characterContentChunkSizeContraint = size;
   1.103 +    }
   1.104 +
   1.105 +    public int getCharacterContentChunkSizeLimit() {
   1.106 +        return characterContentChunkSizeContraint;
   1.107 +    }
   1.108 +
   1.109 +    public void setAttributeValueSizeLimit(int size) {
   1.110 +        if (size < 0 ) {
   1.111 +            size = 0;
   1.112 +        }
   1.113 +
   1.114 +        attributeValueSizeConstraint = size;
   1.115 +    }
   1.116 +
   1.117 +    public int getAttributeValueSizeLimit() {
   1.118 +        return attributeValueSizeConstraint;
   1.119 +    }
   1.120 +
   1.121 +    // ContentHandler
   1.122 +
   1.123 +    public void startDocument() throws SAXException {
   1.124 +    }
   1.125 +
   1.126 +    public void endDocument() throws SAXException {
   1.127 +    }
   1.128 +
   1.129 +    public void startPrefixMapping(String prefix, String uri) throws SAXException {
   1.130 +        addToTable(prefix, _v.prefixes, _serializerVocabulary.prefix, _parserVocabulary.prefix);
   1.131 +        addToTable(uri, _v.namespaceNames, _serializerVocabulary.namespaceName, _parserVocabulary.namespaceName);
   1.132 +    }
   1.133 +
   1.134 +    public void endPrefixMapping(String prefix) throws SAXException {
   1.135 +    }
   1.136 +
   1.137 +    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
   1.138 +        addToNameTable(namespaceURI, qName, localName,
   1.139 +                _v.elements, _serializerVocabulary.elementName, _parserVocabulary.elementName, false);
   1.140 +
   1.141 +        for (int a = 0; a < atts.getLength(); a++) {
   1.142 +            addToNameTable(atts.getURI(a), atts.getQName(a), atts.getLocalName(a),
   1.143 +                    _v.attributes, _serializerVocabulary.attributeName, _parserVocabulary.attributeName, true);
   1.144 +
   1.145 +            String value = atts.getValue(a);
   1.146 +            if (value.length() < attributeValueSizeConstraint) {
   1.147 +                addToTable(value, _v.attributeValues, _serializerVocabulary.attributeValue, _parserVocabulary.attributeValue);
   1.148 +            }
   1.149 +        }
   1.150 +    }
   1.151 +
   1.152 +    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
   1.153 +    }
   1.154 +
   1.155 +    public void characters(char[] ch, int start, int length) throws SAXException {
   1.156 +        if (length < characterContentChunkSizeContraint) {
   1.157 +            addToCharArrayTable(new CharArray(ch, start, length, true));
   1.158 +        }
   1.159 +    }
   1.160 +
   1.161 +    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
   1.162 +    }
   1.163 +
   1.164 +    public void processingInstruction(String target, String data) throws SAXException {
   1.165 +    }
   1.166 +
   1.167 +    public void setDocumentLocator(org.xml.sax.Locator locator) {
   1.168 +    }
   1.169 +
   1.170 +    public void skippedEntity(String name) throws SAXException {
   1.171 +    }
   1.172 +
   1.173 +
   1.174 +
   1.175 +    // LexicalHandler
   1.176 +
   1.177 +    public void comment(char[] ch, int start, int length) throws SAXException {
   1.178 +    }
   1.179 +
   1.180 +    public void startCDATA() throws SAXException {
   1.181 +    }
   1.182 +
   1.183 +    public void endCDATA() throws SAXException {
   1.184 +    }
   1.185 +
   1.186 +    public void startDTD(String name, String publicId, String systemId) throws SAXException {
   1.187 +    }
   1.188 +
   1.189 +    public void endDTD() throws SAXException {
   1.190 +    }
   1.191 +
   1.192 +    public void startEntity(String name) throws SAXException {
   1.193 +    }
   1.194 +
   1.195 +    public void endEntity(String name) throws SAXException {
   1.196 +    }
   1.197 +
   1.198 +
   1.199 +    public void addToTable(String s, Set v, StringIntMap m, StringArray a) {
   1.200 +        if (s.length() == 0) {
   1.201 +            return;
   1.202 +        }
   1.203 +
   1.204 +        if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) {
   1.205 +            a.add(s);
   1.206 +        }
   1.207 +
   1.208 +        v.add(s);
   1.209 +    }
   1.210 +
   1.211 +    public void addToTable(String s, Set v, StringIntMap m, PrefixArray a) {
   1.212 +        if (s.length() == 0) {
   1.213 +            return;
   1.214 +        }
   1.215 +
   1.216 +        if (m.obtainIndex(s) == KeyIntMap.NOT_PRESENT) {
   1.217 +            a.add(s);
   1.218 +        }
   1.219 +
   1.220 +        v.add(s);
   1.221 +    }
   1.222 +
   1.223 +    public void addToCharArrayTable(CharArray c) {
   1.224 +        if (_serializerVocabulary.characterContentChunk.obtainIndex(c.ch, c.start, c.length, false) == KeyIntMap.NOT_PRESENT) {
   1.225 +            _parserVocabulary.characterContentChunk.add(c.ch, c.length);
   1.226 +        }
   1.227 +
   1.228 +        _v.characterContentChunks.add(c.toString());
   1.229 +    }
   1.230 +
   1.231 +    public void addToNameTable(String namespaceURI, String qName, String localName,
   1.232 +            Set v, LocalNameQualifiedNamesMap m, QualifiedNameArray a,
   1.233 +            boolean isAttribute) throws SAXException {
   1.234 +        LocalNameQualifiedNamesMap.Entry entry = m.obtainEntry(qName);
   1.235 +        if (entry._valueIndex > 0) {
   1.236 +            QualifiedName[] names = entry._value;
   1.237 +            for (int i = 0; i < entry._valueIndex; i++) {
   1.238 +                if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) {
   1.239 +                    return;
   1.240 +                }
   1.241 +            }
   1.242 +        }
   1.243 +
   1.244 +        String prefix = getPrefixFromQualifiedName(qName);
   1.245 +
   1.246 +        int namespaceURIIndex = -1;
   1.247 +        int prefixIndex = -1;
   1.248 +        int localNameIndex = -1;
   1.249 +        if (namespaceURI.length() > 0) {
   1.250 +            namespaceURIIndex = _serializerVocabulary.namespaceName.get(namespaceURI);
   1.251 +            if (namespaceURIIndex == KeyIntMap.NOT_PRESENT) {
   1.252 +                throw new SAXException(CommonResourceBundle.getInstance().
   1.253 +                        getString("message.namespaceURINotIndexed", new Object[]{Integer.valueOf(namespaceURIIndex)}));
   1.254 +            }
   1.255 +
   1.256 +            if (prefix.length() > 0) {
   1.257 +                prefixIndex = _serializerVocabulary.prefix.get(prefix);
   1.258 +                if (prefixIndex == KeyIntMap.NOT_PRESENT) {
   1.259 +                    throw new SAXException(CommonResourceBundle.getInstance().
   1.260 +                            getString("message.prefixNotIndexed", new Object[]{Integer.valueOf(prefixIndex)}));
   1.261 +                }
   1.262 +            }
   1.263 +        }
   1.264 +
   1.265 +        localNameIndex = _serializerVocabulary.localName.obtainIndex(localName);
   1.266 +        if (localNameIndex == KeyIntMap.NOT_PRESENT) {
   1.267 +            _parserVocabulary.localName.add(localName);
   1.268 +            localNameIndex = _parserVocabulary.localName.getSize() - 1;
   1.269 +        }
   1.270 +        QualifiedName name = new QualifiedName(prefix, namespaceURI, localName, m.getNextIndex(),
   1.271 +                prefixIndex, namespaceURIIndex, localNameIndex);
   1.272 +        if (isAttribute) {
   1.273 +            name.createAttributeValues(DuplicateAttributeVerifier.MAP_SIZE);
   1.274 +        }
   1.275 +        entry.addQualifiedName(name);
   1.276 +        a.add(name);
   1.277 +
   1.278 +        v.add(name.getQName());
   1.279 +    }
   1.280 +
   1.281 +    public static String getPrefixFromQualifiedName(String qName) {
   1.282 +        int i = qName.indexOf(':');
   1.283 +        String prefix = "";
   1.284 +        if (i != -1) {
   1.285 +            prefix = qName.substring(0, i);
   1.286 +        }
   1.287 +        return prefix;
   1.288 +    }
   1.289 +
   1.290 +}

mercurial