1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/sax/SAXBufferCreator.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,270 @@ 1.4 +/* 1.5 + * Copyright (c) 2005, 2012, 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 + 1.29 +package com.sun.xml.internal.stream.buffer.sax; 1.30 + 1.31 +import com.sun.xml.internal.stream.buffer.AbstractCreator; 1.32 +import org.xml.sax.Attributes; 1.33 +import org.xml.sax.SAXException; 1.34 +import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer; 1.35 +import java.io.IOException; 1.36 +import java.io.InputStream; 1.37 +import org.xml.sax.ContentHandler; 1.38 +import org.xml.sax.DTDHandler; 1.39 +import org.xml.sax.EntityResolver; 1.40 +import org.xml.sax.ErrorHandler; 1.41 +import org.xml.sax.InputSource; 1.42 +import org.xml.sax.Locator; 1.43 +import org.xml.sax.SAXParseException; 1.44 +import org.xml.sax.XMLReader; 1.45 +import org.xml.sax.ext.LexicalHandler; 1.46 + 1.47 +/** 1.48 + * Writes into {@link MutableXMLStreamBuffer} from SAX. 1.49 + * 1.50 + * TODO 1.51 + * Implement the marking the stream on the element when an ID 1.52 + * attribute on the element is defined 1.53 + */ 1.54 +public class SAXBufferCreator extends AbstractCreator 1.55 + implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler, LexicalHandler { 1.56 + protected String[] _namespaceAttributes; 1.57 + 1.58 + protected int _namespaceAttributesPtr; 1.59 + 1.60 + private int depth = 0; 1.61 + 1.62 + public SAXBufferCreator() { 1.63 + _namespaceAttributes = new String[16 * 2]; 1.64 + } 1.65 + 1.66 + public SAXBufferCreator(MutableXMLStreamBuffer buffer) { 1.67 + this(); 1.68 + setBuffer(buffer); 1.69 + } 1.70 + 1.71 + public MutableXMLStreamBuffer create(XMLReader reader, InputStream in) throws IOException, SAXException { 1.72 + return create(reader, in, null); 1.73 + } 1.74 + 1.75 + public MutableXMLStreamBuffer create(XMLReader reader, InputStream in, String systemId) throws IOException, SAXException { 1.76 + if (_buffer == null) { 1.77 + createBuffer(); 1.78 + } 1.79 + _buffer.setSystemId(systemId); 1.80 + reader.setContentHandler(this); 1.81 + reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, this); 1.82 + 1.83 + try { 1.84 + setHasInternedStrings(reader.getFeature(Features.STRING_INTERNING_FEATURE)); 1.85 + } catch (SAXException e) { 1.86 + } 1.87 + 1.88 + 1.89 + if (systemId != null) { 1.90 + InputSource s = new InputSource(systemId); 1.91 + s.setByteStream(in); 1.92 + reader.parse(s); 1.93 + } else { 1.94 + reader.parse(new InputSource(in)); 1.95 + } 1.96 + 1.97 + return getXMLStreamBuffer(); 1.98 + } 1.99 + 1.100 + public void reset() { 1.101 + _buffer = null; 1.102 + _namespaceAttributesPtr = 0; 1.103 + depth=0; 1.104 + } 1.105 + 1.106 + public void startDocument() throws SAXException { 1.107 + storeStructure(T_DOCUMENT); 1.108 + } 1.109 + 1.110 + public void endDocument() throws SAXException { 1.111 + storeStructure(T_END); 1.112 + } 1.113 + 1.114 + public void startPrefixMapping(String prefix, String uri) throws SAXException { 1.115 + cacheNamespaceAttribute(prefix, uri); 1.116 + } 1.117 + 1.118 + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 1.119 + storeQualifiedName(T_ELEMENT_LN, 1.120 + uri, localName, qName); 1.121 + 1.122 + // Has namespaces attributes 1.123 + if (_namespaceAttributesPtr > 0) { 1.124 + storeNamespaceAttributes(); 1.125 + } 1.126 + 1.127 + // Has attributes 1.128 + if (attributes.getLength() > 0) { 1.129 + storeAttributes(attributes); 1.130 + } 1.131 + depth++; 1.132 + } 1.133 + 1.134 + public void endElement(String uri, String localName, String qName) throws SAXException { 1.135 + storeStructure(T_END); 1.136 + if(--depth==0) 1.137 + increaseTreeCount(); // one tree processed 1.138 + } 1.139 + 1.140 + public void characters(char ch[], int start, int length) throws SAXException { 1.141 + storeContentCharacters(T_TEXT_AS_CHAR_ARRAY, ch, start, length); 1.142 + } 1.143 + 1.144 + public void ignorableWhitespace(char ch[], int start, int length) throws SAXException { 1.145 + characters(ch, start, length); 1.146 + } 1.147 + 1.148 + public void processingInstruction(String target, String data) throws SAXException { 1.149 + storeStructure(T_PROCESSING_INSTRUCTION); 1.150 + storeStructureString(target); 1.151 + storeStructureString(data); 1.152 + } 1.153 + 1.154 + public void comment(char[] ch, int start, int length) throws SAXException { 1.155 + storeContentCharacters(T_COMMENT_AS_CHAR_ARRAY, ch, start, length); 1.156 + } 1.157 + 1.158 + // 1.159 + 1.160 + private void cacheNamespaceAttribute(String prefix, String uri) { 1.161 + _namespaceAttributes[_namespaceAttributesPtr++] = prefix; 1.162 + _namespaceAttributes[_namespaceAttributesPtr++] = uri; 1.163 + 1.164 + if (_namespaceAttributesPtr == _namespaceAttributes.length) { 1.165 + final String[] namespaceAttributes = new String[_namespaceAttributesPtr * 2]; 1.166 + System.arraycopy(_namespaceAttributes, 0, namespaceAttributes, 0, _namespaceAttributesPtr); 1.167 + _namespaceAttributes = namespaceAttributes; 1.168 + } 1.169 + } 1.170 + 1.171 + private void storeNamespaceAttributes() { 1.172 + for (int i = 0; i < _namespaceAttributesPtr; i += 2) { 1.173 + int item = T_NAMESPACE_ATTRIBUTE; 1.174 + if (_namespaceAttributes[i].length() > 0) { 1.175 + item |= FLAG_PREFIX; 1.176 + storeStructureString(_namespaceAttributes[i]); 1.177 + } 1.178 + if (_namespaceAttributes[i + 1].length() > 0) { 1.179 + item |= FLAG_URI; 1.180 + storeStructureString(_namespaceAttributes[i + 1]); 1.181 + } 1.182 + storeStructure(item); 1.183 + } 1.184 + _namespaceAttributesPtr = 0; 1.185 + } 1.186 + 1.187 + private void storeAttributes(Attributes attributes) { 1.188 + for (int i = 0; i < attributes.getLength(); i++) { 1.189 + // Skip NS attributes. Some versions of JDK seem to send wrong local name 1.190 + // Also it is not stored correctly by the following. 1.191 + if (attributes.getQName(i).startsWith("xmlns")) 1.192 + continue; 1.193 + storeQualifiedName(T_ATTRIBUTE_LN, 1.194 + attributes.getURI(i), 1.195 + attributes.getLocalName(i), 1.196 + attributes.getQName(i)); 1.197 + 1.198 + storeStructureString(attributes.getType(i)); 1.199 + storeContentString(attributes.getValue(i)); 1.200 + } 1.201 + } 1.202 + 1.203 + private void storeQualifiedName(int item, String uri, String localName, String qName) { 1.204 + if (uri.length() > 0) { 1.205 + item |= FLAG_URI; 1.206 + storeStructureString(uri); 1.207 + } 1.208 + 1.209 + storeStructureString(localName); 1.210 + 1.211 + if (qName.indexOf(':') >= 0) { 1.212 + item |= FLAG_QUALIFIED_NAME; 1.213 + storeStructureString(qName); 1.214 + } 1.215 + 1.216 + storeStructure(item); 1.217 + } 1.218 + 1.219 + 1.220 + // Empty methods for SAX handlers 1.221 + 1.222 + // Entity resolver handler 1.223 + 1.224 + public InputSource resolveEntity (String publicId, String systemId) 1.225 + throws IOException, SAXException 1.226 + { 1.227 + return null; 1.228 + } 1.229 + 1.230 + // DTD handler 1.231 + 1.232 + public void notationDecl (String name, String publicId, String systemId) 1.233 + throws SAXException 1.234 + { } 1.235 + 1.236 + public void unparsedEntityDecl (String name, String publicId, 1.237 + String systemId, String notationName) 1.238 + throws SAXException 1.239 + { } 1.240 + 1.241 + // Content handler 1.242 + 1.243 + public void setDocumentLocator (Locator locator) { } 1.244 + 1.245 + public void endPrefixMapping (String prefix) throws SAXException { } 1.246 + 1.247 + public void skippedEntity (String name) throws SAXException { } 1.248 + 1.249 + // Lexical handler 1.250 + 1.251 + public void startDTD(String name, String publicId, String systemId) throws SAXException { } 1.252 + 1.253 + public void endDTD() throws SAXException { } 1.254 + 1.255 + public void startEntity(String name) throws SAXException { } 1.256 + 1.257 + public void endEntity(String name) throws SAXException { } 1.258 + 1.259 + public void startCDATA() throws SAXException { } 1.260 + 1.261 + public void endCDATA() throws SAXException { } 1.262 + 1.263 + // Error handler 1.264 + 1.265 + public void warning(SAXParseException e) throws SAXException { } 1.266 + 1.267 + public void error(SAXParseException e) throws SAXException { } 1.268 + 1.269 + public void fatalError(SAXParseException e) throws SAXException 1.270 + { 1.271 + throw e; 1.272 + } 1.273 +}