KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > data > DocumentBuilderBase


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.data;
11
12 import java.util.*;
13
14 /*
15  * Abstract base class for generating a document holding its elements in a
16  * tree. Inherit from this class and create your own DocumentBuilder.
17  * For example, have a look at XMLDocumentBuilder.
18  *
19  * @author Klaus Meffert
20  * @since 2.0
21  */

22 public abstract class DocumentBuilderBase {
23   /**@todo add new class DocumentCreatorBase that reads in data written by
24    * DocumentBuilderBase */

25
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.7 $";
28
29   /**
30    * Builds a document from the given input (input data + existing document).
31    * @param a_dataholder the input structure holding the data to
32    * be represented as a generic document
33    * @param a_document the document to put the elements in
34    * @throws Exception
35    * @return the document built up by adding elements
36    *
37    * @author Klaus Meffert
38    * @since 2.0
39    */

40   public Object JavaDoc buildDocument(final IDataCreators a_dataholder,
41                               final Object JavaDoc a_document)
42       throws Exception JavaDoc {
43     // Traverse over input structure.
44
// ------------------------------
45
IDataElementList tree = a_dataholder.getTree();
46     int len = tree.getLength();
47     IDataElement elem;
48     for (int i = 0; i < len; i++) {
49       elem = tree.item(i);
50       doTraverse(elem, a_document, null);
51     }
52     return a_document;
53   }
54
55   /**
56    * Recursive traversing over data tree containing elements to be transformed
57    * into tags.
58    * @param a_elem IDataElement
59    * @param a_document Document
60    * @param a_Element Element
61    * @throws Exception
62    *
63    * @author Klaus Meffert
64    * @since 2.0
65    */

66   private void doTraverse(final IDataElement a_elem, final Object JavaDoc a_document,
67                           final Object JavaDoc a_Element)
68       throws Exception JavaDoc {
69     String JavaDoc tagName = a_elem.getTagName();
70     Object JavaDoc element = createElementGeneric(a_document, a_Element, tagName);
71     Map attributes = a_elem.getAttributes();
72     Set keys = attributes.keySet();
73     Iterator it = keys.iterator();
74     String JavaDoc key, value;
75     while (it.hasNext()) {
76       key = (String JavaDoc) it.next();
77       value = (String JavaDoc) attributes.get(key);
78       setAttribute(element, key, value);
79     }
80     IDataElementList list = a_elem.getChildNodes();
81     if (list != null) {
82       for (int j = 0; j < list.getLength(); j++) {
83         IDataElement elem2 = list.item(j);
84         doTraverse(elem2, a_document, element);
85       }
86     }
87   }
88
89   /**
90    * Generically creates an element (Template Method).
91    * @param a_document could be used as factory to create the element with
92    * @param a_element null or existing element as template
93    * @param a_tagName name of tag to create for the element
94    * @return created element
95    *
96    * @author Klaus Meffert
97    * @since 2.0
98    */

99   private Object JavaDoc createElementGeneric(final Object JavaDoc a_document,
100                                       final Object JavaDoc a_element,
101                                       final String JavaDoc a_tagName) {
102     Object JavaDoc element;
103     if (a_element == null) {
104       element = createElement(a_document, null, a_tagName);
105       documentAppendChild(a_document, element);
106     }
107     else {
108       Object JavaDoc xmlElement2 = createElement(a_document, a_element, a_tagName);
109       elementAppendChild(a_element, xmlElement2);
110       element = xmlElement2;
111     }
112     return element;
113   }
114
115   /**
116    * Append a child to a given document.
117    * @param a_document to append element on (e.g. org.w3c.dom.Document)
118    * @param a_element to append to document (e.g. org.w3c.com.Element)
119    * @return a_document with appended element (e.g. org.w3c.dom.Document)
120    *
121    * @author Klaus Meffert
122    * @since 2.0
123    */

124   protected abstract Object JavaDoc documentAppendChild(Object JavaDoc a_document,
125                                                 Object JavaDoc a_element);
126
127   /**
128    * Append a child to a given element.
129    * @param a_rootElement to append childElement on (e.g. org.w3c.com.Element)
130    * @param a_childElement to append to rootElement (e.g. org.w3c.com.Element)
131    * @return rootElement with appended childElement (e.g. org.w3c.com.Element)
132    *
133    * @author Klaus Meffert
134    * @since 2.0
135    */

136   protected abstract Object JavaDoc elementAppendChild(Object JavaDoc a_rootElement,
137                                                Object JavaDoc a_childElement);
138
139   /**
140    * Creates an element with help for a given document.
141    * @param a_document could be used as factory to create the element with
142    * @param a_element null or existing element as template
143    * @param a_tagName name of tag to create for the element
144    * @return created element
145    *
146    * @author Klaus Meffert
147    * @since 2.0
148    */

149   protected abstract Object JavaDoc createElement(Object JavaDoc a_document, Object JavaDoc a_element,
150                                           String JavaDoc a_tagName);
151
152   /**
153    * Sets an attribute for a given Element.
154    * @param a_element the Element to set an attribute for
155    * (e.g. org.w3c.com.Element)
156    * @param a_key the key of the attribute
157    * @param a_value the value of the attribute
158    *
159    * @author Klaus Meffert
160    * @since 2.0
161    */

162   protected abstract void setAttribute(Object JavaDoc a_element, String JavaDoc a_key,
163                                        String JavaDoc a_value);
164 }
165
Popular Tags