KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > util > rewriter > HTMLRewriter


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 JavaDoc;
21
22 // java.net
23
import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25
26 // this makes it dependent on Swing...need an abstraction WTP
27
import javax.swing.text.html.HTML JavaDoc;
28 import javax.swing.text.MutableAttributeSet JavaDoc;
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 JavaDoc 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 JavaDoc generateNewUrl( String JavaDoc targetURL, HTML.Tag JavaDoc tag, HTML.Attribute JavaDoc attribute)
60     {
61         String JavaDoc fullPath = "";
62         try
63         {
64
65             if (baseURL != null)
66             {
67                 URL JavaDoc full = new URL JavaDoc(new URL JavaDoc(baseURL), targetURL);
68                 fullPath = full.toString();
69             }
70             else
71             {
72                 return targetURL; // leave as is
73
}
74         }
75         catch (Exception JavaDoc 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 JavaDoc rewrite(Reader JavaDoc input, String JavaDoc baseURL)
95                                throws MalformedURLException JavaDoc
96     {
97         String JavaDoc 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 JavaDoc tag, MutableAttributeSet JavaDoc attrs)
110     {
111         return true;
112     }
113
114     public String JavaDoc exitSimpleTagEvent(HTML.Tag JavaDoc tag, MutableAttributeSet JavaDoc attrs)
115     {
116         return null;
117     }
118
119     /*
120      * Start Tag Events
121      */

122     public boolean enterStartTagEvent(HTML.Tag JavaDoc tag, MutableAttributeSet JavaDoc attrs)
123     {
124         return true;
125     }
126
127     public String JavaDoc exitStartTagEvent(HTML.Tag JavaDoc tag, MutableAttributeSet JavaDoc attrs)
128     {
129         return null;
130     }
131
132     /*
133      * Exit Tag Events
134      */

135     public boolean enterEndTagEvent(HTML.Tag JavaDoc tag)
136     {
137         return true;
138     }
139
140     public String JavaDoc exitEndTagEvent(HTML.Tag JavaDoc 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 JavaDoc tag, MutableAttributeSet JavaDoc attrs)
157     {
158     }
159
160 }
161
162
Popular Tags