001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.wsdl.framework;
027:
028: import java.io.IOException;
029: import java.io.OutputStream;
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034:
035: import javax.xml.namespace.QName;
036:
037: import com.sun.xml.internal.ws.util.NamespaceSupport;
038: import com.sun.tools.internal.ws.util.xml.PrettyPrintingXmlWriter;
039:
040: /**
041: * The context used by writer classes.
042: *
043: * @author WS Development Team
044: */
045: public class WriterContext {
046:
047: public WriterContext(OutputStream os) throws IOException {
048: _writer = new PrettyPrintingXmlWriter(os);
049: _nsSupport = new NamespaceSupport();
050: _newPrefixCount = 2;
051: }
052:
053: public void flush() throws IOException {
054: _writer.flush();
055: }
056:
057: public void close() throws IOException {
058: _writer.close();
059: }
060:
061: public void push() {
062: if (_pendingNamespaceDeclarations != null) {
063: throw new IllegalStateException(
064: "prefix declarations are pending");
065: }
066: _nsSupport.pushContext();
067: }
068:
069: public void pop() {
070: _nsSupport.popContext();
071: _pendingNamespaceDeclarations = null;
072: }
073:
074: public String getNamespaceURI(String prefix) {
075: return _nsSupport.getURI(prefix);
076: }
077:
078: public Iterator getPrefixes() {
079: return _nsSupport.getPrefixes();
080: }
081:
082: public String getDefaultNamespaceURI() {
083: return getNamespaceURI("");
084: }
085:
086: public void declarePrefix(String prefix, String uri) {
087: _nsSupport.declarePrefix(prefix, uri);
088: if (_pendingNamespaceDeclarations == null) {
089: _pendingNamespaceDeclarations = new ArrayList();
090: }
091: _pendingNamespaceDeclarations.add(new String[] { prefix, uri });
092: }
093:
094: public String getPrefixFor(String uri) {
095: if ((getDefaultNamespaceURI() != null && getDefaultNamespaceURI()
096: .equals(uri))
097: || uri.equals("")) {
098: return "";
099: } else {
100: return _nsSupport.getPrefix(uri);
101: }
102: }
103:
104: public String findNewPrefix(String base) {
105: return base + Integer.toString(_newPrefixCount++);
106: }
107:
108: public String getTargetNamespaceURI() {
109: return _targetNamespaceURI;
110: }
111:
112: public void setTargetNamespaceURI(String uri) {
113: _targetNamespaceURI = uri;
114: }
115:
116: public void writeStartTag(QName name) throws IOException {
117: _writer.start(getQNameString(name));
118: }
119:
120: public void writeEndTag(QName name) throws IOException {
121: _writer.end(getQNameString(name));
122: }
123:
124: public void writeAttribute(String name, String value)
125: throws IOException {
126: if (value != null) {
127: _writer.attribute(name, value);
128: }
129: }
130:
131: public void writeAttribute(String name, QName value)
132: throws IOException {
133: if (value != null) {
134: _writer.attribute(name, getQNameString(value));
135: }
136: }
137:
138: public void writeAttribute(String name, boolean value)
139: throws IOException {
140: writeAttribute(name, value ? "true" : "false");
141: }
142:
143: public void writeAttribute(String name, Boolean value)
144: throws IOException {
145: if (value != null) {
146: writeAttribute(name, value.booleanValue());
147: }
148: }
149:
150: public void writeAttribute(String name, int value)
151: throws IOException {
152: writeAttribute(name, Integer.toString(value));
153: }
154:
155: public void writeAttribute(String name, Object value,
156: Map valueToXmlMap) throws IOException {
157: String actualValue = (String) valueToXmlMap.get(value);
158: writeAttribute(name, actualValue);
159: }
160:
161: public void writeNamespaceDeclaration(String prefix, String uri)
162: throws IOException {
163: _writer.attribute(getNamespaceDeclarationAttributeName(prefix),
164: uri);
165: }
166:
167: public void writeAllPendingNamespaceDeclarations()
168: throws IOException {
169: if (_pendingNamespaceDeclarations != null) {
170: for (Iterator iter = _pendingNamespaceDeclarations
171: .iterator(); iter.hasNext();) {
172: String[] pair = (String[]) iter.next();
173: writeNamespaceDeclaration(pair[0], pair[1]);
174: }
175: }
176: _pendingNamespaceDeclarations = null;
177: }
178:
179: private String getNamespaceDeclarationAttributeName(String prefix) {
180: if (prefix.equals("")) {
181: return "xmlns";
182: } else {
183: return "xmlns:" + prefix;
184: }
185: }
186:
187: public void writeTag(QName name, String value) throws IOException {
188: _writer.leaf(getQNameString(name), value);
189: }
190:
191: public String getQNameString(QName name) {
192: String nsURI = name.getNamespaceURI();
193: String prefix = getPrefixFor(nsURI);
194: if (prefix == null) {
195: throw new IllegalArgumentException();
196: } else if (prefix.equals("")) {
197: return name.getLocalPart();
198: } else {
199: return prefix + ":" + name.getLocalPart();
200: }
201: }
202:
203: public String getQNameStringWithTargetNamespaceCheck(QName name) {
204: if (name.getNamespaceURI().equals(_targetNamespaceURI)) {
205: return name.getLocalPart();
206: } else {
207: return getQNameString(name);
208: }
209: }
210:
211: public void writeChars(String chars) throws IOException {
212: _writer.chars(chars);
213: }
214:
215: private PrettyPrintingXmlWriter _writer;
216: private NamespaceSupport _nsSupport;
217: private String _targetNamespaceURI;
218: private int _newPrefixCount;
219: private List _pendingNamespaceDeclarations;
220: }
|