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

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 658
45676aaa9d47
permissions
-rw-r--r--

merge

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