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.File;
020: import java.io.FileReader;
021: import java.io.Reader;
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.jetspeed.rewriter.html.SwingParserAdaptor;
034: import org.apache.jetspeed.rewriter.rules.Ruleset;
035: import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor;
036: import org.exolab.castor.mapping.Mapping;
037: import org.exolab.castor.xml.Unmarshaller;
038: import org.w3c.dom.Document;
039: import org.xml.sax.InputSource;
040:
041: /**
042: * RewriterServiceImpl
043: *
044: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
045: * @version $Id: JetspeedRewriterController.java,v 1.2 2004/03/08 00:44:40 jford
046: * Exp $
047: */
048: public class JetspeedRewriterController implements RewriterController {
049: protected final static Log log = LogFactory
050: .getLog(JetspeedRewriterController.class);
051: final static String CONFIG_MAPPING_FILE = "mapping";
052: final static String CONFIG_BASIC_REWRITER = "basic.class";
053: final static String CONFIG_RULESET_REWRITER = "ruleset.class";
054: final static String CONFIG_ADAPTOR_HTML = "adaptor.html";
055: final static String CONFIG_ADAPTOR_XML = "adaptor.xml";
056:
057: // configuration parameters
058: protected String mappingFile = null;
059:
060: /** the Castor mapping file name */
061: protected Mapping mapper = null;
062:
063: /** Collection of rulesets in the system */
064: protected Map rulesets = Collections.synchronizedMap(new HashMap());
065:
066: /** configured basic rewriter class */
067: protected Class basicRewriterClass = BasicRewriter.class;
068:
069: /** configured ruleset rewriter class */
070: protected Class rulesetRewriterClass = RulesetRewriterImpl.class;
071:
072: /** Adaptors */
073: protected Class adaptorHtmlClass = SwingParserAdaptor.class;
074: protected Class adaptorXmlClass = SaxParserAdaptor.class;
075:
076: public JetspeedRewriterController(String mappingFile)
077: throws RewriterException {
078: this .mappingFile = mappingFile;
079: loadMapping();
080: }
081:
082: public JetspeedRewriterController(String mappingFile,
083: List rewriterClasses, List adaptorClasses)
084: throws RewriterException {
085: this .mappingFile = mappingFile;
086: if (rewriterClasses.size() > 0) {
087: this .basicRewriterClass = (Class) rewriterClasses.get(0);
088: if (rewriterClasses.size() > 1) {
089: this .rulesetRewriterClass = (Class) rewriterClasses
090: .get(1);
091: }
092: }
093: if (adaptorClasses.size() > 0) {
094: this .adaptorHtmlClass = (Class) adaptorClasses.get(0);
095: if (adaptorClasses.size() > 1) {
096: this .adaptorXmlClass = (Class) adaptorClasses.get(1);
097: }
098: }
099:
100: loadMapping();
101: }
102:
103: public JetspeedRewriterController(String mappingFile,
104: String basicRewriterClassName,
105: String rulesetRewriterClassName,
106: String adaptorHtmlClassName, String adaptorXmlClassName)
107: throws RewriterException {
108: this (mappingFile, toClassList(basicRewriterClassName,
109: rulesetRewriterClassName), toClassList(
110: adaptorHtmlClassName, adaptorXmlClassName));
111: }
112:
113: protected static List toClassList(String classNameA,
114: String classNameB) {
115: try {
116: List list = new ArrayList(2);
117: if (classNameA != null) {
118: list.add(Class.forName(classNameA));
119: }
120: if (classNameB != null) {
121: list.add(Class.forName(classNameB));
122: }
123: return list;
124: } catch (ClassNotFoundException e) {
125: throw new RuntimeException(e);
126: }
127: }
128:
129: /*
130: * (non-Javadoc)
131: *
132: * @see org.apache.jetspeed.rewriter.RewriterService#createRewriter()
133: */
134: public Rewriter createRewriter() throws InstantiationException,
135: IllegalAccessException {
136: return (Rewriter) basicRewriterClass.newInstance();
137: }
138:
139: /*
140: * (non-Javadoc)
141: *
142: * @see org.apache.jetspeed.rewriter.RewriterService#createRewriter(org.apache.jetspeed.rewriter.rules.Ruleset)
143: */
144: public RulesetRewriter createRewriter(Ruleset ruleset)
145: throws RewriterException {
146: try {
147: RulesetRewriter rewriter = (RulesetRewriter) rulesetRewriterClass
148: .newInstance();
149: rewriter.setRuleset(ruleset);
150: return rewriter;
151: } catch (Exception e) {
152: log.error("Error creating rewriter class", e);
153: }
154: return null;
155: }
156:
157: /*
158: * (non-Javadoc)
159: *
160: * @see org.apache.jetspeed.rewriter.RewriterService#createParserAdaptor(java.lang.String)
161: */
162: public ParserAdaptor createParserAdaptor(String mimeType)
163: throws RewriterException {
164: try {
165: if (mimeType.equals("text/html")) {
166: return (ParserAdaptor) adaptorHtmlClass.newInstance();
167: } else if (mimeType.equals("text/xml")) {
168: return (ParserAdaptor) adaptorXmlClass.newInstance();
169: } else {
170: }
171: } catch (Exception e) {
172: log.error("Error creating rewriter class", e);
173: }
174: return null;
175: }
176:
177: /**
178: * Load the mapping file for ruleset configuration
179: *
180: */
181: protected void loadMapping() throws RewriterException {
182: try {
183: Reader reader = getReader(this .mappingFile);
184:
185: this .mapper = new Mapping();
186: InputSource is = new InputSource(reader);
187: is.setSystemId(this .mappingFile);
188: this .mapper.loadMapping(is);
189: } catch (Exception e) {
190: e.printStackTrace();
191: String msg = "RewriterService: Error in castor mapping creation";
192: log.error(msg, e);
193: throw new RewriterException(msg, e);
194: }
195: }
196:
197: protected Reader getReader(String resource)
198: throws RewriterException {
199: File file = new File(resource);
200: if (file.exists() && file.isFile() && file.canRead()) {
201: try {
202: return new FileReader(file);
203: } catch (Exception e) {
204: throw new RewriterException(
205: "could not open rewriter file " + resource, e);
206: }
207: }
208: throw new RewriterException("could not access rewriter file "
209: + resource);
210: }
211:
212: /*
213: * (non-Javadoc)
214: *
215: * @see org.apache.jetspeed.rewriter.RewriterService#lookupRuleset(java.lang.String)
216: */
217: public Ruleset lookupRuleset(String id) {
218: return (Ruleset) rulesets.get(id);
219: }
220:
221: /*
222: * (non-Javadoc)
223: *
224: * @see org.apache.jetspeed.rewriter.RewriterService#loadRuleset(java.io.Reader)
225: */
226: public Ruleset loadRuleset(Reader reader) {
227: Ruleset ruleset = null;
228: try {
229: DocumentBuilderFactory dbfactory = DocumentBuilderFactory
230: .newInstance();
231: DocumentBuilder builder = dbfactory.newDocumentBuilder();
232:
233: InputSource source = new InputSource(reader);
234:
235: Document doc = builder.parse(source);
236:
237: Unmarshaller unmarshaller = new Unmarshaller(this .mapper);
238:
239: ruleset = (Ruleset) unmarshaller.unmarshal(doc);
240: ruleset.sync();
241: rulesets.put(ruleset.getId(), ruleset);
242:
243: } catch (Throwable t) {
244: log.error("ForwardService: Could not unmarshal: " + reader,
245: t);
246: }
247:
248: return ruleset;
249: }
250:
251: }
|