Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sitemesh/Factory.java


1   package com.sitemesh;
2   
3   import java.util.Properties;
4   import javax.servlet.Config;
5   import javax.servlet.http.HttpServletRequest;
6   import com.sitemesh.mapper.DefaultDecoratorMapper;
7   import com.sitemesh.parser.*;
8   
9   /**
10   * Factory responsible for creating appropriate instances of implementations.
11   * This is a singleton and is obtained through {@link #getInstance(javax.servlet.Config)}.
12   *
13   * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
14   * @version $Revision: 1.7 $
15   */
16  public class Factory {
17  
18    /**
19     * @label Singleton instance
20     */
21    private static Factory instance;
22  
23    /**
24     * ServletConfig or FilterConfig.
25     */
26    protected Config config;
27  
28    /**
29     * Instance of DecoratorMapper.
30     * Because it is thread-safe it can be shared by multiple clients.
31     * @label creates
32     */
33    protected DecoratorMapper decoratorMapper;
34  
35    /**
36     * @directed
37     * @label creates suitable
38     */
39    /*#private PageParser lnkPageParser; */
40  
41    /**
42     * Constructor for default implementation of Factory.
43     * Should never be called by client. Singleton instance should be
44     * obtained instead.
45     *
46     * @see #getInstance(javax.servlet.Config config)
47     */
48    protected Factory(Config config) {
49      this.config = config;
50    }
51  
52    /**
53     * Entry-point for obtaining singleton instance of Factory.
54     */
55    public static Factory getInstance(Config config) {
56      // @todo: Static instance should acount for
57      //        multiple configs using same factory.
58      // @todo: Allow to return different implementations
59      //        of Factory depending on a system/config
60      //        property.
61      if (instance == null) instance = new Factory(config);
62      return instance;
63    }
64  
65    /**
66     * Return instance of DecoratorMapper.
67     */
68    public DecoratorMapper getDecoratorMapper() {
69      // @todo: Allow to return different implementations
70      //        of DecoratorMapper depending on a system/config
71      //        property.
72      if (decoratorMapper == null) {
73        try {
74          decoratorMapper = new DefaultDecoratorMapper();
75          decoratorMapper.init(config, new Properties());
76        }
77        catch (InstantiationException e) {}
78      }
79      return decoratorMapper;
80    }
81  
82    /**
83     * Create a PageParser suitable for the given content-type.
84     *
85     * <p>For example, if the supplied parameter is <code>text/html</code>
86     * a parser shall be returned that can parse HTML accordingly.</p> Never returns null.
87     *
88     * @param contentType The MIME content-type of the data to be parsed
89     * @return Appropriate <code>PageParser</code> for reading data
90     */
91    public PageParser getPageParser(String contentType) {
92      if ( contentType == null ) return defaultParser();
93      else if ( contentType.equals( "text/html" ) ) return new DOMPageParser();
94      else return defaultParser();
95    }
96  
97    /**
98     * Default PageParser if no other can be determined.
99     */
100   private PageParser defaultParser() {
101     return new AbstractPageParser() {
102       public Page parse( byte[] data ) {
103         return new UnParsedPage( data );
104       }
105     };
106   }
107 
108 }