src/share/jaxws_classes/com/sun/xml/internal/ws/util/xml/ContentHandlerToXMLStreamWriter.java

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

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

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

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 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.ws.util.xml;
ohair@286 27
ohair@286 28 import org.xml.sax.Attributes;
ohair@286 29 import org.xml.sax.Locator;
ohair@286 30 import org.xml.sax.SAXException;
ohair@286 31 import org.xml.sax.helpers.DefaultHandler;
ohair@286 32
ohair@286 33 import javax.xml.stream.XMLStreamException;
ohair@286 34 import javax.xml.stream.XMLStreamWriter;
ohair@286 35 import java.util.Stack;
ohair@286 36
ohair@286 37 /**
ohair@286 38 * This is a simple utility class that adapts SAX events into StAX
ohair@286 39 * {@link XMLStreamWriter} events, bridging between
ohair@286 40 * the two parser technologies.
ohair@286 41 *
ohair@286 42 * This ContentHandler does not own the XMLStreamWriter. Therefore, it will
ohair@286 43 * not close or flush the writer at any point.
ohair@286 44 *
ohair@286 45 * @author Ryan.Shoemaker@Sun.COM
ohair@286 46 * @version 1.0
ohair@286 47 */
ohair@286 48 public class ContentHandlerToXMLStreamWriter extends DefaultHandler {
ohair@286 49
ohair@286 50 // SAX events will be sent to this XMLStreamWriter
ohair@286 51 private final XMLStreamWriter staxWriter;
ohair@286 52
ohair@286 53 // storage for prefix bindings
ohair@286 54 private final Stack prefixBindings;
ohair@286 55
ohair@286 56 public ContentHandlerToXMLStreamWriter(XMLStreamWriter staxCore) {
ohair@286 57 this.staxWriter = staxCore;
ohair@286 58 prefixBindings = new Stack(); // default of 10 seems reasonable
ohair@286 59 }
ohair@286 60
ohair@286 61 /*
ohair@286 62 * (non-Javadoc)
ohair@286 63 *
ohair@286 64 * @see org.xml.sax.ContentHandler#endDocument()
ohair@286 65 */
ohair@286 66 public void endDocument() throws SAXException {
ohair@286 67 try {
ohair@286 68 staxWriter.writeEndDocument();
ohair@286 69 staxWriter.flush();
ohair@286 70 } catch (XMLStreamException e) {
ohair@286 71 throw new SAXException(e);
ohair@286 72 }
ohair@286 73 }
ohair@286 74
ohair@286 75 /*
ohair@286 76 * (non-Javadoc)
ohair@286 77 *
ohair@286 78 * @see org.xml.sax.ContentHandler#startDocument()
ohair@286 79 */
ohair@286 80 public void startDocument() throws SAXException {
ohair@286 81 try {
ohair@286 82 staxWriter.writeStartDocument();
ohair@286 83 } catch (XMLStreamException e) {
ohair@286 84 throw new SAXException(e);
ohair@286 85 }
ohair@286 86 }
ohair@286 87
ohair@286 88 /*
ohair@286 89 * (non-Javadoc)
ohair@286 90 *
ohair@286 91 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
ohair@286 92 */
ohair@286 93 public void characters(char[] ch, int start, int length)
ohair@286 94 throws SAXException {
ohair@286 95
ohair@286 96 try {
ohair@286 97 staxWriter.writeCharacters(ch, start, length);
ohair@286 98 } catch (XMLStreamException e) {
ohair@286 99 throw new SAXException(e);
ohair@286 100 }
ohair@286 101
ohair@286 102 }
ohair@286 103
ohair@286 104 /*
ohair@286 105 * (non-Javadoc)
ohair@286 106 *
ohair@286 107 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
ohair@286 108 */
ohair@286 109 public void ignorableWhitespace(char[] ch, int start, int length)
ohair@286 110 throws SAXException {
ohair@286 111
ohair@286 112 characters(ch,start,length);
ohair@286 113 }
ohair@286 114
ohair@286 115 /*
ohair@286 116 * (non-Javadoc)
ohair@286 117 *
ohair@286 118 * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
ohair@286 119 */
ohair@286 120 public void endPrefixMapping(String prefix) throws SAXException {
ohair@286 121 // TODO: no-op?
ohair@286 122
ohair@286 123 // I think we can ignore these SAX events because StAX
ohair@286 124 // automatically scopes the prefix bindings.
ohair@286 125 }
ohair@286 126
ohair@286 127 /*
ohair@286 128 * (non-Javadoc)
ohair@286 129 *
ohair@286 130 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
ohair@286 131 */
ohair@286 132 public void skippedEntity(String name) throws SAXException {
ohair@286 133 try {
ohair@286 134 staxWriter.writeEntityRef(name);
ohair@286 135 } catch (XMLStreamException e) {
ohair@286 136 throw new SAXException(e);
ohair@286 137 }
ohair@286 138 }
ohair@286 139
ohair@286 140 /*
ohair@286 141 * (non-Javadoc)
ohair@286 142 *
ohair@286 143 * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
ohair@286 144 */
ohair@286 145 public void setDocumentLocator(Locator locator) {
ohair@286 146 // TODO: no-op?
ohair@286 147 // there doesn't seem to be any way to pass location info
ohair@286 148 // along to the XMLStreamWriter. On the XMLEventWriter side, you
ohair@286 149 // can set the location info on the event objects.
ohair@286 150 }
ohair@286 151
ohair@286 152 /*
ohair@286 153 * (non-Javadoc)
ohair@286 154 *
ohair@286 155 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
ohair@286 156 * java.lang.String)
ohair@286 157 */
ohair@286 158 public void processingInstruction(String target, String data)
ohair@286 159 throws SAXException {
ohair@286 160
ohair@286 161 try {
ohair@286 162 staxWriter.writeProcessingInstruction(target, data);
ohair@286 163 } catch (XMLStreamException e) {
ohair@286 164 throw new SAXException(e);
ohair@286 165 }
ohair@286 166
ohair@286 167 }
ohair@286 168
ohair@286 169 /*
ohair@286 170 * (non-Javadoc)
ohair@286 171 *
ohair@286 172 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
ohair@286 173 * java.lang.String)
ohair@286 174 */
ohair@286 175 public void startPrefixMapping(String prefix, String uri)
ohair@286 176 throws SAXException {
ohair@286 177
ohair@286 178 // defend against parsers that pass null in for "xmlns" prefix
ohair@286 179 if (prefix == null) {
ohair@286 180 prefix = "";
ohair@286 181 }
ohair@286 182
ohair@286 183 if (prefix.equals("xml")) {
ohair@286 184 return;
ohair@286 185 }
ohair@286 186
ohair@286 187 prefixBindings.add(prefix);
ohair@286 188 prefixBindings.add(uri);
ohair@286 189 }
ohair@286 190
ohair@286 191 /*
ohair@286 192 * (non-Javadoc)
ohair@286 193 *
ohair@286 194 * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
ohair@286 195 * java.lang.String, java.lang.String)
ohair@286 196 */
ohair@286 197 public void endElement(String namespaceURI, String localName, String qName)
ohair@286 198 throws SAXException {
ohair@286 199
ohair@286 200 try {
ohair@286 201 // TODO: is this all we have to do?
ohair@286 202 staxWriter.writeEndElement();
ohair@286 203 } catch (XMLStreamException e) {
ohair@286 204 throw new SAXException(e);
ohair@286 205 }
ohair@286 206 }
ohair@286 207
ohair@286 208 /*
ohair@286 209 * (non-Javadoc)
ohair@286 210 *
ohair@286 211 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
ohair@286 212 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
ohair@286 213 */
ohair@286 214 public void startElement(
ohair@286 215 String namespaceURI,
ohair@286 216 String localName,
ohair@286 217 String qName,
ohair@286 218 Attributes atts)
ohair@286 219 throws SAXException {
ohair@286 220
ohair@286 221 try {
ohair@286 222 staxWriter.writeStartElement(
ohair@286 223 getPrefix(qName),
ohair@286 224 localName,
ohair@286 225 namespaceURI);
ohair@286 226
ohair@286 227 String uri, prefix;
ohair@286 228 while (prefixBindings.size() != 0) {
ohair@286 229 uri = (String)prefixBindings.pop();
ohair@286 230 prefix = (String)prefixBindings.pop();
ohair@286 231 if (prefix.length() == 0) {
ohair@286 232 staxWriter.setDefaultNamespace(uri);
ohair@286 233 } else {
ohair@286 234 staxWriter.setPrefix(prefix, uri);
ohair@286 235 }
ohair@286 236
ohair@286 237 // this method handles "", null, and "xmlns" prefixes properly
ohair@286 238 staxWriter.writeNamespace(prefix, uri);
ohair@286 239 }
ohair@286 240
ohair@286 241 writeAttributes(atts);
ohair@286 242 } catch (XMLStreamException e) {
ohair@286 243 throw new SAXException(e);
ohair@286 244 }
ohair@286 245
ohair@286 246 }
ohair@286 247
ohair@286 248 /**
ohair@286 249 * Generate a StAX writeAttribute event for each attribute
ohair@286 250 *
ohair@286 251 * @param atts
ohair@286 252 * attributes from the SAX event
ohair@286 253 */
ohair@286 254 private void writeAttributes(Attributes atts) throws XMLStreamException {
ohair@286 255 for (int i = 0; i < atts.getLength(); i++) {
ohair@286 256 final String prefix = getPrefix(atts.getQName(i));
ohair@286 257 if(!prefix.equals("xmlns")) { // defend againts broken transformers that report xmlns decls as attrs
ohair@286 258 staxWriter.writeAttribute(
ohair@286 259 prefix,
ohair@286 260 atts.getURI(i),
ohair@286 261 atts.getLocalName(i),
ohair@286 262 atts.getValue(i));
ohair@286 263 }
ohair@286 264 }
ohair@286 265 }
ohair@286 266
ohair@286 267 /**
ohair@286 268 * Pull the prefix off of the specified QName.
ohair@286 269 *
ohair@286 270 * @param qName
ohair@286 271 * the QName
ohair@286 272 * @return the prefix or the empty string if it doesn't exist.
ohair@286 273 */
ohair@286 274 private String getPrefix(String qName) {
ohair@286 275 int idx = qName.indexOf(':');
ohair@286 276 if (idx == -1) {
ohair@286 277 return "";
ohair@286 278 } else {
ohair@286 279 return qName.substring(0, idx);
ohair@286 280 }
ohair@286 281 }
ohair@286 282
ohair@286 283 }

mercurial