Skip to content

Commit

Permalink
[playframework#1094] play.libs.XML.serialize method calls Sun proprie…
Browse files Browse the repository at this point in the history
…tary API

Merge remote-tracking branch '1094/lighthouse-1094-patch'
  • Loading branch information
pepite committed Oct 17, 2011
2 parents 2a5d998 + a1e478e commit e7cab59
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
20 changes: 14 additions & 6 deletions framework/src/play/libs/XML.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.security.Key;
import java.security.Provider;
Expand All @@ -27,6 +28,11 @@
import javax.xml.crypto.dsig.spec.TransformParameterSpec;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
Expand All @@ -35,9 +41,6 @@

import play.Logger;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.io.StringReader;

/**
* XML utils
*/
Expand All @@ -51,9 +54,14 @@ public class XML {
public static String serialize(Document document) {
StringWriter writer = new StringWriter();
try {
new XMLSerializer(writer, null).serialize(document);
} catch (IOException e) {
Logger.warn("Error when serializing xml Document.", e);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(writer);
transformer.transform(domSource, streamResult);
} catch (TransformerException e) {
throw new RuntimeException(
"Error when serializing XML document.", e);
}
return writer.toString();
}
Expand Down
43 changes: 43 additions & 0 deletions framework/test-src/play/libs/XMLTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package play.libs;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;

/**
* Tests for {@link XML} class.
*/
public class XMLTest {

private static final String ORIGINAL_DOCUMENT =
"<?xml version=\"1.0\"?>\n" +
"<feed xmlns=\"http://www.w3.org/2005/Atom\">" +
"<title>Awesome Blog</title>" +
"<link href=\"http://blog.example.com/\"/>" +
"</feed>";
private Document document;

@Before
public void setUp() throws Exception {
document = XML.getDocument(ORIGINAL_DOCUMENT);
}

private static String stripPreamble(String text) {
return text.replaceFirst("<\\?[^?]+\\?>\\s*", "");
}

@Test
public void serializeShouldReturnWellFormedXml() throws Exception {
String outputDocument = XML.serialize(document);
assertEquals(
stripPreamble(ORIGINAL_DOCUMENT),
stripPreamble(outputDocument));
}
}

0 comments on commit e7cab59

Please sign in to comment.