1 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 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 private static final String 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 name) { 60 super(name); 61 } 62 63 public void testUrlModificationWithVisitor() throws Exception { 64 Parser parser = Parser.createParser(HTML_WITH_LINK, null); 65 UrlModifyingVisitor visitor = 66 new UrlModifyingVisitor("localhost://"); 67 parser.visitAllNodesWith(visitor); 68 String result = visitor.getModifiedResult(); 69 assertStringEquals("Expected HTML", 70 MODIFIED_HTML, 71 result); 72 } 73 74 77 public void testPageModification () 78 throws 79 Exception 80 { 81 Parser parser = Parser.createParser (HTML_WITH_LINK, null); 82 NodeList list = parser.parse (null); NodeVisitor visitor = new NodeVisitor () 85 { 86 String 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 result = list.toHtml (); 97 assertStringEquals("Expected HTML", 98 MODIFIED_HTML, 99 result); 100 } 101 } 102 | Popular Tags |