1 9 10 package test.dom4j; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 import org.dom4j.Document; 16 import org.dom4j.io.*; 17 import org.w3c.tidy.Tidy; 18 19 import javax.xml.transform.Transformer ; 20 import javax.xml.transform.TransformerFactory ; 21 import javax.xml.transform.stream.StreamResult ; 22 import javax.xml.transform.stream.StreamSource ; 23 import java.io.*; 24 import java.net.URL ; 25 26 31 public class TestRoundTrip extends AbstractTestCase { 32 33 protected String [] testDocuments = { 34 "xml/fibo.xml", 36 "xml/test/schema/personal-prefix.xsd", 37 "xml/test/test_schema.xml", 39 }; 40 41 public static void main( String [] args ) { 42 TestRunner.run( suite() ); 43 } 44 45 public static Test suite() { 46 return new TestSuite( TestRoundTrip.class ); 47 } 48 49 public TestRoundTrip(String name) { 50 super(name); 51 } 52 53 public void testTextRoundTrip() throws Exception { 56 for ( int i = 0, size = testDocuments.length; i < size; i++ ) { 57 Document doc = parseDocument( testDocuments[i] ); 58 roundTripText( doc ); 59 } 60 } 61 62 public void testSAXRoundTrip() throws Exception { 63 for ( int i = 0, size = testDocuments.length; i < size; i++ ) { 64 Document doc = parseDocument( testDocuments[i] ); 65 roundTripSAX( doc ); 66 } 67 } 68 69 public void testDOMRoundTrip() throws Exception { 70 for ( int i = 0, size = testDocuments.length; i < size; i++ ) { 71 Document doc = parseDocument( testDocuments[i] ); 72 roundTripDOM( doc ); 73 } 74 } 75 76 public void testJAXPRoundTrip() throws Exception { 77 for ( int i = 0, size = testDocuments.length; i < size; i++ ) { 78 Document doc = parseDocument( testDocuments[i] ); 79 roundTripJAXP( doc ); 80 } 81 } 82 83 public void testFullRoundTrip() throws Exception { 84 for ( int i = 0, size = testDocuments.length; i < size; i++ ) { 85 Document doc = parseDocument( testDocuments[i] ); 86 roundTripFull( doc ); 87 } 88 } 89 90 public void testJTidyRoundTrip() throws Exception { 91 Document document = loadHTML( "xml/readme.html" ); 92 93 Document doc1 = roundTripSAX( document ); 95 Document doc2 = roundTripDOM( doc1); 96 Document doc3 = roundTripSAX( doc2 ); 97 Document doc5 = roundTripDOM( doc3 ); 100 101 assertDocumentsEqual( document, doc5 ); 102 } 103 104 protected void setUp() throws Exception { 107 } 108 109 protected Document parseDocument(String file) throws Exception { 110 SAXReader reader = new SAXReader(); 111 return reader.read( file ); 112 } 113 114 protected Document loadHTML( String xmlFile ) throws Exception { 115 InputStream in = openStream( xmlFile ); 116 Tidy tidy = new Tidy(); 117 tidy.setXHTML(true); 118 tidy.setDocType("omit"); 119 org.w3c.dom.Document domDocument = tidy.parseDOM( in, null ); 120 121 DOMReader domReader = new DOMReader(); 122 return domReader.read( domDocument ); 123 } 124 125 protected InputStream openStream(String xmlFile) throws Exception { 126 File file = new File( xmlFile ); 127 if ( file.exists() ) { 128 return new BufferedInputStream( new FileInputStream( file ) ); 129 } 130 try { 131 return new URL ( xmlFile ).openStream(); 132 } catch (IOException e) { 133 throw new FileNotFoundException("resource does not exist " + file.getAbsolutePath() + " and is not a URL"); 134 } 135 } 136 137 protected Document roundTripDOM(Document document) throws Exception { 138 DOMWriter domWriter = new DOMWriter(); 140 org.w3c.dom.Document domDocument = domWriter.write(document); 141 142 DOMReader domReader = new DOMReader(); 144 Document newDocument = domReader.read( domDocument ); 145 146 newDocument.setName( document.getName() ); 148 149 assertDocumentsEqual( document, newDocument ); 150 151 return newDocument; 152 } 153 154 protected Document roundTripJAXP(Document document) throws Exception { 155 TransformerFactory factory = TransformerFactory.newInstance(); 157 Transformer transformer = factory.newTransformer(); 158 159 StringWriter buffer = new StringWriter(); 160 StreamResult streamResult = new StreamResult (buffer); 161 DocumentSource documentSource = new DocumentSource(document); 162 163 transformer.transform(documentSource, streamResult); 164 165 DocumentResult documentResult = new DocumentResult(); 167 StreamSource streamSource = new StreamSource ( new StringReader( buffer.toString() ) ); 168 169 transformer.transform(streamSource, documentResult); 170 171 Document newDocument = documentResult.getDocument(); 172 173 newDocument.setName( document.getName() ); 175 176 assertDocumentsEqual( document, newDocument ); 177 178 return newDocument; 179 } 180 181 protected Document roundTripSAX(Document document) throws Exception { 182 183 SAXContentHandler contentHandler = new SAXContentHandler(); 186 SAXWriter saxWriter = new SAXWriter( contentHandler, null, contentHandler ); 187 188 saxWriter.write( document ); 189 Document newDocument = contentHandler.getDocument(); 190 191 newDocument.setName( document.getName() ); 193 194 assertDocumentsEqual( document, newDocument ); 195 196 return newDocument; 197 } 198 199 protected Document roundTripText(Document document) throws Exception { 200 StringWriter out = new StringWriter(); 201 XMLWriter xmlWriter = new XMLWriter(out); 202 203 xmlWriter.write( document ); 204 205 String xml = out.toString(); 207 208 StringReader in = new StringReader( xml ); 209 SAXReader reader = new SAXReader(); 210 Document newDocument = reader.read(in); 211 212 newDocument.setName( document.getName() ); 214 215 assertDocumentsEqual( document, newDocument ); 216 217 return newDocument; 218 } 219 220 protected Document roundTripFull(Document document) throws Exception { 221 Document doc2 = roundTripDOM( document ); 222 Document doc3 = roundTripSAX( doc2 ); 223 Document doc4 = roundTripText( doc3 ); 224 225 assertDocumentsEqual( document, doc4 ); 226 227 return doc4; 228 } 229 } 230 231 232 233 234 278 | Popular Tags |