src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/sax/SAXBufferCreator.java

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.stream.buffer.sax;
aoqi@0 27
aoqi@0 28 import com.sun.xml.internal.stream.buffer.AbstractCreator;
aoqi@0 29 import org.xml.sax.Attributes;
aoqi@0 30 import org.xml.sax.SAXException;
aoqi@0 31 import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
aoqi@0 32 import java.io.IOException;
aoqi@0 33 import java.io.InputStream;
aoqi@0 34 import org.xml.sax.ContentHandler;
aoqi@0 35 import org.xml.sax.DTDHandler;
aoqi@0 36 import org.xml.sax.EntityResolver;
aoqi@0 37 import org.xml.sax.ErrorHandler;
aoqi@0 38 import org.xml.sax.InputSource;
aoqi@0 39 import org.xml.sax.Locator;
aoqi@0 40 import org.xml.sax.SAXParseException;
aoqi@0 41 import org.xml.sax.XMLReader;
aoqi@0 42 import org.xml.sax.ext.LexicalHandler;
aoqi@0 43
aoqi@0 44 /**
aoqi@0 45 * Writes into {@link MutableXMLStreamBuffer} from SAX.
aoqi@0 46 *
aoqi@0 47 * TODO
aoqi@0 48 * Implement the marking the stream on the element when an ID
aoqi@0 49 * attribute on the element is defined
aoqi@0 50 */
aoqi@0 51 public class SAXBufferCreator extends AbstractCreator
aoqi@0 52 implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler, LexicalHandler {
aoqi@0 53 protected String[] _namespaceAttributes;
aoqi@0 54
aoqi@0 55 protected int _namespaceAttributesPtr;
aoqi@0 56
aoqi@0 57 private int depth = 0;
aoqi@0 58
aoqi@0 59 public SAXBufferCreator() {
aoqi@0 60 _namespaceAttributes = new String[16 * 2];
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 public SAXBufferCreator(MutableXMLStreamBuffer buffer) {
aoqi@0 64 this();
aoqi@0 65 setBuffer(buffer);
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 public MutableXMLStreamBuffer create(XMLReader reader, InputStream in) throws IOException, SAXException {
aoqi@0 69 return create(reader, in, null);
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 public MutableXMLStreamBuffer create(XMLReader reader, InputStream in, String systemId) throws IOException, SAXException {
aoqi@0 73 if (_buffer == null) {
aoqi@0 74 createBuffer();
aoqi@0 75 }
aoqi@0 76 _buffer.setSystemId(systemId);
aoqi@0 77 reader.setContentHandler(this);
aoqi@0 78 reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, this);
aoqi@0 79
aoqi@0 80 try {
aoqi@0 81 setHasInternedStrings(reader.getFeature(Features.STRING_INTERNING_FEATURE));
aoqi@0 82 } catch (SAXException e) {
aoqi@0 83 }
aoqi@0 84
aoqi@0 85
aoqi@0 86 if (systemId != null) {
aoqi@0 87 InputSource s = new InputSource(systemId);
aoqi@0 88 s.setByteStream(in);
aoqi@0 89 reader.parse(s);
aoqi@0 90 } else {
aoqi@0 91 reader.parse(new InputSource(in));
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 return getXMLStreamBuffer();
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 public void reset() {
aoqi@0 98 _buffer = null;
aoqi@0 99 _namespaceAttributesPtr = 0;
aoqi@0 100 depth=0;
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 public void startDocument() throws SAXException {
aoqi@0 104 storeStructure(T_DOCUMENT);
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 public void endDocument() throws SAXException {
aoqi@0 108 storeStructure(T_END);
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 public void startPrefixMapping(String prefix, String uri) throws SAXException {
aoqi@0 112 cacheNamespaceAttribute(prefix, uri);
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
aoqi@0 116 storeQualifiedName(T_ELEMENT_LN,
aoqi@0 117 uri, localName, qName);
aoqi@0 118
aoqi@0 119 // Has namespaces attributes
aoqi@0 120 if (_namespaceAttributesPtr > 0) {
aoqi@0 121 storeNamespaceAttributes();
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 // Has attributes
aoqi@0 125 if (attributes.getLength() > 0) {
aoqi@0 126 storeAttributes(attributes);
aoqi@0 127 }
aoqi@0 128 depth++;
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 public void endElement(String uri, String localName, String qName) throws SAXException {
aoqi@0 132 storeStructure(T_END);
aoqi@0 133 if(--depth==0)
aoqi@0 134 increaseTreeCount(); // one tree processed
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 public void characters(char ch[], int start, int length) throws SAXException {
aoqi@0 138 storeContentCharacters(T_TEXT_AS_CHAR_ARRAY, ch, start, length);
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
aoqi@0 142 characters(ch, start, length);
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 public void processingInstruction(String target, String data) throws SAXException {
aoqi@0 146 storeStructure(T_PROCESSING_INSTRUCTION);
aoqi@0 147 storeStructureString(target);
aoqi@0 148 storeStructureString(data);
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 public void comment(char[] ch, int start, int length) throws SAXException {
aoqi@0 152 storeContentCharacters(T_COMMENT_AS_CHAR_ARRAY, ch, start, length);
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 //
aoqi@0 156
aoqi@0 157 private void cacheNamespaceAttribute(String prefix, String uri) {
aoqi@0 158 _namespaceAttributes[_namespaceAttributesPtr++] = prefix;
aoqi@0 159 _namespaceAttributes[_namespaceAttributesPtr++] = uri;
aoqi@0 160
aoqi@0 161 if (_namespaceAttributesPtr == _namespaceAttributes.length) {
aoqi@0 162 final String[] namespaceAttributes = new String[_namespaceAttributesPtr * 2];
aoqi@0 163 System.arraycopy(_namespaceAttributes, 0, namespaceAttributes, 0, _namespaceAttributesPtr);
aoqi@0 164 _namespaceAttributes = namespaceAttributes;
aoqi@0 165 }
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 private void storeNamespaceAttributes() {
aoqi@0 169 for (int i = 0; i < _namespaceAttributesPtr; i += 2) {
aoqi@0 170 int item = T_NAMESPACE_ATTRIBUTE;
aoqi@0 171 if (_namespaceAttributes[i].length() > 0) {
aoqi@0 172 item |= FLAG_PREFIX;
aoqi@0 173 storeStructureString(_namespaceAttributes[i]);
aoqi@0 174 }
aoqi@0 175 if (_namespaceAttributes[i + 1].length() > 0) {
aoqi@0 176 item |= FLAG_URI;
aoqi@0 177 storeStructureString(_namespaceAttributes[i + 1]);
aoqi@0 178 }
aoqi@0 179 storeStructure(item);
aoqi@0 180 }
aoqi@0 181 _namespaceAttributesPtr = 0;
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 private void storeAttributes(Attributes attributes) {
aoqi@0 185 for (int i = 0; i < attributes.getLength(); i++) {
aoqi@0 186 // Skip NS attributes. Some versions of JDK seem to send wrong local name
aoqi@0 187 // Also it is not stored correctly by the following.
aoqi@0 188 if (attributes.getQName(i).startsWith("xmlns"))
aoqi@0 189 continue;
aoqi@0 190 storeQualifiedName(T_ATTRIBUTE_LN,
aoqi@0 191 attributes.getURI(i),
aoqi@0 192 attributes.getLocalName(i),
aoqi@0 193 attributes.getQName(i));
aoqi@0 194
aoqi@0 195 storeStructureString(attributes.getType(i));
aoqi@0 196 storeContentString(attributes.getValue(i));
aoqi@0 197 }
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 private void storeQualifiedName(int item, String uri, String localName, String qName) {
aoqi@0 201 if (uri.length() > 0) {
aoqi@0 202 item |= FLAG_URI;
aoqi@0 203 storeStructureString(uri);
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 storeStructureString(localName);
aoqi@0 207
aoqi@0 208 if (qName.indexOf(':') >= 0) {
aoqi@0 209 item |= FLAG_QUALIFIED_NAME;
aoqi@0 210 storeStructureString(qName);
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 storeStructure(item);
aoqi@0 214 }
aoqi@0 215
aoqi@0 216
aoqi@0 217 // Empty methods for SAX handlers
aoqi@0 218
aoqi@0 219 // Entity resolver handler
aoqi@0 220
aoqi@0 221 public InputSource resolveEntity (String publicId, String systemId)
aoqi@0 222 throws IOException, SAXException
aoqi@0 223 {
aoqi@0 224 return null;
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 // DTD handler
aoqi@0 228
aoqi@0 229 public void notationDecl (String name, String publicId, String systemId)
aoqi@0 230 throws SAXException
aoqi@0 231 { }
aoqi@0 232
aoqi@0 233 public void unparsedEntityDecl (String name, String publicId,
aoqi@0 234 String systemId, String notationName)
aoqi@0 235 throws SAXException
aoqi@0 236 { }
aoqi@0 237
aoqi@0 238 // Content handler
aoqi@0 239
aoqi@0 240 public void setDocumentLocator (Locator locator) { }
aoqi@0 241
aoqi@0 242 public void endPrefixMapping (String prefix) throws SAXException { }
aoqi@0 243
aoqi@0 244 public void skippedEntity (String name) throws SAXException { }
aoqi@0 245
aoqi@0 246 // Lexical handler
aoqi@0 247
aoqi@0 248 public void startDTD(String name, String publicId, String systemId) throws SAXException { }
aoqi@0 249
aoqi@0 250 public void endDTD() throws SAXException { }
aoqi@0 251
aoqi@0 252 public void startEntity(String name) throws SAXException { }
aoqi@0 253
aoqi@0 254 public void endEntity(String name) throws SAXException { }
aoqi@0 255
aoqi@0 256 public void startCDATA() throws SAXException { }
aoqi@0 257
aoqi@0 258 public void endCDATA() throws SAXException { }
aoqi@0 259
aoqi@0 260 // Error handler
aoqi@0 261
aoqi@0 262 public void warning(SAXParseException e) throws SAXException { }
aoqi@0 263
aoqi@0 264 public void error(SAXParseException e) throws SAXException { }
aoqi@0 265
aoqi@0 266 public void fatalError(SAXParseException e) throws SAXException
aoqi@0 267 {
aoqi@0 268 throw e;
aoqi@0 269 }
aoqi@0 270 }

mercurial