KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > core > XMLBean


1 /*
2  * XMLBean.java
3  *
4  * Created on 18. August 2003, 13:44
5  */

6
7 package org.contineo.core;
8
9 import java.io.File JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.LinkedList JavaDoc;
16 import java.util.List JavaDoc;
17 import org.apache.log4j.Level;
18 import org.apache.log4j.Logger;
19 import org.jdom.Attribute;
20 import org.jdom.Document;
21 import org.jdom.Element;
22 import org.jdom.JDOMException;
23 import org.jdom.input.SAXBuilder;
24 import org.jdom.output.XMLOutputter;
25 /**
26  * Class for using xml-files.
27  * @author Michael Scholz
28  * @version 1.0
29  */

30 public class XMLBean {
31
32     /**
33      * @uml.property name="doc"
34      * @uml.associationEnd
35      */

36     private Document doc;
37
38     /**
39      * @uml.property name="root"
40      * @uml.associationEnd
41      */

42     private Element root;
43
44     /** this points to an ordinary file */
45     private String JavaDoc docPath = null;
46     
47     /** this points to an input stream; it is read-only! */
48     private InputStream JavaDoc docInputStream = null;
49
50     /**
51      * @uml.property name="logger"
52      * @uml.associationEnd
53      */

54     private Logger logger;
55
56     /**
57      * Creates new XMLBean.
58      * @param docname Path of xml-file.
59      */

60     public XMLBean(String JavaDoc docname) {
61         docPath = docname;
62         docInputStream = null;
63         configLogger();
64         initDocument();
65     }
66
67     /**
68      * Creates new XMLBean.
69      * @param docname URL of xml-file.
70      */

71     public XMLBean(URL JavaDoc docname) {
72         try {
73             docPath = docname.toURI().getPath();
74             docInputStream = null;
75         }
76         catch (Exception JavaDoc ex) {
77             logger.error(ex.getMessage());
78         }
79         configLogger();
80         initDocument();
81     }
82     
83     /**
84      * Creates new XMLBean from an input stream; XMLBean is read-only!!!
85      */

86     public XMLBean(InputStream JavaDoc is) {
87         docInputStream = is;
88         docPath = null;
89         configLogger();
90         initDocument();
91     }
92     
93     private void configLogger() {
94         logger = LoggingManager.getLogger(this.getClass());
95     }
96     
97     private void initDocument() {
98         try {
99             SAXBuilder builder = new SAXBuilder();
100             if (docPath != null)
101                 doc = builder.build(docPath);
102             else
103                 doc = builder.build(docInputStream);
104             root = doc.getRootElement();
105         }
106         catch (JDOMException jdome) {
107             if (logger.isEnabledFor(Level.ERROR))
108                 logger.error(jdome.getMessage());
109         }
110     }
111
112     /**
113      * Returns the root element.
114      */

115     public Element getRootElement() {
116         return root;
117     }
118
119     /**
120      * Returns a child element.
121      * @param elemname Name of the child.
122      * @return Child
123      */

124     public Element getChild(String JavaDoc elemname) {
125         if (doc == null)
126             return null;
127         else
128             return root.getChild(elemname);
129     }
130
131     /**
132      * This method finds a child by name and attribute.
133      * @param elemname Name of the child.
134      * @param attribute Name of the child attribute.
135      * @param value Value of the attribute.
136      * @return Child
137      */

138     public Element getChild(String JavaDoc elemname, String JavaDoc attribute, String JavaDoc value) {
139         if (doc == null)
140             return null;
141         else {
142             Element temp = null;
143             List JavaDoc list = root.getChildren(elemname);
144             Iterator JavaDoc iter = list.iterator();
145             String JavaDoc val = "";
146             while(iter.hasNext()) {
147                 Element elem = (Element)iter.next();
148                 val = elem.getAttributeValue(attribute);
149                 if (val != null && val.equals(value)) {
150                     temp = elem;
151                     break;
152                 }
153             }
154             return temp;
155         }
156     }
157
158     /**
159      * This returns the text between a begining and an ending tag.
160      * @param elem Name of the element.
161      * @return Text of the element.
162      */

163     public String JavaDoc getText(Element elem) {
164         if (doc == null)
165             return null;
166         else
167             return elem.getText();
168     }
169     
170     public String JavaDoc getText(String JavaDoc elemname, String JavaDoc attrname, String JavaDoc attrvalue) {
171         Element elem = getChild(elemname, attrname, attrvalue);
172         return elem.getText();
173     }
174
175     /**
176      * This method returns the text of a child element characterized by elementname and attribute.
177      * @param elemname Name of the element.
178      * @param childname Name of the child.
179      * @param attribute Name of the attribute which must element have.
180      * @param value Value of the attribute.
181      * @return Text of the child.
182      */

183     public String JavaDoc getChildText(String JavaDoc elemname, String JavaDoc childname, String JavaDoc attribute, String JavaDoc value) {
184         Element elem = getChild(elemname,attribute,value);
185         elem = elem.getChild(childname);
186         return getText(elem);
187     }
188
189     /**
190      * This method returns the text of all children in the format childname<separator1>childtext<separator2>.
191      * @param elemname Name of the root element.
192      * @param attribute Name of the attribute which must root element have.
193      * @param value Value of the attribute.
194      */

195     public String JavaDoc getAllChildText(String JavaDoc elemname, String JavaDoc attribute, String JavaDoc value, String JavaDoc separator1, String JavaDoc separator2) {
196         String JavaDoc result = "";
197         try {
198             Element elem = getChild(elemname,attribute,value);
199             List JavaDoc list = elem.getChildren();
200             Iterator JavaDoc iter = list.iterator();
201             while (iter.hasNext()) {
202                 Element child = (Element)iter.next();
203                 result += child.getName();
204                 result += separator1;
205                 result += child.getText();
206                 result += separator2;
207             }
208         }
209         catch (Exception JavaDoc ex) {
210             if (logger.isEnabledFor(Level.ERROR))
211                 logger.error(ex.getMessage());
212             ex.printStackTrace();
213         }
214         return result;
215     }
216  
217     /**
218      * Returns an elementattribute.
219      * @param elem Name of the element.
220      * @param attrib Name of the attribute.
221      * @return Attribute
222      */

223     public Attribute getAttribute(Element elem, String JavaDoc attrib) {
224         if (doc == null)
225             return null;
226         else
227             return elem.getAttribute(attrib);
228     }
229     
230     /**
231      * Returns the value of an elementattribute.
232      * @param elem Name of the element.
233      * @param attrib Name of the attribute.
234      * @return Value of the attribute.
235      */

236     public String JavaDoc getAttributeValue(Element elem, String JavaDoc attrib) {
237         if (doc == null)
238             return null;
239         else
240             return elem.getAttributeValue(attrib);
241     }
242
243     /**
244      * This method set the value of an elementattribute.
245      * @param elem Name of the element.
246      * @param attrib Attribute of the element.
247      * @param value New value of the attribute.
248      */

249     public void setAttributeValue(Element elem, String JavaDoc attrib, String JavaDoc value) {
250         elem.getAttribute(attrib).setValue(value);
251     }
252     
253     public void setText(Element elem, String JavaDoc text) {
254         elem.setText(text);
255     }
256
257     /**
258      * This method saves the xml-file connected by XMLBean.
259      * NOTE: only call this on an XMLBean _NOT_ created from an InputStream!
260      */

261     public boolean writeXMLDoc() {
262         // it might be that we do not have an ordinary file,
263
// so we can't write to it
264
if (docPath == null)
265             return false;
266         
267         boolean result = true;
268         try {
269             XMLOutputter outputter = new XMLOutputter(docPath);
270             File JavaDoc file = new File JavaDoc(docPath);
271             OutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
272             //outputter.setIndent(" ");
273
//outputter.setNewlines(true);
274
outputter.output(doc,out);
275             out.close();
276         }
277         catch (Exception JavaDoc ex) {
278             result = false;
279             if (logger.isEnabledFor(Level.WARN))
280                 logger.warn(ex.getMessage());
281         }
282         return result;
283     }
284
285     /** This method returns all direct children of the root element.
286      * @return Childrenlist
287      */

288     public List JavaDoc getRootChild() {
289         if (doc == null)
290             return null;
291         else
292             return root.getChildren();
293     }
294
295     /**
296      * Returns a list of all attributes from an element.
297      * @param elem Name of the element.
298      * @return Attributelist
299      */

300     public List JavaDoc getAllAttribute(Element elem) {
301         if (doc == null)
302             return null;
303         else
304             return elem.getAttributes();
305     }
306     
307     /**
308      * Returns a list of all elements with the given elementname.
309      */

310     public List JavaDoc getAllChild(String JavaDoc elemname) {
311         if (doc == null)
312             return null;
313         else
314             return root.getChildren(elemname);
315     }
316     
317     /**
318      * Returns a list of all children with the specific name and the specific
319      * attribute value;
320      */

321     public List JavaDoc getAllChild(String JavaDoc elemname, String JavaDoc attribute, String JavaDoc value) {
322         List JavaDoc<Element> list = new LinkedList JavaDoc<Element>();
323         List JavaDoc elems = root.getChildren(elemname);
324         Iterator JavaDoc iter = elems.iterator();
325         while (iter.hasNext()) {
326             Element elem = (Element)iter.next();
327             try {
328                 String JavaDoc val = elem.getAttributeValue(attribute);
329                 if (val != null && val.equals(value))
330                     list.add(elem);
331             } catch (Exception JavaDoc e) {
332             }
333         }
334         return list;
335     }
336     
337     /**
338      * Removes an element.
339      * @param elemname Tag name of the element.
340      * @return
341      */

342     public boolean removeChild(String JavaDoc elemname) {
343         return root.removeChild(elemname);
344     }
345 }
346
Popular Tags