001: /*
002: * Created on Jul 21, 2004
003: */
004: package com.openedit.page;
005:
006: import java.io.InputStreamReader;
007: import java.io.Reader;
008: import java.util.ArrayList;
009: import java.util.HashMap;
010: import java.util.Iterator;
011: import java.util.List;
012: import java.util.Locale;
013: import java.util.Map;
014:
015: import org.openedit.repository.ContentItem;
016:
017: import com.openedit.OpenEditException;
018: import com.openedit.config.Configuration;
019: import com.openedit.util.strainer.Filter;
020:
021: /**
022: * This class represents the possible meta data for a Page
023: */
024: public class PageSettings {
025: protected ContentItem fieldXConf;
026: protected Configuration fieldUserDefinedData;
027: protected long fieldModifiedTime;
028:
029: protected String fieldLayout;
030: protected String fieldInnerLayout;
031: protected List fieldGenerators;
032: protected Map fieldProperties;
033: protected List fieldPageActions;
034: protected List fieldPathActions;
035:
036: protected String fieldAlternateContentPath;
037: protected boolean fieldOriginalyExistedContentPath; //used to see if a new file has need added or removed
038:
039: protected Filter fieldEditFilter;
040: protected Filter fieldViewFilter;
041: protected String fieldPageCharacterEncoding;
042: protected boolean fieldModified = false;
043: protected String fieldMimeType;
044: protected PageSettings fieldFallBack;
045: protected PageSettings fieldParent;
046: protected String fieldDefaultLanguage;
047:
048: public String getAlternateContentPath() {
049: return fieldAlternateContentPath;
050: }
051:
052: public void setAlternateContentPath(String alternateContentPath) {
053: fieldAlternateContentPath = alternateContentPath;
054: }
055:
056: public String getPath() {
057: return getXConf().getPath();
058: }
059:
060: public String toString() {
061: if (fieldXConf != null) {
062: return getXConf().getPath();
063: }
064: return super .toString();
065:
066: }
067:
068: public List getGenerators() {
069: //add top level parents last
070: List finalList = new ArrayList(4);
071:
072: PageSettings parent = this ;
073: PageSettings fallbackparent = getFallback();
074: while (parent != null) {
075: if (parent.fieldGenerators != null) {
076: finalList.addAll(parent.fieldGenerators);
077: }
078: if (fallbackparent != null) //first check the mirror site
079: {
080: PageSettings chain = fallbackparent;
081: int count = 0;
082: while (chain != null && count++ < 10) {
083: if (chain.fieldGenerators != null) {
084: finalList.addAll(chain.fieldGenerators);
085: }
086: chain = chain.getFallback();
087: }
088: fallbackparent = fallbackparent.getParent();
089: }
090: parent = parent.getParent();
091: }
092: return finalList;
093: }
094:
095: public void setGenerators(List generators) {
096: fieldGenerators = generators;
097: }
098:
099: public String getLayout() {
100: PageSettings parent = this ;
101: PageSettings fallbackparent = getFallback();
102: while (parent != null) {
103: if (parent.fieldLayout != null) //now check the real parent
104: {
105: return parent.fieldLayout;
106: }
107: if (fallbackparent != null) //first check the mirror site
108: {
109: PageSettings chain = fallbackparent;
110: int count = 0;
111: while (chain != null && count++ < 10) {
112: if (chain.fieldLayout != null) {
113: return chain.fieldLayout;
114: }
115: chain = chain.getFallback();
116: }
117: fallbackparent = fallbackparent.getParent(); //mirror site parent
118: }
119: parent = parent.getParent();
120: }
121: return null;
122: }
123:
124: //Not used
125: public String getInnerLayout() {
126: return getInnerLayoutExcludeSelf(null);
127: }
128:
129: public String getInnerLayoutExcludeSelf(String inPath) {
130: PageSettings parent = this ;
131: PageSettings fallbackparent = getFallback();
132: while (parent != null) {
133: if (parent.fieldInnerLayout != null) //now check the real parent
134: {
135: String fixed = replaceProperty(parent.fieldInnerLayout);
136: if (inPath == null || !inPath.equals(fixed)) {
137: return fixed;//parent.fieldInnerLayout;
138: }
139: }
140: if (fallbackparent != null) //first check the mirror site
141: {
142: PageSettings chain = fallbackparent;
143: int count = 0;
144: while (chain != null && count++ < 10) {
145: if (chain.fieldInnerLayout != null) {
146: String fixed = replaceProperty(chain.fieldInnerLayout);
147: if (inPath == null || !inPath.equals(fixed)) {
148: return fixed;//fallbackparent.fieldInnerLayout;
149: }
150: }
151: chain = chain.getFallback();
152: }
153: fallbackparent = fallbackparent.getParent(); //mirror site parent
154: }
155: parent = parent.getParent();
156: }
157: return null;
158: }
159:
160: public void setLayout(String layout) {
161: fieldLayout = layout;
162: }
163:
164: public void setInnerLayout(String innerLayout) {
165: fieldInnerLayout = innerLayout;
166: }
167:
168: public List getPageActions() {
169: //add top level parents last
170: List finalList = new ArrayList();
171:
172: PageSettings parent = this ;
173: PageSettings fallbackparent = getFallback();
174: while (parent != null) {
175: if (parent.fieldPageActions != null) {
176: finalList.addAll(0, parent.fieldPageActions);
177: }
178: if (fallbackparent != null) //first check the mirror site
179: {
180: PageSettings chain = fallbackparent;
181: int count = 0;
182: while (chain != null && count++ < 10) {
183: if (chain.fieldPageActions != null) {
184: finalList.addAll(0, chain.fieldPageActions);
185: }
186: chain = chain.getFallback();
187: }
188: fallbackparent = fallbackparent.getParent();
189: }
190: parent = parent.getParent();
191: }
192: return finalList;
193:
194: }
195:
196: public void setPageActions(List pageActions) {
197: fieldPageActions = pageActions;
198: }
199:
200: public List getPathActions() {
201: //add top level parents last
202: List finalList = new ArrayList(4);
203:
204: PageSettings parent = this ;
205: PageSettings fallbackparent = getFallback();
206: while (parent != null) {
207: if (parent.fieldPathActions != null) {
208: finalList.addAll(0, parent.fieldPathActions);
209: }
210: if (fallbackparent != null) {
211: PageSettings chain = fallbackparent;
212: int count = 0;
213: while (chain != null && count++ < 10) {
214: if (chain.fieldPathActions != null) {
215: finalList.addAll(0, chain.fieldPathActions);
216: }
217: chain = chain.getFallback();
218: }
219: fallbackparent = fallbackparent.getParent();
220: }
221: parent = parent.getParent();
222: }
223: return finalList; //oldest on bottom
224: }
225:
226: protected int getDepth() {
227: int i = 0;
228: PageSettings settings = this ;
229: while (settings != null) {
230: i++;
231: settings = settings.getParent();
232: }
233: return i;
234: }
235:
236: public Map getProperties() {
237: if (fieldProperties == null) {
238: fieldProperties = new HashMap();
239: }
240: return fieldProperties;
241: }
242:
243: public List getAllPropertyKeysWithoutPrefix(String inPrefix) {
244: List all = new ArrayList();
245: PageSettings parent = this ;
246: PageSettings fallbackparent = getFallback();
247: while (parent != null) {
248: if (parent.fieldProperties != null) {
249: for (Iterator iter = parent.fieldProperties.keySet()
250: .iterator(); iter.hasNext();) {
251: String key = (String) iter.next();
252: if (key.startsWith(inPrefix)) {
253: key = key.substring(inPrefix.length());
254: all.add(key);
255: }
256: }
257: }
258: if (fallbackparent != null) //first check the mirror site
259: {
260: PageSettings chain = fallbackparent;
261: int count = 0;
262: while (chain != null && count++ < 10) {
263: if (chain.fieldProperties != null) {
264: for (Iterator iter = chain.fieldProperties
265: .keySet().iterator(); iter.hasNext();) {
266: String key = (String) iter.next();
267: if (key.startsWith(inPrefix)) {
268: key = key.substring(inPrefix.length());
269: all.add(key);
270: }
271: }
272: }
273: chain = chain.getFallback();
274: }
275: fallbackparent = fallbackparent.getParent();
276: }
277: parent = parent.getParent();
278: }
279: return all;
280:
281: }
282:
283: public PageProperty getProperty(String inKey) {
284: PageSettings parent = this ;
285: PageSettings fallbackparent = getFallback();
286: while (parent != null) {
287: if (parent.fieldProperties != null) {
288: PageProperty val = parent.getFieldProperty(inKey);
289: if (val != null) {
290: return val;
291: }
292: }
293: if (fallbackparent != null) //first check the mirror site
294: {
295: PageSettings chain = fallbackparent;
296: int count = 0;
297: while (chain != null && count++ < 10) {
298: if (chain.fieldProperties != null) {
299: PageProperty val = (PageProperty) chain
300: .getFieldProperty(inKey);
301: if (val != null) {
302: return val;
303: }
304: }
305: chain = chain.getFallback();
306: }
307: fallbackparent = fallbackparent.getParent();
308: }
309: parent = parent.getParent();
310: }
311: return null;
312:
313: }
314:
315: public PageProperty getFieldProperty(String inKey) {
316: PageProperty val = (PageProperty) getProperties().get(inKey);
317: if (val != null) {
318: val.setPath(getXConf().getPath());
319: }
320: return val;
321: }
322:
323: public void setProperties(Map properties) {
324: fieldProperties = properties;
325: }
326:
327: public void putProperty(PageProperty inProperty) {
328: getProperties().put(inProperty.getName(), inProperty);
329: }
330:
331: public Filter getEditFilter() {
332: PageSettings parent = this ;
333: PageSettings fallbackparent = getFallback();
334: while (parent != null) {
335: if (parent.fieldEditFilter != null) {
336: return parent.fieldEditFilter;
337: }
338: if (fallbackparent != null) {
339: PageSettings chain = fallbackparent;
340: int count = 0;
341: while (chain != null && count++ < 10) {
342: if (chain.fieldEditFilter != null) {
343: return chain.fieldEditFilter;
344: }
345: chain = chain.getFallback();
346: }
347: fallbackparent = fallbackparent.getParent();
348: }
349: parent = parent.getParent();
350: }
351: return null;
352: }
353:
354: public void setEditFilter(Filter editFilter) {
355: fieldEditFilter = editFilter;
356: }
357:
358: public Filter getViewFilter() {
359: PageSettings parent = this ;
360: PageSettings fallbackparent = getFallback();
361: while (parent != null) {
362: if (parent.fieldViewFilter != null) {
363: return parent.fieldViewFilter;
364: }
365: if (fallbackparent != null) //first check the mirror site
366: {
367: PageSettings chain = fallbackparent;
368: int count = 0;
369: while (chain != null && count++ < 10) {
370: if (chain.fieldViewFilter != null) {
371: return chain.fieldViewFilter;
372: }
373: chain = chain.getFallback();
374: }
375: fallbackparent = fallbackparent.getParent();
376: }
377: parent = parent.getParent();
378: }
379: return null;
380: }
381:
382: public void setViewFilter(Filter viewFilter) {
383: fieldViewFilter = viewFilter;
384: }
385:
386: public PageSettings getFallback() {
387: return fieldFallBack;
388: }
389:
390: /**
391: * @param inParent
392: */
393: public void setFallBack(PageSettings inParent) {
394: fieldFallBack = inParent;
395:
396: }
397:
398: public boolean isCurrent() {
399: PageSettings parent = this ;
400: PageSettings fallbackparent = getFallback();
401: while (parent != null) {
402: if (!parent.fieldIsCurrent()) //check the real parent
403: {
404: return false;
405: }
406: if (fallbackparent != null) {
407: PageSettings chain = fallbackparent;
408: int count = 0;
409: while (chain != null && count++ < 10) {
410: if (!chain.fieldIsCurrent()) {
411: return false;
412: }
413: chain = chain.getFallback();
414: }
415: fallbackparent = fallbackparent.getParent(); //mirror site parent
416: }
417: parent = parent.getParent();
418: }
419: return true;
420: }
421:
422: public boolean fieldIsCurrent() {
423: // System.out.println("Checking " + getPath());
424: long last = getXConf().getLastModified();
425: // if ( last == -1)
426: // {
427: // return false;
428: // }
429: boolean self = last == getModifiedTime();
430: return self;
431: }
432:
433: public boolean exists() {
434: return getXConf().exists();
435: }
436:
437: public ContentItem getXConf() {
438: return fieldXConf;
439: }
440:
441: public void setXConf(ContentItem inConf) {
442: fieldXConf = inConf;
443: if (inConf != null) {
444: setModifiedTime(inConf.getLastModified());
445: } else {
446: setModifiedTime(-1); //TODO: Why not have 0 in here?
447: }
448: }
449:
450: /**
451: * @return
452: */
453: public Reader getReader() throws OpenEditException {
454: try {
455: String enc = getPageCharacterEncoding();
456: if (enc != null) {
457: return new InputStreamReader(getXConf()
458: .getInputStream(), enc);
459: }
460: return new InputStreamReader(getXConf().getInputStream(),
461: "UTF-8");
462:
463: } catch (Exception ex) {
464: throw new OpenEditException(ex);
465: }
466: }
467:
468: public long getModifiedTime() {
469: return fieldModifiedTime;
470: }
471:
472: public void setModifiedTime(long inLastModifiedTime) {
473: fieldModifiedTime = inLastModifiedTime;
474: }
475:
476: /**
477: * @param inString
478: */
479: public void setPageCharacterEncoding(String inString) {
480: fieldPageCharacterEncoding = inString;
481: }
482:
483: public String getPageCharacterEncoding() {
484: PageSettings parent = this ;
485: PageSettings fallbackparent = getFallback();
486: while (parent != null) {
487: if (parent.fieldPageCharacterEncoding != null) //now check the real parent
488: {
489: return parent.fieldPageCharacterEncoding;
490: }
491: if (fallbackparent != null) //first check the mirror site
492: {
493: PageSettings chain = fallbackparent;
494: int count = 0;
495: while (chain != null && count++ < 10) {
496: if (chain.fieldPageCharacterEncoding != null) {
497: return chain.fieldPageCharacterEncoding;
498: }
499: chain = chain.getFallback();
500: }
501: fallbackparent = fallbackparent.getParent(); //mirror site parent
502: }
503: parent = parent.getParent();
504: }
505: return null;
506:
507: }
508:
509: public Configuration getUserDefinedData() {
510: return fieldUserDefinedData;
511: }
512:
513: public void setUserDefinedData(Configuration inUserDefinedData) {
514: fieldUserDefinedData = inUserDefinedData;
515: }
516:
517: /**
518: * @param inString
519: * @return
520: */
521: public Configuration getUserDefined(String inString) {
522: if (getUserDefinedData() != null) {
523: Configuration config = getUserDefinedData().getChild(
524: inString);
525: if (config != null) {
526: return config;
527: }
528: }
529: return null;
530: }
531:
532: /**
533: * @param inList
534: */
535: public void setPathActions(List inList) {
536: fieldPathActions = inList;
537: }
538:
539: /**
540: * @param inString
541: * @return
542: */
543: public String getPropertyValue(String inString, Locale inLocale) {
544: PageProperty prop = getProperty(inString);
545: if (prop != null) {
546: return prop.getValue(inLocale);
547: }
548: return null;
549: }
550:
551: /**
552: * @return Returns the mimeType.
553: */
554: public String getMimeType() {
555: return fieldMimeType;
556: }
557:
558: /**
559: * @param inMimeType The mimeType to set.
560: */
561: public void setMimeType(String inMimeType) {
562: fieldMimeType = inMimeType;
563: }
564:
565: public PageSettings getParent() {
566: return fieldParent;
567: }
568:
569: public void setParent(PageSettings inParent) {
570: fieldParent = inParent;
571: }
572:
573: public String getFieldAlternativeContentPath() {
574: return fieldAlternateContentPath;
575: }
576:
577: public List getFieldGenerator() {
578: return fieldGenerators;
579: }
580:
581: public String getFieldInnerLayout() {
582: return fieldInnerLayout;
583: }
584:
585: public String getFieldLayout() {
586: return fieldLayout;
587: }
588:
589: public String getFieldPageCharacterEncoding() {
590: return fieldPageCharacterEncoding;
591: }
592:
593: public String getDefaultLanguage() {
594: if (fieldDefaultLanguage == null) {
595: fieldDefaultLanguage = getPropertyValue("defaultlanguage",
596: null);
597: if (fieldDefaultLanguage == null) {
598: fieldDefaultLanguage = "";
599: }
600: }
601: return fieldDefaultLanguage;
602: }
603:
604: public String getPropertyValueFixed(String inKey) {
605: String val = getPropertyValue(inKey, null);
606: return replaceProperty(val);
607: }
608:
609: public String replaceProperty(String inValue) {
610: if (inValue == null) {
611: return inValue;
612: }
613: int start = inValue.indexOf("${");
614: if (start != -1) {
615: int end = inValue.indexOf("}", start);
616: if (end != -1) {
617: String key = inValue.substring(start + 2, end);
618: Object variable = getProperty(key); //check for property
619: if (variable != null) {
620: inValue = inValue.substring(0, start) + variable
621: + inValue.substring(end + 1);
622: }
623: }
624: }
625: return inValue;
626: }
627:
628: public boolean isOriginalyExistedContentPath() {
629: return fieldOriginalyExistedContentPath;
630: }
631:
632: public void setOriginalyExistedContentPath(
633: boolean inOriginalContentPathMissing) {
634: fieldOriginalyExistedContentPath = inOriginalContentPathMissing;
635: }
636:
637: }
|