/ org.htmlparser / src / org / htmlparser / visitors / UrlModifyingVisitor.java
UrlModifyingVisitor.java
 1  // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML
 2  // http://sourceforge.org/projects/htmlparser
 3  // Copyright (C) 2004 Joshua Kerievsky
 4  //
 5  // Revision Control Information
 6  //
 7  // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/visitors/UrlModifyingVisitor.java,v $
 8  // $Author: derrickoswald $
 9  // $Date: 2004/07/31 16:42:35 $
10  // $Revision: 1.46 $
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.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          // process only those nodes that won't be processed by an end tag,
64          // nodes without parents or parents without an end tag, since
65          // the complete processing of all children should happen before
66          // we turn this node back into html text
67          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          // process only those nodes not processed by a parent
78          if (null == parent)
79              // an orphan end tag
80              modifiedResult.append(tag.toHtml());
81          else
82              if (null == parent.getParent ())
83                  // a top level tag with no parents
84                  modifiedResult.append(parent.toHtml());
85      }
86  
87      public String getModifiedResult() {
88          return modifiedResult.toString();
89      }
90  }