Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, 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.ws.encoding; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.List; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import org.xml.sax.helpers.AttributesImpl; |
aoqi@0 | 31 | import org.xml.sax.ContentHandler; |
aoqi@0 | 32 | import org.xml.sax.SAXException; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 35 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 36 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import com.sun.xml.internal.ws.message.stream.StreamMessage; |
aoqi@0 | 39 | import com.sun.xml.internal.ws.encoding.StreamSOAPCodec; |
aoqi@0 | 40 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 41 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 42 | |
aoqi@0 | 43 | /** |
aoqi@0 | 44 | * Complete infoset about a start tag. |
aoqi@0 | 45 | * |
aoqi@0 | 46 | * <p> |
aoqi@0 | 47 | * This is used between {@link StreamMessage} and {@link StreamSOAPCodec} |
aoqi@0 | 48 | * to capture the infoset of the s:Envelope, s:Header, and s:Body elements. |
aoqi@0 | 49 | * |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * <h3>Design Note</h3> |
aoqi@0 | 52 | * <p> |
aoqi@0 | 53 | * Since StAX and SAX uses different null vs empty string convention, one has |
aoqi@0 | 54 | * to choose which format we store things. It can go either way, but I'm assuming |
aoqi@0 | 55 | * that we'll be using StAX more in JAX-WS, so things are kept in the StAX style |
aoqi@0 | 56 | * in this class. |
aoqi@0 | 57 | * |
aoqi@0 | 58 | * @author Kohsuke Kawaguchi |
aoqi@0 | 59 | */ |
aoqi@0 | 60 | public final class TagInfoset { |
aoqi@0 | 61 | /** |
aoqi@0 | 62 | * Namespace declarations on this tag. Read-only. |
aoqi@0 | 63 | * |
aoqi@0 | 64 | * This is an array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }. |
aoqi@0 | 65 | * |
aoqi@0 | 66 | * URIs/prefixes can be null (StAX-style) |
aoqi@0 | 67 | */ |
aoqi@0 | 68 | public final @NotNull String[] ns; |
aoqi@0 | 69 | /** |
aoqi@0 | 70 | * Attributes on this tag. Read-only. |
aoqi@0 | 71 | */ |
aoqi@0 | 72 | public final @NotNull AttributesImpl atts; |
aoqi@0 | 73 | |
aoqi@0 | 74 | /** |
aoqi@0 | 75 | * Prefix of the start tag in stax-style. |
aoqi@0 | 76 | */ |
aoqi@0 | 77 | public final @Nullable String prefix; |
aoqi@0 | 78 | |
aoqi@0 | 79 | /** |
aoqi@0 | 80 | * Namespace URI of the start tag in stax-style. |
aoqi@0 | 81 | */ |
aoqi@0 | 82 | public final @Nullable String nsUri; |
aoqi@0 | 83 | |
aoqi@0 | 84 | /** |
aoqi@0 | 85 | * Local name of the start tag. |
aoqi@0 | 86 | */ |
aoqi@0 | 87 | public final @NotNull String localName; |
aoqi@0 | 88 | |
aoqi@0 | 89 | /** |
aoqi@0 | 90 | * Lazily computed QName (i.e., "foo:bar") |
aoqi@0 | 91 | */ |
aoqi@0 | 92 | private @Nullable String qname; |
aoqi@0 | 93 | |
aoqi@0 | 94 | public TagInfoset(String nsUri, String localName, String prefix, AttributesImpl atts, String... ns) { |
aoqi@0 | 95 | this.nsUri = nsUri; |
aoqi@0 | 96 | this.prefix = prefix; |
aoqi@0 | 97 | this.localName = localName; |
aoqi@0 | 98 | this.atts = atts; |
aoqi@0 | 99 | this.ns = ns; |
aoqi@0 | 100 | } |
aoqi@0 | 101 | |
aoqi@0 | 102 | /** |
aoqi@0 | 103 | * Fills a {@link TagInfoset} object by the current element |
aoqi@0 | 104 | * that the reader points to. |
aoqi@0 | 105 | */ |
aoqi@0 | 106 | public TagInfoset(XMLStreamReader reader) { |
aoqi@0 | 107 | prefix = reader.getPrefix(); |
aoqi@0 | 108 | nsUri = reader.getNamespaceURI(); |
aoqi@0 | 109 | localName = reader.getLocalName(); |
aoqi@0 | 110 | |
aoqi@0 | 111 | int nsc = reader.getNamespaceCount(); |
aoqi@0 | 112 | if(nsc>0) { |
aoqi@0 | 113 | ns = new String[nsc*2]; |
aoqi@0 | 114 | for(int i=0; i<nsc; i++){ |
aoqi@0 | 115 | ns[i*2 ] = fixNull(reader.getNamespacePrefix(i)); |
aoqi@0 | 116 | ns[i*2+1] = fixNull(reader.getNamespaceURI(i)); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } else { |
aoqi@0 | 119 | ns = EMPTY_ARRAY; |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | int ac = reader.getAttributeCount(); |
aoqi@0 | 123 | if(ac>0) { |
aoqi@0 | 124 | atts = new AttributesImpl(); |
aoqi@0 | 125 | StringBuilder sb = new StringBuilder(); |
aoqi@0 | 126 | for(int i=0; i< ac;i++){ |
aoqi@0 | 127 | sb.setLength(0); |
aoqi@0 | 128 | String prefix = reader.getAttributePrefix(i); |
aoqi@0 | 129 | String localName = reader.getAttributeLocalName(i); |
aoqi@0 | 130 | |
aoqi@0 | 131 | String qname; |
aoqi@0 | 132 | if(prefix != null && prefix.length()!=0){ |
aoqi@0 | 133 | sb.append(prefix); |
aoqi@0 | 134 | sb.append(":"); |
aoqi@0 | 135 | sb.append(localName); |
aoqi@0 | 136 | qname = sb.toString(); |
aoqi@0 | 137 | } else { |
aoqi@0 | 138 | qname = localName; |
aoqi@0 | 139 | } |
aoqi@0 | 140 | |
aoqi@0 | 141 | atts.addAttribute( |
aoqi@0 | 142 | fixNull(reader.getAttributeNamespace(i)), |
aoqi@0 | 143 | localName, |
aoqi@0 | 144 | qname, |
aoqi@0 | 145 | reader.getAttributeType(i), |
aoqi@0 | 146 | reader.getAttributeValue(i)); |
aoqi@0 | 147 | } |
aoqi@0 | 148 | } else { |
aoqi@0 | 149 | atts = EMPTY_ATTRIBUTES; |
aoqi@0 | 150 | } |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | /** |
aoqi@0 | 154 | * Writes the start element event. |
aoqi@0 | 155 | */ |
aoqi@0 | 156 | public void writeStart(ContentHandler contentHandler) throws SAXException { |
aoqi@0 | 157 | for( int i=0; i<ns.length; i+=2 ) |
aoqi@0 | 158 | contentHandler.startPrefixMapping(fixNull(ns[i]),fixNull(ns[i+1])); |
aoqi@0 | 159 | contentHandler.startElement(fixNull(nsUri), localName ,getQName(), atts); |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | /** |
aoqi@0 | 163 | * Writes the end element event. |
aoqi@0 | 164 | */ |
aoqi@0 | 165 | public void writeEnd(ContentHandler contentHandler) throws SAXException{ |
aoqi@0 | 166 | contentHandler.endElement(fixNull(nsUri),localName,getQName()); |
aoqi@0 | 167 | for( int i=ns.length-2; i>=0; i-=2 ) { |
aoqi@0 | 168 | contentHandler.endPrefixMapping(fixNull(ns[i])); |
aoqi@0 | 169 | } |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | /** |
aoqi@0 | 173 | * Writes the start element event. |
aoqi@0 | 174 | */ |
aoqi@0 | 175 | public void writeStart(XMLStreamWriter w) throws XMLStreamException { |
aoqi@0 | 176 | // write start tag. |
aoqi@0 | 177 | if(prefix==null) { |
aoqi@0 | 178 | if(nsUri==null) |
aoqi@0 | 179 | w.writeStartElement(localName); |
aoqi@0 | 180 | else { |
aoqi@0 | 181 | //fix Null prefix. otherwise throws XMLStreamException, |
aoqi@0 | 182 | // if the namespace URI has not been bound to a prefix |
aoqi@0 | 183 | w.writeStartElement("",localName,nsUri); |
aoqi@0 | 184 | } |
aoqi@0 | 185 | } else { |
aoqi@0 | 186 | w.writeStartElement(prefix,localName,nsUri); |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | for( int i=0; i<ns.length; i+=2 ) { |
aoqi@0 | 190 | w.writeNamespace(ns[i],ns[i+1]); |
aoqi@0 | 191 | } |
aoqi@0 | 192 | |
aoqi@0 | 193 | for( int i=0; i<atts.getLength(); i++ ) { |
aoqi@0 | 194 | String nsUri = atts.getURI(i); |
aoqi@0 | 195 | if(nsUri==null || nsUri.length() ==0) { |
aoqi@0 | 196 | w.writeAttribute(atts.getLocalName(i),atts.getValue(i)); |
aoqi@0 | 197 | } else { |
aoqi@0 | 198 | String rawName = atts.getQName(i); |
aoqi@0 | 199 | String prefix = rawName.substring(0,rawName.indexOf(':')); |
aoqi@0 | 200 | w.writeAttribute(prefix,nsUri,atts.getLocalName(i),atts.getValue(i)); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | } |
aoqi@0 | 203 | } |
aoqi@0 | 204 | |
aoqi@0 | 205 | private String getQName() { |
aoqi@0 | 206 | if(qname!=null) return qname; |
aoqi@0 | 207 | |
aoqi@0 | 208 | StringBuilder sb = new StringBuilder(); |
aoqi@0 | 209 | if(prefix!=null){ |
aoqi@0 | 210 | sb.append(prefix); |
aoqi@0 | 211 | sb.append(':'); |
aoqi@0 | 212 | sb.append(localName); |
aoqi@0 | 213 | qname = sb.toString(); |
aoqi@0 | 214 | } else { |
aoqi@0 | 215 | qname = localName; |
aoqi@0 | 216 | } |
aoqi@0 | 217 | return qname; |
aoqi@0 | 218 | } |
aoqi@0 | 219 | private static String fixNull(String s) { |
aoqi@0 | 220 | if(s==null) return ""; |
aoqi@0 | 221 | else return s; |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | private static final String[] EMPTY_ARRAY = new String[0]; |
aoqi@0 | 225 | private static final AttributesImpl EMPTY_ATTRIBUTES = new AttributesImpl(); |
aoqi@0 | 226 | |
aoqi@0 | 227 | public String getNamespaceURI(String prefix) { |
aoqi@0 | 228 | int size = ns.length/2; |
aoqi@0 | 229 | for(int i=0; i<size; i++){ |
aoqi@0 | 230 | String p = ns[i*2 ]; |
aoqi@0 | 231 | String n = ns[i*2+1]; |
aoqi@0 | 232 | if (prefix.equals(p)) return n; |
aoqi@0 | 233 | } |
aoqi@0 | 234 | return null; |
aoqi@0 | 235 | } |
aoqi@0 | 236 | |
aoqi@0 | 237 | public String getPrefix(String namespaceURI) { |
aoqi@0 | 238 | int size = ns.length/2; |
aoqi@0 | 239 | for(int i=0; i<size; i++){ |
aoqi@0 | 240 | String p = ns[i*2 ]; |
aoqi@0 | 241 | String n = ns[i*2+1]; |
aoqi@0 | 242 | if (namespaceURI.equals(n)) return p; |
aoqi@0 | 243 | } |
aoqi@0 | 244 | return null; |
aoqi@0 | 245 | } |
aoqi@0 | 246 | //Who wants this? |
aoqi@0 | 247 | public List<String> allPrefixes(String namespaceURI) { |
aoqi@0 | 248 | int size = ns.length/2; |
aoqi@0 | 249 | List<String> l = new java.util.ArrayList<String>(); |
aoqi@0 | 250 | for(int i=0; i<size; i++){ |
aoqi@0 | 251 | String p = ns[i*2 ]; |
aoqi@0 | 252 | String n = ns[i*2+1]; |
aoqi@0 | 253 | if (namespaceURI.equals(n)) l.add(p); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | return l; |
aoqi@0 | 256 | } |
aoqi@0 | 257 | } |