1 27 package org.htmlparser.tests.visitorsTests; 28 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import org.htmlparser.Tag; 33 import org.htmlparser.Text; 34 import org.htmlparser.tests.ParserTestCase; 35 import org.htmlparser.visitors.NodeVisitor; 36 37 public class NodeVisitorTest extends ParserTestCase { 38 39 static 40 { 41 System.setProperty ("org.htmlparser.tests.visitorsTests.NodeVisitorTest", "NodeVisitorTest"); 42 } 43 44 public NodeVisitorTest(String name) { 45 super(name); 46 } 47 48 public void testVisitTag() throws Exception { 49 ParameterVisitor visitor = new ParameterVisitor(); 50 createParser( 51 "<input>" + 52 "<param name='key1'>value1</param>"+ 53 "<param name='key2'>value2</param>"+ 54 "</input>" 55 ); 56 parser.visitAllNodesWith(visitor); 57 assertEquals("value of key1","value1",visitor.getValue("key1")); 58 assertEquals("value of key2","value2",visitor.getValue("key2")); 59 } 60 61 class ParameterVisitor extends NodeVisitor { 62 Map paramsMap = new HashMap (); 63 String lastKeyVisited; 64 65 public String getValue(String key) { 66 return (String )paramsMap.get(key); 67 } 68 69 public void visitStringNode(Text stringNode) { 70 paramsMap.put(lastKeyVisited,stringNode.getText()); 71 } 72 73 public void visitTag(Tag tag) { 74 if (tag.getTagName().equals("PARAM")) { 75 lastKeyVisited = tag.getAttribute("NAME"); 76 } 77 } 78 } 79 } 80 | Popular Tags |