KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > repository > Content


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.repository;
50
51 import java.io.IOException JavaDoc;
52 import java.io.InputStream JavaDoc;
53 import java.io.StringWriter JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.List JavaDoc;
56 import java.util.Locale JavaDoc;
57 import java.util.Map JavaDoc;
58
59 import com.anthonyeden.lib.util.IOUtilities;
60 import org.apache.commons.logging.Log;
61 import org.apache.commons.logging.LogFactory;
62 import org.jpublish.RequestContext;
63 import org.jpublish.SiteContext;
64 import org.jpublish.page.Page;
65 import org.jpublish.util.PathUtilities;
66 import org.jpublish.util.Property;
67 import org.jpublish.view.ViewRenderException;
68 import org.jpublish.view.ViewRenderer;
69
70 /**
71  * This class represents a single Content entity. Each Content object actually wraps a shared ContentInstance object.
72  * Methods which modify the Content's state are only effective for the current request. To change the shared
73  * ContentInstance state use the <code>getContentInstance()</code> method to retrieve the ContentInstance object and
74  * make changes.
75  *
76  * <p>Note that the methods in the Content class are not synchronized because they should not be accessed from more than
77  * a single thread (the current request thread).
78  *
79  * @author Anthony Eden
80  */

81
82 public class Content {
83
84     private Log log = LogFactory.getLog(Content.class);
85
86     private ContentInstance contentInstance = null;
87
88     private Map JavaDoc properties = new HashMap JavaDoc();
89     private String JavaDoc viewRendererName = null;
90     private Locale JavaDoc locale = Locale.getDefault();
91
92     /**
93      * Construct a new Content.
94      *
95      * @param contentInstance The wrapped ContentInstance
96      */

97
98     public Content(ContentInstance contentInstance) {
99         this.contentInstance = contentInstance;
100     }
101
102     /**
103      * Get the wrapped ContentInstance.
104      *
105      * @return The ContentInstance
106      */

107
108     public ContentInstance getContentInstance() {
109         return contentInstance;
110     }
111
112     /**
113      * Get the content's path.
114      *
115      * @return The path
116      */

117
118     public String JavaDoc getPath() {
119         return contentInstance.getPath();
120     }
121
122     /**
123      * Get the view renderer name. This method will return null if the content should use the default view renderer.
124      *
125      * @return The view renderer name or null
126      */

127
128     public String JavaDoc getViewRendererName() {
129         if (viewRendererName == null) {
130             viewRendererName = contentInstance.getViewRendererName();
131         }
132         return viewRendererName;
133     }
134
135     /**
136      * Set the view renderer name. Set this value to null to specify that the default view renderer should be used.
137      *
138      * @param viewRendererName The view renderer name or null to reset
139      */

140
141     public void setViewRendererName(String JavaDoc viewRendererName) {
142         this.viewRendererName = viewRendererName;
143     }
144
145     /**
146      * Get a List of content actions. To add an action to the content just add the action to this List. Content
147      * actions are triggered each time the content is requested.
148      *
149      * @return A List of page actions
150      */

151
152     public List JavaDoc getContentActions() {
153         return contentInstance.getContentActions();
154     }
155
156     /**
157      * Get the named page property using the default Locale. If the property is not found then return null.
158      *
159      * @param name The property name
160      * @return The value or null
161      */

162
163     public String JavaDoc getProperty(String JavaDoc name) {
164         return getProperty(name, getLocale());
165     }
166
167     /**
168      * Get the Locale-specific value for the given named property. If the property is not found then return null. This
169      * method will try to find the most suitable locale by searching the property values in the following manner:
170      *
171      * <p> language + "_" + country + "_" + variant<br> language + "_" + country<br> langauge<br> "" </p>
172      *
173      * @param name The property name
174      * @param locale The locale
175      * @return The value
176      */

177
178     public String JavaDoc getProperty(String JavaDoc name, Locale JavaDoc locale) {
179         Property property = (Property) properties.get(name);
180         if (property == null) {
181             String JavaDoc value = contentInstance.getProperty(name, locale);
182             if (value != null) {
183                 setProperty(name, value, locale);
184                 return value;
185             }
186         }
187
188         if (property == null) {
189             return null;
190         } else {
191             return property.getValue(locale);
192         }
193     }
194
195     /**
196      * Get the named property. This method is equivilent to the <code>getProperty(name)</code> method. This method is
197      * provided as a convenience to view code.
198      *
199      * @param name The property name
200      * @return The value
201      */

202
203     public String JavaDoc get(String JavaDoc name) {
204         return getProperty(name);
205     }
206
207     /**
208      * Set the property value.
209      *
210      * @param name The property name
211      * @param value The property value
212      */

213
214     public void setProperty(String JavaDoc name, String JavaDoc value) {
215         setProperty(name, value, getLocale());
216     }
217
218     /**
219      * Set the property value.
220      *
221      * @param name The property name
222      * @param value The property value
223      * @param locale The Locale
224      */

225
226     public void setProperty(String JavaDoc name, String JavaDoc value, Locale JavaDoc locale) {
227         Property property = (Property) properties.get(name);
228         if (property == null) {
229             // named property not in property map
230
property = new Property(name);
231             properties.put(name, property);
232         }
233         property.setValue(value, locale);
234     }
235
236     /**
237      * Get the content's locale.
238      *
239      * @return The Locale
240      */

241
242     public Locale JavaDoc getLocale() {
243         return locale;
244     }
245
246     /**
247      * Set the current content's locale. This locale is used when retrieving content properties. Set to null to use
248      * the system default locale.
249      *
250      * @param locale The new locale
251      */

252
253     public void setLocale(Locale JavaDoc locale) {
254         if (locale == null) {
255             locale = Locale.getDefault();
256         }
257         this.locale = locale;
258     }
259
260     /**
261      * Execute the content actions.
262      */

263
264     public void executeActions() {
265         log.debug("Executing content actions");
266         contentInstance.executeActions();
267     }
268
269     /**
270      * Return a String representation of the content. Invoking this message will cause any content actions to be
271      * executed. After executing content actions this method will delegate to the ContentInstance object.
272      *
273      * @return The content as a String
274      */

275
276     public String JavaDoc getContentString() throws IOException JavaDoc {
277         if (log.isDebugEnabled()) {
278             log.debug("getContentString() [path=" +
279                     contentInstance.getPath());
280         }
281         executeActions();
282         return contentInstance.getContentString();
283     }
284
285     /**
286      * Return an InputStream for the content. Invoking this method will cause any content actions to be executed. After
287      * executing content actions this method will delegate to the ContentInstance object.
288      *
289      * @return The content InputStream
290      */

291
292     public InputStream JavaDoc getContentInputStream() throws IOException JavaDoc {
293         if (log.isDebugEnabled()) {
294             log.debug("getContentInputStream() [path=" +
295                     contentInstance.getPath());
296         }
297         executeActions();
298         return contentInstance.getContentInputStream();
299     }
300
301     /**
302      * Get the content from the given path and merge it with the given context.
303      *
304      * @param path The content path
305      * @param context The current context
306      * @return The content as a String
307      * @throws IOException
308      * @throws ContentNotFoundException
309      */

310
311     public String JavaDoc render(RequestContext context) throws IOException JavaDoc,
312             ViewRenderException {
313         if (log.isDebugEnabled()) {
314             log.debug("render() [path=" + contentInstance.getPath());
315         }
316
317         String JavaDoc path = getPath();
318
319         if (log.isDebugEnabled()) {
320             log.debug("Rendering content: " + path);
321         }
322
323         StringWriter JavaDoc writer = null;
324         try {
325             writer = new StringWriter JavaDoc();
326
327             Repository repository = contentInstance.getRepository();
328             String JavaDoc name = PathUtilities.makeRepositoryURI(repository.getName(), path);
329             getViewRenderer(context).render(context, name, writer);
330
331             return writer.toString();
332         } finally {
333             IOUtilities.close(writer);
334         }
335     }
336
337     /**
338      * Return a String representation of the content.
339      *
340      * @return The content as a String
341      */

342
343     public String JavaDoc toString() {
344         try {
345             return getContentString();
346         } catch (IOException JavaDoc e) {
347             log.error("IO exception: " + e.getMessage());
348             if (log.isDebugEnabled()) {
349                 e.printStackTrace();
350             }
351             return "IOException: " + e.getMessage();
352         }
353     }
354
355     /**
356      * Return ViewRenderer. This method will first look for a view renderer name using the
357      * Content.getViewRendererName(). If this value is null then the Page.getViewRendererName() will be used. If the
358      * page is null or Page.getViewRendererName() returns null then the default ViewRenderer will be used.
359      *
360      * @param context The RequestContext
361      * @return The ViewRenderer
362      */

363
364     protected ViewRenderer getViewRenderer(RequestContext context) {
365         Page page = context.getPage();
366         SiteContext siteContext = contentInstance.getSiteContext();
367         ViewRenderer renderer = siteContext.getViewRenderer();
368         String JavaDoc viewRendererName = getViewRendererName();
369         if (viewRendererName != null) {
370             if (log.isDebugEnabled()) {
371                 log.debug("View renderer name: " + viewRendererName);
372             }
373             renderer = siteContext.getViewRenderer(viewRendererName);
374         } else if (page != null) {
375             log.debug("Page is not null...checking view renderer name");
376             viewRendererName = page.getViewRendererName();
377             if (log.isDebugEnabled()) {
378                 log.debug("View renderer name: " + viewRendererName);
379             }
380             if (viewRendererName != null) {
381                 renderer = siteContext.getViewRenderer(viewRendererName);
382             }
383         }
384         return renderer;
385     }
386
387 }
388
Popular Tags