src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/ContentHandlerAdaptor.java

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

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 658
45676aaa9d47
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) 1997, 2011, 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.bind.v2.runtime;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29
aoqi@0 30 import javax.xml.stream.XMLStreamException;
aoqi@0 31
aoqi@0 32 import com.sun.istack.internal.FinalArrayList;
aoqi@0 33 import com.sun.istack.internal.SAXException2;
aoqi@0 34
aoqi@0 35 import org.xml.sax.Attributes;
aoqi@0 36 import org.xml.sax.SAXException;
aoqi@0 37 import org.xml.sax.helpers.DefaultHandler;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * Receives SAX2 events and send the equivalent events to
aoqi@0 41 * {@link XMLSerializer}
aoqi@0 42 *
aoqi@0 43 * @author
aoqi@0 44 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
aoqi@0 45 */
aoqi@0 46 final class ContentHandlerAdaptor extends DefaultHandler {
aoqi@0 47
aoqi@0 48 /** Stores newly declared prefix-URI mapping. */
aoqi@0 49 private final FinalArrayList<String> prefixMap = new FinalArrayList<String>();
aoqi@0 50
aoqi@0 51 /** Events will be sent to this object. */
aoqi@0 52 private final XMLSerializer serializer;
aoqi@0 53
aoqi@0 54 private final StringBuffer text = new StringBuffer();
aoqi@0 55
aoqi@0 56
aoqi@0 57 ContentHandlerAdaptor( XMLSerializer _serializer ) {
aoqi@0 58 this.serializer = _serializer;
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 public void startDocument() {
aoqi@0 62 prefixMap.clear();
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 public void startPrefixMapping(String prefix, String uri) {
aoqi@0 66 prefixMap.add(prefix);
aoqi@0 67 prefixMap.add(uri);
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 private boolean containsPrefixMapping(String prefix, String uri) {
aoqi@0 71 for( int i=0; i<prefixMap.size(); i+=2 ) {
aoqi@0 72 if(prefixMap.get(i).equals(prefix)
aoqi@0 73 && prefixMap.get(i+1).equals(uri))
aoqi@0 74 return true;
aoqi@0 75 }
aoqi@0 76 return false;
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
aoqi@0 80 throws SAXException {
aoqi@0 81 try {
aoqi@0 82 flushText();
aoqi@0 83
aoqi@0 84 int len = atts.getLength();
aoqi@0 85
aoqi@0 86 String p = getPrefix(qName);
aoqi@0 87
aoqi@0 88 // is this prefix going to be declared on this element?
aoqi@0 89 if(containsPrefixMapping(p,namespaceURI))
aoqi@0 90 serializer.startElementForce(namespaceURI,localName,p,null);
aoqi@0 91 else
aoqi@0 92 serializer.startElement(namespaceURI,localName, p,null);
aoqi@0 93
aoqi@0 94 // declare namespace events
aoqi@0 95 for( int i=0; i<prefixMap.size(); i+=2 ) {
aoqi@0 96 // forcibly set this binding, instead of using declareNsUri.
aoqi@0 97 // this guarantees that namespaces used in DOM will show up
aoqi@0 98 // as-is in the marshalled output (instead of reassigned to something else,
aoqi@0 99 // which may happen if you'd use declareNsUri.)
aoqi@0 100 serializer.getNamespaceContext().force(
aoqi@0 101 prefixMap.get(i+1), prefixMap.get(i) );
aoqi@0 102 }
aoqi@0 103 // make sure namespaces needed by attributes are bound
aoqi@0 104 for( int i=0; i<len; i++ ) {
aoqi@0 105 String qname = atts.getQName(i);
aoqi@0 106 if(qname.startsWith("xmlns") || atts.getURI(i).length() == 0)
aoqi@0 107 continue;
aoqi@0 108 String prefix = getPrefix(qname);
aoqi@0 109
aoqi@0 110 serializer.getNamespaceContext().declareNamespace(
aoqi@0 111 atts.getURI(i), prefix, true );
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 serializer.endNamespaceDecls(null);
aoqi@0 115 // fire attribute events
aoqi@0 116 for( int i=0; i<len; i++ ) {
aoqi@0 117 // be defensive.
aoqi@0 118 if(atts.getQName(i).startsWith("xmlns"))
aoqi@0 119 continue;
aoqi@0 120 serializer.attribute( atts.getURI(i), atts.getLocalName(i), atts.getValue(i));
aoqi@0 121 }
aoqi@0 122 prefixMap.clear();
aoqi@0 123 serializer.endAttributes();
aoqi@0 124 } catch (IOException e) {
aoqi@0 125 throw new SAXException2(e);
aoqi@0 126 } catch (XMLStreamException e) {
aoqi@0 127 throw new SAXException2(e);
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 private String getPrefix(String qname) {
aoqi@0 132 int idx = qname.indexOf(':');
aoqi@0 133 String prefix = (idx==-1)?qname:qname.substring(0,idx);
aoqi@0 134 return prefix;
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
aoqi@0 138 try {
aoqi@0 139 flushText();
aoqi@0 140 serializer.endElement();
aoqi@0 141 } catch (IOException e) {
aoqi@0 142 throw new SAXException2(e);
aoqi@0 143 } catch (XMLStreamException e) {
aoqi@0 144 throw new SAXException2(e);
aoqi@0 145 }
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 private void flushText() throws SAXException, IOException, XMLStreamException {
aoqi@0 149 if( text.length()!=0 ) {
aoqi@0 150 serializer.text(text.toString(),null);
aoqi@0 151 text.setLength(0);
aoqi@0 152 }
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 public void characters(char[] ch, int start, int length) {
aoqi@0 156 text.append(ch,start,length);
aoqi@0 157 }
aoqi@0 158 }

mercurial