KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > factory > BaseFactory


1 /*
2  * Title: BaseFactory
3  * Description:
4  *
5  * This software is published under the terms of the OpenSymphony Software
6  * License version 1.1, of which a copy has been included with this
7  * distribution in the LICENSE.txt file.
8  */

9
10 package com.opensymphony.module.sitemesh.factory;
11
12 import com.opensymphony.module.sitemesh.Config;
13 import com.opensymphony.module.sitemesh.DecoratorMapper;
14 import com.opensymphony.module.sitemesh.Factory;
15 import com.opensymphony.module.sitemesh.PageParser;
16 import com.opensymphony.module.sitemesh.mapper.PathMapper;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 /**
23  * Base Factory implementation. Provides utility methods for implementation.
24  *
25  * @author <a HREF="mailto:joe@truemesh.com">Joe Walnes</a>
26  * @version $Revision: 1.6 $
27  */

28 public abstract class BaseFactory extends Factory {
29     /** ServletConfig or FilterConfig. */
30     protected Config config = null;
31
32     /**
33      * Instance of {@link com.opensymphony.module.sitemesh.DecoratorMapper}.
34      * Because it is thread-safe, it can be shared by multiple clients. This
35      * is only the last DecoratorMapper in the chain, and all parents will be
36      * automatically delegated to it.
37      */

38     protected DecoratorMapper decoratorMapper = null;
39
40     /** Map that associates content-types with PageParser instances. */
41     protected Map JavaDoc pageParsers = null;
42
43     /** A map of paths that are excluded from decoration */
44     protected PathMapper excludeUrls = null;
45
46     /**
47      * Constructor for default implementation of Factory.
48      * Should never be called by client. Singleton instance should be
49      * obtained instead.
50      *
51      * @see #getInstance(com.opensymphony.module.sitemesh.Config config)
52      */

53     protected BaseFactory(Config config) {
54         this.config = config;
55         clearDecoratorMappers();
56         clearParserMappings();
57         clearExcludeUrls();
58     }
59
60     /** Return instance of DecoratorMapper. */
61     public DecoratorMapper getDecoratorMapper() {
62         return decoratorMapper;
63     }
64
65     /**
66      * Create a PageParser suitable for the given content-type.
67      *
68      * <p>For example, if the supplied parameter is <code>text/html</code>
69      * a parser shall be returned that can parse HTML accordingly. Returns
70      * null if no parser can be found for the supplied content type.</p>
71      *
72      * @param contentType The MIME content-type of the data to be parsed
73      * @return Appropriate <code>PageParser</code> for reading data, or
74      * <code>null</code> if no suitable parser was found.
75      */

76     public PageParser getPageParser(String JavaDoc contentType) {
77         return (PageParser) pageParsers.get(contentType);
78     }
79
80     /**
81      * Determine whether a Page of given content-type should be parsed or not.
82      */

83     public boolean shouldParsePage(String JavaDoc contentType) {
84         return pageParsers.containsKey(contentType);
85     }
86
87     /**
88      * Returns <code>true</code> if the supplied path matches one of the exclude
89      * URLs specified in sitemesh.xml, otherwise returns <code>false</code>.
90      * @param path
91      * @return whether the path is excluded
92      */

93     public boolean isPathExcluded(String JavaDoc path) {
94         return excludeUrls.get(path) != null;
95     }
96
97     /**
98      * Clear all current DecoratorMappers.
99      */

100     protected void clearDecoratorMappers() {
101         decoratorMapper = null;
102     }
103
104     /** Push new DecoratorMapper onto end of chain. */
105     protected void pushDecoratorMapper(String JavaDoc className, Properties JavaDoc properties) {
106         try {
107             Class JavaDoc decoratorMapperClass = null;
108             try {
109                 decoratorMapperClass = loadClass(className, getClass());
110             }
111             catch (NoClassDefFoundError JavaDoc e) {
112                 decoratorMapperClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
113             }
114             DecoratorMapper newMapper = (DecoratorMapper) decoratorMapperClass.newInstance();
115             newMapper.init(config, properties, decoratorMapper);
116             decoratorMapper = newMapper;
117         }
118         catch (ClassNotFoundException JavaDoc e) {
119             report("Could not load DecoratorMapper class : " + className, e);
120         }
121         catch (Exception JavaDoc e) {
122             report("Could not initialize DecoratorMapper : " + className, e);
123         }
124     }
125
126     public static Class JavaDoc loadClass(String JavaDoc className, Class JavaDoc callingClass) throws ClassNotFoundException JavaDoc {
127         try {
128             return Thread.currentThread().getContextClassLoader().loadClass(className);
129         } catch (ClassNotFoundException JavaDoc e) {
130             try {
131                 return Class.forName(className);
132             } catch (ClassNotFoundException JavaDoc ex) {
133                 try {
134                     return BaseFactory.class.getClassLoader().loadClass(className);
135                 } catch (ClassNotFoundException JavaDoc exc) {
136                     return callingClass.getClassLoader().loadClass(className);
137                 }
138             }
139         }
140     }
141   
142     /** Clear all PageParser mappings. */
143     protected void clearParserMappings() {
144         pageParsers = new HashMap JavaDoc();
145     }
146
147     /**
148      * Map new PageParser to given content-type. contentType = null signifies default
149      * PageParser for unknown content-types.
150      */

151     protected void mapParser(String JavaDoc contentType, String JavaDoc className) {
152         if (className.endsWith(".DefaultPageParser")) {
153             return; // Backwards compatability - this can safely be ignored.
154
}
155         try {
156             PageParser pp = (PageParser) Class.forName(className).newInstance();
157             // Store the parser even if the content type is NULL. [This
158
// is most probably the legacy default page parser which
159
// we no longer have a use for]
160
pageParsers.put(contentType, pp);
161         }
162         catch (ClassNotFoundException JavaDoc e) {
163             report("Could not load PageParser class : " + className, e);
164         }
165         catch (Exception JavaDoc e) {
166             report("Could not instantiate PageParser : " + className, e);
167         }
168     }
169
170     protected void addExcludeUrl(String JavaDoc path) {
171         excludeUrls.put("", path);
172     }
173
174     /**
175      * Clears all exclude URLs.
176      */

177     protected void clearExcludeUrls() {
178         excludeUrls = new PathMapper();
179     }
180
181 }
Popular Tags