Source Code Cross Referenced for JetspeedRewriterController.java in  » Portal » jetspeed-2.1.3 » org » apache » jetspeed » rewriter » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » jetspeed 2.1.3 » org.apache.jetspeed.rewriter 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.