KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > xml > XMLDocumentBuilder


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.xml;
11
12 import org.jgap.data.*;
13 import org.w3c.dom.*;
14 import javax.xml.parsers.*;
15
16 /**
17  * Class building an XML file. Can be used to persist objects like Genotype,
18  * Chromosome or Gene (or a list of them) to an XML file.
19  *
20  * @author Klaus Meffert
21  * @since 2.0
22  */

23 public class XMLDocumentBuilder
24     extends DocumentBuilderBase {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private final static String JavaDoc CVS_REVISION = "$Revision: 1.8 $";
27
28   protected void setAttribute(final Object JavaDoc a_xmlElement, final String JavaDoc a_key,
29                               final String JavaDoc a_value) {
30     ( (Element) a_xmlElement).setAttribute(a_key, a_value);
31   }
32
33   protected Object JavaDoc documentAppendChild(final Object JavaDoc a_xmlDocument,
34                                        final Object JavaDoc a_element) {
35     return ( (Document) a_xmlDocument).appendChild( (Element) a_element);
36   }
37
38   protected Object JavaDoc elementAppendChild(final Object JavaDoc a_xmlElement,
39                                       final Object JavaDoc a_element) {
40     return ( (Element) a_xmlElement).appendChild( (Element) a_element);
41   }
42
43   protected Object JavaDoc createElement(final Object JavaDoc a_doc, final Object JavaDoc a_xmlElement,
44                                  final String JavaDoc a_tagName) {
45     return ( (Document) a_doc).createElement(a_tagName);
46   }
47
48   /**
49    * Convenience method to build an XML document from a generic input structure
50    * (of type IDataCreators)
51    * @param a_document the input structure holding the data to be represented
52    * as XML document
53    * @throws Exception
54    * @return Object the XML document
55    *
56    * @author Klaus Meffert
57    * @since 2.0
58    */

59   public Object JavaDoc buildDocument(final IDataCreators a_document)
60       throws Exception JavaDoc {
61     DocumentBuilder m_documentCreator = DocumentBuilderFactory.newInstance().
62         newDocumentBuilder();
63     Document xmlDoc = m_documentCreator.newDocument();
64     return super.buildDocument(a_document, xmlDoc);
65   }
66 }
67
Popular Tags