KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > io > DjenericDocumentHandler


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

27 package com.genimen.djeneric.tools.io;
28
29 import java.awt.Color JavaDoc;
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 import javax.swing.JFrame JavaDoc;
37 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
38
39 import org.w3c.dom.CDATASection JavaDoc;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43
44 import com.genimen.djeneric.language.Messages;
45 import com.genimen.djeneric.repository.DjExtent;
46 import com.genimen.djeneric.repository.DjPersistenceManager;
47 import com.genimen.djeneric.repository.DjProperty;
48 import com.genimen.djeneric.repository.DjRelation;
49 import com.genimen.djeneric.repository.exceptions.CatalogException;
50 import com.genimen.djeneric.repository.exceptions.DjenericException;
51 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
52 import com.genimen.djeneric.structure.EditorDefinition;
53 import com.genimen.djeneric.structure.ExtentUsage;
54 import com.genimen.djeneric.structure.PropertyUsage;
55 import com.genimen.djeneric.structure.ResourceDefinition;
56 import com.genimen.djeneric.structure.ScriptDefinition;
57 import com.genimen.djeneric.tools.modeler.diagrammer.ExtentViewer;
58 import com.genimen.djeneric.util.DjLogger;
59
60 public class DjenericDocumentHandler
61 {
62   private Document JavaDoc _xmlDoc = null;
63
64   public final static String JavaDoc VIEW = "view";
65
66   public final static String JavaDoc VIEW_STRUCTURE_VERSION = "structureversion";
67   public final static int VIEW_STRUCTURE_VERSION_NUMBER = 101;
68
69   public final static String JavaDoc DIAGRAM_ELEMENT = "diagram";
70
71   public final static String JavaDoc EXTENTVIEW_ELEMENT = "extentview";
72   public final static String JavaDoc EXTENTVIEW_TABLE = "extent";
73   public final static String JavaDoc EXTENTVIEW_ALIAS = "alias";
74   public final static String JavaDoc EXTENTVIEW_TYPE = "type";
75   public final static String JavaDoc EXTENTVIEW_X = "x";
76   public final static String JavaDoc EXTENTVIEW_Y = "y";
77   public final static String JavaDoc EXTENTVIEW_WIDTH = "with";
78   public final static String JavaDoc EXTENTVIEW_HEIGHT = "height";
79   public final static String JavaDoc EXTENTVIEW_TCOLOR = "tcolor";
80   public final static String JavaDoc EXTENTVIEW_NCOLOR = "ncolor";
81   public final static String JavaDoc EXTENTVIEW_CCOLOR = "vcolor";
82
83   public final static String JavaDoc NAVIGATOR_ELEMENT = "navigator";
84
85   public final static String JavaDoc NAVIGATOR_EXTENTNODE_ELEMENT = "extentnode";
86   public final static String JavaDoc NAVIGATOR_EXTENTNODE_NAME = "name";
87   public final static String JavaDoc NAVIGATOR_EXTENTNODE_TYPE = "type";
88   public final static String JavaDoc NAVIGATOR_EXTENTNODE_VIA = "via";
89   public final static String JavaDoc NAVIGATOR_EXTENTNODE_DETAIL_COLUMN = "viacolumn";
90   public final static String JavaDoc NAVIGATOR_EXTENTNODE_TITLE = "title";
91   public final static String JavaDoc NAVIGATOR_EXTENTNODE_ALIAS = "alias";
92   public final static String JavaDoc NAVIGATOR_EXTENTNODE_ID = "id";
93   public final static String JavaDoc NAVIGATOR_EXTENTNODE_INTERNAL_CODE = "extentcode";
94   public final static String JavaDoc NAVIGATOR_EXTENTNODE_DESCRIPTOR = "descriptor";
95   public final static String JavaDoc NAVIGATOR_EXTENTNODE_EDIT = "edit";
96   public final static String JavaDoc NAVIGATOR_EXTENTNODE_INSERT = "insert";
97   public final static String JavaDoc NAVIGATOR_EXTENTNODE_DELETE = "delete";
98   public final static String JavaDoc NAVIGATOR_EXTENTNODE_RECURSIVE = "recursive";
99   public final static String JavaDoc NAVIGATOR_EXTENTNODE_QBE_FILTER = "qbefilter";
100
101   public final static String JavaDoc NAVIGATOR_EXTENTNODE_CUSTOM_NODE_CLASS = "customnodeclass";
102   public final static String JavaDoc NAVIGATOR_EXTENTNODE_EDITOR = "editor";
103   public final static String JavaDoc NAVIGATOR_EXTENTNODE_EDITORTARGET = "editortarget";
104   public final static String JavaDoc NAVIGATOR_EXTENTNODE_IMAGEICON = "imageicon";
105
106   public final static String JavaDoc EDITORS_ELEMENT = "editors";
107
108   public final static String JavaDoc EDITOR_ELEMENT = "editor";
109   public final static String JavaDoc EDITOR_NAME = "name";
110   public final static String JavaDoc EDITOR_CUSTOMCLASS = "class";
111   public final static String JavaDoc EDITOR_EXTENTNODE_TYPE = "type";
112   public final static String JavaDoc EDITOR_EXTENTNODE_VIA = "via";
113   public final static String JavaDoc EDITOR_DETAIL_COLUMN = "viacolumn";
114   public final static String JavaDoc EDITOR_TITLE = "title";
115   public final static String JavaDoc EDITOR_QBE_FILTER = "qbefilter";
116   public final static String JavaDoc EDITOR_EXTENTNODE_ID = "id";
117   public final static String JavaDoc EDITOR_EXTENTNODE_ALIAS = "alias";
118   public final static String JavaDoc EDITOR_EXTENTNODE_INTERNAL_CODE = "extentcode";
119   public final static String JavaDoc EDITOR_EDIT = "edit";
120   public final static String JavaDoc EDITOR_INSERT = "insert";
121   public final static String JavaDoc EDITOR_DELETE = "delete";
122   public final static String JavaDoc EDITOR_MULTIROW = "multirow";
123   public final static String JavaDoc EDITOR_ROWSDISPLAYED = "rows";
124   public final static String JavaDoc EDITOR_EMBEDDED_SCRIPT = "embeddedscript";
125
126   public final static String JavaDoc RESOURCE_ELEMENT = "resource";
127   public final static String JavaDoc RESOURCE_ELEMENT_ABSOLUTE_PATH = "abspath";
128   public final static String JavaDoc RESOURCE_ELEMENT_TIMESTAMP = "timestamp";
129
130   public final static String JavaDoc SCRIPT_ELEMENT = "script";
131   public final static String JavaDoc SCRIPT_ELEMENT_TITLE = "title";
132
133   public final static String JavaDoc EDITOR_EXTENT_USAGE_ELEMENT = "extentusage";
134   public final static String JavaDoc EDITOR_PROPERTY_USAGE_ELEMENT = "columnusage";
135   public final static String JavaDoc EDITOR_PROPERTY_USAGE_SEQ = "seq";
136   public final static String JavaDoc EDITOR_PROPERTY_USAGE_COLUMN = "column";
137   public final static String JavaDoc EDITOR_PROPERTY_USAGE_PROMPT = "prompt";
138   public final static String JavaDoc EDITOR_PROPERTY_USAGE_DESCRIPTION = "description";
139   public final static String JavaDoc EDITOR_PROPERTY_USAGE_COMPONENT = "component";
140   public final static String JavaDoc EDITOR_PROPERTY_USAGE_REQUIRED = "required";
141   public final static String JavaDoc EDITOR_PROPERTY_USAGE_UPDATEABLE = "updateable";
142   public final static String JavaDoc EDITOR_PROPERTY_USAGE_QUERYABLE = "queryable";
143   public final static String JavaDoc EDITOR_PROPERTY_USAGE_DISPLAYED = "displayed";
144   public final static String JavaDoc EDITOR_PROPERTY_USAGE_DEFAULT_VALUE = "default";
145   public final static String JavaDoc EDITOR_PROPERTY_USAGE_CUSTOM_EDITOR = "customeditor";
146   public final static String JavaDoc EDITOR_PROPERTY_USAGE_RELATIVE_PATH = "relativepath";
147   public final static String JavaDoc EDITOR_PROPERTY_USAGE_WIDTH = "width";
148   public final static String JavaDoc EDITOR_PROPERTY_USAGE_HEIGHT = "height";
149   public final static String JavaDoc EDITOR_PROPERTY_USAGE_SORT_ORDER = "sortorder";
150
151   private HashMap JavaDoc _allEditorsHashed = null;
152   private EditorDefinition[] _allEditors = null;
153
154   private HashMap JavaDoc _allResourcesHashed = null;
155   private ResourceDefinition[] _allResources = null;
156
157   private ScriptDefinition[] _allScripts = null;
158
159   public DjenericDocumentHandler()
160   {
161   }
162
163   public DjenericDocumentHandler(String JavaDoc xmlString) throws DjenericException
164   {
165     try
166     {
167       ByteArrayInputStream JavaDoc sr = new ByteArrayInputStream JavaDoc(xmlString.getBytes(DjPersistenceManager.ENCODING_METHOD));
168
169       DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance();
170       _xmlDoc = dfactory.newDocumentBuilder().parse(sr);
171     }
172     catch (Exception JavaDoc x)
173     {
174       throw new DjenericException(x);
175     }
176   }
177
178   protected DjExtent findExtent(DjPersistenceManager mgr, String JavaDoc extentName, String JavaDoc extentAlias, String JavaDoc objectType)
179   {
180     DjExtent tab = null;
181     try
182     {
183       tab = mgr.getExtent(extentName);
184     }
185     catch (Exception JavaDoc x)
186     {
187       // Ignore, try again
188
}
189     if (tab == null)
190     {
191       try
192       {
193         tab = mgr.getExtentByInternalCode(extentAlias);
194       }
195       catch (Exception JavaDoc x)
196       {
197         // Ignore, try again
198
}
199     }
200     if (tab == null)
201     {
202       try
203       {
204         tab = mgr.getExtentByObjectType(objectType);
205       }
206       catch (Exception JavaDoc x)
207       {
208         // Ignore, return null below
209
}
210     }
211     return tab;
212   }
213
214   protected DjRelation findRelation(DjExtent parent, DjExtent detail, String JavaDoc relationName, String JavaDoc detailPropertyName)
215   {
216     for (int i = 0; i < parent.getDetailRelationCount(); i++)
217     {
218       DjRelation rel = parent.getDetailRelation(i);
219       if (rel.getName().equals(relationName)) return rel;
220     }
221     // not found by name, now try by detail extent and detail column then
222
for (int i = 0; i < parent.getDetailRelationCount(); i++)
223     {
224       DjRelation rel = parent.getDetailRelation(i);
225       if ((rel.getDetailExtent() == detail) && (rel.getDetailProperty().getName().equalsIgnoreCase(detailPropertyName))) return rel;
226     }
227     // Bummer, not found..
228
return null;
229   }
230
231   protected boolean convert2bool(String JavaDoc str)
232   {
233     return convert2bool(str, false);
234   }
235
236   protected boolean convert2bool(String JavaDoc str, boolean dflt)
237   {
238     if (str == null || str.trim().length() == 0) return dflt;
239     return str.equalsIgnoreCase("true");
240   }
241
242   protected int convert2Int(String JavaDoc str)
243   {
244     int result = 0;
245     if (str.trim().length() > 0)
246     {
247       result = Integer.parseInt(str);
248     }
249     return result;
250   }
251
252   protected String JavaDoc encodeColor(Color JavaDoc c)
253   {
254     return c.getRed() + "," + c.getGreen() + "," + c.getBlue();
255   }
256
257   protected Color JavaDoc decodeColor(String JavaDoc s, Color JavaDoc c2)
258   {
259     try
260     {
261       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, ",");
262       Color JavaDoc c = new Color JavaDoc(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st
263           .nextToken()));
264       return c;
265     }
266     catch (Exception JavaDoc x)
267     {
268       DjLogger.log(x);
269     }
270     return c2;
271   }
272
273   public ExtentViewer[] getExtentViewers(DjPersistenceManager mgr) throws Exception JavaDoc
274   {
275     ArrayList JavaDoc result = new ArrayList JavaDoc();
276
277     Element JavaDoc diagram = (Element JavaDoc) _xmlDoc.getElementsByTagName(DIAGRAM_ELEMENT).item(0);
278
279     if (diagram != null)
280     {
281       NodeList JavaDoc extentviews = diagram.getElementsByTagName(EXTENTVIEW_ELEMENT);
282       for (int t = 0; t < extentviews.getLength(); t++)
283       {
284         if (extentviews.item(t) instanceof Element JavaDoc)
285         {
286           Element JavaDoc extentView = (Element JavaDoc) (extentviews.item(t));
287           DjExtent tab = findExtent(mgr, extentView.getAttribute(EXTENTVIEW_TABLE), extentView
288               .getAttribute(EXTENTVIEW_ALIAS), extentView.getAttribute(EXTENTVIEW_TYPE));
289
290           if (tab == null)
291           {
292             DjLogger.log(Messages.getString("DjenericDocumentHandler.ExtentRemovedFromDiag", extentView
293                 .getAttribute(EXTENTVIEW_TABLE)));
294             continue;
295           }
296           ExtentViewer tv = new ExtentViewer(tab);
297           tv.setX(convert2Int(extentView.getAttribute(EXTENTVIEW_X)));
298           tv.setY(convert2Int(extentView.getAttribute(EXTENTVIEW_Y)));
299           tv.setWidth(convert2Int(extentView.getAttribute(EXTENTVIEW_WIDTH)));
300           tv.setHeight(convert2Int(extentView.getAttribute(EXTENTVIEW_HEIGHT)));
301
302           tv.setExtentColor(decodeColor(extentView.getAttribute(EXTENTVIEW_TCOLOR), tv.getExtentColor()));
303           tv.setNameColor(decodeColor(extentView.getAttribute(EXTENTVIEW_NCOLOR), tv.getNameColor()));
304           tv.setPropertyColor(decodeColor(extentView.getAttribute(EXTENTVIEW_CCOLOR), tv.getPropertyColor()));
305
306           result.add(tv);
307         }
308       }
309     }
310     return (ExtentViewer[]) result.toArray(new ExtentViewer[0]);
311   }
312
313   public ExtentUsage[] getTreeStructure(DjPersistenceManager mgr) throws DjenericException
314   {
315     // First load all editors from the document so we can reference them
316
getEditors(mgr);
317     getResources();
318
319     Element JavaDoc navigator = (Element JavaDoc) _xmlDoc.getElementsByTagName(NAVIGATOR_ELEMENT).item(0);
320     ArrayList JavaDoc result = new ArrayList JavaDoc();
321
322     if (navigator != null)
323     {
324       NodeList JavaDoc extentnodes = navigator.getChildNodes();
325       for (int t = 0; t < extentnodes.getLength(); t++)
326       {
327         if (extentnodes.item(t) instanceof Element JavaDoc)
328         {
329           Element JavaDoc tn = (Element JavaDoc) (extentnodes.item(t));
330           if (tn.getNodeName().equals(NAVIGATOR_EXTENTNODE_ELEMENT))
331           {
332             DjExtent tab = findExtent(mgr, tn.getAttribute(NAVIGATOR_EXTENTNODE_NAME), tn
333                 .getAttribute(NAVIGATOR_EXTENTNODE_ALIAS), tn.getAttribute(NAVIGATOR_EXTENTNODE_TYPE));
334             if (tab == null)
335             {
336               DjLogger.log(Messages.getString("DjenericDocumentHandler.ExtentRemovedFromNav", tn
337                   .getAttribute(NAVIGATOR_EXTENTNODE_NAME)));
338
339               continue;
340             }
341
342             ExtentUsage extentUsage = new ExtentUsage(tab);
343             result.add(extentUsage);
344             extentUsage.setTitle(tn.getAttribute(NAVIGATOR_EXTENTNODE_TITLE));
345             extentUsage.setOqlExpression(tn.getAttribute(NAVIGATOR_EXTENTNODE_QBE_FILTER));
346             extentUsage.setCustomNodeClass(tn.getAttribute(NAVIGATOR_EXTENTNODE_CUSTOM_NODE_CLASS));
347
348             extentUsage.setId(tn.getAttribute(NAVIGATOR_EXTENTNODE_ID));
349             extentUsage.setDescriptorExpression(tn.getAttribute(NAVIGATOR_EXTENTNODE_DESCRIPTOR));
350             extentUsage.setEditAllowed(convert2bool(tn.getAttribute(NAVIGATOR_EXTENTNODE_EDIT)));
351             extentUsage.setInsertAllowed(convert2bool(tn.getAttribute(NAVIGATOR_EXTENTNODE_INSERT)));
352             extentUsage.setDeleteAllowed(convert2bool(tn.getAttribute(NAVIGATOR_EXTENTNODE_DELETE)));
353             extentUsage.setRecursive(convert2bool(tn.getAttribute(NAVIGATOR_EXTENTNODE_RECURSIVE)));
354
355             createDetailsFromDoc(mgr, extentUsage, tn);
356           }
357         }
358       }
359     }
360     return (ExtentUsage[]) result.toArray(new ExtentUsage[0]);
361   }
362
363   protected void createDetailsFromDoc(DjPersistenceManager mgr, ExtentUsage parent, Element JavaDoc parentElement)
364       throws DjenericException
365   {
366     String JavaDoc editorName = parentElement.getAttribute(NAVIGATOR_EXTENTNODE_EDITOR);
367     if (editorName != null && editorName.trim().length() != 0)
368     {
369       EditorDefinition editor = getEditor(editorName);
370       parent.setEditor(editor);
371       parent.setEditorTarget(parentElement.getAttribute(NAVIGATOR_EXTENTNODE_EDITORTARGET));
372     }
373
374     String JavaDoc imageIconName = parentElement.getAttribute(NAVIGATOR_EXTENTNODE_IMAGEICON);
375     if (imageIconName != null && imageIconName.trim().length() != 0)
376     {
377       ResourceDefinition res = getResource(imageIconName);
378       parent.setImageIconResource(res);
379     }
380
381     NodeList JavaDoc children = parentElement.getChildNodes();
382     for (int t = 0; t < children.getLength(); t++)
383     {
384       if (children.item(t) instanceof Element JavaDoc)
385       {
386         Element JavaDoc tn = (Element JavaDoc) (children.item(t));
387         // Backwards compatibility: check for locally defined editor
388
if (tn.getNodeName().equals(EDITOR_ELEMENT))
389         {
390           parent.setEditor(getEditor(generateEditorname(parent)));
391         }
392         else if (tn.getNodeName().equals(NAVIGATOR_EXTENTNODE_ELEMENT))
393         {
394           DjExtent currentExtent = findExtent(mgr, tn.getAttribute(NAVIGATOR_EXTENTNODE_NAME), tn
395               .getAttribute(NAVIGATOR_EXTENTNODE_ALIAS), tn.getAttribute(NAVIGATOR_EXTENTNODE_TYPE));
396           if (currentExtent == null)
397           {
398             DjLogger.log(Messages.getString("DjenericDocumentHandler.ExtentRemovedFromNav", tn
399                 .getAttribute(NAVIGATOR_EXTENTNODE_NAME)));
400             return;
401           }
402
403           String JavaDoc relationName = tn.getAttribute(NAVIGATOR_EXTENTNODE_VIA);
404           if (relationName != null && relationName.trim().length() == 0) relationName = null;
405           DjRelation rel = null;
406           if (relationName != null)
407           {
408             rel = findRelation(parent.getExtent(), currentExtent, relationName, tn
409                 .getAttribute(NAVIGATOR_EXTENTNODE_DETAIL_COLUMN));
410             if (rel == null)
411             {
412               DjLogger.log(Messages.getString("DjenericDocumentHandler.DetailRemoved", relationName, parent.getExtent()
413                   .getName(), currentExtent.getName()));
414               return;
415             }
416           }
417           ExtentUsage extentUsage = new ExtentUsage(currentExtent);
418           parent.addDetail(extentUsage, rel);
419           extentUsage.setTitle(tn.getAttribute(NAVIGATOR_EXTENTNODE_TITLE));
420           extentUsage.setOqlExpression(tn.getAttribute(NAVIGATOR_EXTENTNODE_QBE_FILTER));
421           extentUsage.setCustomNodeClass(tn.getAttribute(NAVIGATOR_EXTENTNODE_CUSTOM_NODE_CLASS));
422           extentUsage.setId(tn.getAttribute(NAVIGATOR_EXTENTNODE_ID));
423           extentUsage.setDescriptorExpression(tn.getAttribute(NAVIGATOR_EXTENTNODE_DESCRIPTOR));
424           extentUsage.setEditAllowed(convert2bool(tn.getAttribute(NAVIGATOR_EXTENTNODE_EDIT)));
425           extentUsage.setInsertAllowed(convert2bool(tn.getAttribute(NAVIGATOR_EXTENTNODE_INSERT)));
426           extentUsage.setDeleteAllowed(convert2bool(tn.getAttribute(NAVIGATOR_EXTENTNODE_DELETE)));
427           extentUsage.setRecursive(convert2bool(tn.getAttribute(NAVIGATOR_EXTENTNODE_RECURSIVE)));
428
429           createDetailsFromDoc(mgr, extentUsage, tn);
430         }
431       }
432     }
433   }
434
435   public EditorDefinition[] getEditors(DjPersistenceManager mgr) throws DjenericException
436   {
437     if (_allEditors != null) return _allEditors;
438
439     NodeList JavaDoc editorElements = _xmlDoc.getElementsByTagName(EDITOR_ELEMENT);
440     ArrayList JavaDoc result = new ArrayList JavaDoc();
441
442     if (editorElements != null)
443     {
444       for (int t = 0; t < editorElements.getLength(); t++)
445       {
446         if (editorElements.item(t) instanceof Element JavaDoc)
447         {
448           Element JavaDoc tn = (Element JavaDoc) (editorElements.item(t));
449           result.add(createEditor(mgr, tn));
450         }
451       }
452     }
453     _allEditors = (EditorDefinition[]) result.toArray(new EditorDefinition[0]);
454
455     _allEditorsHashed = new HashMap JavaDoc();
456
457     for (int i = 0; i < _allEditors.length; i++)
458     {
459       _allEditorsHashed.put(_allEditors[i].getName(), _allEditors[i]);
460     }
461     return _allEditors;
462   }
463
464   public EditorDefinition getEditor(String JavaDoc name) throws CatalogException
465   {
466     if (_allEditorsHashed == null) throw new CatalogException(
467         "Internal error: load editors before calling getEditor(name)");
468
469     return (EditorDefinition) _allEditorsHashed.get(name);
470   }
471
472   public ResourceDefinition[] getResources() throws DjenericException
473   {
474     if (_allResources != null) return _allResources;
475
476     NodeList JavaDoc resourceElements = _xmlDoc.getElementsByTagName(RESOURCE_ELEMENT);
477     ArrayList JavaDoc result = new ArrayList JavaDoc();
478
479     if (resourceElements != null)
480     {
481       for (int t = 0; t < resourceElements.getLength(); t++)
482       {
483         if (resourceElements.item(t) instanceof Element JavaDoc)
484         {
485           Element JavaDoc resel = (Element JavaDoc) (resourceElements.item(t));
486           String JavaDoc base64 = "";
487
488           NodeList JavaDoc nl = resel.getChildNodes();
489           for (int x = 0; x < nl.getLength(); x++)
490           {
491             if (nl.item(x) instanceof CDATASection JavaDoc)
492             {
493               CDATASection JavaDoc d = (CDATASection JavaDoc) nl.item(x);
494               base64 = d.getData();
495             }
496           }
497
498           ResourceDefinition def = new ResourceDefinition();
499           def.setAbsolutePath(resel.getAttribute(RESOURCE_ELEMENT_ABSOLUTE_PATH));
500           def.setTimeStamp(resel.getAttribute(RESOURCE_ELEMENT_TIMESTAMP));
501           try
502           {
503             def.setBase64(base64);
504           }
505           catch (UnsupportedEncodingException JavaDoc e)
506           {
507             throw new DjenericException(e);
508           }
509           result.add(def);
510         }
511       }
512     }
513     _allResources = (ResourceDefinition[]) result.toArray(new ResourceDefinition[0]);
514
515     _allResourcesHashed = new HashMap JavaDoc();
516
517     for (int i = 0; i < _allResources.length; i++)
518     {
519       _allResourcesHashed.put(_allResources[i].getAbsolutePath(), _allResources[i]);
520     }
521
522     return _allResources;
523   }
524
525   public ScriptDefinition[] getScripts(JFrame JavaDoc mainWindow) throws DjenericException
526   {
527     if (_allScripts != null) return _allScripts;
528
529     NodeList JavaDoc scriptElements = _xmlDoc.getElementsByTagName(SCRIPT_ELEMENT);
530     ArrayList JavaDoc result = new ArrayList JavaDoc();
531
532     if (scriptElements != null)
533     {
534       for (int t = 0; t < scriptElements.getLength(); t++)
535       {
536         if (scriptElements.item(t) instanceof Element JavaDoc)
537         {
538           Element JavaDoc e = (Element JavaDoc) (scriptElements.item(t));
539           ScriptDefinition def = createScriptDefinition(e);
540           result.add(def);
541         }
542       }
543     }
544     _allScripts = (ScriptDefinition[]) result.toArray(new ScriptDefinition[0]);
545
546     return _allScripts;
547   }
548
549   protected ScriptDefinition createScriptDefinition(Element JavaDoc scriptElement) throws DjenericException
550   {
551     String JavaDoc base64 = "";
552
553     NodeList JavaDoc nl = scriptElement.getChildNodes();
554     for (int x = 0; x < nl.getLength(); x++)
555     {
556       if (nl.item(x) instanceof CDATASection JavaDoc)
557       {
558         CDATASection JavaDoc d = (CDATASection JavaDoc) nl.item(x);
559         base64 = d.getData();
560       }
561     }
562
563     ScriptDefinition def = new ScriptDefinition();
564     def.setTitle(scriptElement.getAttribute(SCRIPT_ELEMENT_TITLE));
565     try
566     {
567       def.setBase64(base64);
568     }
569     catch (UnsupportedEncodingException JavaDoc e)
570     {
571       throw new DjenericException(e);
572     }
573     return def;
574   }
575
576   public ResourceDefinition getResource(String JavaDoc absPath) throws DjenericException
577   {
578     if (_allResourcesHashed == null) getResources();
579
580     ResourceDefinition result = (ResourceDefinition) _allResourcesHashed.get(absPath);
581     return result;
582   }
583
584   public int getViewStructureVersion()
585   {
586     Element JavaDoc view = (Element JavaDoc) _xmlDoc.getElementsByTagName(VIEW).item(0);
587     String JavaDoc version = view.getAttribute(VIEW_STRUCTURE_VERSION);
588
589     if (version == null || version.trim().length() == 0) version = "100";
590
591     return Integer.parseInt(version);
592   }
593
594   /*
595    * Returns false if the version of the loaded view is higher than the
596    * software currently running. So if this returns false saving the view might
597    * leave out view information that was added using a newer version of
598    * djeneric.
599    */

600   public boolean isVersionSafeToSave()
601   {
602     return getViewStructureVersion() <= VIEW_STRUCTURE_VERSION_NUMBER;
603   }
604
605   protected EditorDefinition createEditor(DjPersistenceManager mgr, Element JavaDoc editorElement) throws DjenericException
606   {
607     EditorDefinition result = null;
608
609     ExtentUsage structure = createEditorStructure(mgr, null, editorElement);
610     String JavaDoc name = editorElement.getAttribute(EDITOR_NAME);
611     if (structure != null)
612     {
613       if (name == null || name.trim().length() == 0) name = generateEditorname(structure);
614
615       result = new EditorDefinition(name, structure);
616     }
617     else
618     {
619       if (name == null) name = "Invalid editor";
620       result = new EditorDefinition(name, structure);
621     }
622
623     String JavaDoc customClassName = editorElement.getAttribute(EDITOR_CUSTOMCLASS);
624     result.setCustomEditorClass(customClassName);
625
626     NodeList JavaDoc scriptElements = editorElement.getElementsByTagName(EDITOR_EMBEDDED_SCRIPT);
627
628     if (scriptElements != null)
629     {
630       for (int t = 0; t < scriptElements.getLength(); t++)
631       {
632         if (scriptElements.item(t) instanceof Element JavaDoc)
633         {
634           Element JavaDoc e = (Element JavaDoc) (scriptElements.item(t));
635           result.setScriptDefinition(createScriptDefinition(e));
636         }
637       }
638     }
639     return result;
640   }
641
642   protected String JavaDoc generateEditorname(ExtentUsage structure)
643   {
644     return structure.getExtent().getName() + "Editor";
645   }
646
647   protected ExtentUsage createEditorStructure(DjPersistenceManager mgr, ExtentUsage parent, Element JavaDoc parentElement)
648       throws CatalogException
649   {
650     ExtentUsage result = null;
651
652     NodeList JavaDoc extentList = parentElement.getChildNodes();
653     for (int i = 0; i < extentList.getLength(); i++)
654     {
655       if (extentList.item(i) instanceof Element JavaDoc)
656       {
657         Element JavaDoc usageElement = (Element JavaDoc) (extentList.item(i));
658         if (usageElement.getNodeName().equals(EDITOR_EXTENT_USAGE_ELEMENT))
659         {
660
661           DjExtent tab = findExtent(mgr, usageElement.getAttribute(NAVIGATOR_EXTENTNODE_NAME), usageElement
662               .getAttribute(NAVIGATOR_EXTENTNODE_ALIAS), usageElement.getAttribute(NAVIGATOR_EXTENTNODE_TYPE));
663           if (tab == null)
664           {
665             DjLogger.log(Messages.getString("DjenericDocumentHandler.ExtentRemovedFromEditor", usageElement
666                 .getAttribute(NAVIGATOR_EXTENTNODE_NAME)));
667             continue;
668           }
669           ExtentUsage extentUsage = new ExtentUsage(tab);
670           if (result == null) result = extentUsage;
671
672           extentUsage.setTitle(usageElement.getAttribute(NAVIGATOR_EXTENTNODE_TITLE));
673           extentUsage.setOqlExpression(usageElement.getAttribute(NAVIGATOR_EXTENTNODE_QBE_FILTER));
674           extentUsage.setId(usageElement.getAttribute(NAVIGATOR_EXTENTNODE_ID));
675           extentUsage.setEditAllowed(convert2bool(usageElement.getAttribute(NAVIGATOR_EXTENTNODE_EDIT)));
676           extentUsage.setInsertAllowed(convert2bool(usageElement.getAttribute(NAVIGATOR_EXTENTNODE_INSERT)));
677           extentUsage.setDeleteAllowed(convert2bool(usageElement.getAttribute(NAVIGATOR_EXTENTNODE_DELETE)));
678           extentUsage.setMultiRow(convert2bool(usageElement.getAttribute(EDITOR_MULTIROW)));
679           extentUsage.setRowsDisplayed(convert2Int(usageElement.getAttribute(EDITOR_ROWSDISPLAYED)));
680
681           addPropertyUsages(extentUsage, usageElement);
682
683           createEditorStructure(mgr, extentUsage, usageElement);
684
685           if (parent != null)
686           {
687             String JavaDoc via = usageElement.getAttribute(NAVIGATOR_EXTENTNODE_VIA);
688             String JavaDoc detailProperty = usageElement.getAttribute(NAVIGATOR_EXTENTNODE_DETAIL_COLUMN);
689             DjRelation rel = findRelation(parent.getExtent(), tab, via, detailProperty);
690             if (rel == null)
691             {
692               DjLogger.log(Messages.getString("DjenericDocumentHandler.RelationRemoved", usageElement
693                   .getAttribute(NAVIGATOR_EXTENTNODE_VIA), parent.getExtent().getName(), tab.getName()));
694               continue;
695             }
696             parent.addDetail(extentUsage, rel);
697           }
698         }
699       }
700     }
701     return result;
702   }
703
704   protected void addPropertyUsages(ExtentUsage extentUsage, Element JavaDoc extentUsageElement) throws CatalogException
705   {
706     NodeList JavaDoc cuList = extentUsageElement.getChildNodes();
707     for (int i = 0; i < cuList.getLength(); i++)
708     {
709       if (cuList.item(i) instanceof Element JavaDoc)
710       {
711         Element JavaDoc cu = (Element JavaDoc) (cuList.item(i));
712         if (cu.getNodeName().equals(EDITOR_PROPERTY_USAGE_ELEMENT))
713         {
714           try
715           {
716             DjProperty prop = extentUsage.getExtent().getProperty(cu.getAttribute(EDITOR_PROPERTY_USAGE_COLUMN));
717             PropertyUsage columnUsage = new PropertyUsage(prop);
718             columnUsage.setSeq(convert2Int(cu.getAttribute(EDITOR_PROPERTY_USAGE_SEQ)));
719             columnUsage.setPrompt(cu.getAttribute(EDITOR_PROPERTY_USAGE_PROMPT));
720             columnUsage.setDescription(cu.getAttribute(EDITOR_PROPERTY_USAGE_DESCRIPTION));
721             columnUsage.setComponentType(cu.getAttribute(EDITOR_PROPERTY_USAGE_COMPONENT));
722             columnUsage.setRequired(convert2bool(cu.getAttribute(EDITOR_PROPERTY_USAGE_REQUIRED)));
723             columnUsage.setUpdateable(convert2bool(cu.getAttribute(EDITOR_PROPERTY_USAGE_UPDATEABLE)));
724             columnUsage.setDisplayWidth(convert2Int(cu.getAttribute(EDITOR_PROPERTY_USAGE_WIDTH)));
725             columnUsage.setDisplayHeight(convert2Int(cu.getAttribute(EDITOR_PROPERTY_USAGE_HEIGHT)));
726             columnUsage.setSortOrder(convert2Int(cu.getAttribute(EDITOR_PROPERTY_USAGE_SORT_ORDER)));
727             columnUsage.setQueryable(convert2bool(cu.getAttribute(EDITOR_PROPERTY_USAGE_QUERYABLE)));
728             columnUsage.setDisplayed(convert2bool(cu.getAttribute(EDITOR_PROPERTY_USAGE_DISPLAYED), true));
729             columnUsage.setDefaultValue(cu.getAttribute(EDITOR_PROPERTY_USAGE_DEFAULT_VALUE));
730             columnUsage.setCustomEditorClass(cu.getAttribute(EDITOR_PROPERTY_USAGE_CUSTOM_EDITOR));
731             columnUsage.setRelativePath(cu.getAttribute(EDITOR_PROPERTY_USAGE_RELATIVE_PATH));
732
733             extentUsage.addPropertyUsage(columnUsage);
734           }
735           catch (ObjectNotDefinedException x)
736           {
737             DjLogger.log(Messages.getString("DjenericDocumentHandler.PropertyRemoved", cu
738                 .getAttribute(EDITOR_PROPERTY_USAGE_COLUMN), extentUsage.getExtent().getName()));
739           }
740         }
741       }
742     }
743     extentUsage.sortPropertyUsages();
744   }
745 }
Popular Tags