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: TestStylesheet.java,v 1.9 2004/06/25 08:03:50 maartenc Exp $
8 */
9
10 package org.dom4j.rule;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15
16 import org.dom4j.AbstractTestCase;
17 import org.dom4j.DocumentHelper;
18 import org.dom4j.Node;
19
20 /*** A test harness to test the use of the Stylesheet and the
21 * XSLT rule engine.
22 *
23 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
24 * @version $Revision: 1.9 $
25 */
26 public class TestStylesheet extends AbstractTestCase {
27
28 protected String[] templates = {
29 "/",
30 "*",
31 "root",
32 "author",
33 "@name",
34 "root/author",
35 "author[@location='UK']",
36 "root/author[@location='UK']",
37 "root//author[@location='UK']",
38 /*
39 "text()[.='James Strachan']"
40 */
41 };
42
43 protected Stylesheet stylesheet;
44
45 // JUnit stuff
46 //-------------------------------------------------------------------------
47 public static void main( String[] args ) {
48 TestRunner.run( suite() );
49 }
50
51 public static Test suite() {
52 return new TestSuite( TestStylesheet.class );
53 }
54
55 public TestStylesheet(String name) {
56 super(name);
57 }
58
59 // Test case(s)
60 //-------------------------------------------------------------------------
61 public void testRules() throws Exception {
62 for ( int i = 0, size = templates.length; i < size; i++ ) {
63 addTemplate( templates[i] );
64 }
65
66 log( "" );
67 log( "........................................" );
68 log( "" );
69 log( "Running stylesheet" );
70
71 stylesheet.run( document );
72
73 log( "Finished" );
74 }
75
76
77 // Implementation methods
78 //-------------------------------------------------------------------------
79 protected void setUp() throws Exception {
80 super.setUp();
81
82 stylesheet = new Stylesheet();
83 stylesheet.setValueOfAction(
84 new Action() {
85 public void run(Node node) {
86 log( "Default ValueOf action on node: " + node );
87 log( "........................................" );
88 }
89 }
90 );
91 }
92 protected void addTemplate( final String match ) {
93 log( "Adding template match: " + match );
94
95 Pattern pattern = DocumentHelper.createPattern( match );
96
97 log( "Pattern: " + pattern );
98 log( "........................................" );
99
100 Action action = new Action() {
101 public void run(Node node) throws Exception {
102 log( "Matched pattern: " + match );
103 log( "Node: " + node.asXML() );
104 log( "........................................" );
105
106 // apply any child templates
107 stylesheet.applyTemplates(node);
108 }
109 };
110 Rule rule = new Rule( pattern, action );
111 stylesheet.addRule( rule );
112 }
113
114 }
115
116
117
118
119 /*
120 * Redistribution and use of this software and associated documentation
121 * ("Software"), with or without modification, are permitted provided
122 * that the following conditions are met:
123 *
124 * 1. Redistributions of source code must retain copyright
125 * statements and notices. Redistributions must also contain a
126 * copy of this document.
127 *
128 * 2. Redistributions in binary form must reproduce the
129 * above copyright notice, this list of conditions and the
130 * following disclaimer in the documentation and/or other
131 * materials provided with the distribution.
132 *
133 * 3. The name "DOM4J" must not be used to endorse or promote
134 * products derived from this Software without prior written
135 * permission of MetaStuff, Ltd. For written permission,
136 * please contact dom4j-info@metastuff.com.
137 *
138 * 4. Products derived from this Software may not be called "DOM4J"
139 * nor may "DOM4J" appear in their names without prior written
140 * permission of MetaStuff, Ltd. DOM4J is a registered
141 * trademark of MetaStuff, Ltd.
142 *
143 * 5. Due credit should be given to the DOM4J Project -
144 * http://www.dom4j.org
145 *
146 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
147 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
148 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
149 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
150 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
151 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
152 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
153 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
154 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
155 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
156 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
157 * OF THE POSSIBILITY OF SUCH DAMAGE.
158 *
159 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
160 *
161 * $Id: TestStylesheet.java,v 1.9 2004/06/25 08:03:50 maartenc Exp $
162 */