KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > page > PageDefinition


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.page;
50
51 import java.io.InputStream JavaDoc;
52 import java.io.InputStreamReader JavaDoc;
53 import java.io.Reader JavaDoc;
54 import java.io.UnsupportedEncodingException JavaDoc;
55 import java.util.HashMap JavaDoc;
56 import java.util.Map JavaDoc;
57
58 import com.anthonyeden.lib.config.Configuration;
59 import com.anthonyeden.lib.config.ConfigurationException;
60 import com.anthonyeden.lib.config.sax.SAXConfigurationFactory;
61 import org.jpublish.SiteContext;
62 import org.jpublish.util.PathUtilities;
63 import org.jpublish.util.encoding.CharacterEncodingManager;
64 import org.jpublish.util.encoding.CharacterEncodingMap;
65
66 /**
67  * A class which represents a page definition.
68  *
69  * @author Anthony Eden
70  */

71
72 public class PageDefinition {
73
74     private SiteContext siteContext = null;
75     private String JavaDoc path = null;
76     private Configuration configuration = null;
77     private Map JavaDoc pageCache = new HashMap JavaDoc();
78
79     /**
80      * Construct a new PageDefinition for the given path.
81      *
82      * @param siteContext The SiteContext
83      * @param path The definition path
84      */

85
86     public PageDefinition(SiteContext siteContext, String JavaDoc path) {
87         this.siteContext = siteContext;
88         this.path = path;
89
90         pageCache = new HashMap JavaDoc();
91     }
92
93     /**
94      * Get the request path.
95      *
96      * @return The request path
97      */

98
99     public String JavaDoc getPath() {
100         return path;
101     }
102
103     /**
104      * Return a Page instance for the given path. The path is used to determine the page name and page type. The
105      * PageDefinition will cache all Page instances.
106      *
107      * @param path The request path
108      * @return The Page instance
109      * @throws ConfigurationException
110      */

111
112     public synchronized PageInstance getPageInstance(String JavaDoc path)
113             throws ConfigurationException {
114         PageInstance page = (PageInstance) pageCache.get(path);
115         if (page == null) {
116             page = new PageInstance(siteContext, path,
117                     PathUtilities.extractPageName(path),
118                     PathUtilities.extractPageType(path),
119                     PathUtilities.extractPageParent(path));
120             page.loadConfiguration(configuration);
121             pageCache.put(path, page);
122         }
123         return page;
124     }
125
126     /**
127      * Load the configuration from the specified Reader.
128      *
129      * @param in The Reader
130      * @throws ConfigurationException
131      */

132
133     public void loadConfiguration(Reader JavaDoc in) throws ConfigurationException {
134         Configuration configuration =
135                 SAXConfigurationFactory.getInstance().getConfiguration(path, in);
136         loadConfiguration(configuration);
137     }
138
139     /**
140      * Load the configuration from the specified InputStream.
141      *
142      * @param in The InputStream
143      * @throws ConfigurationException
144      */

145
146     public void loadConfiguration(InputStream JavaDoc in)
147             throws ConfigurationException {
148         CharacterEncodingManager characterEncodingManager =
149                 siteContext.getCharacterEncodingManager();
150         CharacterEncodingMap characterEncodingMap =
151                 characterEncodingManager.getMap(path);
152         try {
153             //loadConfiguration(new InputStreamReader(in, "UTF-8"));
154
loadConfiguration(new InputStreamReader JavaDoc(in,
155                     characterEncodingMap.getPageEncoding()));
156         } catch (UnsupportedEncodingException JavaDoc e) {
157             throw new ConfigurationException("Error reading page configuration", e);
158         }
159         /*Configuration configuration =
160             SAXConfigurationFactory.getInstance().getConfiguration(path, in);
161         loadConfiguration(configuration);
162         */

163     }
164
165     /**
166      * Load the configuration.
167      *
168      * @param configuration The Configuration object
169      * @throws ConfigurationException
170      */

171
172     public void loadConfiguration(Configuration configuration)
173             throws ConfigurationException {
174         this.configuration = configuration;
175         pageCache.clear();
176     }
177
178 }
179
Popular Tags