1 7 8 package org.dom4j.rule; 9 10 import junit.textui.TestRunner; 11 12 import org.dom4j.AbstractTestCase; 13 import org.dom4j.Document; 14 import org.dom4j.DocumentHelper; 15 import org.dom4j.Node; 16 import org.dom4j.xpath.DefaultXPath; 17 18 24 public class StylesheetTest extends AbstractTestCase { 25 protected String [] templates = { 26 "/", 27 "*", 28 "root", 29 "author", 30 "@name", 31 "root/author", 32 "author[@location='UK']", 33 "root/author[@location='UK']", 34 "root//author[@location='UK']"}; 35 36 protected String [] templates2 = {"/", "title", "para", "*"}; 37 38 protected Stylesheet stylesheet; 39 40 public static void main(String [] args) { 41 TestRunner.run(StylesheetTest.class); 42 } 43 44 public void testRules() throws Exception { 47 for (int i = 0, size = templates.length; i < size; i++) { 48 addTemplate(templates[i]); 49 } 50 51 log(""); 52 log("........................................"); 53 log(""); 54 log("Running stylesheet"); 55 56 stylesheet.run(document); 57 58 log("Finished"); 59 } 60 61 public void testLittleDoc() throws Exception { 62 for (int i = 0, size = templates2.length; i < size; i++) { 63 addTemplate(templates2[i]); 64 } 65 Document doc = getDocument("/xml/test/littledoc.xml"); 66 67 stylesheet = new Stylesheet(); 68 stylesheet.setValueOfAction(new Action() { 69 public void run(Node node) { 70 log("Default ValueOf action on node: " + node); 71 log("........................................"); 72 } 73 }); 74 75 stylesheet.run(doc); 76 } 77 78 public void testFireRuleForNode() throws Exception { 79 final StringBuffer b = new StringBuffer (); 80 81 final Stylesheet s = new Stylesheet(); 82 Pattern pattern = DocumentHelper.createPattern("url"); 83 Action action = new Action() { 84 public void run(Node node) throws Exception { 85 b.append("url"); 86 s.applyTemplates(node); 87 } 88 }; 89 90 Rule r = new Rule(pattern, action); 91 s.addRule(r); 92 93 s.applyTemplates(document, new DefaultXPath("root/author/url")); 94 95 assertEquals("Check url is processed twice", "urlurl", b.toString()); 96 } 97 98 protected void setUp() throws Exception { 101 super.setUp(); 102 103 stylesheet = new Stylesheet(); 104 stylesheet.setValueOfAction(new Action() { 105 public void run(Node node) { 106 log("Default ValueOf action on node: " + node); 107 log("........................................"); 108 } 109 }); 110 } 111 112 protected void addTemplate(final String match) { 113 log("Adding template match: " + match); 114 115 Pattern pattern = DocumentHelper.createPattern(match); 116 117 log("Pattern: " + pattern); 118 log("........................................"); 119 120 Action action = new Action() { 121 public void run(Node node) throws Exception { 122 log("Matched pattern: " + match); 123 log("Node: " + node.asXML()); 124 log("........................................"); 125 126 stylesheet.applyTemplates(node); 128 } 129 }; 130 131 Rule rule = new Rule(pattern, action); 132 stylesheet.addRule(rule); 133 } 134 } 135 136 172 | Popular Tags |