1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.rewriter;
18
19 import java.io.File;
20 import java.io.FileReader;
21 import java.io.Reader;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.jetspeed.rewriter.html.SwingParserAdaptor;
34 import org.apache.jetspeed.rewriter.rules.Ruleset;
35 import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor;
36 import org.exolab.castor.mapping.Mapping;
37 import org.exolab.castor.xml.Unmarshaller;
38 import org.w3c.dom.Document;
39 import org.xml.sax.InputSource;
40
41 /***
42 * RewriterServiceImpl
43 *
44 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
45 * @version $Id: JetspeedRewriterController.java,v 1.2 2004/03/08 00:44:40 jford
46 * Exp $
47 */
48 public class JetspeedRewriterController implements RewriterController
49 {
50 protected final static Log log = LogFactory.getLog(JetspeedRewriterController.class);
51 final static String CONFIG_MAPPING_FILE = "mapping";
52 final static String CONFIG_BASIC_REWRITER = "basic.class";
53 final static String CONFIG_RULESET_REWRITER = "ruleset.class";
54 final static String CONFIG_ADAPTOR_HTML = "adaptor.html";
55 final static String CONFIG_ADAPTOR_XML = "adaptor.xml";
56
57
58 protected String mappingFile = null;
59
60 /*** the Castor mapping file name */
61 protected Mapping mapper = null;
62
63 /*** Collection of rulesets in the system */
64 protected Map rulesets = Collections.synchronizedMap(new HashMap());
65
66 /*** configured basic rewriter class */
67 protected Class basicRewriterClass = BasicRewriter.class;
68
69 /*** configured ruleset rewriter class */
70 protected Class rulesetRewriterClass = RulesetRewriterImpl.class;
71
72 /*** Adaptors */
73 protected Class adaptorHtmlClass = SwingParserAdaptor.class;
74 protected Class adaptorXmlClass = SaxParserAdaptor.class;
75
76 public JetspeedRewriterController( String mappingFile ) throws RewriterException
77 {
78 this.mappingFile = mappingFile;
79 loadMapping();
80 }
81
82 public JetspeedRewriterController( String mappingFile, List rewriterClasses, List adaptorClasses )
83 throws RewriterException
84 {
85 this.mappingFile = mappingFile;
86 if (rewriterClasses.size() > 0)
87 {
88 this.basicRewriterClass = (Class) rewriterClasses.get(0);
89 if (rewriterClasses.size() > 1)
90 {
91 this.rulesetRewriterClass = (Class) rewriterClasses.get(1);
92 }
93 }
94 if (adaptorClasses.size() > 0)
95 {
96 this.adaptorHtmlClass = (Class) adaptorClasses.get(0);
97 if (adaptorClasses.size() > 1)
98 {
99 this.adaptorXmlClass = (Class) adaptorClasses.get(1);
100 }
101 }
102
103 loadMapping();
104 }
105
106 public JetspeedRewriterController( String mappingFile, String basicRewriterClassName, String rulesetRewriterClassName,
107 String adaptorHtmlClassName, String adaptorXmlClassName )
108 throws RewriterException
109 {
110 this(mappingFile, toClassList(basicRewriterClassName,rulesetRewriterClassName), toClassList(adaptorHtmlClassName,adaptorXmlClassName));
111 }
112
113 protected static List toClassList(String classNameA, String classNameB)
114 {
115 try
116 {
117 List list = new ArrayList(2);
118 if ( classNameA != null )
119 {
120 list.add(Class.forName(classNameA));
121 }
122 if ( classNameB != null )
123 {
124 list.add(Class.forName(classNameB));
125 }
126 return list;
127 }
128 catch (ClassNotFoundException e)
129 {
130 throw new RuntimeException(e);
131 }
132 }
133
134
135
136
137
138
139 public Rewriter createRewriter() throws InstantiationException, IllegalAccessException
140 {
141 return (Rewriter) basicRewriterClass.newInstance();
142 }
143
144
145
146
147
148
149 public RulesetRewriter createRewriter( Ruleset ruleset ) throws RewriterException
150 {
151 try
152 {
153 RulesetRewriter rewriter = (RulesetRewriter) rulesetRewriterClass.newInstance();
154 rewriter.setRuleset(ruleset);
155 return rewriter;
156 }
157 catch (Exception e)
158 {
159 log.error("Error creating rewriter class", e);
160 }
161 return null;
162 }
163
164
165
166
167
168
169 public ParserAdaptor createParserAdaptor( String mimeType ) throws RewriterException
170 {
171 try
172 {
173 if (mimeType.equals("text/html"))
174 {
175 return (ParserAdaptor) adaptorHtmlClass.newInstance();
176 }
177 else if (mimeType.equals("text/xml"))
178 {
179 return (ParserAdaptor) adaptorXmlClass.newInstance();
180 }
181 else
182 {
183 }
184 }
185 catch (Exception e)
186 {
187 log.error("Error creating rewriter class", e);
188 }
189 return null;
190 }
191
192 /***
193 * Load the mapping file for ruleset configuration
194 *
195 */
196 protected void loadMapping() throws RewriterException
197 {
198 try
199 {
200 Reader reader = getReader(this.mappingFile);
201
202 this.mapper = new Mapping();
203 InputSource is = new InputSource(reader);
204 is.setSystemId(this.mappingFile);
205 this.mapper.loadMapping(is);
206 }
207 catch (Exception e)
208 {
209 e.printStackTrace();
210 String msg = "RewriterService: Error in castor mapping creation";
211 log.error(msg, e);
212 throw new RewriterException(msg, e);
213 }
214 }
215
216 protected Reader getReader(String resource)
217 throws RewriterException
218 {
219 File file = new File(resource);
220 if (file.exists() && file.isFile() && file.canRead())
221 {
222 try
223 {
224 return new FileReader(file);
225 }
226 catch (Exception e)
227 {
228 throw new RewriterException("could not open rewriter file " + resource, e);
229 }
230 }
231 throw new RewriterException("could not access rewriter file " + resource);
232 }
233
234
235
236
237
238
239 public Ruleset lookupRuleset( String id )
240 {
241 return (Ruleset) rulesets.get(id);
242 }
243
244
245
246
247
248
249 public Ruleset loadRuleset( Reader reader )
250 {
251 Ruleset ruleset = null;
252 try
253 {
254 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
255 DocumentBuilder builder = dbfactory.newDocumentBuilder();
256
257 InputSource source = new InputSource(reader);
258
259 Document doc = builder.parse(source);
260
261 Unmarshaller unmarshaller = new Unmarshaller(this.mapper);
262
263 ruleset = (Ruleset) unmarshaller.unmarshal(doc);
264 ruleset.sync();
265 rulesets.put(ruleset.getId(), ruleset);
266
267 }
268 catch (Throwable t)
269 {
270 log.error("ForwardService: Could not unmarshal: " + reader, t);
271 }
272
273 return ruleset;
274 }
275
276 }