KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > application > customizing > UiUtility


1 /*
2  * ====================================================================
3  * Project: openmdx, http://www.openmdx.org/
4  * Name: $Id: UiUtility.java,v 1.9 2005/02/22 01:18:52 wfro Exp $
5  * Description: UiUtility
6  * Revision: $Revision: 1.9 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2005/02/22 01:18:52 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.application.customizing;
57 import java.io.File JavaDoc;
58 import java.io.FileNotFoundException JavaDoc;
59 import java.io.FileOutputStream JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.io.OutputStreamWriter JavaDoc;
62 import java.io.UnsupportedEncodingException JavaDoc;
63 import java.io.Writer JavaDoc;
64 import java.util.ArrayList JavaDoc;
65 import java.util.HashMap JavaDoc;
66 import java.util.Iterator JavaDoc;
67 import java.util.List JavaDoc;
68 import java.util.Map JavaDoc;
69 import java.util.Set JavaDoc;
70
71 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
72 import javax.xml.parsers.ParserConfigurationException JavaDoc;
73
74 import org.opencrx.base.text.conversion.XMLWriter;
75 import org.openmdx.base.application.control.Application;
76 import org.openmdx.base.application.control.ApplicationController;
77 import org.openmdx.base.application.control.CmdLineOption;
78 import org.openmdx.base.exception.ServiceException;
79 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject;
80 import org.openmdx.compatibility.base.dataprovider.cci.DataproviderObject_1_0;
81 import org.openmdx.compatibility.base.dataprovider.cci.SystemAttributes;
82 import org.openmdx.compatibility.base.dataprovider.importer.xml.XmlImporter;
83 import org.openmdx.compatibility.base.naming.Path;
84 import org.openmdx.uses.java.beans.ExceptionListener;
85 import org.openmdx.uses.org.apache.commons.collections.map.ListOrderedMap;
86 import org.w3c.dom.Document JavaDoc;
87 import org.w3c.dom.NodeList JavaDoc;
88 import org.xml.sax.SAXException JavaDoc;
89
90 /**
91  * Command line utility which supports the management of openCRX ui configuration files:
92  * <ul>
93  * <li>merge: merges locale separated ui files (en_US, de_DE) to one merged file.</li>
94  * <li>split: splits a merged ui file to locale separated files.</li>
95  * </ul>
96  * <p>
97  *
98  * @author wfro
99  */

100 public class UiUtility
101   extends Application
102   implements ExceptionListener {
103     
104   //-------------------------------------------------------------------------
105
public UiUtility(
106   ) {
107     super(
108       APP_NAME,
109       VERSION,
110       HELP_TEXT,
111       createCmdLineOptions()
112     );
113   }
114         
115   //-------------------------------------------------------------------------
116
public void exceptionThrown(
117          Exception JavaDoc exception
118   ){
119      exception.printStackTrace();
120   }
121
122   //-------------------------------------------------------------------------
123
protected void init(
124   ) throws Exception JavaDoc {
125       // locales. get locale and assert en_US to be the first in the list
126
this.locales = new ArrayList JavaDoc();
127       if(getCmdLineArgs().hasArg("locale")) {
128         this.locales.addAll(getCmdLineArgs().getValues("locale"));
129       }
130       if((this.locales.size() == 0) || !"en_US".equals(this.locales.get(0))) {
131           this.locales.add(0, "en_US");
132       }
133       // sourceDir
134
this.sourceDir = null;
135       if(getCmdLineArgs().hasArg("sourceDir")) {
136           this.sourceDir = new File JavaDoc(getCmdLineArgs().getFirstValue("sourceDir"));
137       }
138       else {
139           this.sourceDir = new File JavaDoc(".");
140       }
141       // targetDir
142
this.targetDir = null;
143       if(getCmdLineArgs().hasArg("targetDir")) {
144           this.targetDir = new File JavaDoc(getCmdLineArgs().getFirstValue("targetDir"));
145       }
146       else {
147           this.targetDir = new File JavaDoc(".");
148       }
149       // format
150
this.format = "table";
151       if(getCmdLineArgs().hasArg("format")) {
152           this.format = getCmdLineArgs().getFirstValue("format");
153       }
154   }
155
156   //-------------------------------------------------------------------------
157
private Map JavaDoc lookupElementDefinition(
158       Document JavaDoc document,
159       String JavaDoc elementDefinitionName
160   ) {
161       Map JavaDoc elementDefinition = new HashMap JavaDoc();
162       NodeList JavaDoc elementDefinitionNodes = document.getElementsByTagName("ElementDefinition");
163       for(int i = 0; i < elementDefinitionNodes.getLength(); i++) {
164         org.w3c.dom.Node JavaDoc elementDefinitionNode = elementDefinitionNodes.item(i);
165         org.w3c.dom.NamedNodeMap JavaDoc elementDefinitionNodeAttributes = elementDefinitionNode.getAttributes();
166         org.w3c.dom.Attr JavaDoc elementDefinitionNodeName = (org.w3c.dom.Attr JavaDoc)elementDefinitionNodeAttributes.getNamedItem("name");
167         if(
168           (elementDefinitionNodeName != null) &&
169           elementDefinitionName.equals(elementDefinitionNodeName.getValue())
170         ) {
171             org.w3c.dom.NodeList JavaDoc textNodes = elementDefinitionNode.getChildNodes();
172             for(int j = 0; j < textNodes.getLength(); j++) {
173                 org.w3c.dom.Node JavaDoc textNode = textNodes.item(j);
174                 if(textNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
175                     if("Text".equals(textNode.getNodeName())) {
176                         org.w3c.dom.NamedNodeMap JavaDoc textNodeAttributes = textNode.getAttributes();
177                         org.w3c.dom.Attr JavaDoc textNodeType = (org.w3c.dom.Attr JavaDoc)textNodeAttributes.getNamedItem("type");
178                         if(
179                             (textNodeType != null) &&
180                             ("Label".equals(textNodeType.getValue()) || "ToolTip".equals(textNodeType.getValue()))
181                         ) {
182                             org.w3c.dom.NodeList JavaDoc attributeNodes = textNode.getChildNodes();
183                             for(int k = 0; k < attributeNodes.getLength(); k++) {
184                                 org.w3c.dom.Node JavaDoc attributeNode = attributeNodes.item(k);
185                                 if(attributeNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
186                                     if(attributeNode.hasChildNodes()) {
187                                         elementDefinition.put(
188                                             attributeNode.getNodeName() + "_" + textNodeType.getValue(),
189                                             attributeNode.getFirstChild().getNodeValue()
190                                         );
191                                     }
192                                 }
193                             }
194                         }
195                     }
196                     else {
197                         System.out.println("WARNING: skipping node " + textNode.getNodeName());
198                     }
199                 }
200             }
201         }
202       }
203       return elementDefinition;
204   }
205   
206   //-------------------------------------------------------------------------
207
private Map JavaDoc lookupElementDefinitionByType(
208       String JavaDoc type,
209       Document JavaDoc document,
210       String JavaDoc elementDefinitionName,
211       String JavaDoc alternateId
212   ) {
213       Map JavaDoc alternateElementDefinition = new HashMap JavaDoc();
214       NodeList JavaDoc alternateElementDefinitionNodes = document.getElementsByTagName(type);
215       for(int i = 0; i < alternateElementDefinitionNodes.getLength(); i++) {
216         org.w3c.dom.Node JavaDoc alternateElementDefinitionNode = alternateElementDefinitionNodes.item(i);
217         org.w3c.dom.NamedNodeMap JavaDoc elementDefinitionNodeAttributes = alternateElementDefinitionNode.getAttributes();
218         org.w3c.dom.Attr JavaDoc alternateElementDefinitionNodeName = (org.w3c.dom.Attr JavaDoc)elementDefinitionNodeAttributes.getNamedItem("name");
219         org.w3c.dom.Attr JavaDoc alternateElementDefinitionNodeId = (org.w3c.dom.Attr JavaDoc)elementDefinitionNodeAttributes.getNamedItem("id");
220         if(
221           (alternateElementDefinitionNodeName != null) &&
222           elementDefinitionName.equals(alternateElementDefinitionNodeName.getValue()) &&
223           alternateId.equals(alternateElementDefinitionNodeId.getValue())
224         ) {
225             org.w3c.dom.NodeList JavaDoc textNodes = alternateElementDefinitionNode.getChildNodes();
226             for(int j = 0; j < textNodes.getLength(); j++) {
227                 org.w3c.dom.Node JavaDoc textNode = textNodes.item(j);
228                 if(textNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
229                     if("Text".equals(textNode.getNodeName())) {
230                         org.w3c.dom.NamedNodeMap JavaDoc textNodeAttributes = textNode.getAttributes();
231                         org.w3c.dom.Attr JavaDoc textNodeType = (org.w3c.dom.Attr JavaDoc)textNodeAttributes.getNamedItem("type");
232                         if(
233                             (textNodeType != null) &&
234                             ("Label".equals(textNodeType.getValue()) || "ToolTip".equals(textNodeType.getValue()))
235                         ) {
236                             org.w3c.dom.NodeList JavaDoc attributeNodes = textNode.getChildNodes();
237                             for(int k = 0; k < attributeNodes.getLength(); k++) {
238                                 org.w3c.dom.Node JavaDoc attributeNode = attributeNodes.item(k);
239                                 if(attributeNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
240                                     if(attributeNode.hasChildNodes()) {
241                                         alternateElementDefinition.put(
242                                             attributeNode.getNodeName() + "_" + textNodeType.getValue(),
243                                             attributeNode.getFirstChild().getNodeValue()
244                                         );
245                                     }
246                                 }
247                             }
248                         }
249                     }
250                     else {
251                         System.out.println("WARNING: skipping node " + textNode.getNodeName());
252                     }
253                 }
254             }
255         }
256       }
257       return alternateElementDefinition;
258   }
259   
260   //-------------------------------------------------------------------------
261
private void writeAsSchema(
262       Writer JavaDoc w,
263       Writer JavaDoc fw,
264       Map JavaDoc elementDefinitions,
265       int localeIndex
266   ) throws ServiceException, IOException JavaDoc {
267       String JavaDoc providerName ="CRX";
268       String JavaDoc segmentName = "Standard";
269       if(elementDefinitions.size() > 0) {
270           DataproviderObject_1_0 obj = (DataproviderObject_1_0)elementDefinitions.values().iterator().next();
271           providerName = obj.path().get(2);
272           segmentName = obj.path().get(4);
273       }
274       String JavaDoc s = null;
275       s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
276           "<org.openmdx.base.Authority xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" name=\"org:openmdx:ui1\" xsi:noNamespaceSchemaLocation=\"xri:+resource/org/openmdx/ui1/xmi/ui1.xsd\">\n" +
277           " <_object/>\n" +
278           " <_content>\n" +
279           " <provider>\n" +
280           " <org.openmdx.base.Provider qualifiedName=\"" + providerName + "\" _operation=\"null\">\n" +
281           " <_object/>\n" +
282           " <_content>\n" +
283           " <segment>\n" +
284           " <org.openmdx.ui1.Segment qualifiedName=\"" + segmentName + "\" _operation=\"null\">\n" +
285           " <_object/>\n" +
286           " <_content>\n" +
287           " <elementDefinition>\n";
288       w.write(s, 0, s.length());
289       for(
290           Iterator JavaDoc i = elementDefinitions.values().iterator();
291           i.hasNext();
292       ) {
293           DataproviderObject_1_0 element = (DataproviderObject_1_0)i.next();
294           String JavaDoc elementDefinitionType = (String JavaDoc)element.values(SystemAttributes.OBJECT_CLASS).get(0);
295           if(
296               "org:openmdx:ui1:ElementDefinition".equals(elementDefinitionType) ||
297               "org:openmdx:ui1:AlternateElementDefinition".equals(elementDefinitionType) ||
298               "org:openmdx:ui1:AdditionalElementDefinition".equals(elementDefinitionType)
299           ) {
300               boolean allLocales = localeIndex < 0;
301               // write only if either label or toolTip for specified locale exists
302
if(
303                   allLocales ||
304                   ((element.values("label").get(localeIndex) != null) && !"".equals(element.values("label").get(localeIndex))) ||
305                   ((element.values("toolTip").get(localeIndex) != null) && !"".equals(element.values("toolTip").get(localeIndex)))
306               ) {
307                   boolean isNested = false;
308                   if("org:openmdx:ui1:AlternateElementDefinition".equals(elementDefinitionType)) {
309                       s = " <org.openmdx.ui1.ElementDefinition name=\"" + element.path().getParent().getParent().getBase() + "\" _operation=\"null\">\n" +
310                           " <_object/>\n" +
311                           " <_content>\n" +
312                           " <alternateElementDefinition>\n" +
313                           " <org.openmdx.ui1.AlternateElementDefinition id=\"" + element.path().getBase() + "\" _operation=\"create\">\n" +
314                           " <_object>\n";
315                       isNested = true;
316                   }
317                   else if("org:openmdx:ui1:AdditionalElementDefinition".equals(elementDefinitionType)) {
318                       s = " <org.openmdx.ui1.ElementDefinition name=\"" + element.path().getParent().getParent().getBase() + "\" _operation=\"null\">\n" +
319                           " <_object/>\n" +
320                           " <_content>\n" +
321                           " <additionalElementDefinition>\n" +
322                           " <org.openmdx.ui1.AdditionalElementDefinition id=\"" + element.path().getBase() + "\" _operation=\"create\">\n" +
323                           " <_object>\n";
324                       isNested = true;
325                   }
326                   else {
327                       s = " <org.openmdx.ui1.ElementDefinition name=\"" + element.path().getBase() + "\" _operation=\"create\">\n" +
328                           " <_object>\n";
329                       isNested = false;
330                   }
331                   w.write(s, 0, s.length());
332     
333                   // <label>
334
boolean tagWritten = false;
335                   int startIndex = allLocales ? 0 : localeIndex;
336                   int endIndex = allLocales ? this.locales.size()-1 : localeIndex;
337                   for(
338                       int j = startIndex;
339                       j < endIndex + 1;
340                       j++
341                   ) {
342                       String JavaDoc label = element.values("label").size() > j
343                           ? (String JavaDoc)element.values("label").get(j)
344                           : "";
345                       if(!"".equals(label)) {
346                           String JavaDoc indent = isNested ? " " : "";
347         
348                           // <label>
349
if(!tagWritten) {
350                               s = indent + " <label>\n";
351                               w.write(s, 0, s.length());
352                               tagWritten = true;
353                           }
354                           
355                           // <item>
356
s = indent + " <_item>";
357                           w.write(s, 0, s.length());
358                           s = label;
359                           fw.write(s, 0, s.length());
360                           s = "</_item>\n";
361                           w.write(s, 0, s.length());
362                       }
363                   }
364                   // </label>
365
if(tagWritten) {
366                       String JavaDoc indent = isNested ? " " : "";
367                       s = indent + " </label>\n";
368                       w.write(s, 0, s.length());
369                   }
370                                     
371                   // <toolTip>
372
tagWritten = false;
373                   for(
374                       int j = startIndex;
375                       j < endIndex + 1;
376                       j++
377                   ) {
378                       String JavaDoc toolTip = element.values("toolTip").size() > j
379                           ? (String JavaDoc)element.values("toolTip").get(j)
380                           : "";
381                       if(!"".equals(toolTip)) {
382                           String JavaDoc indent = isNested ? " " : "";
383         
384                           // <toolTip>
385
if(!tagWritten) {
386                               s = indent + " <toolTip>\n";
387                               w.write(s, 0, s.length());
388                               tagWritten = true;
389                           }
390                           
391                           // item
392
s = indent + " <_item>";
393                           w.write(s, 0, s.length());
394                           s = toolTip;
395                           fw.write(s, 0, s.length());
396                           s = "</_item>\n";
397                           w.write(s, 0, s.length());
398                       }
399                   }
400                   // </toolTip>
401
if(tagWritten) {
402                       String JavaDoc indent = isNested ? " " : "";
403                       s = indent + " </toolTip>\n";
404                       w.write(s, 0, s.length());
405                   }
406                   
407                   if("org:openmdx:ui1:AlternateElementDefinition".equals(elementDefinitionType)) {
408                       s = " </_object>\n" +
409                           " <_content/>\n" +
410                           " </org.openmdx.ui1.AlternateElementDefinition>\n" +
411                           " </alternateElementDefinition>\n" +
412                           " </_content>\n" +
413                           " </org.openmdx.ui1.ElementDefinition>\n";
414                   }
415                   else if("org:openmdx:ui1:AdditionalElementDefinition".equals(elementDefinitionType)) {
416                       s = " </_object>\n" +
417                           " <_content/>\n" +
418                           " </org.openmdx.ui1.AdditionalElementDefinition>\n" +
419                           " </additionalElementDefinition>\n" +
420                           " </_content>\n" +
421                           " </org.openmdx.ui1.ElementDefinition>\n";
422                   }
423                   else {
424                       s = " </_object>\n" +
425                           " <_content/>\n" +
426                           " </org.openmdx.ui1.ElementDefinition>\n";
427                   }
428                   w.write(s, 0, s.length());
429               }
430           }
431       }
432       s = " </elementDefinition>\n" +
433           " </_content>\n" +
434           " </org.openmdx.ui1.Segment>\n" +
435           " </segment>\n" +
436           " </_content>\n" +
437           " </org.openmdx.base.Provider>\n" +
438           " </provider>\n" +
439           " </_content>\n" +
440           "</org.openmdx.base.Authority>\n";
441       w.write(s, 0, s.length());
442   }
443
444   //-------------------------------------------------------------------------
445
/**
446    * @param localIndex if < 0 ==> read all locales, else read elements of
447    * specified locale
448    */

449   private void readAsSchema(
450       File JavaDoc file,
451       Map JavaDoc mergedElementDefinitions,
452       int localeIndex
453   ) throws ServiceException {
454       Map JavaDoc elementDefinitions = ListOrderedMap.decorate(new HashMap JavaDoc());
455       if(file.exists()) {
456           XmlImporter importer = new XmlImporter(
457             elementDefinitions,
458             false
459           );
460           System.out.println("loading " + file);
461           try {
462             importer.process(
463               new String JavaDoc[]{file.getAbsolutePath()}
464             );
465           }
466           catch(ServiceException e) {
467             e.log();
468             System.out.println("STATUS: " + e.getMessage());
469           }
470           catch(Exception JavaDoc e) {
471               new ServiceException(e).log();
472               System.out.println("STATUS: " + e.getMessage());
473           }
474       }
475       
476       // merge entries
477
Set JavaDoc keySet = localeIndex <= 0 ? elementDefinitions.keySet() : mergedElementDefinitions.keySet();
478       try {
479           for(Iterator JavaDoc j = keySet.iterator(); j.hasNext(); ) {
480             Path key = (Path)j.next();
481             // merge entry
482
if(mergedElementDefinitions.get(key) != null) {
483               DataproviderObject mergedElementDefinition = (DataproviderObject)mergedElementDefinitions.get(key);
484               String JavaDoc mergedElementDefinitionType = (String JavaDoc)mergedElementDefinition.values(SystemAttributes.OBJECT_CLASS).get(0);
485               if(
486                 "org:openmdx:ui1:ElementDefinition".equals(mergedElementDefinitionType) ||
487                 "org:openmdx:ui1:AlternateElementDefinition".equals(mergedElementDefinitionType) ||
488                 "org:openmdx:ui1:AdditionalElementDefinition".equals(mergedElementDefinitionType)
489               ) {
490                   DataproviderObject elementDefinition = (DataproviderObject)elementDefinitions.get(key);
491                   if(mergedElementDefinition.getValues("label") != null) {
492                     mergedElementDefinition.values("label").add(
493                       (elementDefinition != null) && (elementDefinition.values("label").size() > 0)
494                         ? elementDefinition.values("label").get(0)
495                         : "" // empty string as default
496
);
497                   }
498                   if(mergedElementDefinition.getValues("toolTip") != null) {
499                     mergedElementDefinition.values("toolTip").add(
500                       (elementDefinition != null) && (elementDefinition.values("toolTip").size() > 0)
501                         ? elementDefinition.values("toolTip").get(0)
502                         : "" // empty string as default
503
);
504                   }
505               }
506             }
507             // add if it does not exist. Only add for locale=0 (en_US)
508
else if(localeIndex <= 0) {
509                 mergedElementDefinitions.put(
510                     key,
511                     elementDefinitions.get(key)
512                 );
513             }
514             // locale > 0 requires that locale=0 exists. Complain if it
515
// does not
516
else {
517               System.err.println("entry " + key + " of locale " + locales.get(localeIndex) + " has no corresponding entry for locale " + locales.get(0) + ". Not loading");
518             }
519           }
520       }
521       catch(Exception JavaDoc e) {
522           System.err.println("Can not import. Reason is " + e.getMessage());
523       }
524   }
525   
526   //-------------------------------------------------------------------------
527
private void readAsTable(
528       File JavaDoc file,
529       File JavaDoc templateFile,
530       Map JavaDoc elementDefinitions
531   ) throws ServiceException {
532       XmlImporter importer = new XmlImporter(
533           elementDefinitions,
534           false
535       );
536       System.out.println("loading " + templateFile);
537       try {
538           importer.process(
539               new String JavaDoc[]{templateFile.getAbsolutePath()}
540           );
541       }
542       catch(ServiceException e) {
543           e.log();
544           System.out.println("STATUS: " + e.getMessage());
545       }
546       catch(Exception JavaDoc e) {
547           new ServiceException(e).log();
548           System.out.println("STATUS: " + e.getMessage());
549       }
550       try {
551           System.out.println("loading " + file.getAbsolutePath());
552           org.w3c.dom.Document JavaDoc mergedElementDefinitions = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
553           for(Iterator JavaDoc i = elementDefinitions.values().iterator(); i.hasNext(); ) {
554               DataproviderObject elementDefinition = (DataproviderObject)i.next();
555               String JavaDoc elementDefinitionType = (String JavaDoc)elementDefinition.values(SystemAttributes.OBJECT_CLASS).get(0);
556               Map JavaDoc mergedElementDefinition = null;
557               if("org:openmdx:ui1:ElementDefinition".equals(elementDefinitionType)) {
558                   mergedElementDefinition =
559                       this.lookupElementDefinition(
560                           mergedElementDefinitions,
561                           elementDefinition.path().getBase()
562                       );
563               }
564               else if("org:openmdx:ui1:AlternateElementDefinition".equals(elementDefinitionType)) {
565                   mergedElementDefinition =
566                       this.lookupElementDefinitionByType(
567                           "AlternateElementDefinition",
568                           mergedElementDefinitions,
569                           elementDefinition.path().getParent().getParent().getBase(),
570                           elementDefinition.path().getBase()
571                       );
572               }
573               else if("org:openmdx:ui1:AdditionalElementDefinition".equals(elementDefinitionType)) {
574                   mergedElementDefinition =
575                       this.lookupElementDefinitionByType(
576                           "AdditionalElementDefinition",
577                           mergedElementDefinitions,
578                           elementDefinition.path().getParent().getParent().getBase(),
579                           elementDefinition.path().getBase()
580                       );
581               }
582               for(int j = 1; j < locales.size(); j++) { // skip en_US
583
String JavaDoc label = null;
584                   String JavaDoc toolTip = null;
585                   if(mergedElementDefinition != null) {
586                       label = (String JavaDoc)mergedElementDefinition.get(locales.get(j) + "_Label");
587                       toolTip = (String JavaDoc)mergedElementDefinition.get(locales.get(j) + "_ToolTip");
588                   }
589                   elementDefinition.values("label").add(label == null ? "" : label);
590                   elementDefinition.values("toolTip").add(toolTip == null ? "" : toolTip);
591               }
592           }
593       }
594       catch(ParserConfigurationException JavaDoc e) {
595           System.err.println("ParserConfigurationException: can not load file " + file.getAbsolutePath());
596       }
597       catch(SAXException JavaDoc e) {
598           System.err.println("SAXException: can not load file " + file.getAbsolutePath());
599       }
600       catch(IOException JavaDoc e) {
601           System.err.println("IOException: can not load file " + file.getAbsolutePath());
602       }
603   }
604   
605   //-------------------------------------------------------------------------
606
private void writeAsTable(
607       Writer JavaDoc w,
608       Writer JavaDoc fw,
609       Map JavaDoc elementDefinitions
610   ) throws ServiceException, IOException JavaDoc {
611       String JavaDoc s = null;
612       s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
613       w.write(s, 0, s.length());
614       s = "<ElementDefinitions>\n";
615       w.write(s, 0, s.length());
616       for(
617           Iterator JavaDoc j = elementDefinitions.values().iterator();
618           j.hasNext();
619       ) {
620           DataproviderObject_1_0 entry = (DataproviderObject_1_0)j.next();
621           String JavaDoc elementDefinitionType = (String JavaDoc)entry.values(SystemAttributes.OBJECT_CLASS).get(0);
622           if("org:openmdx:ui1:ElementDefinition".equals(elementDefinitionType)) {
623               s = " <ElementDefinition name=\"" + entry.path().getBase() + "\">\n";
624           }
625           else if("org:openmdx:ui1:AlternateElementDefinition".equals(elementDefinitionType)) {
626               s = " <AlternateElementDefinition name=\"" + entry.path().getParent().getParent().getBase() + "\" id=\"" + entry.path().getBase() + "\">\n";
627           }
628           else {
629               s = " <AdditionalElementDefinition name=\"" + entry.path().getParent().getParent().getBase() + "\" id=\"" + entry.path().getBase() + "\">\n";
630           }
631           w.write(s, 0, s.length());
632           // label
633
if(entry.getValues("label") != null) {
634               s = " <Text type=\"Label\">\n";
635               w.write(s, 0, s.length());
636               for(int k = 0; k < locales.size(); k++) {
637                   s = " <" + locales.get(k) + ">";
638                   w.write(s, 0, s.length());
639                   s = (String JavaDoc)entry.values("label").get(k);
640                   if(s == null) s = "";
641                   fw.write(s, 0, s.length());
642                   s = "</" + locales.get(k) + ">\n";
643                   w.write(s, 0, s.length());
644               }
645               s = " </Text>\n";
646               w.write(s, 0, s.length());
647           }
648           // toolTip
649
if(entry.getValues("toolTip") != null) {
650               s = " <Text type=\"ToolTip\">\n";
651               w.write(s, 0, s.length());
652               for(int k = 0; k < locales.size(); k++) {
653                   s = " <" + locales.get(k) + ">";
654                   w.write(s, 0, s.length());
655                   s = (String JavaDoc)entry.values("toolTip").get(k);
656                   if(s == null) s = "";
657                   fw.write(s, 0, s.length());
658                   s = "</" + locales.get(k) + ">\n";
659                   w.write(s, 0, s.length());
660               }
661               s = " </Text>\n";
662               w.write(s, 0, s.length());
663           }
664           if("org:openmdx:ui1:ElementDefinition".equals(elementDefinitionType)) {
665               s = " </ElementDefinition>\n";
666           }
667           else if("org:openmdx:ui1:AlternateElementDefinition".equals(elementDefinitionType)) {
668               s = " </AlternateElementDefinition>\n";
669           }
670           else {
671               s = " </AdditionalElementDefinition>\n";
672           }
673           w.write(s, 0, s.length());
674       }
675       s = "</ElementDefinitions>\n";
676       w.write(s, 0, s.length());
677   }
678   
679   //-------------------------------------------------------------------------
680
protected void split(
681       List JavaDoc locales,
682       File JavaDoc sourceDir,
683       File JavaDoc targetDir,
684       String JavaDoc format
685   ) throws ServiceException {
686       String JavaDoc en_US_Dir = targetDir.getAbsolutePath() + File.separatorChar + "en_US";
687       System.out.println("sourceDir=" + sourceDir.getAbsolutePath());
688       File JavaDoc[] en_US_files = new File JavaDoc(en_US_Dir).listFiles();
689       if(en_US_files == null) {
690           System.out.println("ERROR: directory not found: " + en_US_Dir);
691           return;
692       }
693       
694       // en_US files are leading. process all files
695
for(int u = 0; u < en_US_files.length; u++) {
696           Map JavaDoc elementDefinitions = ListOrderedMap.decorate(new HashMap JavaDoc());
697           File JavaDoc file = new File JavaDoc(sourceDir.getAbsolutePath() + File.separatorChar + en_US_files[u].getName());
698           if("table".equals(this.format)) {
699               this.readAsTable(
700                   file,
701                   en_US_files[u],
702                   elementDefinitions
703               );
704           }
705           else {
706               this.readAsSchema(
707                   file,
708                   elementDefinitions,
709                   -1
710               );
711           }
712          
713           // split and store as XML
714
for(int j = 1; j < locales.size(); j++) { // never write en_US
715
String JavaDoc outFileName = targetDir.getAbsolutePath() + File.separatorChar + locales.get(j) + File.separatorChar + en_US_files[u].getName();
716               try {
717                   File JavaDoc outFile = new File JavaDoc(outFileName);
718                   if(outFile.exists()) {
719                       File JavaDoc renamed = new File JavaDoc(outFile.getParent() + File.separatorChar + ".#" + outFile.getName());
720                       if(!outFile.renameTo(renamed)) {
721                           System.out.println("WARNING: can not move file " + outFile.getAbsolutePath() + " to " + renamed.getAbsolutePath() + ". Skipping");
722                           continue;
723                       }
724                   }
725                   else if(!outFile.getParentFile().exists()) {
726                       if(!outFile.getParentFile().mkdir()) {
727                           System.out.println("WARNING: can not create directory " + outFile.getParentFile().getAbsolutePath() + ". Skipping");
728                           continue;
729                       }
730                   }
731                   System.out.println("writing file " + outFileName);
732                   Writer JavaDoc w = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(outFileName), "UTF-8");
733                   Writer JavaDoc fw = new XMLWriter(w);
734                   this.writeAsSchema(
735                       w,
736                       fw,
737                       elementDefinitions,
738                       j
739                   );
740                   w.close();
741               }
742               catch(FileNotFoundException JavaDoc e) {
743                   System.err.println("can not create file " + outFileName);
744               }
745               catch(UnsupportedEncodingException JavaDoc e) {
746                   System.err.println("can not create file with encoding UTF-8 " + outFileName);
747               }
748               catch(IOException JavaDoc e) {
749                   System.err.println("error writing to file " + outFileName);
750               }
751           }
752       }
753   }
754
755   //-------------------------------------------------------------------------
756
protected void merge(
757       List JavaDoc locales,
758       File JavaDoc sourceDir,
759       File JavaDoc targetDir,
760       String JavaDoc format
761   ) throws ServiceException {
762
763       String JavaDoc en_US_Dir = sourceDir.getAbsolutePath() + File.separatorChar + "en_US";
764       File JavaDoc[] en_US_files = new File JavaDoc(en_US_Dir).listFiles();
765       System.out.println("sourceDir=" + sourceDir.getAbsolutePath());
766       if(en_US_files == null) {
767           System.out.println("ERROR: directory not found: " + en_US_Dir);
768           return;
769       }
770       
771       // process all ui files
772
for(int u = 0; u < en_US_files.length; u++) {
773
774           // get all locale specific files for en_US_files[k]
775
Map JavaDoc mergedElementDefinitions = ListOrderedMap.decorate(new HashMap JavaDoc());
776           for(int i = 0; i < locales.size(); i++) {
777               File JavaDoc file = new File JavaDoc(sourceDir.getAbsolutePath() + File.separatorChar + locales.get(i) + File.separatorChar + en_US_files[u].getName());
778               this.readAsSchema(
779                   file,
780                   mergedElementDefinitions,
781                   i
782               );
783           }
784     
785           // dump mergedElementDefinitions as UTF-8 encoded XML
786
String JavaDoc outFileName = targetDir.getAbsolutePath() + File.separatorChar + en_US_files[u].getName();
787           try {
788               // rename existing file
789
File JavaDoc outFile = new File JavaDoc(outFileName);
790               if(outFile.exists()) {
791                   File JavaDoc renamed = new File JavaDoc(outFile.getParent() + File.separatorChar + ".#" + outFile.getName());
792                   if(!outFile.renameTo(renamed)) {
793                       System.out.println("WARNING: can not move file " + outFile.getAbsolutePath() + " to " + renamed.getAbsolutePath() + ". Skipping");
794                       continue;
795                   }
796               }
797               else if(!outFile.getParentFile().exists()) {
798                   if(!outFile.getParentFile().mkdir()) {
799                       System.out.println("WARNING: can not create directory " + outFile.getParentFile().getAbsolutePath() + ". Skipping");
800                       continue;
801                   }
802               }
803               System.out.println("writing file " + outFileName);
804               Writer JavaDoc w = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(outFileName), "UTF-8");
805               Writer JavaDoc fw = new XMLWriter(w);
806               if("table".equals(format)) {
807                   this.writeAsTable(
808                       w,
809                       fw,
810                       mergedElementDefinitions
811                   );
812               }
813               else {
814                   this.writeAsSchema(
815                       w,
816                       fw,
817                       mergedElementDefinitions,
818                       -1
819                   );
820               }
821               w.close();
822           }
823           catch(FileNotFoundException JavaDoc e) {
824               System.err.println("can not create file " + outFileName);
825           }
826           catch(UnsupportedEncodingException JavaDoc e) {
827               System.err.println("can not create file with encoding UTF-8 " + outFileName);
828           }
829           catch(IOException JavaDoc e) {
830               System.err.println("error writing to file " + outFileName);
831           }
832       }
833   }
834   
835   //-------------------------------------------------------------------------
836
protected void run(
837   ) throws Exception JavaDoc {
838       String JavaDoc command = "merge";
839       if (getCmdLineArgs().hasArg("merge")) {
840           command = "merge";
841       }
842       if (getCmdLineArgs().hasArg("split")) {
843           command = "split";
844       }
845       if("merge".equals(command)){
846           this.merge(this.locales, this.sourceDir, this.targetDir, this.format);
847       }
848       else if("split".equals(command)){
849           this.split(this.locales, this.sourceDir, this.targetDir, this.format);
850       }
851   }
852
853   //-------------------------------------------------------------------------
854
protected void release(
855   ) throws Exception JavaDoc {
856     System.out.println("shutdown");
857   }
858
859   //-------------------------------------------------------------------------
860
public static void main(
861     String JavaDoc[] args
862   ) {
863     ApplicationController controller = new ApplicationController(args);
864     controller.initLogging(LOG_CONFIG_NAME, LOG_SOURCE);
865     controller.registerApplication(new UiUtility());
866     controller.run();
867   }
868   
869   //-------------------------------------------------------------------------
870
private static List JavaDoc createCmdLineOptions(
871   ) {
872     ArrayList JavaDoc options = new ArrayList JavaDoc();
873     
874     // merge command
875
options.add(
876       new CmdLineOption(
877         "merge",
878         "Merge locale separated files to one merged file"
879       )
880     );
881
882     // split command
883
options.add(
884       new CmdLineOption(
885         "split",
886         "Split merged file to locale separated files"
887       )
888     );
889     
890     
891     // dir (split directory)
892
options.add(
893       new CmdLineOption(
894         "sourceDir",
895         "directory containing the source files",
896         1,
897         1
898       )
899     );
900     options.add(
901       new CmdLineOption(
902         "targetDir",
903         "directory containing the target files",
904         1,
905         1
906       )
907     );
908     // locale
909
options.add(
910       new CmdLineOption(
911         "locale",
912         "list of locales to process",
913         0,
914         Integer.MAX_VALUE
915       )
916     );
917     // format
918
options.add(
919       new CmdLineOption(
920         "format",
921         "ui source format for split; ui target format for merge [table|schema]",
922         0,
923         Integer.MAX_VALUE
924       )
925     );
926     return options;
927   }
928   
929   //-------------------------------------------------------------------------
930
// Variables
931
//-------------------------------------------------------------------------
932

933   // The version gets set by CVS
934
private static final String JavaDoc VERSION = "$Revision: 1.9 $";
935   
936   // The application name
937
private static final String JavaDoc APP_NAME = "UiMapper";
938   
939   // The logging configuration name
940
private static final String JavaDoc LOG_CONFIG_NAME = "UiMapper";
941   
942   // The logging log source name
943
private static final String JavaDoc LOG_SOURCE = APP_NAME;
944   
945   // Application help
946
private static final String JavaDoc HELP_TEXT = "UiMapper splits and merges ui definitions";
947   
948   // command line options
949
private List JavaDoc locales = null;
950   private File JavaDoc sourceDir = null;
951   private File JavaDoc targetDir = null;
952   private String JavaDoc format = null;
953   
954 }
955
956 //--- End of File -----------------------------------------------------------
957
Popular Tags