aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.encoding; aoqi@0: aoqi@0: import java.util.List; aoqi@0: aoqi@0: import org.xml.sax.helpers.AttributesImpl; aoqi@0: import org.xml.sax.ContentHandler; aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: import javax.xml.stream.XMLStreamReader; aoqi@0: import javax.xml.stream.XMLStreamWriter; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: aoqi@0: import com.sun.xml.internal.ws.message.stream.StreamMessage; aoqi@0: import com.sun.xml.internal.ws.encoding.StreamSOAPCodec; aoqi@0: import com.sun.istack.internal.Nullable; aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: aoqi@0: /** aoqi@0: * Complete infoset about a start tag. aoqi@0: * aoqi@0: *

aoqi@0: * This is used between {@link StreamMessage} and {@link StreamSOAPCodec} aoqi@0: * to capture the infoset of the s:Envelope, s:Header, and s:Body elements. aoqi@0: * aoqi@0: * aoqi@0: *

Design Note

aoqi@0: *

aoqi@0: * Since StAX and SAX uses different null vs empty string convention, one has aoqi@0: * to choose which format we store things. It can go either way, but I'm assuming aoqi@0: * that we'll be using StAX more in JAX-WS, so things are kept in the StAX style aoqi@0: * in this class. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class TagInfoset { aoqi@0: /** aoqi@0: * Namespace declarations on this tag. Read-only. aoqi@0: * aoqi@0: * This is an array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }. aoqi@0: * aoqi@0: * URIs/prefixes can be null (StAX-style) aoqi@0: */ aoqi@0: public final @NotNull String[] ns; aoqi@0: /** aoqi@0: * Attributes on this tag. Read-only. aoqi@0: */ aoqi@0: public final @NotNull AttributesImpl atts; aoqi@0: aoqi@0: /** aoqi@0: * Prefix of the start tag in stax-style. aoqi@0: */ aoqi@0: public final @Nullable String prefix; aoqi@0: aoqi@0: /** aoqi@0: * Namespace URI of the start tag in stax-style. aoqi@0: */ aoqi@0: public final @Nullable String nsUri; aoqi@0: aoqi@0: /** aoqi@0: * Local name of the start tag. aoqi@0: */ aoqi@0: public final @NotNull String localName; aoqi@0: aoqi@0: /** aoqi@0: * Lazily computed QName (i.e., "foo:bar") aoqi@0: */ aoqi@0: private @Nullable String qname; aoqi@0: aoqi@0: public TagInfoset(String nsUri, String localName, String prefix, AttributesImpl atts, String... ns) { aoqi@0: this.nsUri = nsUri; aoqi@0: this.prefix = prefix; aoqi@0: this.localName = localName; aoqi@0: this.atts = atts; aoqi@0: this.ns = ns; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Fills a {@link TagInfoset} object by the current element aoqi@0: * that the reader points to. aoqi@0: */ aoqi@0: public TagInfoset(XMLStreamReader reader) { aoqi@0: prefix = reader.getPrefix(); aoqi@0: nsUri = reader.getNamespaceURI(); aoqi@0: localName = reader.getLocalName(); aoqi@0: aoqi@0: int nsc = reader.getNamespaceCount(); aoqi@0: if(nsc>0) { aoqi@0: ns = new String[nsc*2]; aoqi@0: for(int i=0; i0) { aoqi@0: atts = new AttributesImpl(); aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: for(int i=0; i< ac;i++){ aoqi@0: sb.setLength(0); aoqi@0: String prefix = reader.getAttributePrefix(i); aoqi@0: String localName = reader.getAttributeLocalName(i); aoqi@0: aoqi@0: String qname; aoqi@0: if(prefix != null && prefix.length()!=0){ aoqi@0: sb.append(prefix); aoqi@0: sb.append(":"); aoqi@0: sb.append(localName); aoqi@0: qname = sb.toString(); aoqi@0: } else { aoqi@0: qname = localName; aoqi@0: } aoqi@0: aoqi@0: atts.addAttribute( aoqi@0: fixNull(reader.getAttributeNamespace(i)), aoqi@0: localName, aoqi@0: qname, aoqi@0: reader.getAttributeType(i), aoqi@0: reader.getAttributeValue(i)); aoqi@0: } aoqi@0: } else { aoqi@0: atts = EMPTY_ATTRIBUTES; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Writes the start element event. aoqi@0: */ aoqi@0: public void writeStart(ContentHandler contentHandler) throws SAXException { aoqi@0: for( int i=0; i=0; i-=2 ) { aoqi@0: contentHandler.endPrefixMapping(fixNull(ns[i])); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Writes the start element event. aoqi@0: */ aoqi@0: public void writeStart(XMLStreamWriter w) throws XMLStreamException { aoqi@0: // write start tag. aoqi@0: if(prefix==null) { aoqi@0: if(nsUri==null) aoqi@0: w.writeStartElement(localName); aoqi@0: else { aoqi@0: //fix Null prefix. otherwise throws XMLStreamException, aoqi@0: // if the namespace URI has not been bound to a prefix aoqi@0: w.writeStartElement("",localName,nsUri); aoqi@0: } aoqi@0: } else { aoqi@0: w.writeStartElement(prefix,localName,nsUri); aoqi@0: } aoqi@0: aoqi@0: for( int i=0; i allPrefixes(String namespaceURI) { aoqi@0: int size = ns.length/2; aoqi@0: List l = new java.util.ArrayList(); aoqi@0: for(int i=0; i