1 56 package org.opencrx.application.customizing; 57 import java.io.File ; 58 import java.io.FileNotFoundException ; 59 import java.io.FileOutputStream ; 60 import java.io.IOException ; 61 import java.io.OutputStreamWriter ; 62 import java.io.UnsupportedEncodingException ; 63 import java.io.Writer ; 64 import java.util.ArrayList ; 65 import java.util.HashMap ; 66 import java.util.Iterator ; 67 import java.util.List ; 68 import java.util.Map ; 69 import java.util.Set ; 70 71 import javax.xml.parsers.DocumentBuilderFactory ; 72 import javax.xml.parsers.ParserConfigurationException ; 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 ; 87 import org.w3c.dom.NodeList ; 88 import org.xml.sax.SAXException ; 89 90 100 public class UiUtility 101 extends Application 102 implements ExceptionListener { 103 104 public UiUtility( 106 ) { 107 super( 108 APP_NAME, 109 VERSION, 110 HELP_TEXT, 111 createCmdLineOptions() 112 ); 113 } 114 115 public void exceptionThrown( 117 Exception exception 118 ){ 119 exception.printStackTrace(); 120 } 121 122 protected void init( 124 ) throws Exception { 125 this.locales = new ArrayList (); 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 this.sourceDir = null; 135 if(getCmdLineArgs().hasArg("sourceDir")) { 136 this.sourceDir = new File (getCmdLineArgs().getFirstValue("sourceDir")); 137 } 138 else { 139 this.sourceDir = new File ("."); 140 } 141 this.targetDir = null; 143 if(getCmdLineArgs().hasArg("targetDir")) { 144 this.targetDir = new File (getCmdLineArgs().getFirstValue("targetDir")); 145 } 146 else { 147 this.targetDir = new File ("."); 148 } 149 this.format = "table"; 151 if(getCmdLineArgs().hasArg("format")) { 152 this.format = getCmdLineArgs().getFirstValue("format"); 153 } 154 } 155 156 private Map lookupElementDefinition( 158 Document document, 159 String elementDefinitionName 160 ) { 161 Map elementDefinition = new HashMap (); 162 NodeList elementDefinitionNodes = document.getElementsByTagName("ElementDefinition"); 163 for(int i = 0; i < elementDefinitionNodes.getLength(); i++) { 164 org.w3c.dom.Node elementDefinitionNode = elementDefinitionNodes.item(i); 165 org.w3c.dom.NamedNodeMap elementDefinitionNodeAttributes = elementDefinitionNode.getAttributes(); 166 org.w3c.dom.Attr elementDefinitionNodeName = (org.w3c.dom.Attr )elementDefinitionNodeAttributes.getNamedItem("name"); 167 if( 168 (elementDefinitionNodeName != null) && 169 elementDefinitionName.equals(elementDefinitionNodeName.getValue()) 170 ) { 171 org.w3c.dom.NodeList textNodes = elementDefinitionNode.getChildNodes(); 172 for(int j = 0; j < textNodes.getLength(); j++) { 173 org.w3c.dom.Node 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 textNodeAttributes = textNode.getAttributes(); 177 org.w3c.dom.Attr textNodeType = (org.w3c.dom.Attr )textNodeAttributes.getNamedItem("type"); 178 if( 179 (textNodeType != null) && 180 ("Label".equals(textNodeType.getValue()) || "ToolTip".equals(textNodeType.getValue())) 181 ) { 182 org.w3c.dom.NodeList attributeNodes = textNode.getChildNodes(); 183 for(int k = 0; k < attributeNodes.getLength(); k++) { 184 org.w3c.dom.Node 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 private Map lookupElementDefinitionByType( 208 String type, 209 Document document, 210 String elementDefinitionName, 211 String alternateId 212 ) { 213 Map alternateElementDefinition = new HashMap (); 214 NodeList alternateElementDefinitionNodes = document.getElementsByTagName(type); 215 for(int i = 0; i < alternateElementDefinitionNodes.getLength(); i++) { 216 org.w3c.dom.Node alternateElementDefinitionNode = alternateElementDefinitionNodes.item(i); 217 org.w3c.dom.NamedNodeMap elementDefinitionNodeAttributes = alternateElementDefinitionNode.getAttributes(); 218 org.w3c.dom.Attr alternateElementDefinitionNodeName = (org.w3c.dom.Attr )elementDefinitionNodeAttributes.getNamedItem("name"); 219 org.w3c.dom.Attr alternateElementDefinitionNodeId = (org.w3c.dom.Attr )elementDefinitionNodeAttributes.getNamedItem("id"); 220 if( 221 (alternateElementDefinitionNodeName != null) && 222 elementDefinitionName.equals(alternateElementDefinitionNodeName.getValue()) && 223 alternateId.equals(alternateElementDefinitionNodeId.getValue()) 224 ) { 225 org.w3c.dom.NodeList textNodes = alternateElementDefinitionNode.getChildNodes(); 226 for(int j = 0; j < textNodes.getLength(); j++) { 227 org.w3c.dom.Node 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 textNodeAttributes = textNode.getAttributes(); 231 org.w3c.dom.Attr textNodeType = (org.w3c.dom.Attr )textNodeAttributes.getNamedItem("type"); 232 if( 233 (textNodeType != null) && 234 ("Label".equals(textNodeType.getValue()) || "ToolTip".equals(textNodeType.getValue())) 235 ) { 236 org.w3c.dom.NodeList attributeNodes = textNode.getChildNodes(); 237 for(int k = 0; k < attributeNodes.getLength(); k++) { 238 org.w3c.dom.Node 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 private void writeAsSchema( 262 Writer w, 263 Writer fw, 264 Map elementDefinitions, 265 int localeIndex 266 ) throws ServiceException, IOException { 267 String providerName ="CRX"; 268 String 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 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 i = elementDefinitions.values().iterator(); 291 i.hasNext(); 292 ) { 293 DataproviderObject_1_0 element = (DataproviderObject_1_0)i.next(); 294 String elementDefinitionType = (String )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 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 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 label = element.values("label").size() > j 343 ? (String )element.values("label").get(j) 344 : ""; 345 if(!"".equals(label)) { 346 String indent = isNested ? " " : ""; 347 348 if(!tagWritten) { 350 s = indent + " <label>\n"; 351 w.write(s, 0, s.length()); 352 tagWritten = true; 353 } 354 355 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 if(tagWritten) { 366 String indent = isNested ? " " : ""; 367 s = indent + " </label>\n"; 368 w.write(s, 0, s.length()); 369 } 370 371 tagWritten = false; 373 for( 374 int j = startIndex; 375 j < endIndex + 1; 376 j++ 377 ) { 378 String toolTip = element.values("toolTip").size() > j 379 ? (String )element.values("toolTip").get(j) 380 : ""; 381 if(!"".equals(toolTip)) { 382 String indent = isNested ? " " : ""; 383 384 if(!tagWritten) { 386 s = indent + " <toolTip>\n"; 387 w.write(s, 0, s.length()); 388 tagWritten = true; 389 } 390 391 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 if(tagWritten) { 402 String 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 449 private void readAsSchema( 450 File file, 451 Map mergedElementDefinitions, 452 int localeIndex 453 ) throws ServiceException { 454 Map elementDefinitions = ListOrderedMap.decorate(new HashMap ()); 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 []{file.getAbsolutePath()} 464 ); 465 } 466 catch(ServiceException e) { 467 e.log(); 468 System.out.println("STATUS: " + e.getMessage()); 469 } 470 catch(Exception e) { 471 new ServiceException(e).log(); 472 System.out.println("STATUS: " + e.getMessage()); 473 } 474 } 475 476 Set keySet = localeIndex <= 0 ? elementDefinitions.keySet() : mergedElementDefinitions.keySet(); 478 try { 479 for(Iterator j = keySet.iterator(); j.hasNext(); ) { 480 Path key = (Path)j.next(); 481 if(mergedElementDefinitions.get(key) != null) { 483 DataproviderObject mergedElementDefinition = (DataproviderObject)mergedElementDefinitions.get(key); 484 String mergedElementDefinitionType = (String )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 : "" ); 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 : "" ); 504 } 505 } 506 } 507 else if(localeIndex <= 0) { 509 mergedElementDefinitions.put( 510 key, 511 elementDefinitions.get(key) 512 ); 513 } 514 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 e) { 522 System.err.println("Can not import. Reason is " + e.getMessage()); 523 } 524 } 525 526 private void readAsTable( 528 File file, 529 File templateFile, 530 Map 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 []{templateFile.getAbsolutePath()} 540 ); 541 } 542 catch(ServiceException e) { 543 e.log(); 544 System.out.println("STATUS: " + e.getMessage()); 545 } 546 catch(Exception 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 mergedElementDefinitions = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file); 553 for(Iterator i = elementDefinitions.values().iterator(); i.hasNext(); ) { 554 DataproviderObject elementDefinition = (DataproviderObject)i.next(); 555 String elementDefinitionType = (String )elementDefinition.values(SystemAttributes.OBJECT_CLASS).get(0); 556 Map 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++) { String label = null; 584 String toolTip = null; 585 if(mergedElementDefinition != null) { 586 label = (String )mergedElementDefinition.get(locales.get(j) + "_Label"); 587 toolTip = (String )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 e) { 595 System.err.println("ParserConfigurationException: can not load file " + file.getAbsolutePath()); 596 } 597 catch(SAXException e) { 598 System.err.println("SAXException: can not load file " + file.getAbsolutePath()); 599 } 600 catch(IOException e) { 601 System.err.println("IOException: can not load file " + file.getAbsolutePath()); 602 } 603 } 604 605 private void writeAsTable( 607 Writer w, 608 Writer fw, 609 Map elementDefinitions 610 ) throws ServiceException, IOException { 611 String 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 j = elementDefinitions.values().iterator(); 618 j.hasNext(); 619 ) { 620 DataproviderObject_1_0 entry = (DataproviderObject_1_0)j.next(); 621 String elementDefinitionType = (String )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 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 )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 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 )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 protected void split( 681 List locales, 682 File sourceDir, 683 File targetDir, 684 String format 685 ) throws ServiceException { 686 String en_US_Dir = targetDir.getAbsolutePath() + File.separatorChar + "en_US"; 687 System.out.println("sourceDir=" + sourceDir.getAbsolutePath()); 688 File [] en_US_files = new File (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 for(int u = 0; u < en_US_files.length; u++) { 696 Map elementDefinitions = ListOrderedMap.decorate(new HashMap ()); 697 File file = new File (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 for(int j = 1; j < locales.size(); j++) { String outFileName = targetDir.getAbsolutePath() + File.separatorChar + locales.get(j) + File.separatorChar + en_US_files[u].getName(); 716 try { 717 File outFile = new File (outFileName); 718 if(outFile.exists()) { 719 File renamed = new File (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 w = new OutputStreamWriter (new FileOutputStream (outFileName), "UTF-8"); 733 Writer fw = new XMLWriter(w); 734 this.writeAsSchema( 735 w, 736 fw, 737 elementDefinitions, 738 j 739 ); 740 w.close(); 741 } 742 catch(FileNotFoundException e) { 743 System.err.println("can not create file " + outFileName); 744 } 745 catch(UnsupportedEncodingException e) { 746 System.err.println("can not create file with encoding UTF-8 " + outFileName); 747 } 748 catch(IOException e) { 749 System.err.println("error writing to file " + outFileName); 750 } 751 } 752 } 753 } 754 755 protected void merge( 757 List locales, 758 File sourceDir, 759 File targetDir, 760 String format 761 ) throws ServiceException { 762 763 String en_US_Dir = sourceDir.getAbsolutePath() + File.separatorChar + "en_US"; 764 File [] en_US_files = new File (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 for(int u = 0; u < en_US_files.length; u++) { 773 774 Map mergedElementDefinitions = ListOrderedMap.decorate(new HashMap ()); 776 for(int i = 0; i < locales.size(); i++) { 777 File file = new File (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 String outFileName = targetDir.getAbsolutePath() + File.separatorChar + en_US_files[u].getName(); 787 try { 788 File outFile = new File (outFileName); 790 if(outFile.exists()) { 791 File renamed = new File (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 w = new OutputStreamWriter (new FileOutputStream (outFileName), "UTF-8"); 805 Writer 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 e) { 824 System.err.println("can not create file " + outFileName); 825 } 826 catch(UnsupportedEncodingException e) { 827 System.err.println("can not create file with encoding UTF-8 " + outFileName); 828 } 829 catch(IOException e) { 830 System.err.println("error writing to file " + outFileName); 831 } 832 } 833 } 834 835 protected void run( 837 ) throws Exception { 838 String 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 protected void release( 855 ) throws Exception { 856 System.out.println("shutdown"); 857 } 858 859 public static void main( 861 String [] 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 private static List createCmdLineOptions( 871 ) { 872 ArrayList options = new ArrayList (); 873 874 options.add( 876 new CmdLineOption( 877 "merge", 878 "Merge locale separated files to one merged file" 879 ) 880 ); 881 882 options.add( 884 new CmdLineOption( 885 "split", 886 "Split merged file to locale separated files" 887 ) 888 ); 889 890 891 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 options.add( 910 new CmdLineOption( 911 "locale", 912 "list of locales to process", 913 0, 914 Integer.MAX_VALUE 915 ) 916 ); 917 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 933 private static final String VERSION = "$Revision: 1.9 $"; 935 936 private static final String APP_NAME = "UiMapper"; 938 939 private static final String LOG_CONFIG_NAME = "UiMapper"; 941 942 private static final String LOG_SOURCE = APP_NAME; 944 945 private static final String HELP_TEXT = "UiMapper splits and merges ui definitions"; 947 948 private List locales = null; 950 private File sourceDir = null; 951 private File targetDir = null; 952 private String format = null; 953 954 } 955 956
| Popular Tags
|