1 27 package org.htmlparser.visitors; 28 29 import org.htmlparser.Node; 30 import org.htmlparser.Remark; 31 import org.htmlparser.Text; 32 import org.htmlparser.tags.CompositeTag; 33 import org.htmlparser.tags.ImageTag; 34 import org.htmlparser.tags.LinkTag; 35 import org.htmlparser.Tag; 36 37 public class UrlModifyingVisitor extends NodeVisitor { 38 private String linkPrefix; 39 private StringBuffer modifiedResult; 40 41 public UrlModifyingVisitor(String linkPrefix) { 42 super(true,true); 43 this.linkPrefix =linkPrefix; 44 modifiedResult = new StringBuffer (); 45 } 46 47 public void visitRemarkNode (Remark remarkNode) 48 { 49 modifiedResult.append (remarkNode.toHtml()); 50 } 51 52 public void visitStringNode(Text stringNode) 53 { 54 modifiedResult.append (stringNode.toHtml()); 55 } 56 57 public void visitTag(Tag tag) 58 { 59 if (tag instanceof LinkTag) 60 ((LinkTag)tag).setLink(linkPrefix + ((LinkTag)tag).getLink()); 61 else if (tag instanceof ImageTag) 62 ((ImageTag)tag).setImageURL(linkPrefix + ((ImageTag)tag).getImageURL()); 63 if (null == tag.getParent () 68 && (!(tag instanceof CompositeTag) || null == ((CompositeTag)tag).getEndTag ())) 69 modifiedResult.append(tag.toHtml()); 70 } 71 72 public void visitEndTag(Tag tag) 73 { 74 Node parent; 75 76 parent = tag.getParent (); 77 if (null == parent) 79 modifiedResult.append(tag.toHtml()); 81 else 82 if (null == parent.getParent ()) 83 modifiedResult.append(parent.toHtml()); 85 } 86 87 public String getModifiedResult() { 88 return modifiedResult.toString(); 89 } 90 } 91 | Popular Tags |