KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > page > Page


1
2 package com.openedit.page;
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.InputStreamReader JavaDoc;
6 import java.io.Reader JavaDoc;
7 import java.io.StringWriter JavaDoc;
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Date JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Locale JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.openedit.repository.ContentItem;
18 import org.openedit.repository.RepositoryException;
19
20 import com.openedit.Generator;
21 import com.openedit.OpenEditException;
22 import com.openedit.WebPageRequest;
23 import com.openedit.error.ContentNotAvailableException;
24 import com.openedit.generators.Output;
25 import com.openedit.util.FileUtils;
26 import com.openedit.util.OutputFiller;
27 import com.openedit.util.PathUtilities;
28 import com.openedit.util.strainer.Filter;
29
30
31 public class Page
32 {
33     private static Log log = LogFactory.getLog(Page.class);
34     public static final String JavaDoc BLANK_LAYOUT = "NOLAYOUT";
35     
36     protected String JavaDoc fieldPath;
37     protected PageSettings fieldPageSettings;
38     protected List JavaDoc fieldGenerators;
39     protected ContentItem fieldContentItem;
40     protected Date JavaDoc fieldOriginalyModified;
41     protected boolean fieldOriginalyExistedContentPath; //used to see if a new file has need added or removed
42

43     public Page( String JavaDoc inPath, PageSettings inMetaData )
44     {
45         fieldPath = inPath;
46         setPageSettings(inMetaData );
47     }
48     /**
49      *
50      */

51     public Page()
52     {
53     }
54     public Page( Page inPage )
55     {
56         this( inPage.getPath(), inPage.getPageSettings() );
57         fieldContentItem = inPage.getContentItem();
58     }
59     /**
60      * Returns the filter that the currently logged-in user must satisfy in order for this page to
61      * be edited.
62      *
63      * @return Filter
64      */

65     public Filter getEditFilter()
66     {
67         return getPageSettings().getEditFilter();
68     }
69
70     public boolean isBinary()
71     {
72         if ( getMimeType() == null)
73         {
74             return false; //There are many more text formats we support
75
}
76         if( getMimeType().startsWith("text") )
77         {
78             return false;
79         }
80         if( getMimeType().indexOf("xml") > -1 )
81         {
82             return false;
83         }
84         return true;
85         
86     }
87     
88     public boolean isFolder()
89     {
90         return getContentItem().isFolder();
91     }
92     
93     public boolean exists()
94     {
95         return getContentItem().exists();
96     }
97
98     public Date JavaDoc getLastModified()
99     {
100         return getContentItem().lastModified();
101     }
102
103     /**
104      * This is asking if the settings have been changed under it
105      * @return
106      */

107     
108     public boolean isCurrent()
109     {
110         //boolean isZero = getLastModified().getTime() == 0;
111
if ( getLastModified() == null)
112         {
113             return false;
114         }
115         boolean pageCurrent = getLastModified().equals( getOriginalyModified() );
116         if( !pageCurrent)
117         {
118             return false;
119         }
120         boolean metaDataCurrent = getPageSettings().isCurrent();
121         return metaDataCurrent;
122     }
123     /**
124      * DOCME
125      *
126      * @param inDateFormat DOCME
127      *
128      * @return DOCME
129      */

130     public String JavaDoc getLastModified(String JavaDoc inDateFormat)
131     {
132         SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(inDateFormat);
133
134         return format.format(getLastModified());
135     }
136
137     /**
138      * DOCME
139      *
140      * @return DOCME
141      */

142     public String JavaDoc getMimeType()
143     {
144         return getPageSettings().getMimeType();
145     }
146
147     /**
148      * Get a List of page actions. To add an action to the page just add the action to this List.
149      * Page actions are triggered each time the page is requested.
150      *
151      * @return A List of page actions
152      */

153     public List JavaDoc getPageActions()
154     {
155         List JavaDoc actions = getPageSettings().getPageActions();
156         if( isHtml() )
157         {
158             return actions;
159         }
160         List JavaDoc copy = new ArrayList JavaDoc(actions.size());
161         for (Iterator JavaDoc iter = actions.iterator(); iter.hasNext();)
162         {
163             PageAction action = (PageAction) iter.next();
164             if ( action.isIncludesAll() )
165             {
166                 copy.add(action);
167             }
168         }
169         return copy;
170
171     }
172     /**
173      * Get a List of page actions. To add an action to the page just add the action to this List.
174      * Page actions are triggered each time the page is requested.
175      *
176      * @return A List of page actions
177      */

178     public List JavaDoc getPathActions()
179     {
180         List JavaDoc actions = getPageSettings().getPathActions();
181         if( isHtml() )
182         {
183             return actions;
184         }
185         List JavaDoc copy = new ArrayList JavaDoc(actions.size());
186         for (Iterator JavaDoc iter = actions.iterator(); iter.hasNext();)
187         {
188             PageAction action = (PageAction) iter.next();
189             if ( action.isIncludesAll() )
190             {
191                 copy.add(action);
192             }
193         }
194         return copy;
195     }
196
197     /**
198      * Get the request path.
199      *
200      * @return The request path
201      */

202     public String JavaDoc getPath()
203     {
204         return fieldPath;
205     }
206
207     public String JavaDoc getDirectory()
208     {
209         String JavaDoc path = PathUtilities.extractDirectoryPath(getPath());
210         // urlpath is the address the link came in on
211
return path;
212     }
213     public String JavaDoc getDirectoryRoot()
214     {
215         int slash = getPath().indexOf('/',1);
216         if( slash > 1)
217         {
218             return getPath().substring(0,slash);
219         }
220         // urlpath is the address the link came in on
221
return "";
222     }
223
224     public String JavaDoc getName()
225     {
226         String JavaDoc path = PathUtilities.extractFileName(getPath());
227         // urlpath is the address the link came in on
228
return path;
229     }
230     
231     /**
232      * Get the named page property using the default Locale. If the property is not found then
233      * return null.
234      *
235      * @param name The property name
236      *
237      * @return The value or null
238      */

239     public String JavaDoc getProperty(String JavaDoc name)
240     {
241         return getProperty(name, (Locale JavaDoc)null);
242     }
243     public String JavaDoc getProperty(String JavaDoc name, String JavaDoc language)
244     {
245         if( language != null )
246         {
247 // String[] locales = language.split("_");
248
// String lang = locales[0];
249
// String code2 = "";
250
// String code3 = "";
251
// if( locales.length> 1)
252

253 // Locale loc = new Locale();
254
PageProperty property = (PageProperty) getPageSettings().getProperty(name);
255             if (property != null)
256             {
257                 return property.getValue(language);
258             }
259             else
260             {
261                 return null;
262             }
263         }
264         else
265         {
266             return getProperty(name,(Locale JavaDoc)null );
267         }
268     }
269     /**
270      * Get the Locale-specific value for the given named property. If the property is not found
271      * then return null. This method will try to find the most suitable locale by searching the
272      * property values in the following manner:
273      *
274      * <p>
275      * language + "_" + country + "_" + variant<br> language + "_" + country<br> langauge<br> ""
276      * </p>
277      *
278      * @param name The property name
279      * @param locale The locale
280      *
281      * @return The value
282      */

283     public String JavaDoc getProperty(String JavaDoc name, Locale JavaDoc language)
284     {
285         debug("Get property [name=" + name + ",locale=" + language + "]");
286         PageProperty property = (PageProperty) getPageSettings().getProperty(name);
287
288         if (property != null)
289         {
290             return property.getValue(language);
291         }
292         else
293         {
294             return null;
295         }
296     }
297     public void debug( String JavaDoc inMessage )
298     {
299         log.debug( inMessage );
300         //System.out.println( inMessage );
301
}
302
303     /**
304      * Returns the filter that the currently logged-in user must satisfy in order for this page to
305      * be viewed.
306      *
307      * @return Filter
308      */

309     public Filter getViewFilter()
310     {
311         return getPageSettings().getViewFilter();
312     }
313
314     /**
315      * Get the named property. This method is equivilent to the <code>getProperty(name)</code>
316      * method. This method is provided as a convenience to Velocity code.
317      *
318      * @param name The property name
319      *
320      * @return The value
321      */

322     public String JavaDoc get(String JavaDoc name)
323     {
324         //debug("get(" + name + ") called to retrieve property");
325
return getProperty(name);
326     }
327
328
329     /**
330      * DOCME
331      *
332      * @return DOCME
333      */

334     public String JavaDoc toString()
335     {
336         return getPath();
337     }
338
339     public void setPageSettings(PageSettings inSettings)
340     {
341         fieldPageSettings = inSettings;
342     }
343     public PageSettings getPageSettings()
344     {
345         return fieldPageSettings;
346     }
347     
348     /**
349      * @return
350      */

351     public String JavaDoc getAlternateContentPath()
352     {
353         return getPageSettings().getAlternateContentPath();
354     }
355     public List JavaDoc fieldGenerators()
356     {
357         return fieldGenerators;
358     }
359     /**
360      * @return
361      */

362     public List JavaDoc getGenerator()
363     {
364         if ( fieldGenerators == null)
365         {
366             fieldGenerators = getPageSettings().getGenerators();
367         }
368         return fieldGenerators;
369     }
370     
371     public InputStream JavaDoc getInputStream() throws ContentNotAvailableException
372     {
373         try
374         {
375             return getContentItem().getInputStream();
376         }
377         catch( RepositoryException e )
378         {
379             throw new ContentNotAvailableException(e.getMessage(), getPath() );
380         }
381     }
382     /**
383      * @return
384      */

385     public String JavaDoc getLayout()
386     {
387         return getPageSettings().getLayout();
388     }
389     
390     public String JavaDoc getInnerLayout()
391     {
392         PageSettings parent = getPageSettings();
393         while( parent != null)
394         {
395             String JavaDoc layout = parent.getInnerLayout();
396             if( layout == null || !layout.equals(getPath()))
397             {
398                 return layout;
399             }
400             parent = parent.getParent();
401         }
402         return null;
403     }
404     
405     public boolean hasLayout()
406     {
407         String JavaDoc layout = getLayout();
408         
409         return layout != null && !layout.equals(BLANK_LAYOUT);
410     }
411     public boolean hasInnerLayout()
412     {
413         String JavaDoc il = getInnerLayout();
414         if ( il == null)
415         {
416             return false;
417         }
418         if ( il.equals(BLANK_LAYOUT))
419         {
420             return false;
421         }
422         if( il.equalsIgnoreCase(getPath()))
423         {
424             return false;
425         }
426         return true;
427     }
428     
429     public ContentItem getContentItem()
430     {
431         return fieldContentItem;
432     }
433     public void setContentItem( ContentItem revision )
434     {
435         fieldContentItem = revision;
436         if ( revision != null)
437         {
438             fieldOriginalyModified = revision.lastModified();
439         }
440     }
441     
442     public Reader JavaDoc getReader() throws OpenEditException
443     {
444         if ( exists() )
445         {
446             InputStreamReader JavaDoc in = null;
447             try
448             {
449                 in = new InputStreamReader JavaDoc( getContentItem().getInputStream(), getCharacterEncoding() );
450             } catch ( Exception JavaDoc ex )
451             {
452                 throw new OpenEditException(ex);
453             }
454             return in;
455         }
456         else
457         {
458             throw new ContentNotAvailableException("No such page " + getPath(),getPath() );
459         }
460     }
461     protected Date JavaDoc getOriginalyModified()
462     {
463         return fieldOriginalyModified;
464     }
465     /* (non-javadoc)
466      * @see org.jpublish.Page#getCharacterEncoding()
467      */

468     public String JavaDoc getCharacterEncoding()
469     {
470         //Its UTF-8 unless otherwise setup in the xconf's
471
String JavaDoc encoding = getPageSettings().getPageCharacterEncoding();
472         if ( encoding == null)
473         {
474             return "UTF-8";
475         }
476         return encoding;
477     }
478
479     public String JavaDoc getContent() throws OpenEditException
480     {
481         StringWriter JavaDoc out = new StringWriter JavaDoc();
482         Reader JavaDoc reader = null;
483         try
484         {
485             reader = getReader();
486             new OutputFiller().fill( reader, out);
487         }
488         catch (IOException JavaDoc ex)
489         {
490             log.error( ex );
491             throw new OpenEditException(ex);
492         }
493         finally
494         {
495             FileUtils.safeClose(reader);
496         }
497         return out.toString();
498     }
499     /**
500      * @param inReq
501      */

502     public WebPageRequest generate(WebPageRequest inReq, Output inOut) throws OpenEditException
503     {
504         WebPageRequest req = inReq;
505         if ( inReq.getPage() != this)
506         {
507             req = inReq.copy(this);
508         }
509         if ( !req.hasRedirected() )
510         {
511             for (Iterator JavaDoc iter = getGenerator().iterator(); iter.hasNext();)
512             {
513                 Generator gen= (Generator) iter.next();
514                 if ( gen.canGenerate(inReq))
515                 {
516                     gen.generate(req,this,inOut);
517                     break;
518                 }
519             }
520         }
521         return req;
522     }
523     /**
524      * @return
525      */

526     public boolean isHtml() {
527         String JavaDoc mime = getMimeType();
528         if ( mime != null && mime.endsWith("html"))
529         {
530             return true;
531         }
532         return false;
533     }
534     public boolean isImage() {
535         String JavaDoc mime = getMimeType();
536         if ( mime != null && mime.startsWith("image"))
537         {
538             return true;
539         }
540         return false;
541     }
542     public boolean isOriginalyExistedContentPath()
543     {
544         return fieldOriginalyExistedContentPath;
545     }
546     public void setOriginalyExistedContentPath(boolean inOriginalContentPathMissing)
547     {
548         fieldOriginalyExistedContentPath = inOriginalContentPathMissing;
549     }
550     public boolean isDraft()
551     {
552         if( getContentItem().getActualPath().indexOf(".draft.") > -1 && exists() )
553         {
554             return true;
555         }
556         return false;
557     }
558 }
559
Popular Tags