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.aegis.util.jdom;
019:
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javax.xml.stream.XMLStreamException;
024: import javax.xml.stream.XMLStreamWriter;
025:
026: import org.jdom.Attribute;
027: import org.jdom.CDATA;
028: import org.jdom.Comment;
029: import org.jdom.Content;
030: import org.jdom.Document;
031: import org.jdom.Element;
032: import org.jdom.EntityRef;
033: import org.jdom.Namespace;
034: import org.jdom.Text;
035:
036: public class StaxSerializer {
037: public void writeDocument(Document doc, XMLStreamWriter writer)
038: throws XMLStreamException {
039: writer.writeStartDocument("1.0");
040:
041: for (Iterator itr = doc.getContent().iterator(); itr.hasNext();) {
042: Content content = (Content) itr.next();
043:
044: if (content instanceof Element) {
045: writeElement((Element) content, writer);
046: }
047: }
048:
049: writer.writeEndDocument();
050: }
051:
052: public void writeElement(Element e, XMLStreamWriter writer)
053: throws XMLStreamException {
054: // need to check if the namespace is declared before we write the
055: // start element because that will put the namespace in the context.
056: String elPrefix = e.getNamespacePrefix();
057: String elUri = e.getNamespaceURI();
058:
059: String boundPrefix = writer.getPrefix(elUri);
060: boolean writeElementNS = false;
061: if (boundPrefix == null || !elPrefix.equals(boundPrefix)) {
062: writeElementNS = true;
063: }
064:
065: writer.writeStartElement(elPrefix, e.getName(), elUri);
066:
067: List namespaces = e.getAdditionalNamespaces();
068: for (Iterator itr = namespaces.iterator(); itr.hasNext();) {
069: Namespace ns = (Namespace) itr.next();
070:
071: String prefix = ns.getPrefix();
072: String uri = ns.getURI();
073:
074: writer.setPrefix(prefix, uri);
075: writer.writeNamespace(prefix, uri);
076:
077: if (elUri.equals(uri) && elPrefix.equals(prefix)) {
078: writeElementNS = false;
079: }
080: }
081:
082: for (Iterator itr = e.getAttributes().iterator(); itr.hasNext();) {
083: Attribute attr = (Attribute) itr.next();
084: String attPrefix = attr.getNamespacePrefix();
085: String attUri = attr.getNamespaceURI();
086:
087: if (attUri == null || attUri.equals("")) {
088: writer.writeAttribute(attr.getName(), attr.getValue());
089: } else {
090: writer.writeAttribute(attPrefix, attUri,
091: attr.getName(), attr.getValue());
092:
093: if (!isDeclared(writer, attPrefix, attUri)) {
094: if (elUri.equals(attUri)
095: && elPrefix.equals(attPrefix)) {
096: if (writeElementNS) {
097: writer.setPrefix(attPrefix, attUri);
098: writer.writeNamespace(attPrefix, attUri);
099: writeElementNS = false;
100: }
101: } else {
102: writer.setPrefix(attPrefix, attUri);
103: writer.writeNamespace(attPrefix, attUri);
104: }
105: }
106: }
107: }
108:
109: if (writeElementNS) {
110: if (elPrefix == null || elPrefix.length() == 0) {
111: writer.writeDefaultNamespace(elUri);
112: } else {
113: writer.writeNamespace(elPrefix, elUri);
114: }
115: }
116:
117: for (Iterator itr = e.getContent().iterator(); itr.hasNext();) {
118: Content n = (Content) itr.next();
119: if (n instanceof CDATA) {
120: writer.writeCData(n.getValue());
121: } else if (n instanceof Text) {
122: writer.writeCharacters(((Text) n).getText());
123: } else if (n instanceof Element) {
124: writeElement((Element) n, writer);
125: } else if (n instanceof Comment) {
126: writer.writeComment(n.getValue());
127: } else if (n instanceof EntityRef) {
128: // EntityRef ref = (EntityRef) n;
129: // writer.writeEntityRef(ref.)
130: }
131: }
132:
133: writer.writeEndElement();
134: }
135:
136: /**
137: * @param writer
138: * @param prefix
139: * @param uri
140: * @throws XMLStreamException
141: */
142: private boolean isDeclared(XMLStreamWriter writer, String prefix,
143: String uri) throws XMLStreamException {
144: for (Iterator pxs = writer.getNamespaceContext().getPrefixes(
145: uri); pxs.hasNext();) {
146: String px = (String) pxs.next();
147: if (px.equals(prefix)) {
148: return true;
149: }
150: }
151: return false;
152: }
153: }
|