-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
xml/src/main/java/org/springframework/xml/dom/DomUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2007 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.xml.dom; | ||
|
||
import javax.xml.transform.Source; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.dom.DOMResult; | ||
import javax.xml.transform.dom.DOMSource; | ||
|
||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
|
||
/** | ||
* Convenient utility methods for dealing with DOM. | ||
* | ||
* @author Arjen Poutsma | ||
*/ | ||
public abstract class DomUtils { | ||
|
||
/** | ||
* Returns the root element of the given source, transforming it if necessary. | ||
* | ||
* @param source the source to get the root element from | ||
* @param transformer a transformer | ||
* @return the root element | ||
*/ | ||
public static Element getRootElement(Source source, Transformer transformer) throws TransformerException { | ||
if (source instanceof DOMSource) { | ||
DOMSource domSource = (DOMSource) source; | ||
Node node = domSource.getNode(); | ||
if (node == null) { | ||
return null; | ||
} | ||
else if (node.getNodeType() == Node.ELEMENT_NODE) { | ||
return (Element) node; | ||
} | ||
else if (node.getNodeType() == Node.DOCUMENT_NODE) { | ||
Document document = (Document) node; | ||
return document.getDocumentElement(); | ||
} | ||
} | ||
DOMResult domResult = new DOMResult(); | ||
transformer.transform(source, domResult); | ||
Document document = (Document) domResult.getNode(); | ||
return document.getDocumentElement(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
Provides classes that help with DOM: the Document Object Model. Mostly for internal use by the framework. | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
xml/src/test/java/org/springframework/xml/dom/DomUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2007 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.xml.dom; | ||
|
||
import java.io.StringReader; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.transform.Source; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.dom.DOMSource; | ||
import javax.xml.transform.sax.SAXSource; | ||
import javax.xml.transform.stream.StreamSource; | ||
|
||
import junit.framework.TestCase; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.xml.sax.InputSource; | ||
|
||
public class DomUtilsTest extends TestCase { | ||
|
||
private Transformer transformer; | ||
|
||
private static final String NAMESPACE = "http://springframework.org/spring-ws"; | ||
|
||
private static final String LOCAL_NAME = "Root"; | ||
|
||
private static final String XML = "<" + LOCAL_NAME + " xmlns='" + NAMESPACE + "'/>"; | ||
|
||
protected void setUp() throws Exception { | ||
transformer = TransformerFactory.newInstance().newTransformer(); | ||
} | ||
|
||
public void testGetRootElementDomSource() throws Exception { | ||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | ||
documentBuilderFactory.setNamespaceAware(true); | ||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); | ||
Document document = documentBuilder.newDocument(); | ||
Element rootElement = document.createElementNS(NAMESPACE, LOCAL_NAME); | ||
document.appendChild(rootElement); | ||
|
||
testSource(new DOMSource(document)); | ||
} | ||
|
||
public void testGetRootElementSaxSource() throws Exception { | ||
InputSource inputSource = new InputSource(new StringReader(XML)); | ||
testSource(new SAXSource(inputSource)); | ||
} | ||
|
||
public void testGetRootElementStreamSource() throws Exception { | ||
testSource(new StreamSource(new StringReader(XML))); | ||
} | ||
|
||
private void testSource(Source source) throws TransformerException { | ||
Element result = DomUtils.getRootElement(source, transformer); | ||
assertNotNull("No result", result); | ||
assertEquals("Invalid namespace", NAMESPACE, result.getNamespaceURI()); | ||
assertEquals("Invalid local name", LOCAL_NAME, result.getLocalName()); | ||
} | ||
} |