KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Jul 21, 2004
3  */

4 package com.openedit.page;
5
6 import java.io.InputStreamReader JavaDoc;
7 import java.io.Reader JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Locale JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.openedit.repository.ContentItem;
18
19 import com.openedit.OpenEditException;
20 import com.openedit.config.Configuration;
21 import com.openedit.util.strainer.Filter;
22
23 /**
24  * @author Matthew Avery, mavery@einnovation.com
25  *
26  * This class represents the possible meta data for a Page
27  */

28 public class PageSettings
29 {
30     private static Log log = LogFactory.getLog(PageSettings.class);
31     
32     //public static final PageSettings BLANK_META_DATA = new BlankPageSettings();
33
protected ContentItem fieldXConf;
34     protected Configuration fieldUserDefinedData;
35     protected long fieldModifiedTime;
36     
37     protected String JavaDoc fieldLayout;
38     protected String JavaDoc fieldInnerLayout;
39     protected List JavaDoc fieldGenerators;
40     protected Map JavaDoc fieldProperties;
41     protected List JavaDoc fieldPageActions;
42     protected List JavaDoc fieldPathActions;
43     protected String JavaDoc fieldAlternateContentPath;
44     protected Filter fieldEditFilter;
45     protected Filter fieldViewFilter;
46     protected String JavaDoc fieldPageCharacterEncoding;
47     protected boolean fieldModified = false;
48     protected String JavaDoc fieldMimeType;
49     protected PageSettings fieldFallBack;
50     protected PageSettings fieldParent;
51     protected String JavaDoc fieldDefaultLanguage;
52     
53     public String JavaDoc getAlternateContentPath()
54     {
55         PageSettings parent = this;
56         PageSettings fallbackparent = getFallback();
57         while ( parent != null)
58         {
59             if( parent.fieldAlternateContentPath != null) //now check the real parent
60
{
61                 return parent.fieldAlternateContentPath;
62             }
63             if ( fallbackparent != null) //first check the mirror site
64
{
65                 if( fallbackparent.fieldAlternateContentPath != null)
66                 {
67                     return fallbackparent.fieldAlternateContentPath;
68                 }
69                 fallbackparent = fallbackparent.getParent(); //mirror site parent
70
}
71             parent = parent.getParent();
72         }
73         return null;
74     }
75     public void setAlternateContentPath( String JavaDoc alternateContentPath )
76     {
77         fieldAlternateContentPath = alternateContentPath;
78     }
79     public String JavaDoc getPath()
80     {
81         return getXConf().getPath();
82     }
83     public String JavaDoc toString()
84     {
85         if ( fieldXConf != null)
86         {
87             return getXConf().getPath();
88         }
89         return super.toString();
90         
91     }
92     public List JavaDoc getGenerators()
93     {
94         //add top level parents last
95
List JavaDoc finalList = new ArrayList JavaDoc(4);
96     
97         PageSettings parent = this;
98         PageSettings fallbackparent = getFallback();
99         while ( parent != null)
100         {
101             if( parent.fieldGenerators != null)
102             {
103                 finalList.addAll(parent.fieldGenerators);
104             }
105             if ( fallbackparent != null) //first check the mirror site
106
{
107                 if( fallbackparent.fieldGenerators != null)
108                 {
109                     finalList.addAll(fallbackparent.fieldGenerators);
110                 }
111                 fallbackparent = fallbackparent.getParent();
112             }
113             parent = parent.getParent();
114         }
115         return finalList;
116     }
117     public void setGenerators( List JavaDoc generators )
118     {
119         fieldGenerators = generators;
120     }
121     public String JavaDoc getLayout()
122     {
123         PageSettings parent = this;
124         PageSettings fallbackparent = getFallback();
125         while ( parent != null)
126         {
127             if( parent.fieldLayout != null) //now check the real parent
128
{
129                 return parent.fieldLayout;
130             }
131             if ( fallbackparent != null) //first check the mirror site
132
{
133                 if( fallbackparent.fieldLayout != null)
134                 {
135                     return fallbackparent.fieldLayout;
136                 }
137                 fallbackparent = fallbackparent.getParent(); //mirror site parent
138
}
139             parent = parent.getParent();
140         }
141         return null;
142     }
143     public String JavaDoc getInnerLayout()
144     {
145         PageSettings parent = this;
146         PageSettings fallbackparent = getFallback();
147         while ( parent != null)
148         {
149             if( parent.fieldInnerLayout != null) //now check the real parent
150
{
151                 return parent.fieldInnerLayout;
152             }
153             if ( fallbackparent != null) //first check the mirror site
154
{
155                 if( fallbackparent.fieldInnerLayout != null)
156                 {
157                     return fallbackparent.fieldInnerLayout;
158                 }
159                 fallbackparent = fallbackparent.getParent(); //mirror site parent
160
}
161             parent = parent.getParent();
162         }
163         return null;
164     }
165     protected void debug( String JavaDoc inMessage )
166     {
167         log.debug( inMessage );
168         //System.out.println( inMessage );
169
}
170     
171     public void setLayout( String JavaDoc layout )
172     {
173         fieldLayout = layout;
174     }
175     public void setInnerLayout( String JavaDoc innerLayout )
176     {
177         fieldInnerLayout = innerLayout;
178     }
179     public List JavaDoc getPageActions()
180     {
181         //add top level parents last
182
List JavaDoc finalList = new ArrayList JavaDoc();
183     
184         PageSettings parent = this;
185         PageSettings fallbackparent = getFallback();
186         while ( parent != null)
187         {
188             if( parent.fieldPageActions != null)
189             {
190                 finalList.addAll(0,parent.fieldPageActions);
191             }
192             if ( fallbackparent != null) //first check the mirror site
193
{
194                 if( fallbackparent.fieldPageActions != null)
195                 {
196                     finalList.addAll(0,fallbackparent.fieldPageActions);
197                 }
198                 fallbackparent = fallbackparent.getParent();
199             }
200             parent = parent.getParent();
201         }
202         return finalList;
203
204     }
205     public void setPageActions( List JavaDoc pageActions )
206     {
207         fieldPageActions = pageActions;
208     }
209     public List JavaDoc getPathActions()
210     {
211         //add top level parents last
212
List JavaDoc finalList = new ArrayList JavaDoc(4);
213     
214         PageSettings parent = this;
215         PageSettings fallbackparent = getFallback();
216         while ( parent != null)
217         {
218             if( parent.fieldPathActions != null)
219             {
220                 finalList.addAll(0,parent.fieldPathActions);
221             }
222             if ( fallbackparent != null) //first check the mirror site
223
{
224                 //dont count this unless we are at the same depth
225
if( fallbackparent.fieldPathActions != null)
226                 {
227                     finalList.addAll(0,fallbackparent.fieldPathActions);
228                 }
229                 fallbackparent = fallbackparent.getParent();
230             }
231             parent = parent.getParent();
232         }
233         return finalList; //oldest on bottom
234
}
235
236     protected int getDepth()
237     {
238         int i = 0;
239         PageSettings settings = this;
240         while( settings != null)
241         {
242             i++;
243             settings = settings.getParent();
244         }
245         return i;
246     }
247     public Map JavaDoc getProperties()
248     {
249         if ( fieldProperties == null )
250         {
251             fieldProperties = new HashMap JavaDoc();
252         }
253         return fieldProperties;
254     }
255     public List JavaDoc getAllPropertyKeysWithoutPrefix(String JavaDoc inPrefix)
256     {
257         List JavaDoc all = new ArrayList JavaDoc();
258         PageSettings parent = this;
259         PageSettings fallbackparent = getFallback();
260         while ( parent != null)
261         {
262             if( parent.fieldProperties != null)
263             {
264                 for (Iterator JavaDoc iter = parent.fieldProperties.keySet().iterator(); iter.hasNext();)
265                 {
266                     String JavaDoc key = (String JavaDoc ) iter.next();
267                     if( key.startsWith(inPrefix))
268                     {
269                         key = key.substring(inPrefix.length());
270                         all.add(key);
271                     }
272                 }
273             }
274             if ( fallbackparent != null) //first check the mirror site
275
{
276                 if( fallbackparent.fieldProperties != null)
277                 {
278                     for (Iterator JavaDoc iter = fallbackparent.fieldProperties.keySet().iterator(); iter.hasNext();)
279                     {
280                         String JavaDoc key = (String JavaDoc) iter.next();
281                         if( key.startsWith(inPrefix))
282                         {
283                             key = key.substring(inPrefix.length());
284                             all.add(key);
285                         }
286                     }
287                 }
288                 fallbackparent = fallbackparent.getParent();
289             }
290             parent = parent.getParent();
291         }
292         return all;
293         
294     }
295     
296     public PageProperty getProperty(String JavaDoc inKey)
297     {
298         PageSettings parent = this;
299         PageSettings fallbackparent = getFallback();
300         while ( parent != null)
301         {
302             if( parent.fieldProperties != null)
303             {
304                 PageProperty val = parent.getFieldProperty(inKey);
305                 if ( val != null)
306                 {
307                     return val;
308                 }
309             }
310             if ( fallbackparent != null) //first check the mirror site
311
{
312                 if( fallbackparent.fieldProperties != null)
313                 {
314                     PageProperty val = (PageProperty)fallbackparent.getFieldProperty(inKey);
315                     if ( val != null)
316                     {
317                         return val;
318                     }
319                 }
320                 fallbackparent = fallbackparent.getParent();
321             }
322             parent = parent.getParent();
323         }
324         return null;
325
326     }
327     public PageProperty getFieldProperty(String JavaDoc inKey )
328     {
329         PageProperty val = (PageProperty)getProperties().get(inKey);
330         if (val != null )
331         {
332             val.setPath(getXConf().getPath());
333         }
334         return val;
335     }
336     public void setProperties( Map JavaDoc properties )
337     {
338         fieldProperties = properties;
339     }
340     public void putProperty(PageProperty inProperty)
341     {
342         getProperties().put( inProperty.getName(), inProperty);
343     }
344
345     public Filter getEditFilter()
346     {
347         PageSettings parent = this;
348         PageSettings fallbackparent = getFallback();
349         while ( parent != null)
350         {
351             if( parent.fieldEditFilter != null)
352             {
353                 return parent.fieldEditFilter;
354             }
355             if ( fallbackparent != null)
356             {
357                 if( fallbackparent.fieldEditFilter != null)
358                 {
359                     return fallbackparent.fieldEditFilter;
360                 }
361                 fallbackparent = fallbackparent.getParent();
362             }
363             parent = parent.getParent();
364         }
365         return null;
366     }
367     public void setEditFilter( Filter editFilter )
368     {
369         fieldEditFilter = editFilter;
370     }
371     public Filter getViewFilter()
372     {
373         PageSettings parent = this;
374         PageSettings fallbackparent = getFallback();
375         while ( parent != null)
376         {
377             if( parent.fieldViewFilter != null)
378             {
379                 return parent.fieldViewFilter;
380             }
381             if ( fallbackparent != null) //first check the mirror site
382
{
383                 if( fallbackparent.fieldViewFilter != null)
384                 {
385                     return fallbackparent.fieldViewFilter;
386                 }
387                 fallbackparent = fallbackparent.getParent();
388             }
389             parent = parent.getParent();
390         }
391         return null;
392     }
393     public void setViewFilter( Filter viewFilter )
394     {
395         fieldViewFilter = viewFilter;
396     }
397     public PageSettings getFallback()
398     {
399         return fieldFallBack;
400     }
401     /**
402      * @param inParent
403      */

404     public void setFallBack(PageSettings inParent)
405     {
406         fieldFallBack = inParent;
407         
408     }
409
410     public boolean isCurrent()
411     {
412         PageSettings parent = this;
413         PageSettings fallbackparent = getFallback();
414         while ( parent != null)
415         {
416             if( !parent.fieldIsCurrent() ) //check the real parent
417
{
418                 return false;
419             }
420             if ( fallbackparent != null)
421             {
422                 if( !fallbackparent.fieldIsCurrent() )
423                 {
424                     return false;
425                 }
426                 fallbackparent = fallbackparent.getParent(); //mirror site parent
427
}
428             parent = parent.getParent();
429         }
430         return true;
431     }
432     public boolean fieldIsCurrent()
433     {
434         if ( getXConf().lastModified() == null)
435         {
436             return false;
437         }
438         boolean self = getXConf().lastModified().getTime() == getModifiedTime();
439         return self;
440     }
441     public boolean exists()
442     {
443         return getXConf().exists();
444     }
445     public ContentItem getXConf()
446     {
447         return fieldXConf;
448     }
449     public void setXConf(ContentItem inConf)
450     {
451         fieldXConf = inConf;
452         if ( inConf != null && inConf.lastModified() != null)
453         {
454             setModifiedTime( inConf.lastModified().getTime() );
455         }
456         else
457         {
458             setModifiedTime( -1 ); //TODO: Why not have 0 in here?
459
}
460     }
461     /**
462      * @return
463      */

464     public Reader JavaDoc getReader() throws OpenEditException
465     {
466         try
467         {
468             String JavaDoc enc = getPageCharacterEncoding();
469             if ( enc != null)
470             {
471                 return new InputStreamReader JavaDoc(getXConf().getInputStream(), enc );
472             }
473             return new InputStreamReader JavaDoc(getXConf().getInputStream(), "UTF-8");
474
475         }
476         catch ( Exception JavaDoc ex )
477         {
478             throw new OpenEditException(ex);
479         }
480     }
481     public long getModifiedTime()
482     {
483         return fieldModifiedTime;
484     }
485     public void setModifiedTime(long inLastModifiedTime)
486     {
487         fieldModifiedTime = inLastModifiedTime;
488     }
489     /**
490      * @param inString
491      */

492     public void setPageCharacterEncoding(String JavaDoc inString)
493     {
494         fieldPageCharacterEncoding = inString;
495     }
496     public String JavaDoc getPageCharacterEncoding()
497     {
498         PageSettings parent = this;
499         PageSettings fallbackparent = getFallback();
500         while ( parent != null)
501         {
502             if( parent.fieldPageCharacterEncoding != null) //now check the real parent
503
{
504                 return parent.fieldPageCharacterEncoding;
505             }
506             if ( fallbackparent != null) //first check the mirror site
507
{
508                 if( fallbackparent.fieldPageCharacterEncoding != null)
509                 {
510                     return fallbackparent.fieldPageCharacterEncoding;
511                 }
512                 fallbackparent = fallbackparent.getParent(); //mirror site parent
513
}
514             parent = parent.getParent();
515         }
516         return null;
517
518     }
519     public Configuration getUserDefinedData()
520     {
521         return fieldUserDefinedData;
522     }
523     public void setUserDefinedData(Configuration inUserDefinedData)
524     {
525         fieldUserDefinedData = inUserDefinedData;
526     }
527     /**
528      * @param inString
529      * @return
530      */

531     public Configuration getUserDefined(String JavaDoc inString)
532     {
533         if ( getUserDefinedData() != null)
534         {
535             Configuration config = getUserDefinedData().getChild(inString);
536             if ( config != null )
537             {
538                 return config;
539             }
540         }
541         return null;
542     }
543     /**
544      * @param inList
545      */

546     public void setPathActions(List JavaDoc inList)
547     {
548         fieldPathActions = inList;
549     }
550     /**
551      * @param inString
552      * @return
553      */

554     public String JavaDoc getPropertyValue(String JavaDoc inString, Locale JavaDoc inLocale)
555     {
556         PageProperty prop = getProperty(inString);
557         if ( prop != null)
558         {
559             return prop.getValue(inLocale);
560         }
561         return null;
562     }
563     
564     /**
565      * @return Returns the mimeType.
566      */

567     public String JavaDoc getMimeType() {
568         return fieldMimeType;
569     }
570     /**
571      * @param inMimeType The mimeType to set.
572      */

573     public void setMimeType(String JavaDoc inMimeType) {
574         fieldMimeType = inMimeType;
575     }
576     
577     public PageSettings getParent()
578     {
579         return fieldParent;
580     }
581     public void setParent(PageSettings inParent)
582     {
583         fieldParent = inParent;
584     }
585     public String JavaDoc getFieldAlternativeContentPath()
586     {
587         return fieldAlternateContentPath;
588     }
589     public List JavaDoc getFieldGenerator()
590     {
591         return fieldGenerators;
592     }
593     public String JavaDoc getFieldInnerLayout()
594     {
595         return fieldInnerLayout;
596     }
597     public String JavaDoc getFieldLayout()
598     {
599         return fieldLayout;
600     }
601     public String JavaDoc getFieldPageCharacterEncoding()
602     {
603         return fieldPageCharacterEncoding;
604     }
605     public String JavaDoc getDefaultLanguage()
606     {
607         if( fieldDefaultLanguage == null)
608         {
609             fieldDefaultLanguage = getPropertyValue("defaultlanguage", null);
610             if( fieldDefaultLanguage == null)
611             {
612                 fieldDefaultLanguage = "";
613             }
614         }
615         return fieldDefaultLanguage;
616     }
617     
618     public String JavaDoc getPropertyValueFixed( String JavaDoc inKey )
619     {
620         String JavaDoc val = getPropertyValue(inKey, null);
621         return replaceProperty(val);
622     }
623     public String JavaDoc replaceProperty(String JavaDoc inValue)
624     {
625         if( inValue == null)
626         {
627             return inValue;
628         }
629         int start = inValue.indexOf("${");
630         if( start != -1)
631         {
632             int end = inValue.indexOf("}",start);
633             if( end != -1)
634             {
635                 String JavaDoc key = inValue.substring(start+2,end);
636                 Object JavaDoc variable = getProperty(key); //check for property
637
if( variable != null)
638                 {
639                     inValue = inValue.substring(0,start) + variable + inValue.substring(end+1);
640                 }
641             }
642         }
643         
644         return inValue;
645     }
646
647     
648 }
649
Popular Tags