|
| 1 | +/* |
| 2 | + * Copyright 2006 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ws.soap.axiom; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import javax.xml.namespace.QName; |
| 25 | +import javax.xml.stream.XMLStreamConstants; |
| 26 | + |
| 27 | +import org.apache.axiom.om.OMAttribute; |
| 28 | +import org.apache.axiom.om.OMContainer; |
| 29 | +import org.apache.axiom.om.OMElement; |
| 30 | +import org.apache.axiom.om.OMFactory; |
| 31 | +import org.apache.axiom.om.OMNamespace; |
| 32 | +import org.xml.sax.Attributes; |
| 33 | +import org.xml.sax.ContentHandler; |
| 34 | +import org.xml.sax.Locator; |
| 35 | +import org.xml.sax.SAXException; |
| 36 | +import org.xml.sax.ext.LexicalHandler; |
| 37 | + |
| 38 | +import org.springframework.util.Assert; |
| 39 | +import org.springframework.xml.namespace.QNameUtils; |
| 40 | + |
| 41 | +/** |
| 42 | + * Specific SAX {@link ContentHandler} and {@link LexicalHandler} that adds the resulting AXIOM OMElement to a specified |
| 43 | + * parent element when <code>endDocument</code> is called. Used for returing <code>SAXResult</code>s from Axiom |
| 44 | + * elements. |
| 45 | + * |
| 46 | + * @author Arjen Poutsma |
| 47 | + * @since 1.0.0 |
| 48 | + */ |
| 49 | +class AxiomHandler implements ContentHandler, LexicalHandler { |
| 50 | + |
| 51 | + private final OMFactory factory; |
| 52 | + |
| 53 | + private final List elements = new ArrayList(); |
| 54 | + |
| 55 | + private Map namespaces = new HashMap(); |
| 56 | + |
| 57 | + private final OMContainer container; |
| 58 | + |
| 59 | + private int charactersType = XMLStreamConstants.CHARACTERS; |
| 60 | + |
| 61 | + AxiomHandler(OMContainer container, OMFactory factory) { |
| 62 | + Assert.notNull(container, "'container' must not be null"); |
| 63 | + Assert.notNull(factory, "'factory' must not be null"); |
| 64 | + this.factory = factory; |
| 65 | + this.container = container; |
| 66 | + } |
| 67 | + |
| 68 | + private OMContainer getParent() { |
| 69 | + if (!elements.isEmpty()) { |
| 70 | + return (OMContainer) elements.get(elements.size() - 1); |
| 71 | + } |
| 72 | + else { |
| 73 | + return container; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public void startPrefixMapping(String prefix, String uri) throws SAXException { |
| 78 | + namespaces.put(prefix, uri); |
| 79 | + } |
| 80 | + |
| 81 | + public void endPrefixMapping(String prefix) throws SAXException { |
| 82 | + namespaces.remove(prefix); |
| 83 | + } |
| 84 | + |
| 85 | + public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { |
| 86 | + OMContainer parent = getParent(); |
| 87 | + OMElement element = factory.createOMElement(localName, null, parent); |
| 88 | + for (Iterator iterator = namespaces.entrySet().iterator(); iterator.hasNext();) { |
| 89 | + Map.Entry entry = (Map.Entry) iterator.next(); |
| 90 | + String prefix = (String) entry.getKey(); |
| 91 | + if (prefix.length() == 0) { |
| 92 | + element.declareDefaultNamespace((String) entry.getValue()); |
| 93 | + } |
| 94 | + else { |
| 95 | + element.declareNamespace((String) entry.getValue(), prefix); |
| 96 | + } |
| 97 | + } |
| 98 | + QName qname = QNameUtils.toQName(uri, qName); |
| 99 | + element.setLocalName(qname.getLocalPart()); |
| 100 | + element.setNamespace(element.findNamespace(qname.getNamespaceURI(), qname.getPrefix())); |
| 101 | + for (int i = 0; i < atts.getLength(); i++) { |
| 102 | + QName attrName = QNameUtils.toQName(atts.getURI(i), atts.getQName(i)); |
| 103 | + String value = atts.getValue(i); |
| 104 | + if (!atts.getQName(i).startsWith("xmlns")) { |
| 105 | + OMNamespace namespace = factory.createOMNamespace(attrName.getNamespaceURI(), attrName.getPrefix()); |
| 106 | + OMAttribute attribute = factory.createOMAttribute(attrName.getLocalPart(), namespace, value); |
| 107 | + element.addAttribute(attribute); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + elements.add(element); |
| 112 | + } |
| 113 | + |
| 114 | + public void endElement(String uri, String localName, String qName) throws SAXException { |
| 115 | + elements.remove(elements.size() - 1); |
| 116 | + } |
| 117 | + |
| 118 | + public void characters(char ch[], int start, int length) throws SAXException { |
| 119 | + String data = new String(ch, start, length); |
| 120 | + OMContainer parent = getParent(); |
| 121 | + factory.createOMText(parent, data, charactersType); |
| 122 | + } |
| 123 | + |
| 124 | + public void ignorableWhitespace(char ch[], int start, int length) throws SAXException { |
| 125 | + charactersType = XMLStreamConstants.SPACE; |
| 126 | + characters(ch, start, length); |
| 127 | + charactersType = XMLStreamConstants.CHARACTERS; |
| 128 | + } |
| 129 | + |
| 130 | + public void processingInstruction(String target, String data) throws SAXException { |
| 131 | + OMContainer parent = getParent(); |
| 132 | + factory.createOMProcessingInstruction(parent, target, data); |
| 133 | + } |
| 134 | + |
| 135 | + public void comment(char ch[], int start, int length) throws SAXException { |
| 136 | + String content = new String(ch, start, length); |
| 137 | + OMContainer parent = getParent(); |
| 138 | + factory.createOMComment(parent, content); |
| 139 | + } |
| 140 | + |
| 141 | + public void startCDATA() throws SAXException { |
| 142 | + charactersType = XMLStreamConstants.CDATA; |
| 143 | + } |
| 144 | + |
| 145 | + public void endCDATA() throws SAXException { |
| 146 | + charactersType = XMLStreamConstants.CHARACTERS; |
| 147 | + } |
| 148 | + |
| 149 | + public void startEntity(String name) throws SAXException { |
| 150 | + charactersType = XMLStreamConstants.ENTITY_REFERENCE; |
| 151 | + } |
| 152 | + |
| 153 | + public void endEntity(String name) throws SAXException { |
| 154 | + charactersType = XMLStreamConstants.CHARACTERS; |
| 155 | + } |
| 156 | + |
| 157 | + /* |
| 158 | + * Unsupported |
| 159 | + */ |
| 160 | + |
| 161 | + public void setDocumentLocator(Locator locator) { |
| 162 | + } |
| 163 | + |
| 164 | + public void startDocument() throws SAXException { |
| 165 | + } |
| 166 | + |
| 167 | + public void endDocument() throws SAXException { |
| 168 | + } |
| 169 | + |
| 170 | + public void skippedEntity(String name) throws SAXException { |
| 171 | + } |
| 172 | + |
| 173 | + public void startDTD(String name, String publicId, String systemId) throws SAXException { |
| 174 | + } |
| 175 | + |
| 176 | + public void endDTD() throws SAXException { |
| 177 | + } |
| 178 | +} |
0 commit comments