KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > transformer > JetspeedTransformerService


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.services.transformer;
18
19 // turbine stuff
20
import java.io.Reader JavaDoc;
21
22 import javax.servlet.ServletConfig JavaDoc;
23
24 import org.apache.jetspeed.util.rewriter.ClipperRewriter;
25 import org.apache.turbine.services.TurbineBaseService;
26 import org.apache.turbine.util.Log;
27
28 /**
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  */

33 public class JetspeedTransformerService
34     extends TurbineBaseService
35     implements TransformerService
36 {
37     protected ClipperRewriter rewriter = null;
38     String JavaDoc convertedString;
39
40     /**
41      * This is the early initialization method called by the
42      * Turbine <code>Service</code> framework
43      * @param conf The <code>ServletConfig</code>
44      */

45     public void init(ServletConfig JavaDoc conf)
46     {
47         // controls if already initialized
48
if (getInit())
49         {
50             return;
51         }
52
53         rewriter = new ClipperRewriter();
54         setInit(true);
55     }
56
57     /**
58      * Late init. Don't return control until early init says we're done.
59      */

60     public void init()
61     {
62         while (!getInit())
63         {
64             try
65             {
66                 Thread.sleep(500);
67                 Log.info("JetspeedTransformerService: Waiting for init()...");
68             }
69             catch (InterruptedException JavaDoc ie)
70             {
71                 Log.error(ie);
72             }
73         }
74     }
75
76     /**
77      * Finds an element in a web page
78      *
79      * @param htmlReader Reader for the html rewriter
80      * @param url page address
81      * @param element a part of the element to search
82      */

83     public String JavaDoc findElement(Reader JavaDoc htmlReader, String JavaDoc url, String JavaDoc element)
84     {
85         // If not indicated, assume to find the first element
86
return clipElementsNumber(htmlReader, url, element, null, 1);
87     }
88
89     /**
90      * Clips the part of a web page between startElement and stopElement
91      *
92      * @param htmlReader Reader for the html rewriter
93      * @param url page address
94      * @param startElement the first element to clip
95      * @param lastElement the last element to clip
96      */

97     public String JavaDoc clipElements(
98         Reader JavaDoc htmlReader,
99         String JavaDoc url,
100         String JavaDoc startElement,
101         String JavaDoc stopElement)
102     {
103         // If not indicated, assume to find the first startElement
104
return clipElementsNumber(
105             htmlReader,
106             url,
107             startElement,
108             stopElement,
109             1);
110     }
111
112     /**
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 rewriter
121      * @param url page address
122      * @param element the element to search
123      * @param tagNumber the number of the element to search
124      */

125     public String JavaDoc findElementNumber(
126         Reader JavaDoc htmlReader,
127         String JavaDoc url,
128         String JavaDoc element,
129         int tagNumber)
130     {
131         return clipElementsNumber(htmlReader, url, element, null, tagNumber);
132     }
133
134     /**
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 here
141      * ........<img> <---Stops here
142      *
143      * @param htmlReader Reader for the html rewriter
144      * @param url page address
145      * @param startElement the first element to clip
146      * @param stopElement the last element to clip
147      * @param tagNumber the number of the first element to clip
148      */

149     public String JavaDoc clipElementsNumber(
150         Reader JavaDoc htmlReader,
151         String JavaDoc url,
152         String JavaDoc startElement,
153         String JavaDoc stopElement,
154         int tagNumber)
155     {
156         rewriter.setStartElement(startElement);
157         rewriter.setStopElement(stopElement);
158         rewriter.setStartElementNumber(tagNumber);
159
160         try
161         {
162             convertedString = rewriter.rewrite(htmlReader, url);
163         }
164         catch (Exception JavaDoc e)
165         {
166             Log.info("Exception occurred:" + e.toString());
167             e.printStackTrace();
168         }
169
170         return convertedString;
171     }
172
173 }
174
Popular Tags