001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.rewriter;
018:
019: import java.io.Reader;
020: import java.io.Writer;
021:
022: /**
023: * Rewriter
024: *
025: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
026: * @version $Id: Rewriter.java 516448 2007-03-09 16:25:47Z ate $
027: */
028: public interface Rewriter {
029: /**
030: * Parses the reader of content receiving call backs for rewriter events.
031: * This method does not rewrite, but only parses. Useful for readonly operations.
032: * The configured parser can parse over different stream formats returning a
033: * normalized (org.sax.xml) attribute and element based events.
034: *
035: * @param adaptor the parser adaptor which handles generating SAX-like events called back on this object.
036: * @param reader the input stream over the content to be parsed.
037: * @exception RewriteException when a parsing error occurs or unexpected content is found.
038: */
039: void parse(ParserAdaptor adaptor, Reader reader)
040: throws RewriterException;
041:
042: /**
043: * Parses the reader of content receiving call backs for rewriter events.
044: * The content is rewritten to the output stream.
045: * The configured parser can parse over different stream formats returning a
046: * normalized (org.sax.xml) attribute and element based events.
047: *
048: * @param adaptor the parser adaptor which handles generating SAX-like events called back on this object.
049: * @param reader the input stream over the content to be parsed.
050: * @param writer the output stream where content is rewritten to.
051: * @exception RewriteException when a parsing error occurs or unexpected content is found.
052: */
053: void rewrite(ParserAdaptor adaptor, Reader reader, Writer writer)
054: throws RewriterException;
055:
056: /**
057: * This event is the inteface between the Rewriter and ParserAdaptor for rewriting URLs.
058: * The ParserAdaptor calls back the Rewriter when it finds a URL that is a candidate to be
059: * rewritten. The Rewriter rewrites the URL and returns it as the result of this function.
060: *
061: * @param url the URL to be rewritten
062: * @param tag The tag being processed
063: * @param attribute The current attribute being processsed
064: */
065: String rewriteUrl(String url, String tag, String attribute);
066:
067: /**
068: * Returns true if the tag should be removed, otherwise false.
069: * Removing a tag only removes the tag but not the contents in
070: * between the start and end tag.
071: *
072: * @return true if the tag should be removed.
073: */
074: boolean shouldRemoveTag(String tag);
075:
076: /**
077: * Returns true if the tag should be stripped, otherwise false.
078: * Stripping tags removes the start and end tag, plus all tags
079: * and content in between the start and end tag.
080: *
081: * @return true if the tag should be stripped.
082: */
083: boolean shouldStripTag(String tag);
084:
085: /**
086: * Returns true if all comments should be removed.
087: *
088: * @return true If all comments should be removed.
089: */
090: boolean shouldRemoveComments();
091:
092: /**
093: * Sets the base URL for rewriting. This URL is the base
094: * from which other URLs are generated.
095: *
096: * @param base The base URL for this rewriter
097: */
098: void setBaseUrl(String base);
099:
100: /**
101: * Gets the base URL for rewriting. This URL is the base
102: * from which other URLs are generated.
103: *
104: * @return The base URL for this rewriter
105: */
106: String getBaseUrl();
107:
108: /**
109: * Gets a new URL relative to Base according to the site / and URL
110: * rewriting rules of java.net.URL
111: *
112: * @return The new URL from path, relative to the base URL (or path, if path is absolute)
113: */
114: String getBaseRelativeUrl(String path);
115:
116: /**
117: * Gets whether this rewriter require a proxy server.
118: *
119: * @return true if it requires a proxy
120: */
121: boolean getUseProxy();
122:
123: /**
124: * Set whether this rewriter require a proxy server.
125: *
126: * @param useProxy true if it requires a proxy
127: */
128: void setUseProxy(boolean useProxy);
129:
130: /**
131: * Rewriter event called back on the leading edge of processing a simple tag by the ParserAdaptor.
132: * Returns false to indicate to the ParserAdaptor to short-circuit processing on this tag.
133: *
134: * @param tag The name of the tag being processed.
135: * @param attrs The attribute list for the tag.
136: * @return Should return true to continue processing the tag in the ParserAdaptor, false to indicate that processing is completed.
137: */
138: boolean enterSimpleTagEvent(String tag, MutableAttributes attrs);
139:
140: /**
141: * Rewriter event called back on the trailing edge of a simple tag by the ParserAdaptor.
142: * Returns a String that can be appended to the rewritten output for the given tag, or null to indicate no content available.
143: *
144: * @param tag The name of the tag being processed.
145: * @param attrs The attribute list for the tag.
146: * @return Returns a String that can be appended to the rewritten output for the given tag, or null to indicate no content available.
147: */
148: String exitSimpleTagEvent(String tag, MutableAttributes attrs);
149:
150: /**
151: * Rewriter event called back on the leading edge of processing a start tag by the ParserAdaptor.
152: * Returns false to indicate to the ParserAdaptor to short-circuit processing on this tag.
153: *
154: * @param tag The name of the tag being processed.
155: * @param attrs The attribute list for the tag.
156: * @return Should return true to continue processing the tag in the ParserAdaptor, false to indicate that processing is completed.
157: */
158: boolean enterStartTagEvent(String tag, MutableAttributes attrs);
159:
160: /**
161: * Rewriter event called back on the trailing edge of a start tag by the ParserAdaptor.
162: * Returns a String that can be appended to the rewritten output for the given tag, or null to indicate no content available.
163: *
164: * @param tag The name of the tag being processed.
165: * @param attrs The attribute list for the tag.
166: * @return Returns a String that can be appended to the rewritten output for the given tag, or null to indicate no content available.
167: */
168: String exitStartTagEvent(String tag, MutableAttributes attrs);
169:
170: /**
171: * Rewriter event called back on the leading edge of processing an end tag by the ParserAdaptor.
172: * Returns false to indicate to the ParserAdaptor to short-circuit processing on this tag.
173: *
174: * @param tag The name of the tag being processed.
175: * @param attrs The attribute list for the tag.
176: * @return Should return true to continue processing the tag in the ParserAdaptor, false to indicate that processing is completed.
177: */
178: boolean enterEndTagEvent(String tag);
179:
180: /**
181: * Rewriter event called back on the trailing edge of a end tag by the ParserAdaptor.
182: * Returns a String that can be appended to the rewritten output for the given tag, or null to indicate no content available.
183: *
184: * @param tag The name of the tag being processed.
185: * @param attrs The attribute list for the tag.
186: * @return Returns a String that can be appended to the rewritten output for the given tag, or null to indicate no content available.
187: */
188: String exitEndTagEvent(String tag);
189:
190: /**
191: * Rewriter event called back when text is found for
192: * Returns false to indicate to the ParserAdaptor to short-circuit processing on this tag.
193: *
194: * @param values an array of characters containing the text.
195: * @param param
196: * @return Should return true to continue processing the tag in the ParserAdaptor, false to indicate that processing is completed.
197: */
198: boolean enterText(char[] values, int param);
199:
200: /**
201: * Rewriter event called back just before tag conversion (rewriter callbacks) begins by the ParserAdaptor.
202: *
203: * @param tag The name of the tag being processed.
204: * @param attrs The attribute list for the tag.
205: */
206: void enterConvertTagEvent(String tag, MutableAttributes attrs);
207:
208: }
|