001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.soap.saaj.support;
018:
019: import java.util.Iterator;
020: import javax.xml.soap.Name;
021: import javax.xml.soap.Node;
022: import javax.xml.soap.SOAPElement;
023: import javax.xml.soap.Text;
024:
025: import org.springframework.xml.sax.AbstractXmlReader;
026: import org.xml.sax.Attributes;
027: import org.xml.sax.InputSource;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.helpers.AttributesImpl;
030:
031: /**
032: * SAX <code>XMLReader</code> that reads from a SAAJ <code>Node</code>. Consumes <code>XMLEvents</code> from an
033: * <code>XMLEventReader</code>, and calls the corresponding methods on the SAX callback interfaces.
034: *
035: * @author Arjen Poutsma
036: * @see javax.xml.soap.Node
037: * @see javax.xml.soap.SOAPElement
038: * @since 1.0.0
039: */
040: public class SaajXmlReader extends AbstractXmlReader {
041:
042: private final Node startNode;
043:
044: /**
045: * Constructs a new instance of the <code>SaajXmlReader</code> that reads from the given <code>Node</code>.
046: *
047: * @param startNode the SAAJ <code>Node</code> to read from
048: */
049: public SaajXmlReader(Node startNode) {
050: this .startNode = startNode;
051: }
052:
053: /**
054: * Parses the StAX XML reader passed at construction-time.
055: * <p/>
056: * <strong>Note</strong> that the given <code>InputSource</code> is not read, but ignored.
057: *
058: * @param ignored is ignored
059: * @throws org.xml.sax.SAXException A SAX exception, possibly wrapping a <code>XMLStreamException</code>
060: */
061: public final void parse(InputSource ignored) throws SAXException {
062: parse();
063: }
064:
065: /**
066: * Parses the StAX XML reader passed at construction-time.
067: * <p/>
068: * <strong>Note</strong> that the given system identifier is not read, but ignored.
069: *
070: * @param ignored is ignored
071: * @throws SAXException A SAX exception, possibly wrapping a <code>XMLStreamException</code>
072: */
073: public final void parse(String ignored) throws SAXException {
074: parse();
075: }
076:
077: private void parse() throws SAXException {
078: if (getContentHandler() != null) {
079: getContentHandler().startDocument();
080: }
081: handleNode(startNode);
082: if (getContentHandler() != null) {
083: getContentHandler().endDocument();
084: }
085: }
086:
087: private void handleNode(Node node) throws SAXException {
088: if (node instanceof SOAPElement) {
089: handleElement((SOAPElement) node);
090: } else if (node instanceof Text) {
091: Text text = (Text) node;
092: handleText(text);
093: }
094: }
095:
096: private void handleText(Text text) throws SAXException {
097: if (getContentHandler() != null) {
098: char[] ch = text.getValue().toCharArray();
099: getContentHandler().characters(ch, 0, ch.length);
100: }
101: }
102:
103: private void handleElement(SOAPElement element) throws SAXException {
104: Name elementName = element.getElementName();
105: if (getContentHandler() != null) {
106: for (Iterator iterator = element.getNamespacePrefixes(); iterator
107: .hasNext();) {
108: String prefix = (String) iterator.next();
109: String namespaceUri = element.getNamespaceURI(prefix);
110: getContentHandler().startPrefixMapping(prefix,
111: namespaceUri);
112: }
113: getContentHandler().startElement(elementName.getURI(),
114: elementName.getLocalName(),
115: elementName.getQualifiedName(),
116: getAttributes(element));
117: }
118: for (Iterator iterator = element.getChildElements(); iterator
119: .hasNext();) {
120: Node child = (Node) iterator.next();
121: handleNode(child);
122: }
123: if (getContentHandler() != null) {
124: getContentHandler().endElement(elementName.getURI(),
125: elementName.getLocalName(),
126: elementName.getQualifiedName());
127: for (Iterator iterator = element.getNamespacePrefixes(); iterator
128: .hasNext();) {
129: String prefix = (String) iterator.next();
130: getContentHandler().endPrefixMapping(prefix);
131: }
132: }
133: }
134:
135: private Attributes getAttributes(SOAPElement element) {
136: AttributesImpl attributes = new AttributesImpl();
137: for (Iterator iterator = element.getAllAttributes(); iterator
138: .hasNext();) {
139: Name attributeName = (Name) iterator.next();
140: String attributeValue = element
141: .getAttributeValue(attributeName);
142: attributes.addAttribute(attributeName.getURI(),
143: attributeName.getLocalName(), attributeName
144: .getQualifiedName(), "CDATA",
145: attributeValue);
146: }
147: return attributes;
148: }
149:
150: }
|