1 /* 2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. 3 * 4 * This software is open source. 5 * See the bottom of this file for the licence. 6 * 7 * $Id: TestXSLT.java,v 1.8 2004/06/25 08:03:47 maartenc Exp $ 8 */ 9 10 package org.dom4j; 11 12 import java.util.List; 13 14 import javax.xml.transform.Source; 15 import javax.xml.transform.Transformer; 16 import javax.xml.transform.TransformerFactory; 17 import javax.xml.transform.stream.StreamSource; 18 19 import junit.framework.Test; 20 import junit.framework.TestSuite; 21 import junit.textui.TestRunner; 22 23 import org.dom4j.io.DocumentResult; 24 import org.dom4j.io.DocumentSource; 25 import org.dom4j.io.SAXReader; 26 27 /*** Tests that XSLT works correctly 28 * 29 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 30 * @version $Revision: 1.8 $ 31 */ 32 public class TestXSLT extends AbstractTestCase { 33 34 public static void main( String[] args ) { 35 TestRunner.run( suite() ); 36 } 37 38 public static Test suite() { 39 return new TestSuite( TestXSLT.class ); 40 } 41 42 public TestXSLT(String name) { 43 super(name); 44 } 45 46 // Test case(s) 47 //------------------------------------------------------------------------- 48 public void testTransform() throws Exception { 49 Document transformedDoc = transform( "/xml/nitf/ashtml.xsl" ); 50 51 //log( transformedDoc.asXML() ); 52 53 assertTrue( "Transformed Document is not null", transformedDoc != null ); 54 55 List h1List = transformedDoc.selectNodes( "/html//h1" ); 56 57 assertTrue( "At least one <h1>", h1List.size() > 0 ); 58 59 List pList = transformedDoc.selectNodes( "//p" ); 60 61 assertTrue( "At least one <p>", pList.size() > 0 ); 62 } 63 64 // Implementation methods 65 //------------------------------------------------------------------------- 66 protected void setUp() throws Exception { 67 SAXReader reader = new SAXReader(); 68 document = reader.read(getClass().getResource("/xml/nitf/sample.xml")); 69 } 70 71 protected Document transform(String xsl) throws Exception { 72 assertTrue( "Document is not null", document != null ); 73 74 // load the transformer 75 TransformerFactory factory = TransformerFactory.newInstance(); 76 Transformer transformer = factory.newTransformer( 77 new StreamSource(getClass().getResourceAsStream(xsl)) 78 ); 79 80 // now lets create the TrAX source and result 81 // objects and do the transformation 82 Source source = new DocumentSource( document ); 83 DocumentResult result = new DocumentResult(); 84 transformer.transform( source, result ); 85 86 return result.getDocument(); 87 } 88 89 } 90 91 92 93 94 /* 95 * Redistribution and use of this software and associated documentation 96 * ("Software"), with or without modification, are permitted provided 97 * that the following conditions are met: 98 * 99 * 1. Redistributions of source code must retain copyright 100 * statements and notices. Redistributions must also contain a 101 * copy of this document. 102 * 103 * 2. Redistributions in binary form must reproduce the 104 * above copyright notice, this list of conditions and the 105 * following disclaimer in the documentation and/or other 106 * materials provided with the distribution. 107 * 108 * 3. The name "DOM4J" must not be used to endorse or promote 109 * products derived from this Software without prior written 110 * permission of MetaStuff, Ltd. For written permission, 111 * please contact dom4j-info@metastuff.com. 112 * 113 * 4. Products derived from this Software may not be called "DOM4J" 114 * nor may "DOM4J" appear in their names without prior written 115 * permission of MetaStuff, Ltd. DOM4J is a registered 116 * trademark of MetaStuff, Ltd. 117 * 118 * 5. Due credit should be given to the DOM4J Project - 119 * http://www.dom4j.org 120 * 121 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 122 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 123 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 124 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 125 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 126 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 127 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 128 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 129 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 130 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 131 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 132 * OF THE POSSIBILITY OF SUCH DAMAGE. 133 * 134 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. 135 * 136 * $Id: TestXSLT.java,v 1.8 2004/06/25 08:03:47 maartenc Exp $ 137 */