KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > xml > XmlModule


1 /*
2  * Created on May 25, 2004
3  */

4 package com.openedit.modules.xml;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.dom4j.Element;
12
13 import com.openedit.OpenEditException;
14 import com.openedit.WebPageRequest;
15 import com.openedit.modules.BaseModule;
16 import com.openedit.util.PathUtilities;
17
18 /**
19  * Allows XML files to be loaded as Dom4J elements and data can be
20  * cached for later use
21  * @author cburkey
22  *
23  */

24 public class XmlModule extends BaseModule
25 {
26     private static final Log log = LogFactory.getLog(XmlModule.class);
27     protected XmlArchive fieldXmlArchive;
28     
29     
30     public XmlFile loadXmlFile( WebPageRequest inReq) throws Exception JavaDoc
31     {
32             //This can be specified within the page action with a <property name="xmlfile">./data.xml</property>
33
String JavaDoc id = inReq.getRequestParameter("fileid");
34         if( id == null)
35         {
36             id = inReq.getCurrentAction().getChildValue("fileid");
37         }
38         XmlFile element = getXmlArchive().loadXmlFile(id);
39         
40         if( element == null)
41         {
42             String JavaDoc path = inReq.getCurrentAction().getChildValue("path");
43             if( element != null && path == null )
44             {
45                 path = element.getPath();
46             }
47             String JavaDoc fullPath = PathUtilities.buildRelative(path, inReq.getPath());
48             String JavaDoc name = inReq.getCurrentAction().getChildValue("element-name");
49             List JavaDoc attribs = inReq.getCurrentAction().getConfig().getChildren("attribute");
50             element = getXmlArchive().createXmlFile(id, fullPath, name, attribs);
51         }
52         inReq.putPageValue("xmlfile", element);
53             
54         return element;
55     }
56     public void selectElement(WebPageRequest inReq) throws Exception JavaDoc
57     {
58         String JavaDoc eid = inReq.getRequestParameter("elementid");
59         if( eid != null)
60         {
61             XmlFile file = loadXmlFile(inReq);
62             Element element = file.getElementById(eid);
63             inReq.putSessionValue("selectedelement", element);
64         }
65     }
66     public void deleteElement(WebPageRequest inReq) throws Exception JavaDoc
67     {
68         XmlFile file = loadXmlFile(inReq);
69         Element element = (Element)inReq.getSessionValue("selectedelement");
70         file.deleteElement(element);
71         getXmlArchive().saveXml(file, inReq.getUser());
72         inReq.putSessionValue("selectedelement", null);
73     }
74     public void saveElement(WebPageRequest inReq) throws Exception JavaDoc
75     {
76         XmlFile file = loadXmlFile(inReq);
77         Element element = (Element)inReq.getSessionValue("selectedelement");
78         String JavaDoc id = element.attributeValue("id");
79         element = file.getElementById(id);
80         if( element == null)
81         {
82             throw new OpenEditException("No such row " + id);
83         }
84         for (Iterator JavaDoc iter = file.getAttributes().iterator(); iter.hasNext();)
85         {
86             AttributeDesc attrib = (AttributeDesc) iter.next();
87             String JavaDoc value = inReq.getRequestParameter(attrib.getId());
88             element.addAttribute(attrib.getId(), value);
89         }
90         getXmlArchive().saveXml(file, inReq.getUser());
91         inReq.putPageValue("message", "save complete");
92         inReq.putSessionValue("selectedelement", element);
93     }
94
95
96     public void addElement(WebPageRequest inReq) throws Exception JavaDoc
97     {
98         XmlFile file = loadXmlFile(inReq);
99         Element element = file.addNewElement();
100         inReq.putSessionValue("selectedelement", element);
101     }
102
103     public XmlArchive getXmlArchive()
104     {
105         return fieldXmlArchive;
106     }
107
108     public void setXmlArchive(XmlArchive inXmlArchive)
109     {
110         fieldXmlArchive = inXmlArchive;
111     }
112 }
113
Popular Tags