1 9 10 package test.dom4j.xpath; 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.NodeFilter; 17 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 18 import test.dom4j.AbstractTestCase; 19 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 28 public class TestFilter extends AbstractTestCase { 29 30 protected static boolean VERBOSE = false; 31 32 protected static String [] paths = { 33 ".[name()='author']", 34 ".[name()='XXXX']", 35 ".[.='James Strachan']", 36 ".[.='XXXX']" 37 }; 38 39 40 public static void main( String [] args ) { 41 TestRunner.run( suite() ); 42 } 43 44 public static Test suite() { 45 return new TestSuite( TestFilter.class ); 46 } 47 48 public TestFilter(String name) { 49 super(name); 50 } 51 52 public void testXPaths() throws Exception { 55 int size = paths.length; 56 for ( int i = 0; i < size; i++ ) { 57 testXPath( paths[i] ); 58 } 59 } 60 61 protected void testXPath(String xpathExpression) throws Exception { 64 NodeFilter nodeFilter = O3DocumentHelper.createXPathFilter( xpathExpression ); 65 assertTrue( "No NodeFilter object was created", nodeFilter != null ); 66 67 if ( VERBOSE ) { 68 log( "Evaluating XPathFilter: " + xpathExpression + " using NodeFilter: " + nodeFilter ); 69 } 70 71 log( "Evaluating XPathFilter: " + xpathExpression ); 72 73 List list = document.selectNodes("//author"); 74 for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 75 Node node = (Node) iter.next(); 76 77 if ( nodeFilter.matches( node ) ) { 78 log( "Matches node: "+ node.asXML() ); 79 } 80 else { 81 log( "No match for node: "+ node.asXML() ); 82 } 83 84 } 85 } 86 } 87 88 89 90 91 135 | Popular Tags |