KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > io > ModelDocumentHandler


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.modeler.io;
31
32 import java.io.ByteArrayOutputStream JavaDoc;
33 import java.io.UnsupportedEncodingException JavaDoc;
34
35 import javax.xml.parsers.DocumentBuilder JavaDoc;
36 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
37 import javax.xml.transform.OutputKeys JavaDoc;
38 import javax.xml.transform.Transformer JavaDoc;
39 import javax.xml.transform.TransformerFactory JavaDoc;
40 import javax.xml.transform.dom.DOMSource JavaDoc;
41 import javax.xml.transform.stream.StreamResult JavaDoc;
42
43 import org.w3c.dom.CDATASection JavaDoc;
44 import org.w3c.dom.DOMException JavaDoc;
45 import org.w3c.dom.Document JavaDoc;
46 import org.w3c.dom.Element JavaDoc;
47
48 import com.genimen.djeneric.structure.EditorDefinition;
49 import com.genimen.djeneric.structure.ExtentUsage;
50 import com.genimen.djeneric.structure.PropertyUsage;
51 import com.genimen.djeneric.structure.ResourceDefinition;
52 import com.genimen.djeneric.structure.ScriptDefinition;
53 import com.genimen.djeneric.tools.io.DjenericDocumentHandler;
54 import com.genimen.djeneric.tools.modeler.ModelEditor;
55 import com.genimen.djeneric.tools.modeler.diagrammer.ExtentViewer;
56 import com.genimen.djeneric.tools.modeler.userperspective.ExtentNode;
57 import com.genimen.djeneric.tools.modeler.userperspective.ViewEditor;
58
59 public class ModelDocumentHandler extends DjenericDocumentHandler
60 {
61   public ModelDocumentHandler(String JavaDoc xmlString) throws Exception JavaDoc
62   {
63     super(xmlString);
64   }
65
66   public ModelDocumentHandler() throws Exception JavaDoc
67   {
68   }
69
70   protected Element JavaDoc addExtentUsageToDoc(ExtentNode theNode, Document JavaDoc doc, Element JavaDoc parent)
71   {
72     Element JavaDoc extentElement = doc.createElement(NAVIGATOR_EXTENTNODE_ELEMENT);
73
74     parent.appendChild(extentElement);
75
76     extentElement.setAttribute(NAVIGATOR_EXTENTNODE_NAME, "" + theNode.getExtent().getName());
77     extentElement.setAttribute(NAVIGATOR_EXTENTNODE_ALIAS, "" + theNode.getExtent().getInternalCode());
78     extentElement.setAttribute(NAVIGATOR_EXTENTNODE_TYPE, "" + theNode.getExtent().getObjectType());
79
80     if (theNode.getTitle() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_TITLE, theNode.getTitle());
81     if (theNode.getOqlFilter() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_QBE_FILTER, theNode
82         .getOqlFilter());
83     if (theNode.getCustomNodeClass() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_CUSTOM_NODE_CLASS,
84                                                                          theNode.getCustomNodeClass());
85     if (theNode.getId() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_ID, theNode.getId());
86     if (theNode.getDescriptorExpression() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DESCRIPTOR, theNode
87         .getDescriptorExpression());
88
89     extentElement.setAttribute(NAVIGATOR_EXTENTNODE_EDIT, "" + theNode.isEditAllowed());
90     extentElement.setAttribute(NAVIGATOR_EXTENTNODE_INSERT, "" + theNode.isInsertAllowed());
91     extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DELETE, "" + theNode.isDeleteAllowed());
92     extentElement.setAttribute(NAVIGATOR_EXTENTNODE_RECURSIVE, "" + theNode.isRecursive());
93
94     if (theNode.getVia() != null)
95     {
96       extentElement.setAttribute(NAVIGATOR_EXTENTNODE_VIA, theNode.getVia().getRelationName());
97       extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DETAIL_COLUMN, theNode.getVia().getDetailPropertyName());
98     }
99
100     // Do not store editors and children of recursive nodes:
101
if (!theNode.isRecursive())
102     {
103       EditorDefinition editor = theNode.getEditor();
104       if (editor != null && !editor.isMarkedForDelete())
105       {
106         extentElement.setAttribute(NAVIGATOR_EXTENTNODE_EDITOR, editor.getName());
107         extentElement.setAttribute(NAVIGATOR_EXTENTNODE_EDITORTARGET, theNode.getEditorTarget());
108       }
109
110       ResourceDefinition img = theNode.getImageIconResource();
111       if (img != null && !img.isMarkedForDelete())
112       {
113         extentElement.setAttribute(NAVIGATOR_EXTENTNODE_IMAGEICON, img.getAbsolutePath());
114       }
115
116       ExtentNode[] children = theNode.getChildren();
117       for (int i = 0; i < children.length; i++)
118       {
119         addExtentUsageToDoc(children[i], doc, extentElement);
120       }
121     }
122     return extentElement;
123   }
124
125   protected Element JavaDoc addEditorUsagesDoc(ExtentUsage theNode, Document JavaDoc doc, Element JavaDoc parent)
126   {
127     Element JavaDoc extentElement = doc.createElement(EDITOR_EXTENT_USAGE_ELEMENT);
128
129     parent.appendChild(extentElement);
130
131     extentElement.setAttribute(EDITOR_NAME, "" + theNode.getExtent().getName());
132     extentElement.setAttribute(EDITOR_EXTENTNODE_ALIAS, "" + theNode.getExtent().getInternalCode());
133     extentElement.setAttribute(EDITOR_EXTENTNODE_TYPE, "" + theNode.getExtent().getObjectType());
134
135     if (theNode.getTitle() != null) extentElement.setAttribute(EDITOR_TITLE, theNode.getTitle());
136     if (theNode.getOqlExpression() != null) extentElement.setAttribute(EDITOR_QBE_FILTER, theNode.getOqlExpression());
137
138     if (theNode.getId() != null) extentElement.setAttribute(EDITOR_EXTENTNODE_ID, theNode.getId());
139     if (theNode.getDescriptorExpression() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DESCRIPTOR, theNode
140         .getDescriptorExpression());
141
142     extentElement.setAttribute(EDITOR_EDIT, "" + theNode.isEditAllowed());
143     extentElement.setAttribute(EDITOR_INSERT, "" + theNode.isInsertAllowed());
144     extentElement.setAttribute(EDITOR_DELETE, "" + theNode.isDeleteAllowed());
145
146     extentElement.setAttribute(EDITOR_MULTIROW, "" + theNode.isMultiRow());
147     extentElement.setAttribute(EDITOR_ROWSDISPLAYED, "" + theNode.getRowsDisplayed());
148     addPropertyUsagesToDoc(theNode, doc, extentElement);
149
150     if (theNode.getVia() != null)
151     {
152       extentElement.setAttribute(EDITOR_EXTENTNODE_VIA, theNode.getVia().getRelationName());
153       extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DETAIL_COLUMN, theNode.getVia().getDetailPropertyName());
154     }
155
156     ExtentUsage[] children = theNode.getChildren();
157     for (int i = 0; i < children.length; i++)
158     {
159       addEditorUsagesDoc(children[i], doc, extentElement);
160     }
161     return extentElement;
162   }
163
164   private String JavaDoc nvl(String JavaDoc value)
165   {
166     if (value == null) return "";
167     return value;
168   }
169
170   public void addPropertyUsagesToDoc(ExtentUsage theNode, Document JavaDoc doc, Element JavaDoc parent)
171   {
172     theNode.sortPropertyUsages();
173     for (int i = 0; i < theNode.getPropertyUsageCount(); i++)
174     {
175       Element JavaDoc columnElement = doc.createElement(EDITOR_PROPERTY_USAGE_ELEMENT);
176       parent.appendChild(columnElement);
177       PropertyUsage cu = theNode.getPropertyUsage(i);
178
179       if (cu.getBaseProperty() == null) continue;
180
181       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_SEQ, "" + cu.getSeq());
182       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_COLUMN, cu.getBaseProperty().getName());
183       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_PROMPT, cu.getPrompt());
184       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_DESCRIPTION, cu.getDescription());
185       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_COMPONENT, cu.getComponentTypeName());
186       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_REQUIRED, "" + cu.isRequired());
187       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_UPDATEABLE, "" + cu.isUpdateable());
188       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_WIDTH, "" + cu.getDisplayWidth());
189       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_HEIGHT, "" + cu.getDisplayHeight());
190       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_SORT_ORDER, "" + cu.getSortOrder());
191       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_DEFAULT_VALUE, nvl(cu.getDefaultValue()));
192       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_CUSTOM_EDITOR, nvl(cu.getCustomEditorClass()));
193       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_RELATIVE_PATH, nvl(cu.getRelativePath()));
194       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_QUERYABLE, "" + cu.isQueryable());
195       columnElement.setAttribute(EDITOR_PROPERTY_USAGE_DISPLAYED, "" + cu.isDisplayed());
196     }
197   }
198
199   protected void addEditorToDoc(EditorDefinition definition, Document JavaDoc doc, Element JavaDoc editors) throws DOMException JavaDoc,
200       UnsupportedEncodingException JavaDoc
201   {
202     Element JavaDoc editor = doc.createElement(EDITOR_ELEMENT);
203     editors.appendChild(editor);
204     editor.setAttribute(EDITOR_NAME, definition.getName());
205     if (definition.getCustomEditorClass() != null) editor.setAttribute(EDITOR_CUSTOMCLASS, definition
206         .getCustomEditorClass());
207     if (definition.getUsages() != null) addEditorUsagesDoc(definition.getUsages(), doc, editor);
208
209     Element JavaDoc scriptElem = doc.createElement(EDITOR_EMBEDDED_SCRIPT);
210
211     CDATASection JavaDoc descr = doc.createCDATASection(definition.getScriptDefinition().getBase64());
212     scriptElem.appendChild(descr);
213     editor.appendChild(scriptElem);
214   }
215
216   public void addResourceToDoc(ResourceDefinition def, Document JavaDoc doc, Element JavaDoc parent) throws DOMException JavaDoc,
217       UnsupportedEncodingException JavaDoc
218   {
219
220     Element JavaDoc descrElem = doc.createElement(RESOURCE_ELEMENT);
221     descrElem.setAttribute(RESOURCE_ELEMENT_ABSOLUTE_PATH, def.getAbsolutePath());
222     descrElem.setAttribute(RESOURCE_ELEMENT_TIMESTAMP, def.getTimeStamp());
223
224     CDATASection JavaDoc descr = doc.createCDATASection(def.getBase64());
225     descrElem.appendChild(descr);
226     parent.appendChild(descrElem);
227   }
228
229   public void addScriptToDoc(ScriptDefinition script, Document JavaDoc doc, Element JavaDoc parent) throws DOMException JavaDoc,
230       UnsupportedEncodingException JavaDoc
231   {
232     Element JavaDoc scriptElem = doc.createElement(SCRIPT_ELEMENT);
233     scriptElem.setAttribute(SCRIPT_ELEMENT_TITLE, script.getTitle());
234
235     CDATASection JavaDoc descr = doc.createCDATASection(script.getBase64());
236     scriptElem.appendChild(descr);
237     parent.appendChild(scriptElem);
238   }
239
240   public String JavaDoc createXmlString(ModelEditor editor) throws Exception JavaDoc
241   {
242     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
243     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
244     Document JavaDoc doc = builder.newDocument();
245
246     Element JavaDoc root = doc.createElement(VIEW);
247     root.setAttribute(VIEW_STRUCTURE_VERSION, String.valueOf(VIEW_STRUCTURE_VERSION_NUMBER));
248     Element JavaDoc diagram = doc.createElement(DIAGRAM_ELEMENT);
249
250     doc.appendChild(root);
251     root.appendChild(diagram);
252
253     for (int i = 0; i < editor.getExtentViewerCount(); i++)
254     {
255       ExtentViewer tv = editor.getExtentViewer(i);
256
257       Element JavaDoc extentViewer = doc.createElement(EXTENTVIEW_ELEMENT);
258       extentViewer.setAttribute(EXTENTVIEW_TABLE, "" + tv.getExtent().getName());
259       extentViewer.setAttribute(EXTENTVIEW_ALIAS, "" + tv.getExtent().getInternalCode());
260       extentViewer.setAttribute(EXTENTVIEW_TYPE, "" + tv.getExtent().getObjectType());
261       extentViewer.setAttribute(EXTENTVIEW_X, "" + tv.getX());
262       extentViewer.setAttribute(EXTENTVIEW_Y, "" + tv.getY());
263       extentViewer.setAttribute(EXTENTVIEW_WIDTH, "" + tv.getWidth());
264       extentViewer.setAttribute(EXTENTVIEW_HEIGHT, "" + tv.getHeight());
265       extentViewer.setAttribute(EXTENTVIEW_TCOLOR, encodeColor(tv.getExtentColor()));
266       extentViewer.setAttribute(EXTENTVIEW_NCOLOR, encodeColor(tv.getNameColor()));
267       extentViewer.setAttribute(EXTENTVIEW_CCOLOR, encodeColor(tv.getPropertyColor()));
268
269       diagram.appendChild(extentViewer);
270     }
271
272     Element JavaDoc navigator = doc.createElement(NAVIGATOR_ELEMENT);
273     root.appendChild(navigator);
274     ViewEditor viewEditor = editor.getViewEditor();
275
276     ExtentNode[] extentNodes = viewEditor.getRootExtents();
277     for (int i = 0; i < extentNodes.length; i++)
278     {
279       addExtentUsageToDoc(extentNodes[i], doc, navigator);
280     }
281
282     Element JavaDoc editors = doc.createElement(EDITORS_ELEMENT);
283     root.appendChild(editors);
284
285     EditorDefinition[] editorDefs = viewEditor.getEditors();
286     for (int i = 0; i < editorDefs.length; i++)
287     {
288       addEditorToDoc(editorDefs[i], doc, editors);
289     }
290
291     ResourceDefinition[] resources = viewEditor.getResources();
292     for (int i = 0; i < resources.length; i++)
293     {
294       if (!resources[i].isMarkedForDelete()) addResourceToDoc(resources[i], doc, root);
295     }
296
297     ScriptDefinition[] scripts = viewEditor.getScripts();
298     for (int i = 0; i < scripts.length; i++)
299     {
300       if (!scripts[i].isMarkedForDelete()) addScriptToDoc(scripts[i], doc, root);
301     }
302
303     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
304
305     Transformer JavaDoc serializer = TransformerFactory.newInstance().newTransformer();
306     serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
307     serializer.setOutputProperty(OutputKeys.INDENT, "yes");
308     serializer.transform(new DOMSource JavaDoc(doc), new StreamResult JavaDoc(baos));
309
310     return baos.toString();
311   }
312
313 }
Popular Tags