1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.transformer;
18
19
20 import java.io.Reader;
21
22 import javax.servlet.ServletConfig;
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 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 conf)
46 {
47
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 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 findElement(Reader htmlReader, String url, String element)
84 {
85
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 clipElements(
98 Reader htmlReader,
99 String url,
100 String startElement,
101 String stopElement)
102 {
103
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 findElementNumber(
126 Reader htmlReader,
127 String url,
128 String 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 clipElementsNumber(
150 Reader htmlReader,
151 String url,
152 String startElement,
153 String 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 e)
165 {
166 Log.info("Exception occurred:" + e.toString());
167 e.printStackTrace();
168 }
169
170 return convertedString;
171 }
172
173 }