KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > rule > TestStylesheet


1 /*
2  * Copyright 2001 (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.1 2003/07/07 10:30:30 per_nyfelt Exp $
8  */

9
10 package test.dom4j.rule;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Node;
16 import org.dom4j.rule.Action;
17 import org.dom4j.rule.Pattern;
18 import org.dom4j.rule.Rule;
19 import org.dom4j.rule.Stylesheet;
20 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
21 import test.dom4j.AbstractTestCase;
22
23 /** A test harness to test the use of the Stylesheet and the
24   * XSLT rule engine.
25   *
26   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
27   * @version $Revision: 1.1 $
28   */

29 public class TestStylesheet extends AbstractTestCase {
30
31     protected String JavaDoc[] templates = {
32         "/",
33         "*",
34         "root",
35         "author",
36         "@name",
37         "root/author",
38         "author[@location='UK']",
39         "root/author[@location='UK']",
40         "root//author[@location='UK']",
41 /*
42         "text()[.='James Strachan']"
43 */

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

166
Popular Tags