Skip to content

Commit 320dee5

Browse files
committed
1 parent 8aa15a0 commit 320dee5

File tree

2 files changed

+268
-0
lines changed

2 files changed

+268
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.io.ByteArrayOutputStream;
20+
import java.io.StringReader;
21+
22+
import org.apache.axiom.om.OMAbstractFactory;
23+
import org.apache.axiom.om.OMDocument;
24+
import org.apache.axiom.om.OMElement;
25+
import org.apache.axiom.om.OMFactory;
26+
import org.apache.axiom.om.OMNamespace;
27+
import org.custommonkey.xmlunit.XMLTestCase;
28+
import org.xml.sax.InputSource;
29+
import org.xml.sax.XMLReader;
30+
import org.xml.sax.helpers.XMLReaderFactory;
31+
32+
public class AxiomHandlerTest extends XMLTestCase {
33+
34+
private static final String XML_1 = "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" +
35+
"<root xmlns='namespace'>" +
36+
"<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
37+
"</root>";
38+
39+
private static final String XML_2_EXPECTED = "<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" +
40+
"<child xmlns='namespace2' />" + "</root>";
41+
42+
private static final String XML_2_SNIPPET =
43+
"<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />";
44+
45+
private AxiomHandler handler;
46+
47+
private OMDocument result;
48+
49+
private XMLReader xmlReader;
50+
51+
private OMFactory factory;
52+
53+
protected void setUp() throws Exception {
54+
factory = OMAbstractFactory.getOMFactory();
55+
result = factory.createOMDocument();
56+
xmlReader = XMLReaderFactory.createXMLReader();
57+
}
58+
59+
public void testContentHandlerDocumentNamespacePrefixes() throws Exception {
60+
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
61+
handler = new AxiomHandler(result, factory);
62+
xmlReader.setContentHandler(handler);
63+
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
64+
xmlReader.parse(new InputSource(new StringReader(XML_1)));
65+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
66+
result.serialize(bos);
67+
assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
68+
}
69+
70+
public void testContentHandlerDocumentNoNamespacePrefixes() throws Exception {
71+
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
72+
handler = new AxiomHandler(result, factory);
73+
xmlReader.setContentHandler(handler);
74+
xmlReader.parse(new InputSource(new StringReader(XML_1)));
75+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
76+
result.serialize(bos);
77+
assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
78+
}
79+
80+
public void testContentHandlerElement() throws Exception {
81+
OMNamespace namespace = factory.createOMNamespace("namespace", "");
82+
OMElement rootElement = factory.createOMElement("root", namespace, result);
83+
handler = new AxiomHandler(rootElement, factory);
84+
xmlReader.setContentHandler(handler);
85+
xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
86+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
87+
result.serialize(bos);
88+
assertXMLEqual("Invalid result", XML_2_EXPECTED, bos.toString("UTF-8"));
89+
}
90+
}

0 commit comments

Comments
 (0)