KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > xml > BuilderWriter


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.xml;
11
12 import java.util.*;
13 import org.mmbase.module.core.MMObjectBuilder;
14 import org.mmbase.bridge.NodeManager;
15 import org.mmbase.core.CoreField;
16 import org.mmbase.core.util.Fields;
17 import org.mmbase.datatypes.DataType;
18 import org.mmbase.util.XMLEntityResolver;
19 import org.mmbase.util.DynamicDate;
20 import org.mmbase.util.logging.*;
21
22 import org.w3c.dom.*;
23
24 /**
25  * Class for creating builder configuration documents.
26  * Use this class to create a DOM document for a given builder.
27  * The document can then be used internally or serialized using a number of
28  * utility methods.
29  * This writer takes extension of builders (inheritance) into account.
30  *
31  * XXX This class actually transforms nice builder XML's to stupid automatic XML's in deprecated format, forgetting all comments, alls datatype extensions.
32  * TODO: This is broken!
33  *
34  * @since MMBase-1.6
35  * @author Pierre van Rooden
36  * @version $Id: BuilderWriter.java,v 1.23 2006/04/10 15:29:43 michiel Exp $
37  */

38 public class BuilderWriter extends DocumentWriter {
39
40     private static final Logger log = Logging.getLoggerInstance(BuilderWriter.class);
41
42     /**
43      * If true, the builder will expand when writing.
44      * Expanding means that all data of a builder is written, including non-overriden data
45      * from parent builders.
46      */

47     private boolean expandBuilder = false;
48     /**
49      * Hold a reference to the builder for which to create an XML document.
50      */

51     private MMObjectBuilder builder;
52
53     /**
54      * Constructs the document writer.
55      * The constructor calls its super to create a basic document, based on the builder document type.
56      * @param builder the builder for which to create an XML document.
57      */

58     public BuilderWriter(MMObjectBuilder builder) throws DOMException {
59         super("builder", BuilderReader.PUBLIC_ID_BUILDER,
60                         XMLEntityResolver.DOMAIN + XMLEntityResolver.DTD_SUBPATH + BuilderReader.DTD_BUILDER);
61         this.builder=builder;
62         getMessageRetriever("org.mmbase.util.xml.resources.builderwriter");
63     }
64
65     /**
66      * Generates the document. Can only be called once.
67      * @throws DOMException when an error occurred during generation
68      */

69     protected void generate() throws DOMException {
70         Element root=document.getDocumentElement();
71         addComment("builder.configuration",builder.getTableName(),root);
72         root.setAttribute("name",""+builder.getTableName());
73         root.setAttribute("maintainer",builder.getMaintainer());
74         root.setAttribute("version",""+builder.getVersion());
75         MMObjectBuilder parent=null;
76         if (!expandBuilder) {
77             parent=builder.getParentBuilder();
78             if (parent!=null) {
79                 root.setAttribute("extends",parent.getTableName());
80             }
81         }
82         // status
83
addComment("builder.status",root);
84         addContentElement("status","active",root);
85         // classfile
86
String JavaDoc data=builder.getClass().getName();
87         if ((parent==null) || (!parent.getClass().getName().equals(data))) {
88             // strip - do we want it like this?
89
if (data.startsWith("org.mmbase.module.builders.")) {
90                 data=data.substring(27);
91             }
92             addComment("builder.classfile",root);
93             addContentElement("classfile",data,root);
94         }
95         //searchage
96
data=builder.getSearchAge();
97         if ((parent==null) || (!parent.getSearchAge().equals(data))) {
98             addComment("builder.searchage",root);
99             addContentElement("searchage",data,root);
100         }
101         // names
102
Element names=document.createElement("names");
103         addComment("builder.names",root);
104         root.appendChild(names);
105         // names.singular
106
Map datamap=builder.getSingularNames();
107         addComment("builder.singular",names);
108         for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) {
109             Map.Entry em= (Map.Entry)i.next();
110             String JavaDoc language=(String JavaDoc)em.getKey();
111             String JavaDoc name=(String JavaDoc)em.getValue();
112             if ((parent==null) || !(name.equals(parent.getSingularName(language)))) {
113                 Element elm=addContentElement("singular",name,names);
114                 elm.setAttribute("xml:lang",language);
115             }
116         }
117         // names.plural
118
datamap=builder.getPluralNames();
119         addComment("builder.plural",names);
120         for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) {
121             Map.Entry em= (Map.Entry)i.next();
122             String JavaDoc language=(String JavaDoc)em.getKey();
123             String JavaDoc name=(String JavaDoc)em.getValue();
124             if ((parent==null) || !(name.equals(parent.getPluralName(language)))) {
125                 Element elm=addContentElement("plural",name,names);
126                 elm.setAttribute("xml:lang",language);
127             }
128         }
129         // descriptions
130
Element descriptions=document.createElement("descriptions");
131         addComment("builder.descriptions",root);
132         root.appendChild(descriptions);
133         // names.description
134
datamap=builder.getDescriptions();
135         addComment("builder.description",root);
136         for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) {
137             Map.Entry em= (Map.Entry)i.next();
138             String JavaDoc language=(String JavaDoc)em.getKey();
139             String JavaDoc description=(String JavaDoc)em.getValue();
140             if ((parent==null) || !(description.equals(parent.getDescription(language)))) {
141                 Element elm=addContentElement("description",description,descriptions);
142                 elm.setAttribute("xml:lang",language);
143             }
144         }
145         // properties
146
Element properties=document.createElement("properties");
147         addComment("builder.properties",root);
148         root.appendChild(properties);
149         // properties.property
150
datamap=builder.getInitParameters();
151         for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) {
152             Map.Entry em= (Map.Entry)i.next();
153             String JavaDoc propname=(String JavaDoc)em.getKey();
154             String JavaDoc propvalue=(String JavaDoc)em.getValue();
155             if ((parent==null) || !(propvalue.equals(parent.getInitParameter(propname)))) {
156                 Element elm=addContentElement("property",propvalue,properties);
157                 elm.setAttribute("name",propname);
158             }
159         }
160         // fieldlist
161
Element fieldlist=document.createElement("fieldlist");
162         addComment("builder.fieldlist",root);
163         root.appendChild(fieldlist);
164         // obtain all fields defined in the builder
165
List fields=builder.getFields(NodeManager.ORDER_CREATE);
166         for (Iterator f=fields.iterator(); f.hasNext();) {
167             CoreField fielddef=(CoreField)f.next();
168             // skip otype, cannot occur in a builder xml file (doh)
169
String JavaDoc fieldname=fielddef.getName();
170             if (fieldname.equals("otype")) continue;
171             CoreField parentField = null;
172             if (parent != null) {
173                 parentField = parent.getField(fieldname);
174             }
175             // check guidata
176
Element descriptionsElm = null;
177             datamap = fielddef.getLocalizedDescription().asMap();
178             for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) {
179                 Map.Entry em= (Map.Entry)i.next();
180                 Locale locale = (Locale)em.getKey();
181                 String JavaDoc description=(String JavaDoc)em.getValue();
182                 if ((parentField==null) || !(description.equals(parentField.getDescription(locale)))) {
183                     if (descriptionsElm == null) descriptionsElm = document.createElement("descriptions");
184                     Element elm=addContentElement("description", description, descriptionsElm);
185                     elm.setAttribute("xml:lang", locale.toString());
186                 }
187             }
188             Element guiElm = null;
189             datamap = fielddef.getLocalizedGUIName().asMap();
190             for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) {
191                 Map.Entry em= (Map.Entry)i.next();
192                 Locale locale = (Locale)em.getKey();
193                 String JavaDoc name=(String JavaDoc)em.getValue();
194                 if ((parentField==null) || !(name.equals(parentField.getGUIName(locale)))) {
195                     if (guiElm == null) guiElm = document.createElement("gui");
196                     Element elm=addContentElement("guiname", name, guiElm);
197                     elm.setAttribute("xml:lang", locale.toString());
198                 }
199             }
200             Element positionsElm = null;
201
202             // positions
203
// input
204
int pos=fielddef.getEditPosition();
205             if ((parentField==null) || (pos!=parentField.getEditPosition())) {
206                 if (positionsElm == null) positionsElm = document.createElement("positions");
207                 addComment("builder.field.editor.positions.input", positionsElm);
208                 addContentElement("input", "" + pos, positionsElm);
209             }
210             // list
211
pos=fielddef.getListPosition();
212             if ((parentField==null) || (pos!=parentField.getListPosition())) {
213                 if (positionsElm == null) positionsElm = document.createElement("positions");
214                 addComment("builder.field.editor.positions.list", positionsElm);
215                 addContentElement("list", "" + pos, positionsElm);
216             }
217             // search
218
pos=fielddef.getSearchPosition();
219             if ((parentField==null) || (pos!=parentField.getSearchPosition())) {
220                 if (positionsElm == null) positionsElm = document.createElement("positions");
221                 addComment("builder.field.editor.positions.search", positionsElm);
222                 addContentElement("search", "" + pos, positionsElm);
223             }
224
225             Element dataTypeElm = null;
226             DataType dataType = fielddef.getDataType();
227             if ((parentField == null) || !dataType.equals(parentField.getDataType())) {
228                 dataTypeElm = (Element) document.importNode(dataType.toXml(), true);
229             }
230
231             if ((parentField == null) || (dataTypeElm != null) || (descriptionsElm != null) || (guiElm != null) || (positionsElm != null)) {
232                 addComment("builder.field", fieldname, ""+fielddef.getStoragePosition(), fieldlist);
233                 Element field=document.createElement("field");
234                 fieldlist.appendChild(field);
235                 if (descriptionsElm != null) {
236                     addComment("builder.field.descriptions",field);
237                     field.appendChild(descriptionsElm);
238                 }
239                 if (guiElm != null) {
240                     addComment("builder.field.gui",field);
241                     field.appendChild(guiElm);
242                 }
243                 if (positionsElm != null) {
244                     addComment("builder.field.editor",field);
245                     Element editor=document.createElement("editor");
246                     editor.appendChild(positionsElm);
247                     field.appendChild(editor);
248                 }
249                 if (dataTypeElm != null) {
250                     addComment("builder.field.datatype",field);
251                     field.appendChild(dataTypeElm);
252                 }
253                 Element db = document.createElement("db");
254                 addComment("builder.field.db",field);
255                 field.appendChild(db);
256                 addComment("builder.field.db.name",db);
257                 addContentElement("name",fielddef.getName(),db);
258                 if (parentField==null) {
259                     String JavaDoc sType = Fields.getTypeDescription(fielddef.getType());
260                     addComment("builder.field.db.type",db);
261                     Element dbtype=addContentElement("type",sType,db);
262                     String JavaDoc sState = Fields.getStateDescription(fielddef.getState());
263                     dbtype.setAttribute("state",sState);
264                     int size = fielddef.getMaxLength();
265                     if (size>-1) {
266                         dbtype.setAttribute("size",""+size);
267                     }
268                     dbtype.setAttribute("notnull",""+fielddef.isRequired());
269                     dbtype.setAttribute("key",""+fielddef.isUnique());
270                 }
271             }
272         }
273     }
274
275     /**
276      * Sets whether the builder will expand when writing.
277      * Expanding means that all data of a builder is written, including non-overriden data
278      * from parent builders.
279      * @param value if true, the buidler will expand
280      */

281     public void setExpandBuilder(boolean value) {
282         expandBuilder=value;
283     }
284
285     /**
286      * Gets whether the builder will expand when writing.
287      * Expanding means that all data of a builder is written, including non-overriden data
288      * from parent builders.
289      * @return if true, the buidler will expand
290      */

291     public boolean getExpandBuilder() {
292         return expandBuilder;
293     }
294 }
295
Popular Tags