1 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 29 public class TestStylesheet extends AbstractTestCase { 30 31 protected String [] 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 44 }; 45 46 protected Stylesheet stylesheet; 47 48 public static void main( String [] args ) { 51 TestRunner.run( suite() ); 52 } 53 54 public static Test suite() { 55 return new TestSuite( TestStylesheet.class ); 56 } 57 58 public TestStylesheet(String name) { 59 super(name); 60 } 61 62 public void testRules() throws Exception { 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 protected void setUp() throws Exception { 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 match ) throws Exception { 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 { 105 log( "Matched pattern: " + match ); 106 log( "Node: " + node.asXML() ); 107 log( "........................................" ); 108 109 stylesheet.applyTemplates(node); 111 } 112 }; 113 Rule rule = new Rule( pattern, action ); 114 stylesheet.addRule( rule ); 115 } 116 117 } 118 119 120 121 122 166 | Popular Tags |