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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util.rewriter;
1819// java.io20import java.io.Reader;
2122// java.net23import java.net.MalformedURLException;
24import java.net.URL;
2526// this makes it dependent on Swing...need an abstraction WTP27import javax.swing.text.html.HTML;
28import javax.swing.text.MutableAttributeSet;
2930/***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 */3839publicclassHTMLRewriter implements Rewriter40 {
41/*42 * Construct a basic HTML Rewriter43 *44 */45publicHTMLRewriter()
46 {
47 }
4849protected String baseURL = null;
5051/*52 * This callback is called by the HTMLParserAdaptor implementation to write53 * 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 */59public String generateNewUrl( String targetURL, HTML.Tag tag, HTML.Attribute attribute)
60 {
61 String fullPath = "";
62try63 {
6465if (baseURL != null)
66 {
67 URL full = new URL(new URL(baseURL), targetURL);
68 fullPath = full.toString();
69 }
70else71 {
72return targetURL; // leave as is73 }
74 }
75catch (Exception e)
76 {
77 System.err.println(e);
78 }
79return fullPath;
8081 }
828384/*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 */89publicboolean proxyAllTags()
90 {
91returntrue; //false;92 }
9394public String rewrite(Reader input, String baseURL)
95 throws MalformedURLException
96 {
97 String rewrittenHTML = "";
98this.baseURL = baseURL;
99100HTMLParserAdaptor parser = newSwingParserAdaptor(this);
101 rewrittenHTML = parser.run(input);
102103return rewrittenHTML;
104 }
105106/*107 * Simple Tag Events108 */109publicboolean enterSimpleTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
110 {
111returntrue;
112 }
113114public String exitSimpleTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
115 {
116returnnull;
117 }
118119/*120 * Start Tag Events121 */122publicboolean enterStartTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
123 {
124returntrue;
125 }
126127public String exitStartTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
128 {
129returnnull;
130 }
131132/*133 * Exit Tag Events134 */135publicboolean enterEndTagEvent(HTML.Tag tag)
136 {
137returntrue;
138 }
139140public String exitEndTagEvent(HTML.Tag tag)
141 {
142returnnull;
143 }
144145/*146 * Text Event147 */148publicboolean enterText(char[] values, int param)
149 {
150returntrue;
151 }
152153/*154 * Convert Tag Events155 */156publicvoid convertTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
157 {
158 }
159160 }
161