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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial