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.DocumentResult; 17 import org.dom4j.io.DocumentSource; 18 import org.dom4j.io.SAXReader; 19 20 import javax.xml.transform.Source ; 21 import javax.xml.transform.Transformer ; 22 import javax.xml.transform.TransformerFactory ; 23 import javax.xml.transform.stream.StreamSource ; 24 import java.util.List ; 25 26 31 public class TestXSLT extends AbstractTestCase { 32 33 public static void main( String [] args ) { 34 TestRunner.run( suite() ); 35 } 36 37 public static Test suite() { 38 return new TestSuite( TestXSLT.class ); 39 } 40 41 public TestXSLT(String name) { 42 super(name); 43 } 44 45 public void testTransform() throws Exception { 48 Document transformedDoc = transform( "xml/nitf/ashtml.xsl" ); 49 50 52 assertTrue( "Transformed Document is not null", transformedDoc != null ); 53 54 List h1List = transformedDoc.selectNodes( "/html//h1" ); 55 56 assertTrue( "At least one <h1>", h1List.size() > 0 ); 57 58 List pList = transformedDoc.selectNodes( "//p" ); 59 60 assertTrue( "At least one <p>", pList.size() > 0 ); 61 } 62 63 protected void setUp() throws Exception { 66 SAXReader reader = new SAXReader(); 67 document = reader.read( "xml/nitf/sample.xml" ); 68 } 69 70 protected Document transform(String xsl) throws Exception { 71 assertTrue( "Document is not null", document != null ); 72 73 TransformerFactory factory = TransformerFactory.newInstance(); 75 Transformer transformer = factory.newTransformer( 76 new StreamSource ( xsl ) 77 ); 78 79 Source source = new DocumentSource( document ); 82 DocumentResult result = new DocumentResult(); 83 transformer.transform( source, result ); 84 85 return result.getDocument(); 86 } 87 88 } 89 90 91 92 93 137 | Popular Tags |