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.parser;
027:
028: import java.io.IOException;
029: import java.io.OutputStream;
030: import java.util.Iterator;
031:
032: import javax.xml.namespace.QName;
033:
034: import com.sun.tools.internal.ws.wsdl.document.schema.Schema;
035: import com.sun.tools.internal.ws.wsdl.document.schema.SchemaAttribute;
036: import com.sun.tools.internal.ws.wsdl.document.schema.SchemaDocument;
037: import com.sun.tools.internal.ws.wsdl.document.schema.SchemaElement;
038: import com.sun.tools.internal.ws.wsdl.framework.WriterContext;
039:
040: /**
041: * A writer for XML Schema fragments within a WSDL document.
042: *
043: * @author WS Development Team
044: */
045: public class SchemaWriter {
046:
047: public SchemaWriter() {
048: }
049:
050: public void write(SchemaDocument document, OutputStream os)
051: throws IOException {
052: WriterContext context = new WriterContext(os);
053: writeSchema(context, document.getSchema());
054: context.flush();
055: }
056:
057: public void writeSchema(WriterContext context, Schema schema)
058: throws IOException {
059: context.push();
060: try {
061: writeTopSchemaElement(context, schema);
062: } catch (Exception e) {
063: } finally {
064: context.pop();
065: }
066: }
067:
068: protected void writeTopSchemaElement(WriterContext context,
069: Schema schema) throws IOException {
070: SchemaElement schemaElement = schema.getContent();
071: QName name = schemaElement.getQName();
072:
073: // make sure that all namespaces we expect are actually declared
074: for (Iterator iter = schema.prefixes(); iter.hasNext();) {
075: String prefix = (String) iter.next();
076: String expectedURI = schema.getURIForPrefix(prefix);
077: if (!expectedURI.equals(context.getNamespaceURI(prefix))) {
078: context.declarePrefix(prefix, expectedURI);
079: }
080: }
081:
082: for (Iterator iter = schemaElement.prefixes(); iter.hasNext();) {
083: String prefix = (String) iter.next();
084: String uri = schemaElement.getURIForPrefix(prefix);
085: context.declarePrefix(prefix, uri);
086: }
087:
088: context.writeStartTag(name);
089:
090: for (Iterator iter = schemaElement.attributes(); iter.hasNext();) {
091: SchemaAttribute attribute = (SchemaAttribute) iter.next();
092: if (attribute.getNamespaceURI() == null) {
093: context.writeAttribute(attribute.getLocalName(),
094: attribute.getValue(context));
095: } else {
096: context.writeAttribute(context.getQNameString(attribute
097: .getQName()), attribute.getValue(context));
098: }
099: }
100:
101: context.writeAllPendingNamespaceDeclarations();
102:
103: for (Iterator iter = schemaElement.children(); iter.hasNext();) {
104: SchemaElement child = (SchemaElement) iter.next();
105: writeSchemaElement(context, child);
106: }
107:
108: context.writeEndTag(name);
109: }
110:
111: protected void writeSchemaElement(WriterContext context,
112: SchemaElement schemaElement) throws IOException {
113: QName name = schemaElement.getQName();
114:
115: if (schemaElement.declaresPrefixes()) {
116: context.push();
117: }
118:
119: context.writeStartTag(name);
120:
121: if (schemaElement.declaresPrefixes()) {
122: for (Iterator iter = schemaElement.prefixes(); iter
123: .hasNext();) {
124: String prefix = (String) iter.next();
125: String uri = schemaElement.getURIForPrefix(prefix);
126: context.writeNamespaceDeclaration(prefix, uri);
127: context.declarePrefix(prefix, uri);
128: }
129: }
130:
131: for (Iterator iter = schemaElement.attributes(); iter.hasNext();) {
132: SchemaAttribute attribute = (SchemaAttribute) iter.next();
133: if (attribute.getNamespaceURI() == null) {
134: context.writeAttribute(attribute.getLocalName(),
135: attribute.getValue(context));
136: } else {
137: context.writeAttribute(context.getQNameString(attribute
138: .getQName()), attribute.getValue(context));
139: }
140: }
141:
142: for (Iterator iter = schemaElement.children(); iter.hasNext();) {
143: SchemaElement child = (SchemaElement) iter.next();
144: writeSchemaElement(context, child);
145: }
146:
147: context.writeEndTag(name);
148:
149: if (schemaElement.declaresPrefixes()) {
150: context.pop();
151: }
152: }
153: }
|