KickJava   Java API By Example, From Geeks To Geeks.

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


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/UrlModifyingVisitorTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/03/12 13:39:46 $
10
// $Revision: 1.19 $
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 org.htmlparser.Parser;
30 import org.htmlparser.Tag;
31 import org.htmlparser.tags.ImageTag;
32 import org.htmlparser.tags.LinkTag;
33 import org.htmlparser.tests.ParserTestCase;
34 import org.htmlparser.util.NodeList;
35 import org.htmlparser.visitors.NodeVisitor;
36 import org.htmlparser.visitors.UrlModifyingVisitor;
37
38 public class UrlModifyingVisitorTest extends ParserTestCase {
39
40     static
41     {
42         System.setProperty ("org.htmlparser.tests.visitorsTests.UrlModifyingVisitorTest", "UrlModifyingVisitorTest");
43     }
44
45     private static final String JavaDoc HTML_WITH_LINK =
46     "<HTML><BODY>" +
47         "<A HREF=\"mylink.html\"><IMG SRC=\"mypic.jpg\">" +
48         "</A><IMG SRC=\"my second image.gif\">" +
49     "</BODY></HTML>";
50
51     // Note: links are only quoted if needed
52
private static final String JavaDoc MODIFIED_HTML =
53     "<HTML><BODY>" +
54         "<A HREF=\"localhost://mylink.html\">" +
55         "<IMG SRC=\"localhost://mypic.jpg\"></A>" +
56         "<IMG SRC=\"localhost://my second image.gif\">" +
57     "</BODY></HTML>";
58
59     public UrlModifyingVisitorTest(String JavaDoc name) {
60         super(name);
61     }
62
63     public void testUrlModificationWithVisitor() throws Exception JavaDoc {
64         Parser parser = Parser.createParser(HTML_WITH_LINK, null);
65         UrlModifyingVisitor visitor =
66             new UrlModifyingVisitor("localhost://");
67         parser.visitAllNodesWith(visitor);
68         String JavaDoc result = visitor.getModifiedResult();
69         assertStringEquals("Expected HTML",
70             MODIFIED_HTML,
71             result);
72     }
73
74     /**
75      * Test a better method of modifying an HTML page.
76      */

77     public void testPageModification ()
78         throws
79             Exception JavaDoc
80     {
81         Parser parser = Parser.createParser (HTML_WITH_LINK, null);
82         NodeList list = parser.parse (null); // no filter
83
// make an inner class that does the same thing as the UrlModifyingVisitor
84
NodeVisitor visitor = new NodeVisitor ()
85         {
86             String JavaDoc linkPrefix = "localhost://";
87             public void visitTag (Tag tag)
88             {
89                 if (tag instanceof LinkTag)
90                     ((LinkTag)tag).setLink(linkPrefix + ((LinkTag)tag).getLink());
91                 else if (tag instanceof ImageTag)
92                     ((ImageTag)tag).setImageURL(linkPrefix + ((ImageTag)tag).getImageURL());
93             }
94         };
95         list.visitAllNodesWith (visitor);
96         String JavaDoc result = list.toHtml ();
97         assertStringEquals("Expected HTML",
98             MODIFIED_HTML,
99             result);
100     }
101 }
102
Popular Tags