KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > visitorsTests > NodeVisitorTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/visitorsTests/NodeVisitorTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/05/24 16:18:34 $
10
// $Revision: 1.16 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.tests.visitorsTests;
28
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
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 JavaDoc name) {
45         super(name);
46     }
47
48     public void testVisitTag() throws Exception JavaDoc {
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 JavaDoc paramsMap = new HashMap JavaDoc();
63         String JavaDoc lastKeyVisited;
64
65         public String JavaDoc getValue(String JavaDoc key) {
66             return (String JavaDoc)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