1 package net.sf.mmapps.modules.config;
2
3 import java.io.*;
4 import java.util.*;
5
6 import simplexml.XMLElement;
7
8 /***
9 * This is a utility class to serialize configurations to xml
10 * @author Kees Jongenburger
11 * @version $Id: ConfigurationXMLWriter.java,v 1.4 2004/05/03 11:18:36 keesj Exp $
12 **/
13 public abstract class ConfigurationXMLWriter {
14
15 public static void writeConfiguration(Configuration config, File baseDir) throws Exception {
16 if (!baseDir.exists()) {
17 baseDir.mkdirs();
18 }
19 NodeManagerConfigurations coreNodeManagersConfigurations = config.getNodeManagerConfigurations();
20 if (coreNodeManagersConfigurations.size() > 0) {
21
22 File buildersDir = new File(baseDir, "builders");
23 if (!buildersDir.exists()) {
24 buildersDir.mkdir();
25 }
26 File coreBuildersDir = new File(buildersDir, "core");
27 if (!coreBuildersDir.exists()) {
28 coreBuildersDir.mkdir();
29 }
30
31 for (int x = 0; x < coreNodeManagersConfigurations.size(); x++) {
32 NodeManagerConfiguration nodeManagerConfiguration = coreNodeManagersConfigurations.getNodeManagerConfiguration(x);
33 String content = createNodeManagerConfiguration(nodeManagerConfiguration);
34 File file = new File(coreBuildersDir, nodeManagerConfiguration.getName() + ".xml");
35 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
36 bw.write("<?xml version=\"1.0\"?>\n");
37 bw.write(
38 "<!DOCTYPE builder PUBLIC \"-//MMBase//DTD builder config 1.1//EN\" \"http://www.mmbase.org/dtd/builder_1_1.dtd\">\n");
39
40 bw.write(content);
41 bw.flush();
42 bw.close();
43 }
44 }
45 ApplicationConfigurations applicationConfigurations = config.getApplicationConfigurations();
46 if (applicationConfigurations.size() > 0) {
47
48 File applicationsDir = new File(baseDir, "applications");
49 if (!applicationsDir.exists()) {
50 applicationsDir.mkdir();
51 }
52 for (int x = 0; x < applicationConfigurations.size(); x++) {
53 ApplicationConfiguration appConfig = applicationConfigurations.getApplicationConfiguration(x);
54 writeApplication(appConfig, applicationsDir);
55 }
56 }
57 }
58
59 /***
60 * based on the data in the ApplicationConfiguration this method create Files and directories
61 * containg xml fiels (in the current directory)
62 * @param appconfig the application configuration
63 * @param basedir the config/applications directory
64 **/
65 public static void writeApplication(ApplicationConfiguration appconfig, File basedir) {
66 try {
67
68 File builderDir = new File(basedir, appconfig.getName() + File.separator + "builders");
69 builderDir.mkdirs();
70 System.err.println(appconfig.getName());
71 NodeManagerConfigurations nodeManagerConfigurations = appconfig.getNodeManagerConfigurations();
72
73 for (int x = 0; x < nodeManagerConfigurations.size(); x++) {
74 NodeManagerConfiguration nodeManagerConfiguration = nodeManagerConfigurations.getNodeManagerConfiguration(x);
75 String content = ConfigurationXMLWriter.createNodeManagerConfiguration(nodeManagerConfiguration);
76 File file = new File(builderDir, nodeManagerConfiguration.getName() + ".xml");
77 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
78 bw.write("<?xml version=\"1.0\"?>\n");
79 bw.write(
80 "<!DOCTYPE builder PUBLIC \"-//MMBase//DTD builder config 1.1//EN\" \"http://www.mmbase.org/dtd/builder_1_1.dtd\">\n");
81
82 bw.write(content);
83 bw.flush();
84 bw.close();
85
86 }
87
88 String content = createApplicationConfiguration(appconfig);
89 File appFile = new File(basedir, appconfig.getName() + ".xml");
90 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(appFile)));
91 bw.write("<?xml version=\"1.0\" ?>\n");
92 bw.write(
93 "<!DOCTYPE application PUBLIC \"-//MMBase//DTD application config 1.1//EN\" \"http://www.mmbase.org/dtd/application_1_1.dtd\">\n");
94
95 bw.write(content);
96 bw.flush();
97 bw.close();
98
99 } catch (FileNotFoundException fnfe) {
100 System.err.println(fnfe.getMessage());
101 } catch (IOException ioe) {
102 System.err.println(ioe.getMessage());
103 }
104 }
105
106 /***
107 * @return a String containing the xml definition of the application
108 **/
109 public static String createApplicationConfiguration(ApplicationConfiguration appconfig) {
110 XMLElement xmle = new XMLElement();
111 xmle.setTagName("application");
112 xmle.addProperty("name", appconfig.getName());
113 xmle.addProperty("maintainer", "mmbase.org");
114 xmle.addProperty("version", "1");
115 xmle.addProperty("auto-deploy", "true");
116
117 if (appconfig.getDepends().size() > 0) {
118 XMLElement r = new XMLElement();
119 r.setTagName("requirements");
120 Strings depends = appconfig.getDepends();
121 for (int y = 0; y < depends.size(); y++) {
122 XMLElement dep = new XMLElement();
123 dep.setTagName("requires");
124 dep.addProperty("name", depends.getString(y));
125 dep.addProperty("maintainer", "mmbase.org");
126 dep.addProperty("version", "1");
127 r.addChild(dep);
128 }
129 xmle.addChild(r);
130 }
131 XMLElement neededBuilderList = new XMLElement();
132 neededBuilderList.setTagName("neededbuilderlist");
133 NodeManagerConfigurations nodeManagerConfigurations = appconfig.getNodeManagerConfigurations();
134 for (int x = 0; x < nodeManagerConfigurations.size(); x++) {
135 NodeManagerConfiguration nc = nodeManagerConfigurations.getNodeManagerConfiguration(x);
136 XMLElement builder = new XMLElement();
137 builder.setTagName("builder");
138 builder.addProperty("maintainer", nc.getMaintainer());
139 builder.addProperty("version", nc.getVersion());
140 builder.setContent(nc.getName());
141 neededBuilderList.addChild(builder);
142 }
143
144 Strings otherNodeManagers = appconfig.getRequiredNodeManagers();
145 for (int x = 0; x < otherNodeManagers.size(); x++) {
146 String name = otherNodeManagers.getString(x);
147 if (nodeManagerConfigurations.getNodeManagerConfiguration(name) == null) {
148 XMLElement builder = new XMLElement();
149 builder.setTagName("builder");
150 builder.addProperty("maintainer", "unknown");
151 builder.addProperty("version", "unknown");
152 builder.setContent(name);
153 builder.setComment("external builder not defined in this application");
154 neededBuilderList.addChild(builder);
155 }
156 }
157
158 xmle.addChild(neededBuilderList);
159
160 XMLElement neededRelationDefinitionList = new XMLElement();
161 neededRelationDefinitionList.setTagName("neededreldeflist");
162
163
164 RelationTypes relationTypes = appconfig.getRelationTypes();
165 Hashtable at = new Hashtable();
166 for (int x = 0; x < relationTypes.size(); x++) {
167 RelationType rt = relationTypes.getRelationType(x);
168 if (at.get(rt.getName()) == null) {
169 XMLElement relation = new XMLElement();
170 relation.setTagName("reldef");
171 relation.addProperty("source", rt.getSourceName());
172 relation.addProperty("target", rt.getDestinationName());
173 relation.addProperty("guisourcename", rt.getName());
174 relation.addProperty("guitargetname", rt.getName());
175 relation.addProperty("direction", rt.getDirectionality());
176 relation.addProperty("builder", rt.getNodeManagerName());
177 neededRelationDefinitionList.addChild(relation);
178 at.put(rt.getName(), rt.getName());
179 }
180
181 }
182 xmle.addChild(neededRelationDefinitionList);
183
184 XMLElement allowedRelationList = new XMLElement();
185 allowedRelationList.setTagName("allowedrelationlist");
186
187 RelationManagerConfigurations relationManagerConfigurations = appconfig.getRelationManagerConfigurations();
188 for (int x = 0; x < relationManagerConfigurations.size(); x++) {
189 RelationManagerConfiguration rc = relationManagerConfigurations.getRelationManagerConfiguration(x);
190
191 XMLElement relation = new XMLElement();
192 relation.setTagName("relation");
193 relation.addProperty("from", rc.getSourceNodeManagerName());
194 relation.addProperty("to", rc.getDestinationNodeManagerName());
195 relation.addProperty("type", rc.getRelationTypeName());
196 allowedRelationList.addChild(relation);
197 }
198
199 xmle.addChild(allowedRelationList);
200
201
202 XMLElement datasourceList = new XMLElement();
203 datasourceList.setTagName("datasourcelist");
204 XMLElement relationsourceList = new XMLElement();
205 relationsourceList.setTagName("relationsourcelist");
206
207 for (int x = 0; x < otherNodeManagers.size(); x++) {
208 String name = otherNodeManagers.getString(x);
209 if (nodeManagerConfigurations.getNodeManagerConfiguration(name) == null) {
210
211 XMLElement element = new XMLElement();
212 element.setTagName("datasource");
213 element.addProperty("builder", name);
214 element.addProperty("path", appconfig.getName() + File.separator + name + ".xml");
215 datasourceList.addChild(element);
216 }
217 }
218
219 for (int x = 0; x < nodeManagerConfigurations.size(); x++) {
220 NodeManagerConfiguration nc = nodeManagerConfigurations.getNodeManagerConfiguration(x);
221
222 XMLElement element = new XMLElement();
223
224 element.addProperty("builder", nc.getName());
225 element.addProperty("path", appconfig.getName() + File.separator + nc.getName() + ".xml");
226
227 if (nc.getExtends().equals("insrel")) {
228 element.setTagName("relationsource");
229 relationsourceList.addChild(element);
230 } else {
231 element.setTagName("datasource");
232 datasourceList.addChild(element);
233
234 }
235
236 }
237
238 xmle.addChild(datasourceList);
239 xmle.addChild(relationsourceList);
240
241 XMLElement contextsourceList = new XMLElement();
242 contextsourceList.setTagName("contextsourcelist");
243 XMLElement contextsource = new XMLElement();
244 contextsource.setTagName("contextsource");
245 contextsource.addProperty("path", appconfig.getName() + File.separator + "backup.xml");
246 contextsource.addProperty("type", "depth");
247 contextsource.addProperty("goal", "backup");
248 contextsourceList.addChild(contextsource);
249 xmle.addChild(contextsourceList);
250
251 XMLElement description = new XMLElement();
252 description.setTagName("description");
253 description.setContent("desc");
254 xmle.addChild(description);
255 XMLElement installNotice = new XMLElement();
256 installNotice.setTagName("install-notice");
257 installNotice.setContent("installed");
258 xmle.addChild(installNotice);
259 return xmle.toString();
260
261 }
262
263 /***
264 * @return an String representation of an nodemanager configuration (builder.xml)
265 **/
266 public static String createNodeManagerConfiguration(NodeManagerConfiguration nodeManagerConfiguration) {
267 XMLElement builder = new XMLElement();
268 builder.setComment("created by " + ConfigurationXMLWriter.class.getName());
269 builder.setComment(nodeManagerConfiguration.getName());
270 builder.setTagName("builder");
271 builder.addProperty("maintainer", nodeManagerConfiguration.getMaintainer());
272 builder.addProperty("version", nodeManagerConfiguration.getVersion());
273 builder.addProperty("extends", nodeManagerConfiguration.getExtends());
274 builder.addProperty("name", nodeManagerConfiguration.getName());
275
276 if (nodeManagerConfiguration.getClassFile() != null) {
277 XMLElement classFileElement = new XMLElement();
278 classFileElement.setTagName("classfile");
279 classFileElement.setContent(nodeManagerConfiguration.getClassFile());
280 builder.addChild(classFileElement);
281 }
282
283 XMLElement searchAge = new XMLElement();
284 searchAge.setTagName("searchage");
285 searchAge.setContent(nodeManagerConfiguration.getSearchAge());
286 builder.addChild(searchAge);
287
288 XMLElement names = new XMLElement();
289 names.setTagName("names");
290
291 Enumeration elements = nodeManagerConfiguration.getSingularGUINames().keys();
292 while (elements.hasMoreElements()) {
293 String key = (String) elements.nextElement();
294 XMLElement singular = new XMLElement();
295 singular.setTagName("singular");
296 singular.addProperty("xml:lang", key);
297 singular.setContent(nodeManagerConfiguration.getSingularGUIName(key));
298 names.addChild(singular);
299 }
300
301 elements = nodeManagerConfiguration.getPluralGUINames().keys();
302 while (elements.hasMoreElements()) {
303 String key = (String) elements.nextElement();
304 XMLElement plural = new XMLElement();
305 plural.setTagName("plural");
306 plural.addProperty("xml:lang", key);
307 plural.setContent(nodeManagerConfiguration.getPluralGUIName(key));
308 names.addChild(plural);
309 }
310 builder.addChild(names);
311
312 XMLElement descriptions = new XMLElement();
313 descriptions.setTagName("descriptions");
314 elements = nodeManagerConfiguration.getDescriptions().keys();
315 while (elements.hasMoreElements()) {
316 String key = (String) elements.nextElement();
317 XMLElement description = new XMLElement();
318 description.setTagName("description");
319 description.addProperty("xml:lang", key);
320 description.setContent(nodeManagerConfiguration.getDescription(key));
321 descriptions.addChild(description);
322
323 }
324
325 builder.addChild(descriptions);
326
327 XMLElement properties = new XMLElement();
328 properties.setTagName("properties");
329 elements = nodeManagerConfiguration.getPropeties().keys();
330 while (elements.hasMoreElements()) {
331 String key = (String) elements.nextElement();
332 XMLElement property = new XMLElement();
333 property.setTagName("property");
334 property.addProperty("name", key);
335 property.setContent(nodeManagerConfiguration.getProperty(key));
336 properties.addChild(property);
337
338 }
339 builder.addChild(properties);
340
341 builder.addChild(getXMLFieldConfigurations(nodeManagerConfiguration.getFieldConfigurations()));
342 return builder.toString();
343 }
344
345 /***
346 * @return an string representation of the xml configuration of a node field
347 **/
348 public static XMLElement getXMLFieldConfigurations(FieldConfigurations fieldConfigurations) {
349 XMLElement fieldList = new XMLElement();
350 fieldList.setTagName("fieldlist");
351 for (int x = 0; x < fieldConfigurations.size(); x++) {
352 FieldConfiguration fieldConfiguration = fieldConfigurations.getFieldConfiguration(x);
353 XMLElement field = new XMLElement();
354 field.setTagName("field");
355
356
357 XMLElement descriptions = new XMLElement();
358 descriptions.setTagName("descriptions");
359 Enumeration elements = fieldConfiguration.getDescriptions().keys();
360 while (elements.hasMoreElements()) {
361 String key = (String) elements.nextElement();
362 XMLElement description = new XMLElement();
363 description.setTagName("description");
364 description.addProperty("xml:lang", key);
365 description.setContent(fieldConfiguration.getGUIName(key));
366 descriptions.addChild(description);
367
368 }
369 field.addChild(descriptions);
370
371
372 XMLElement gui = new XMLElement();
373 gui.setTagName("gui");
374
375 elements = fieldConfiguration.getGUINames().keys();
376 while (elements.hasMoreElements()) {
377 String key = (String) elements.nextElement();
378 XMLElement guiname = new XMLElement();
379 guiname.setTagName("guiname");
380 guiname.addProperty("xml:lang", key);
381 guiname.setContent(fieldConfiguration.getGUIName(key));
382 gui.addChild(guiname);
383
384 }
385 XMLElement guitype = new XMLElement();
386 guitype.setTagName("guitype");
387 guitype.setContent(fieldConfiguration.getGUIType());
388 gui.addChild(guitype);
389 field.addChild(gui);
390
391 XMLElement editor = new XMLElement();
392 editor.setTagName("editor");
393 XMLElement positions = new XMLElement();
394 positions.setTagName("positions");
395 XMLElement input = new XMLElement();
396 input.setTagName("input");
397 input.setContent(fieldConfiguration.getEditorPosition());
398 positions.addChild(input);
399
400 XMLElement list = new XMLElement();
401 list.setTagName("list");
402 list.setContent(fieldConfiguration.getListPosition());
403 positions.addChild(list);
404
405 XMLElement search = new XMLElement();
406 search.setTagName("search");
407 search.setContent(fieldConfiguration.getSearchPosition());
408 positions.addChild(search);
409
410 editor.addChild(positions);
411 field.addChild(editor);
412
413 XMLElement db = new XMLElement();
414 db.setTagName("db");
415 XMLElement name = new XMLElement();
416 name.setTagName("name");
417 name.setContent(fieldConfiguration.getName());
418 db.addChild(name);
419
420 XMLElement type = new XMLElement();
421 type.setTagName("type");
422 type.addProperty("state", fieldConfiguration.getState());
423 type.addProperty("notnull", String.valueOf(fieldConfiguration.isNotNull()));
424 type.addProperty("key", String.valueOf(fieldConfiguration.isKey()));
425 if (fieldConfiguration.getDocType() != null) {
426 type.addProperty("doctype", fieldConfiguration.getDocType());
427 }
428 if (fieldConfiguration.getSize() != null) {
429 type.addProperty("size", fieldConfiguration.getSize());
430 }
431 type.setContent(fieldConfiguration.getType());
432
433 db.addChild(type);
434 field.addChild(db);
435 fieldList.addChild(field);
436 }
437 return fieldList;
438 }
439 }