001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.wsdl;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Stack;
023:
024: import javax.wsdl.Binding;
025: import javax.wsdl.BindingFault;
026: import javax.wsdl.BindingInput;
027: import javax.wsdl.BindingOperation;
028: import javax.wsdl.BindingOutput;
029: import javax.wsdl.Definition;
030: import javax.wsdl.Message;
031: import javax.wsdl.Operation;
032: import javax.wsdl.Port;
033: import javax.wsdl.Service;
034: import javax.wsdl.Types;
035: import javax.xml.namespace.NamespaceContext;
036: import javax.xml.namespace.QName;
037: import javax.xml.stream.XMLStreamException;
038: import javax.xml.stream.XMLStreamWriter;
039:
040: public class PrettyPrintXMLStreamWriter implements XMLStreamWriter {
041:
042: static final Map<Class<?>, Integer> WSDL_INDENT_MAP = new HashMap<Class<?>, Integer>();
043: static final int DEFAULT_INDENT_LEVEL = 2;
044:
045: XMLStreamWriter baseWriter;
046:
047: int indent;
048: Stack<CurrentElement> elems = new Stack<CurrentElement>();
049: QName currElem;
050: boolean nestedStartElement;
051:
052: public PrettyPrintXMLStreamWriter(XMLStreamWriter writer,
053: Class<?> parent) {
054: baseWriter = writer;
055: indent = getIndentLevel(parent);
056: }
057:
058: public void writeSpaces() throws XMLStreamException {
059: for (int i = 0; i < indent; i++) {
060: baseWriter.writeCharacters(" ");
061: }
062: }
063:
064: public void indentWithSpaces() throws XMLStreamException {
065: writeSpaces();
066: indent();
067: }
068:
069: public void indent() {
070: indent += DEFAULT_INDENT_LEVEL;
071: }
072:
073: public void unindent() {
074: indent -= DEFAULT_INDENT_LEVEL;
075: }
076:
077: public void close() throws XMLStreamException {
078: baseWriter.close();
079: }
080:
081: public void flush() throws XMLStreamException {
082: baseWriter.flush();
083: }
084:
085: public NamespaceContext getNamespaceContext() {
086: return baseWriter.getNamespaceContext();
087: }
088:
089: public java.lang.String getPrefix(java.lang.String uri)
090: throws XMLStreamException {
091: return baseWriter.getPrefix(uri);
092: }
093:
094: public java.lang.Object getProperty(java.lang.String name)
095: throws IllegalArgumentException {
096: return baseWriter.getProperty(name);
097: }
098:
099: public void setDefaultNamespace(java.lang.String uri)
100: throws XMLStreamException {
101: baseWriter.setDefaultNamespace(uri);
102: }
103:
104: public void setNamespaceContext(NamespaceContext context)
105: throws XMLStreamException {
106: baseWriter.setNamespaceContext(context);
107: }
108:
109: public void setPrefix(java.lang.String prefix, java.lang.String uri)
110: throws XMLStreamException {
111: baseWriter.setPrefix(prefix, uri);
112: }
113:
114: public void writeAttribute(java.lang.String localName,
115: java.lang.String value) throws XMLStreamException {
116: writeAttribute(null, localName, value);
117: }
118:
119: public void writeAttribute(java.lang.String namespaceURI,
120: java.lang.String localName, java.lang.String value)
121: throws XMLStreamException {
122: writeAttribute(null, namespaceURI, localName, value);
123: }
124:
125: public void writeAttribute(java.lang.String prefix,
126: java.lang.String namespaceURI, java.lang.String localName,
127: java.lang.String value) throws XMLStreamException {
128: baseWriter.writeAttribute(prefix, namespaceURI, localName,
129: value);
130: }
131:
132: public void writeCData(java.lang.String data)
133: throws XMLStreamException {
134: baseWriter.writeCData(data);
135: }
136:
137: public void writeCharacters(char[] text, int start, int len)
138: throws XMLStreamException {
139: baseWriter.writeCharacters(text, start, len);
140: }
141:
142: public void writeCharacters(java.lang.String text)
143: throws XMLStreamException {
144: baseWriter.writeCharacters(text);
145: }
146:
147: public void writeComment(java.lang.String data)
148: throws XMLStreamException {
149: baseWriter.writeComment(data);
150: }
151:
152: public void writeDefaultNamespace(java.lang.String namespaceURI)
153: throws XMLStreamException {
154: baseWriter.writeDefaultNamespace(namespaceURI);
155: }
156:
157: public void writeDTD(java.lang.String dtd)
158: throws XMLStreamException {
159: baseWriter.writeDTD(dtd);
160: }
161:
162: public void writeEmptyElement(java.lang.String localName)
163: throws XMLStreamException {
164: baseWriter.writeEmptyElement(localName);
165: }
166:
167: public void writeEmptyElement(java.lang.String namespaceURI,
168: java.lang.String localName) throws XMLStreamException {
169: writeEmptyElement(null, namespaceURI, localName);
170: }
171:
172: public void writeEmptyElement(java.lang.String prefix,
173: java.lang.String localName, java.lang.String namespaceURI)
174: throws XMLStreamException {
175: baseWriter.writeEmptyElement(prefix, localName, namespaceURI);
176: }
177:
178: public void writeEndDocument() throws XMLStreamException {
179: baseWriter.writeEndDocument();
180: }
181:
182: public void writeEndElement() throws XMLStreamException {
183: CurrentElement elem = (CurrentElement) elems.pop();
184: unindent();
185: if (elem.hasChildElements()) {
186: baseWriter.writeCharacters("\n");
187: writeSpaces();
188: }
189: baseWriter.writeEndElement();
190: if (elems.empty()) {
191: baseWriter.writeCharacters("\n");
192: }
193: }
194:
195: public void writeEntityRef(java.lang.String name)
196: throws XMLStreamException {
197: baseWriter.writeEntityRef(name);
198: }
199:
200: public void writeNamespace(java.lang.String prefix,
201: java.lang.String namespaceURI) throws XMLStreamException {
202: baseWriter.writeNamespace(prefix, namespaceURI);
203: }
204:
205: public void writeProcessingInstruction(java.lang.String target)
206: throws XMLStreamException {
207: baseWriter.writeProcessingInstruction(target);
208: }
209:
210: public void writeProcessingInstruction(java.lang.String target,
211: java.lang.String data) throws XMLStreamException {
212: baseWriter.writeProcessingInstruction(target, data);
213: }
214:
215: public void writeStartDocument() throws XMLStreamException {
216: baseWriter.writeStartDocument();
217: }
218:
219: public void writeStartDocument(java.lang.String version)
220: throws XMLStreamException {
221: baseWriter.writeStartDocument(version);
222: }
223:
224: public void writeStartDocument(java.lang.String encoding,
225: java.lang.String version) throws XMLStreamException {
226: baseWriter.writeStartDocument(encoding, version);
227: }
228:
229: public void writeStartElement(java.lang.String localName)
230: throws XMLStreamException {
231: writeStartElement(null, null, localName);
232: }
233:
234: public void writeStartElement(java.lang.String namespaceURI,
235: java.lang.String localName) throws XMLStreamException {
236: writeStartElement(null, namespaceURI, localName);
237: }
238:
239: public void writeStartElement(java.lang.String prefix,
240: java.lang.String localName, java.lang.String namespaceURI)
241: throws XMLStreamException {
242: QName currElemName = new QName(namespaceURI, localName);
243: if (elems.empty()) {
244: indentWithSpaces();
245: } else {
246: baseWriter.writeCharacters("");
247: baseWriter.writeCharacters("\n");
248: indentWithSpaces();
249: CurrentElement elem = (CurrentElement) elems.peek();
250: elem.setChildElements(true);
251: }
252: baseWriter.writeStartElement(prefix, localName, namespaceURI);
253: elems.push(new CurrentElement(currElemName));
254: }
255:
256: private int getIndentLevel(Class<?> parent) {
257: Integer result = (Integer) WSDL_INDENT_MAP.get(parent);
258: if (result == null) {
259: return DEFAULT_INDENT_LEVEL;
260: }
261: return result.intValue();
262: }
263:
264: static {
265: WSDL_INDENT_MAP.put(Definition.class, new Integer(
266: DEFAULT_INDENT_LEVEL));
267: WSDL_INDENT_MAP.put(Binding.class, new Integer(
268: DEFAULT_INDENT_LEVEL * 2));
269: WSDL_INDENT_MAP.put(BindingFault.class, new Integer(
270: DEFAULT_INDENT_LEVEL * 3));
271: WSDL_INDENT_MAP.put(BindingInput.class, new Integer(
272: DEFAULT_INDENT_LEVEL * 3));
273: WSDL_INDENT_MAP.put(BindingOutput.class, new Integer(
274: DEFAULT_INDENT_LEVEL * 3));
275: WSDL_INDENT_MAP.put(BindingOperation.class, new Integer(
276: DEFAULT_INDENT_LEVEL * 3));
277: WSDL_INDENT_MAP.put(Message.class, new Integer(
278: DEFAULT_INDENT_LEVEL * 2));
279: WSDL_INDENT_MAP.put(Operation.class, new Integer(
280: DEFAULT_INDENT_LEVEL * 3));
281: WSDL_INDENT_MAP.put(Port.class, new Integer(
282: DEFAULT_INDENT_LEVEL * 3));
283: WSDL_INDENT_MAP.put(Service.class, new Integer(
284: DEFAULT_INDENT_LEVEL * 2));
285: WSDL_INDENT_MAP.put(Types.class, new Integer(
286: DEFAULT_INDENT_LEVEL * 2));
287: }
288:
289: class CurrentElement {
290: private QName name;
291: private boolean hasChildElements;
292:
293: CurrentElement(QName qname) {
294: name = qname;
295: }
296:
297: public QName getQName() {
298: return name;
299: }
300:
301: public boolean hasChildElements() {
302: return hasChildElements;
303: }
304:
305: public void setChildElements(boolean childElements) {
306: hasChildElements = childElements;
307: }
308: }
309:
310: }
|