001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.encoding;
038:
039: import org.xml.sax.helpers.AttributesImpl;
040: import org.xml.sax.ContentHandler;
041: import org.xml.sax.SAXException;
042:
043: import javax.xml.stream.XMLStreamReader;
044: import javax.xml.stream.XMLStreamWriter;
045: import javax.xml.stream.XMLStreamException;
046:
047: import com.sun.xml.ws.message.stream.StreamMessage;
048: import com.sun.xml.ws.encoding.StreamSOAPCodec;
049: import com.sun.istack.Nullable;
050: import com.sun.istack.NotNull;
051:
052: /**
053: * Complete infoset about a start tag.
054: *
055: * <p>
056: * This is used between {@link StreamMessage} and {@link StreamSOAPCodec}
057: * to capture the infoset of the s:Envelope, s:Header, and s:Body elements.
058: *
059: *
060: * <h3>Design Note</h3>
061: * <p>
062: * Since StAX and SAX uses different null vs empty string convention, one has
063: * to choose which format we store things. It can go either way, but I'm assuming
064: * that we'll be using StAX more in JAX-WS, so things are kept in the StAX style
065: * in this class.
066: *
067: * @author Kohsuke Kawaguchi
068: */
069: public final class TagInfoset {
070: /**
071: * Namespace declarations on this tag. Read-only.
072: *
073: * URIs/prefixes can be null (StAX-style)
074: */
075: public final @NotNull
076: String[] ns;
077: /**
078: * Attributes on this tag. Read-only.
079: */
080: public final @NotNull
081: AttributesImpl atts;
082:
083: /**
084: * Prefix of the start tag in stax-style.
085: */
086: public final @Nullable
087: String prefix;
088:
089: /**
090: * Namespace URI of the start tag in stax-style.
091: */
092: public final @Nullable
093: String nsUri;
094:
095: /**
096: * Local name of the start tag.
097: */
098: public final @NotNull
099: String localName;
100:
101: /**
102: * Lazily computed QName (i.e., "foo:bar")
103: */
104: private @Nullable
105: String qname;
106:
107: public TagInfoset(String nsUri, String localName, String prefix,
108: AttributesImpl atts, String... ns) {
109: this .nsUri = nsUri;
110: this .prefix = prefix;
111: this .localName = localName;
112: this .atts = atts;
113: this .ns = ns;
114: }
115:
116: /**
117: * Fills a {@link TagInfoset} object by the current element
118: * that the reader points to.
119: */
120: public TagInfoset(XMLStreamReader reader) {
121: prefix = reader.getPrefix();
122: nsUri = reader.getNamespaceURI();
123: localName = reader.getLocalName();
124:
125: int nsc = reader.getNamespaceCount();
126: if (nsc > 0) {
127: ns = new String[nsc * 2];
128: for (int i = 0; i < nsc; i++) {
129: ns[i * 2] = fixNull(reader.getNamespacePrefix(i));
130: ns[i * 2 + 1] = fixNull(reader.getNamespaceURI(i));
131: }
132: } else {
133: ns = EMPTY_ARRAY;
134: }
135:
136: int ac = reader.getAttributeCount();
137: if (ac > 0) {
138: atts = new AttributesImpl();
139: StringBuilder sb = new StringBuilder();
140: for (int i = 0; i < ac; i++) {
141: sb.setLength(0);
142: String prefix = reader.getAttributePrefix(i);
143: String localName = reader.getAttributeLocalName(i);
144:
145: String qname;
146: if (prefix != null && prefix.length() != 0) {
147: sb.append(prefix);
148: sb.append(":");
149: sb.append(localName);
150: qname = sb.toString();
151: } else {
152: qname = localName;
153: }
154:
155: atts.addAttribute(fixNull(reader
156: .getAttributeNamespace(i)), localName, qname,
157: reader.getAttributeType(i), reader
158: .getAttributeValue(i));
159: }
160: } else {
161: atts = EMPTY_ATTRIBUTES;
162: }
163: }
164:
165: /**
166: * Writes the start element event.
167: */
168: public void writeStart(ContentHandler contentHandler)
169: throws SAXException {
170: for (int i = 0; i < ns.length; i += 2)
171: contentHandler.startPrefixMapping(fixNull(ns[i]),
172: fixNull(ns[i + 1]));
173: contentHandler.startElement(fixNull(nsUri), localName,
174: getQName(), atts);
175: }
176:
177: /**
178: * Writes the end element event.
179: */
180: public void writeEnd(ContentHandler contentHandler)
181: throws SAXException {
182: contentHandler
183: .endElement(fixNull(nsUri), localName, getQName());
184: for (int i = ns.length - 2; i >= 0; i -= 2) {
185: contentHandler.endPrefixMapping(fixNull(ns[i]));
186: }
187: }
188:
189: /**
190: * Writes the start element event.
191: */
192: public void writeStart(XMLStreamWriter w) throws XMLStreamException {
193: // write start tag. Arrrgggghhh!!
194: if (prefix == null) {
195: if (nsUri == null)
196: w.writeStartElement(localName);
197: else
198: w.writeStartElement(nsUri, localName);
199: } else {
200: w.writeStartElement(prefix, localName, nsUri);
201: }
202:
203: for (int i = 0; i < ns.length; i += 2) {
204: w.writeNamespace(ns[i], ns[i + 1]);
205: }
206:
207: for (int i = 0; i < atts.getLength(); i++) {
208: String nsUri = atts.getURI(i);
209: if (nsUri == null || nsUri.length() == 0) {
210: w
211: .writeAttribute(atts.getLocalName(i), atts
212: .getValue(i));
213: } else {
214: String rawName = atts.getQName(i);
215: String prefix = rawName.substring(0, rawName
216: .indexOf(':'));
217: w.writeAttribute(prefix, nsUri, atts.getLocalName(i),
218: atts.getValue(i));
219: }
220: }
221: }
222:
223: private String getQName() {
224: if (qname != null)
225: return qname;
226:
227: StringBuilder sb = new StringBuilder();
228: if (prefix != null) {
229: sb.append(prefix);
230: sb.append(':');
231: sb.append(localName);
232: qname = sb.toString();
233: } else {
234: qname = localName;
235: }
236: return qname;
237: }
238:
239: private static String fixNull(String s) {
240: if (s == null)
241: return "";
242: else
243: return s;
244: }
245:
246: private static final String[] EMPTY_ARRAY = new String[0];
247: private static final AttributesImpl EMPTY_ATTRIBUTES = new AttributesImpl();
248: }
|