View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.util.rewriter;
18  
19  // java.io
20  import java.io.Reader;
21  
22  // java.net
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  
26  // this makes it dependent on Swing...need an abstraction WTP
27  import javax.swing.text.html.HTML;
28  import javax.swing.text.MutableAttributeSet;
29   
30  /***
31   *                  
32   * Basic Rewriter for rewriting HTML content. 
33   *
34   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
35   * @version $Id: HTMLRewriter.java,v 1.6 2004/02/23 03:18:59 jford Exp $
36   * 
37   */
38  
39  public class HTMLRewriter implements Rewriter
40  {
41      /*
42       * Construct a basic HTML Rewriter
43       *
44       */
45      public HTMLRewriter()
46      {
47      }
48  
49      protected String baseURL = null;
50  
51      /*    
52       * This callback is called by the HTMLParserAdaptor implementation to write
53       * back all rewritten URLs to point to the proxy server.
54       * Given the targetURL, rewrites the link as a link back to the proxy server.
55       *
56       * @return the rewritten URL to the proxy server.
57       *
58       */
59      public String generateNewUrl( String targetURL, HTML.Tag tag, HTML.Attribute attribute)
60      {
61          String fullPath = "";
62          try
63          {
64  
65              if (baseURL != null)
66              {
67                  URL full = new URL(new URL(baseURL), targetURL);
68                  fullPath = full.toString();
69              }
70              else
71              {
72                  return targetURL; // leave as is
73              }
74          }
75          catch (Exception e)
76          {
77              System.err.println(e);
78          }
79          return fullPath;
80  
81      }
82  
83      
84      /*
85       * Returns true if all rewritten URLs should be sent back to the proxy server.
86       *
87       * @return true if all URLs are rewritten back to proxy server.
88       */
89      public boolean proxyAllTags()
90      {
91          return true; //false;
92      }
93  
94      public String rewrite(Reader input, String baseURL)
95                                 throws MalformedURLException
96      {
97          String rewrittenHTML = "";
98          this.baseURL = baseURL;
99  
100         HTMLParserAdaptor parser = new SwingParserAdaptor(this);
101         rewrittenHTML = parser.run(input);
102 
103         return rewrittenHTML;
104     }
105 
106     /*
107      * Simple Tag Events
108      */
109     public boolean enterSimpleTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
110     {
111         return true;
112     }
113 
114     public String exitSimpleTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
115     {
116         return null;
117     }
118 
119     /*
120      * Start Tag Events
121      */
122     public boolean enterStartTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
123     {
124         return true;
125     }
126 
127     public String exitStartTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
128     {
129         return null;
130     }
131 
132     /*
133      * Exit Tag Events
134      */
135     public boolean enterEndTagEvent(HTML.Tag tag)
136     {
137         return true;
138     }
139 
140     public String exitEndTagEvent(HTML.Tag tag)
141     {
142         return null;
143     }
144 
145     /*
146      * Text Event
147      */
148     public boolean enterText(char[] values, int param)
149     {
150         return true;
151     }
152 
153     /*
154      * Convert Tag Events
155      */
156     public void convertTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
157     {
158     }
159 
160 }
161