KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Oct 15, 2004
3  */

4 package com.openedit.page;
5
6 import java.io.IOException JavaDoc;
7 import java.io.StringReader JavaDoc;
8 import java.io.StringWriter JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 import org.dom4j.DocumentHelper;
14 import org.dom4j.Element;
15 import org.dom4j.io.OutputFormat;
16 import org.dom4j.io.XMLWriter;
17
18 import com.openedit.OpenEditException;
19 import com.openedit.OpenEditRuntimeException;
20 import com.openedit.config.Configuration;
21 import com.openedit.config.XMLConfiguration;
22 import com.openedit.util.XmlUtil;
23
24 /**
25  * @author Matthew Avery, mavery@einnovation.com
26  */

27 public class XconfConfiguration extends XMLConfiguration
28 {
29     
30     public XconfConfiguration()
31     {
32         
33     }
34
35     public String JavaDoc getViewRequirementsXml()
36     {
37         XMLConfiguration conf = (XMLConfiguration)getViewRequirements();
38         if( conf == null)
39         {
40             return null;
41         }
42         return asInnerXml("view-requirements");
43     }
44     public String JavaDoc getEditRequirementsXml()
45     {
46         XMLConfiguration conf = (XMLConfiguration)getEditRequirements();
47         if( conf == null)
48         {
49             return null;
50         }
51         return asInnerXml("edit-requirements");
52     }
53
54     private String JavaDoc asInnerXml(String JavaDoc inName)
55     {
56         Element root = DocumentHelper.createElement(inName);
57         XMLConfiguration conf = (XMLConfiguration)getChild(inName);
58         conf.appendXml(conf,root);
59         StringWriter JavaDoc text = new StringWriter JavaDoc();
60         OutputFormat format = OutputFormat.createPrettyPrint();
61         format.setEncoding("UTF-8");
62         XMLWriter out = new XMLWriter(text, format);
63         try
64         {
65             out.write((Element)root.elementIterator().next());
66         }
67         catch (IOException JavaDoc ex)
68         {
69             throw new OpenEditRuntimeException(ex);
70         }
71         String JavaDoc xml = text.toString();
72         return xml;
73
74     }
75
76
77     public Configuration getViewRequirements()
78     {
79         return getChild( "view-requirements" );
80     }
81     
82     public Configuration getEditRequirements()
83     {
84         return getChild( "edit-requirements" );
85     }
86     
87     public String JavaDoc getContentFile()
88     {
89         return getChildValue( "contentfile" );
90     }
91     public String JavaDoc getOverrideDirectory()
92     {
93         return getChildValue( "overridedirectory" );
94     }
95     
96     public List JavaDoc getGenerators()
97     {
98         return getChildren("generator");
99     }
100     
101     public Configuration getLayout()
102     {
103         return getChild("layout");
104     }
105     
106     public Configuration getInnerLayout()
107     {
108         return getChild("inner-layout");
109     }
110     public Configuration getProperty(String JavaDoc inId)
111     {
112         Configuration config = selectSingleNode("//property[@name='" + inId + "']");
113         return config;
114     }
115     
116     public List JavaDoc getProperties()
117     {
118         return getChildren("property");
119     }
120     
121     public List JavaDoc getPageActions()
122     {
123         return getChildren( "page-action" );
124     }
125     public List JavaDoc getPathActions()
126     {
127         return getChildren( "path-action" );
128     }
129 //More stuff
130
protected String JavaDoc fieldWorkingDir;
131     protected String JavaDoc fieldPagePath;
132     public static final String JavaDoc VIEW_REQ = "view-requirements";
133     public static final String JavaDoc WRITE_REQ = "edit-requirements";
134     protected Configuration fieldConfig;
135     protected List JavaDoc fieldStandardSettings;
136     
137
138     public void setConfig(Configuration inConfig)
139     {
140         fieldConfig = inConfig;
141     }
142     public String JavaDoc get(String JavaDoc inKey)
143     {
144         Configuration child = getChild(inKey);
145         if (child != null)
146         {
147             return child.getValue();
148         }
149         return null;
150     }
151     public String JavaDoc getGeneratorChildValue(String JavaDoc inChildName)
152     {
153         //find this generator and get the name out
154
Configuration prop = selectSingleNode("//generator/" + inChildName);
155         if (prop != null)
156         {
157             return prop.getValue();
158         }
159         return null;
160     }
161
162 // public boolean checkedAttrib(String inName, String inKey)
163
// {
164
// if (propertyValue( inKey) != null)
165
// {
166
// return true;
167
// }
168
// return false;
169
// }
170
public boolean checkedText(String JavaDoc inName, String JavaDoc inText)
171     {
172         for (Iterator JavaDoc iter = getChildIterator(inName); iter.hasNext();)
173         {
174             Configuration element = (Configuration) iter.next();
175             if (inText.equalsIgnoreCase(element.getValue()))
176             {
177                 return true;
178             }
179         }
180         return false;
181     }
182     public String JavaDoc propertyValue( String JavaDoc inKey )
183     {
184         String JavaDoc val = null;
185         for (Iterator JavaDoc iter = getChildIterator("property"); iter.hasNext();)
186         {
187             Configuration element = (Configuration) iter.next();
188             if (inKey.equalsIgnoreCase(element.getAttribute("name")))
189             {
190                 val = element.getValue();
191                 if( val == null )
192                 {
193                     //This is used for non languages based look ups
194
for (Iterator JavaDoc iterator = element.getChildIterator("value"); iterator.hasNext();)
195                     {
196                         Configuration valconf = (Configuration) iterator.next();
197                         val = valconf.getValue();
198                         break;
199                     }
200                 }
201             }
202         }
203         return val;
204         
205     }
206     public boolean isTrueProperty(String JavaDoc inKey)
207     {
208         String JavaDoc value = propertyValue(inKey);
209         if ( value != null )
210         {
211             return value.equalsIgnoreCase("true");
212         }
213         return false;
214     }
215     public boolean isEmptyProperty(String JavaDoc inKey)
216     {
217         String JavaDoc value = propertyValue(inKey);
218         if ( value != null )
219         {
220             return false;
221         }
222         return true;
223     }
224     public boolean isFalseProperty(String JavaDoc inKey)
225     {
226         String JavaDoc value = propertyValue(inKey);
227         if ( value != null )
228         {
229             return value.equalsIgnoreCase("false");
230         }
231         return false;
232     }
233     public boolean isBlankLayout()
234     {
235         Configuration layout = getChild("layout");
236         if ( layout != null )
237         {
238             if( layout.getValue() == null ||
239                 layout.getValue().length() == 0 ||
240                 Page.BLANK_LAYOUT.equals(layout.getValue()) )
241             {
242                 return true;
243             }
244         }
245         return false;
246     }
247     public boolean isBlankInnerLayout()
248     {
249         Configuration layout = getChild("inner-layout");
250         if ( layout != null )
251         {
252             if( layout.getValue() == null ||
253                 layout.getValue().length() == 0 ||
254                 Page.BLANK_LAYOUT.equals(layout.getValue()) )
255             {
256                 return true;
257             }
258         }
259         return false;
260     }
261     /**
262      * Returns the list of page actions as XML in UTF-8 encoding.
263      *
264      * @return An XML fragment
265      */

266     public String JavaDoc listPageActions()
267     {
268         return outerXml("page-action");
269     }
270     
271     public List JavaDoc getAllProperties()
272     {
273         List JavaDoc props = new ArrayList JavaDoc();
274         for (Iterator JavaDoc iter = getChildIterator("property"); iter.hasNext();)
275         {
276             Configuration element = (Configuration) iter.next();
277             props.add(element.getAttribute("name"));
278         }
279         props.remove("title");
280         props.remove("keywords");
281         props.remove("description");
282         if( props.size() > 0)
283         {
284             props.add(0,"description");
285         }
286         else
287         {
288             props.add("description");
289         }
290         props.add(0,"keywords");
291         props.add(0,"title");
292         return props;
293     }
294     public List JavaDoc getUserProperties()
295     {
296         List JavaDoc props = new ArrayList JavaDoc();
297         for (Iterator JavaDoc iter = getAllProperties().iterator(); iter.hasNext();)
298         {
299             String JavaDoc name = (String JavaDoc) iter.next();
300             if ( !getStandardSettings().contains(name) )
301             {
302                 props.add(name);
303             }
304                 
305         }
306         return props;
307     }
308     /**
309      * Method removeProperty.
310      * @param name
311      */

312     public void removeProperty(String JavaDoc name)
313     {
314         //TODO how do we get the attribute out of here with XPATH
315
Configuration prop = (Configuration) selectSingleNode("//property[@name='" + name + "']");
316         if (prop != null)
317         {
318             prop.getParent().removeChild(prop);
319         }
320     }
321     /**
322      * Method saveProperty.
323      * @param name
324      * @param value
325      */

326     public void saveProperty(String JavaDoc name, String JavaDoc value, String JavaDoc inLocale)
327     {
328         //If its already here just update it. Otherwise add it
329
//if value is null remove property altogether
330
if (value == null)
331         {
332             removeProperty(name);
333         }
334         else
335         {
336             Configuration prop =
337                 (Configuration) selectSingleNode("//property[@name='" + name + "']");
338             if( inLocale != null && inLocale.length() == 0)
339             {
340                 inLocale = null;
341             }
342             if (prop == null)
343             {
344                 prop = addChild("property");
345                 prop.setAttribute("name", name);
346                 if( inLocale == null )
347                 {
348                     prop.setValue(value);
349                     return;
350                 }
351             }
352             boolean existing = false;
353             String JavaDoc defaultval = prop.getValue();
354             //clear default prop
355
if( defaultval != null)
356             {
357                 Configuration inline = prop.addChild("value");
358                 inline.setValue(defaultval);
359                 prop.setValue(null);
360             }
361             
362             for (Iterator JavaDoc iter = prop.getChildIterator("value"); iter.hasNext();)
363             {
364                 Configuration valueconfig = (Configuration ) iter.next();
365                 String JavaDoc vlocale = valueconfig.getAttribute("locale");
366                 if( inLocale == null && vlocale == null)
367                 {
368                     valueconfig.setValue(value);
369                     existing = true;
370                     break;
371                 }
372                 if( vlocale != null && vlocale.equals(inLocale))
373                 {
374                     valueconfig.setValue(value);
375                     existing = true;
376                     break;
377                 }
378             }
379             if( !existing)
380             {
381                 //add new
382
Configuration valueconf = prop.addChild("value");
383                 valueconf.setAttribute("locale", inLocale);
384                 valueconf.setValue(value);
385             }
386         }
387     }
388     /**
389      * @param xpath
390      * @return
391      */

392     private Configuration selectSingleNode(String JavaDoc xpath)
393     {
394         if ( xpath == null)
395         {
396             return null;
397         }
398 // property[@name='" + name + "']
399
//TODO: Implement this soon
400
if( xpath.indexOf('[') == -1)
401         {
402             //generator/stylesheet
403
//then this must be a simple look up
404
String JavaDoc[] path = xpath.split("\\/");
405             Configuration parent = this; //TODO: go to root level
406
for (int i = 0; i < path.length; i++)
407             {
408                 if ( path[i].trim().length() == 0)
409                 {
410                     continue;
411                 }
412                 Configuration child = parent.getChild(path[i]);
413                 if ( child == null)
414                 {
415                     return parent;
416                 }
417                 parent = child;
418             }
419         }
420         else
421         {
422             String JavaDoc child = xpath.substring(2,xpath.indexOf('['));
423             String JavaDoc attrib = xpath.substring(xpath.indexOf('@') + 1,xpath.indexOf('='));
424             String JavaDoc value = xpath.substring(xpath.indexOf('\'') + 1,xpath.lastIndexOf('\''));
425             for (Iterator JavaDoc iter = getChildren(child).iterator(); iter.hasNext();)
426             {
427                 Configuration element = (Configuration) iter.next();
428                 if ( value.equals( element.getAttribute(attrib)))
429                     {
430                     return element;
431                     }
432             }
433         }
434         return null;
435     }
436
437     public boolean isMissing(String JavaDoc inConfiguration)
438     {
439         Configuration elem = getChild(inConfiguration);
440         if (elem == null )
441         {
442             return true;
443         }
444         else
445         {
446             return false;
447         }
448     }
449     public void saveConfiguration(String JavaDoc inName, String JavaDoc inText)
450     {
451         
452         if ( inText != null && inText.trim().length() == 0)
453         {
454             inText = null;
455         }
456         
457         Configuration prop = getChild(inName);
458         if (prop != null)
459         {
460             if ( inText != null)
461             {
462                 prop.setValue(inText);
463             }
464             else
465             {
466                 removeChild(prop);
467             }
468         }
469         else if ( inText != null)
470         {
471             addChild(inName).setValue(inText);
472         }
473     }
474     
475     /**
476      * Method removeConfigurations.
477      * @param string
478      */

479     public void removeConfigurations(String JavaDoc inName)
480     {
481         for (Iterator JavaDoc iter = getChildren(inName).iterator(); iter.hasNext();)
482         {
483             Configuration elem = (Configuration) iter.next();
484             elem.getParent().removeChild(elem);
485         }
486     }
487     
488     protected String JavaDoc outerXml(String JavaDoc inField)
489     {
490         StringWriter JavaDoc wri = new StringWriter JavaDoc();
491         for (Iterator JavaDoc iter = getChildIterator(inField); iter.hasNext();)
492         {
493             XMLConfiguration read = (XMLConfiguration) iter.next();
494             wri.write(read.toXml("UTF-8"));
495         }
496         return wri.toString();
497     }
498
499     protected String JavaDoc innerXml(String JavaDoc inField)
500     {
501         Configuration read = getChild(inField);
502         if (read != null)
503         {
504             StringWriter JavaDoc wri = new StringWriter JavaDoc();
505
506             for (Iterator JavaDoc iter = read.getChildren().iterator(); iter.hasNext();)
507             {
508                 XMLConfiguration child = (XMLConfiguration) iter.next();
509                 wri.write(child.toXml("UTF-8"));
510             }
511             return wri.toString();
512         }
513         return null;
514     }
515     public String JavaDoc getWritePermissions()
516     {
517         return innerXml(WRITE_REQ);
518     }
519     /*
520      * This allows someone to reset the value in XML
521      */

522     public void setReadPermissions(String JavaDoc inXML) throws OpenEditException
523     {
524         resetValueWithXml(VIEW_REQ, inXML);
525     }
526     protected void resetValueWithXml(String JavaDoc inField, String JavaDoc inXML) throws OpenEditException
527     {
528         try
529         {
530             Configuration read = getChild(inField);
531             if (read != null)
532             {
533                 removeChild(read);
534             }
535             //
536
if (inXML != null && inXML.length() > 0)
537             {
538                 appendXml("<" + inField + ">\n" + inXML + "\n</" + inField + ">");
539             }
540         }
541         catch (Exception JavaDoc ex)
542         {
543             throw new OpenEditException(ex);
544         }
545     }
546     public void setAllActions(String JavaDoc inXML) throws OpenEditException
547     {
548             removeConfigurations("page-action");
549             appendXml(inXML);
550     }
551
552     /**
553      * @param inFilename
554      */

555     public void removeScriptAction(String JavaDoc inFilename)
556     {
557         //make sure its not already in there
558
for (Iterator JavaDoc iter = getChildIterator("page-action"); iter.hasNext();)
559         {
560             Configuration element = (Configuration) iter.next();
561             if ("Script.run".equals(element.getAttribute("name")))
562             {
563                 for (Iterator JavaDoc iterator = element.getChildIterator("property"); iterator.hasNext();)
564                 {
565                     Configuration prop = (Configuration) iterator.next();
566                     if ("code".equals(prop.getAttribute("name"))
567                         && inFilename.equals(prop.getValue()))
568                     {
569                         removeChild(element);
570                     }
571                 }
572             }
573         }
574
575     }
576
577     public void appendScriptAction(String JavaDoc inCode) throws OpenEditException
578     {
579         removeScriptAction(inCode);
580         Configuration top = addChild("page-action");
581         top.setAttribute("name", "Script.run");
582         Configuration prop = top.addChild("property");
583         prop.setAttribute("name", "code");
584         prop.setValue(inCode);
585     }
586     protected void appendXml(String JavaDoc inXML) throws OpenEditException
587     {
588         if (inXML != null && inXML.length() > 0)
589         {
590             StringReader JavaDoc in = new StringReader JavaDoc("<tmp>" + inXML + "</tmp>");
591             XMLConfiguration tmpconfig = new XMLConfiguration();
592             Element child = new XmlUtil().getXml(in, "UTF-8");
593             tmpconfig.populate(child);
594             for (Iterator JavaDoc iter = tmpconfig.getChildren().iterator(); iter.hasNext();)
595             {
596                 Configuration element = (Configuration) iter.next();
597                 addChild(element);
598             }
599         }
600     }
601
602     public void setWritePermissions(String JavaDoc inXML) throws OpenEditException
603     {
604         resetValueWithXml(WRITE_REQ, inXML);
605     }
606     public boolean isEmpty()
607     {
608         return
609             getChildren() == null
610             || getChildren().size() == 0;
611     }
612
613
614
615     public void setContentFile(String JavaDoc inContentFile)
616     {
617         saveConfiguration("contentfile",inContentFile);
618     }
619
620     /**
621      *
622      */

623     public void removeAllProperties()
624     {
625         List JavaDoc props = new ArrayList JavaDoc();
626         for (Iterator JavaDoc iter = getChildIterator("property"); iter.hasNext();)
627         {
628             Configuration element = (Configuration) iter.next();
629             props.add(element );
630         }
631         for (Iterator JavaDoc iter = props.iterator(); iter.hasNext();)
632         {
633             Configuration element = (Configuration) iter.next();
634             removeChild(element);
635             
636         }
637
638     }
639
640     public List JavaDoc getStandardSettings()
641     {
642         if ( fieldStandardSettings == null)
643         { //TODO: Move to external config
644
fieldStandardSettings = new ArrayList JavaDoc();
645 // fieldStandardSettings.add("title");
646
// fieldStandardSettings.add("keywords");
647
// fieldStandardSettings.add("description");
648
fieldStandardSettings.add("editable");
649             fieldStandardSettings.add("showToolbar");
650             fieldStandardSettings.add("saveasxhtml");
651             fieldStandardSettings.add("contentfile");
652             fieldStandardSettings.add("encoding");
653             fieldStandardSettings.add("mimetype");
654             fieldStandardSettings.add("overridedirectory");
655             fieldStandardSettings.add("virtual");
656         }
657         return fieldStandardSettings;
658     }
659     public void setStandardSettings(List JavaDoc inStandardSettings)
660     {
661         fieldStandardSettings = inStandardSettings;
662     }
663
664     /**
665      * @param inString
666      */

667     public void removeElements(String JavaDoc inString)
668     {
669         Configuration config = getChild(inString);
670         while(config != null )
671         {
672             removeChild(config);
673             config = getChild(inString);
674         }
675     }
676
677 }
678
Popular Tags