KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on May 18, 2006
3  */

4 package com.openedit.modules.xml;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.dom4j.Element;
11
12 public class XmlFile
13 {
14     protected long fieldLastModified = -1;
15     protected String JavaDoc fieldPath;
16     protected String JavaDoc fieldId;
17     protected String JavaDoc fieldElementName;
18     
19     protected Element fieldRoot;
20     protected List JavaDoc fieldAttributes;
21     protected boolean fieldExist;
22     
23     public String JavaDoc getPath()
24     {
25         return fieldPath;
26     }
27     public void setPath(String JavaDoc inFile)
28     {
29         fieldPath = inFile;
30     }
31     public String JavaDoc getId()
32     {
33         return fieldId;
34     }
35     public void setId(String JavaDoc inId)
36     {
37         fieldId = inId;
38     }
39     public long getLastModified()
40     {
41         return fieldLastModified;
42     }
43     public void setLastModified(long inLastModified)
44     {
45         fieldLastModified = inLastModified;
46     }
47     public Element getRoot()
48     {
49         return fieldRoot;
50     }
51     public void setRoot(Element inRoot)
52     {
53         fieldRoot = inRoot;
54     }
55     public List JavaDoc getElements()
56     {
57         return getRoot().elements(getElementName());
58     }
59     public List JavaDoc getAttributes()
60     {
61         //this is a list of attributes as defined by the action that created us
62
if (fieldAttributes == null)
63         {
64             fieldAttributes = new ArrayList JavaDoc();
65         }
66         return fieldAttributes;
67     }
68     public void addAttribute(AttributeDesc inDesc)
69     {
70         getAttributes().add(inDesc);
71     }
72     public Element getElementById(String JavaDoc inEid)
73     {
74         if( inEid == null)
75         {
76             return null;
77         }
78         for (Iterator JavaDoc iter = getElements().iterator(); iter.hasNext();)
79         {
80             Element element = (Element) iter.next();
81             if ( inEid.equals(element.attributeValue("id")))
82             {
83                 return element;
84             }
85         }
86         return null;
87     }
88     public void deleteElement(Element inEid)
89     {
90         getRoot().remove(inEid);
91     }
92     public Element addNewElement()
93     {
94         Element child = getRoot().addElement(getElementName());
95             for (Iterator JavaDoc iter = getAttributes().iterator(); iter.hasNext();)
96             {
97                 AttributeDesc attrib = (AttributeDesc) iter.next();
98                 if ( attrib.getId().equals("id"))
99                 {
100                     child.addAttribute(attrib.getId(), "" + System.currentTimeMillis() );
101                 }
102                 else
103                 {
104                     child.addAttribute(attrib.getId(), ""); //Defaults
105
}
106             }
107         return child;
108     }
109     public String JavaDoc getElementName()
110     {
111         return fieldElementName;
112     }
113     public void setElementName(String JavaDoc inElementName)
114     {
115         fieldElementName = inElementName;
116     }
117     public boolean isExist()
118     {
119         return fieldExist;
120     }
121     public void setExist(boolean inExist)
122     {
123         fieldExist = inExist;
124     }
125 }
126
Popular Tags