KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.openedit.modules.xml;
2
3 import java.io.Reader JavaDoc;
4 import java.io.StringWriter JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import org.dom4j.DocumentHelper;
11 import org.dom4j.Element;
12 import org.openedit.repository.filesystem.StringItem;
13
14 import com.openedit.OpenEditException;
15 import com.openedit.config.Configuration;
16 import com.openedit.page.Page;
17 import com.openedit.page.manage.PageManager;
18 import com.openedit.users.User;
19 import com.openedit.util.FileUtils;
20 import com.openedit.util.XmlUtil;
21
22 public class XmlArchive
23 {
24     protected Map JavaDoc fieldCache;
25     protected PageManager fieldPageManager;
26     protected XmlUtil fieldXmlUtil;
27     
28     public XmlFile loadXmlFile( String JavaDoc inId) throws OpenEditException
29     {
30         XmlFile element = (XmlFile)getCache().get(inId);
31         if( element != null)
32         {
33             Page input = getPageManager().getPage(element.getPath());
34             if ( element.getLastModified() != input.getLastModified().getTime() )
35             {
36                 return null;
37             }
38         }
39         return element;
40     }
41     
42     
43     public XmlFile createXmlFile( String JavaDoc inId, String JavaDoc path, String JavaDoc name, List JavaDoc inAttributes) throws OpenEditException
44     {
45             //This can be specified within the page action with a <property name="xmlfile">./data.xml</property>
46
XmlFile element = (XmlFile)getCache().get(inId);
47         boolean found = false;
48         Page input = getPageManager().getPage(path);
49         if ( element == null || element.getLastModified() != input.getLastModified().getTime() )
50         {
51             Element root = null;
52             if( !input.exists() )
53             {
54                 root = DocumentHelper.createElement(inId);
55                 
56             }
57             else
58             {
59                 found = true;
60                 Reader JavaDoc in = input.getReader();
61                 try
62                 {
63                     root= getXmlUtil().getXml(in,"UTF-8");
64                 }
65                 finally
66                 {
67                     FileUtils.safeClose(in);
68                 }
69             }
70             element = new XmlFile();
71             element.setRoot(root);
72             element.setExist(found);
73             element.setElementName(name);
74             element.setPath(path);
75             element.setLastModified(input.getLastModified().getTime());
76             element.setId(inId);
77             if( inAttributes != null)
78             {
79                 for (Iterator JavaDoc iter = inAttributes.iterator(); iter.hasNext();)
80                 {
81                     Configuration config = (Configuration) iter.next();
82                     
83                     AttributeDesc desc = new AttributeDesc();
84                     desc.setDescription(config.getAttribute("description"));
85                     desc.setId(config.getAttribute("id"));
86                     desc.setSize(config.getAttribute("size"));
87                     desc.setType(config.getAttribute("type"));
88                     element.addAttribute(desc);
89                 }
90             }
91             getCache().put(inId, element);
92         }
93             
94         return element;
95     }
96     
97     
98     public Map JavaDoc getCache()
99     {
100         if (fieldCache == null)
101         {
102             fieldCache = new HashMap JavaDoc();
103         }
104         return fieldCache;
105     }
106
107     public void setCache(Map JavaDoc inCache)
108     {
109         fieldCache = inCache;
110     }
111
112     protected void saveXml(XmlFile inFile, User inUser) throws Exception JavaDoc
113     {
114         XmlUtil util = new XmlUtil();
115         StringWriter JavaDoc outw = new StringWriter JavaDoc();
116         util.saveXml(inFile.getRoot(), outw, null);
117         Page out = getPageManager().getPage(inFile.getPath());
118         StringItem item = new StringItem(out.getPath(),outw.toString(),out.getCharacterEncoding());
119         item.setMessage("Updated list of sites");
120         if (inUser != null)
121         {
122             item.setAuthor(inUser.getUserName());
123         }
124         out.setContentItem(item);
125         getPageManager().putPage(out);
126     }
127
128
129     public PageManager getPageManager()
130     {
131         return fieldPageManager;
132     }
133
134
135     public void setPageManager(PageManager inPageManager)
136     {
137         fieldPageManager = inPageManager;
138     }
139
140     public XmlUtil getXmlUtil()
141     {
142         if (fieldXmlUtil == null)
143         {
144             fieldXmlUtil = new XmlUtil();
145         }
146         return fieldXmlUtil;
147     }
148
149
150     public void setXmlUtil(XmlUtil inXmlUtil)
151     {
152         fieldXmlUtil = inXmlUtil;
153     }
154
155     
156 }
157
Popular Tags