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.services.transformer;
1819// turbine stuff20import java.io.Reader;
2122import javax.servlet.ServletConfig;
2324import org.apache.jetspeed.util.rewriter.ClipperRewriter;
25import org.apache.turbine.services.TurbineBaseService;
26import org.apache.turbine.util.Log;
2728/***29 * Simple implementation of the TransformerService.30 * @author <a href="mailto:mmari@ce.unipr.it">Marco Mari</a>31 * @version $Id: JetspeedTransformerService.java,v 1.2 2004/02/23 03:39:10 jford Exp $* 32 */33publicclassJetspeedTransformerService34extends TurbineBaseService
35 implements TransformerService36 {
37protectedClipperRewriter rewriter = null;
38 String convertedString;
3940/***41 * This is the early initialization method called by the 42 * Turbine <code>Service</code> framework43 * @param conf The <code>ServletConfig</code>44 */45publicvoid init(ServletConfig conf)
46 {
47// controls if already initialized48if (getInit())
49 {
50return;
51 }
5253 rewriter = newClipperRewriter();
54 setInit(true);
55 }
5657/***58 * Late init. Don't return control until early init says we're done.59 */60publicvoid init()
61 {
62while (!getInit())
63 {
64try65 {
66 Thread.sleep(500);
67 Log.info("JetspeedTransformerService: Waiting for init()...");
68 }
69catch (InterruptedException ie)
70 {
71 Log.error(ie);
72 }
73 }
74 }
7576/***77 * Finds an element in a web page78 * 79 * @param htmlReader Reader for the html rewriter80 * @param url page address81 * @param element a part of the element to search82 */83public String findElement(Reader htmlReader, String url, String element)
84 {
85// If not indicated, assume to find the first element86return clipElementsNumber(htmlReader, url, element, null, 1);
87 }
8889/***90 * Clips the part of a web page between startElement and stopElement91 * 92 * @param htmlReader Reader for the html rewriter93 * @param url page address94 * @param startElement the first element to clip95 * @param lastElement the last element to clip96 */97public String clipElements(
98 Reader htmlReader,
99 String url,
100 String startElement,
101 String stopElement)
102 {
103// If not indicated, assume to find the first startElement104return clipElementsNumber(
105 htmlReader,
106 url,
107 startElement,
108 stopElement,
109 1);
110 }
111112/***113 * Finds in an HTML page the "tagNumber" tag of type "element"114 * Example: element = "p", tagNumber = "3"115 * Page content:116 * <p>..</p>117 * <p>..</p>118 * <p>.. <---Finds this 119 * 120 * @param htmlReader Reader for the html rewriter121 * @param url page address122 * @param element the element to search123 * @param tagNumber the number of the element to search124 */125public String findElementNumber(
126 Reader htmlReader,
127 String url,
128 String element,
129int tagNumber)
130 {
131return clipElementsNumber(htmlReader, url, element, null, tagNumber);
132 }
133134/***135 * Clips a part of a web page, starting from the "tagNumber" "startElement"136 * Example: startElement = "p", tagNumber = "3", stopElement = "img"137 * Page content:138 * <p>..</p>139 * <p>..</p>140 * <p>.. <---Starts here141 * ........<img> <---Stops here142 * 143 * @param htmlReader Reader for the html rewriter144 * @param url page address145 * @param startElement the first element to clip146 * @param stopElement the last element to clip147 * @param tagNumber the number of the first element to clip148 */149public String clipElementsNumber(
150 Reader htmlReader,
151 String url,
152 String startElement,
153 String stopElement,
154int tagNumber)
155 {
156 rewriter.setStartElement(startElement);
157 rewriter.setStopElement(stopElement);
158 rewriter.setStartElementNumber(tagNumber);
159160try161 {
162 convertedString = rewriter.rewrite(htmlReader, url);
163 }
164catch (Exception e)
165 {
166 Log.info("Exception occurred:" + e.toString());
167 e.printStackTrace();
168 }
169170return convertedString;
171 }
172173 }