| 1 10 11 package org.enhydra.jawe.xml; 12 13 import org.enhydra.jawe.xml.elements.*; 14 15 import java.util.*; 16 import java.io.*; 17 import java.net.URL ; 18 import javax.swing.*; 19 import java.awt.*; 20 import javax.swing.tree.*; 21 22 import javax.xml.transform.*; 23 import javax.xml.transform.dom.*; 24 import javax.xml.transform.stream.*; 25 import org.w3c.dom.*; 26 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.parsers.DocumentBuilder ; 29 import org.apache.xml.serialize.XMLSerializer; 30 import org.apache.xml.serialize.OutputFormat; 31 import org.apache.xerces.parsers.DOMParser; 32 import org.xml.sax.*; 33 34 import org.enhydra.jawe.xml.elements.Package; 35 36 43 public class XMLUtil { 44 45 46 private static final String EMPTY_STRING =""; 47 48 private static final String HTML_OPEN ="<html>"; 49 50 private static final String HTML_CLOSE ="</html>"; 51 52 private static final String STRONG_OPEN ="<strong>"; 53 54 private static final String STRONG_CLOSE ="</strong>"; 55 56 private static final String LINE_BREAK ="<br>"; 57 58 private static final String COLON_SPACE =": "; 59 60 private static Window myWindow; 61 62 private static ResourceBundle defaultResources; 63 private static ResourceBundle choosenResources; 64 private static Properties properties; 65 66 private static XMLUtil.XMLFilter allFilter = new XMLUtil.XMLFilter(null); 68 private static XMLUtil.XMLFilter xmlFilter = new XMLUtil.XMLFilter("xml"); 69 private static XMLUtil.XMLFilter xpdlFilter = new XMLUtil.XMLFilter("xpdl"); 70 private static XMLUtil.XMLFilter jpgFilter = new XMLUtil.XMLFilter("jpg"); 71 private static XMLUtil.XMLFilter svgFilter = new XMLUtil.XMLFilter("svg"); 72 private static XMLUtil.XMLFilter txtFilter = new XMLUtil.XMLFilter("txt"); 73 74 private static javax.swing.filechooser.FileFilter [] lastChoosenFilter= 75 new javax.swing.filechooser.FileFilter [5]; 76 static { 77 lastChoosenFilter[0]=xpdlFilter; 78 lastChoosenFilter[1]=jpgFilter; 79 lastChoosenFilter[2]=svgFilter; 80 lastChoosenFilter[3]=txtFilter; 81 lastChoosenFilter[4]=allFilter; 82 } 83 84 private static JFileChooser chooser; 85 86 static { 87 String userDir=System.getProperty("user.dir"); 88 chooser=new JFileChooser(userDir); 89 } 90 91 public static final int INFORMATION_MESSAGE = JOptionPane.INFORMATION_MESSAGE; 92 public static final int ERROR_MESSAGE = JOptionPane.ERROR_MESSAGE; 93 94 98 private static final class XMLFilter extends javax.swing.filechooser.FileFilter { 99 private String myExtension=null; 100 101 XMLFilter (String extension) { 102 this.myExtension=extension; 103 } 104 105 public String getExtension () { 106 return myExtension; 107 } 108 109 public boolean accept (File f) { 110 if(f != null) { 111 if(f.isDirectory()) { 112 return true; 113 } 114 if (myExtension!=null) { 115 String extension = null; 116 String fName = f.getName(); 117 int i = fName.lastIndexOf('.'); 118 if(i>0 && i<fName.length()-1) { 119 extension=fName.substring(i+1).toLowerCase(); 120 } 121 122 if(extension != null && extension.equalsIgnoreCase(myExtension)) { 123 return true; 124 } 125 } else { 126 return true; 127 } 128 } 129 return false; 130 } 131 132 public String getDescription () { 133 if (myExtension!=null) { 134 return XMLUtil.getLanguageDependentString(myExtension.toUpperCase()+"Description"); 135 } else { 136 return XMLUtil.getLanguageDependentString("ALLDescription"); 137 } 138 } 139 } 140 142 public static void setApplicationWindow (Window w) { 143 myWindow=w; 144 } 145 146 public static void setDefaultResources (ResourceBundle defaultR) { 147 defaultResources=defaultR; 148 } 149 150 public static void setChoosenResources (ResourceBundle choosenR) { 151 choosenResources=choosenR; 152 } 153 154 public static void setProperties (Properties props) { 155 properties=props; 156 } 157 158 159 public static void message(String message,int type) { 160 JOptionPane.showMessageDialog(myWindow,message, 161 XMLUtil.getLanguageDependentString("Title"),type); 162 } 163 164 174 public static String dialog(Component parent,String message, 175 int mode,int filteringMode,String initialName) { 176 chooser.updateUI(); 177 chooser.setDialogTitle(message); 178 int setFilterIndex = 0; 179 switch (filteringMode) { 180 case 0: 181 chooser.setAcceptAllFileFilterUsed(false); 182 chooser.setFileFilter(allFilter); 183 chooser.setFileFilter(xmlFilter); 184 chooser.setFileFilter(xpdlFilter); 185 chooser.removeChoosableFileFilter(jpgFilter); 186 chooser.removeChoosableFileFilter(svgFilter); 187 chooser.removeChoosableFileFilter(txtFilter); 188 setFilterIndex = 0; 189 break; 190 case 1: 191 chooser.setAcceptAllFileFilterUsed(false); 192 chooser.setFileFilter(allFilter); 193 chooser.setFileFilter(jpgFilter); 194 chooser.removeChoosableFileFilter(xmlFilter); 195 chooser.removeChoosableFileFilter(xpdlFilter); 196 chooser.removeChoosableFileFilter(svgFilter); 197 chooser.removeChoosableFileFilter(txtFilter); 198 setFilterIndex = 1; 199 break; 200 case 2: 201 chooser.setAcceptAllFileFilterUsed(false); 202 chooser.setFileFilter(allFilter); 203 chooser.setFileFilter(svgFilter); 204 chooser.removeChoosableFileFilter(xmlFilter); 205 chooser.removeChoosableFileFilter(xpdlFilter); 206 chooser.removeChoosableFileFilter(jpgFilter); 207 chooser.removeChoosableFileFilter(txtFilter); 208 setFilterIndex = 2; 209 break; 210 case 3: 211 chooser.setAcceptAllFileFilterUsed(false); 212 chooser.setFileFilter(allFilter); 213 chooser.setFileFilter(txtFilter); 214 chooser.removeChoosableFileFilter(xmlFilter); 215 chooser.removeChoosableFileFilter(xpdlFilter); 216 chooser.removeChoosableFileFilter(jpgFilter); 217 chooser.removeChoosableFileFilter(svgFilter); 218 setFilterIndex = 3; 219 break; 220 default: 221 chooser.setAcceptAllFileFilterUsed(false); 222 chooser.setFileFilter(allFilter); 223 chooser.removeChoosableFileFilter(xmlFilter); 224 chooser.removeChoosableFileFilter(xpdlFilter); 225 chooser.removeChoosableFileFilter(jpgFilter); 226 chooser.removeChoosableFileFilter(svgFilter); 227 chooser.removeChoosableFileFilter(txtFilter); 228 setFilterIndex = 4; 229 break; 230 } 231 try { 232 chooser.setFileFilter(lastChoosenFilter[setFilterIndex]); 233 } catch (Exception ex) {} 234 235 if (initialName!=null && initialName.length()>0) { 236 chooser.setSelectedFile(new File(initialName)); 237 } 238 239 int returnVal=-1; 240 String fileName=null; 241 242 while (true) { 243 if (mode==0) { 244 returnVal = chooser.showOpenDialog(parent); 245 } 246 else { 247 returnVal = chooser.showSaveDialog(parent); 248 } 249 250 if(returnVal == JFileChooser.APPROVE_OPTION) { 251 File f=chooser.getSelectedFile(); 252 fileName=f.getAbsolutePath(); 253 String extension=((XMLUtil.XMLFilter)chooser.getFileFilter()).getExtension(); 257 int dotIndex=f.getName().lastIndexOf("."); 258 259 String oldFilename=fileName; 260 if (mode==1) { 261 if ((filteringMode>=0 && filteringMode<=2) && extension!=null && 262 dotIndex==-1) { 263 fileName+="."+extension; 264 } 265 if (new File(fileName).exists()) { 267 int r=JOptionPane.showConfirmDialog(myWindow, 268 XMLUtil.getLanguageDependentString("WarningFileAlreadyExistsDoYouWantToReplaceIt"), 269 XMLUtil.getLanguageDependentString("Title"),JOptionPane.YES_NO_OPTION); 270 if (r==JOptionPane.NO_OPTION) { 271 fileName=oldFilename; 272 continue; 273 } 274 } 275 276 } else { 280 if (mode==0 && !f.exists()) { 281 if ((filteringMode>=0 && filteringMode<=2) && dotIndex==-1) { 282 fileName+="."+extension; 283 if (!new File(fileName).exists()) { 284 fileName=null; 285 } 286 } else { 287 fileName=null; 288 } 289 } 290 if (fileName==null) { 291 XMLUtil.message( 292 XMLUtil.getLanguageDependentString("WarningFileDoesNotExistPleaseSelectExistingFileToOpen"), 293 JOptionPane.WARNING_MESSAGE); 294 fileName=oldFilename; 295 continue; 296 } 297 } 298 } 299 break; 300 } 301 302 try { 303 lastChoosenFilter[setFilterIndex]=chooser.getFileFilter(); 304 } catch (Exception ex) {} 305 306 return fileName; 307 } 308 309 319 public static String getLanguageDependentString(String nm) { 320 String str; 321 try { 322 str=choosenResources.getString(nm); 323 } catch (MissingResourceException mre) { 324 try { 325 str=defaultResources.getString(nm); 326 } catch (MissingResourceException mre1) { 327 str = null; 328 } catch (NullPointerException npe) { 329 str=null; 330 } 331 } catch (NullPointerException npe) { 332 334 ResourceBundle orig=ResourceBundle. 335 getBundle("org.enhydra.jawe.resources.JaWE"); 336 try { 337 str=orig.getString(nm); 338 } catch (Exception ex) { 339 str=null; 340 } 341 } 342 return str; 343 } 344 345 351 public static URL getResource(String key) { 352 try { 353 String name=properties.getProperty(key); 354 if (name != null) { 355 URL url = XMLUtil.class.getClassLoader().getResource(name); 356 return url; 357 } 358 } catch (Exception ex) {} 359 return null; 360 } 361 362 368 public static String [] tokenize(String input,String boundary) { 369 if (input==null) input=""; 370 Vector v = new Vector(); 371 StringTokenizer t = new StringTokenizer(input,boundary); 372 String cmd[]; 373 374 while (t.hasMoreTokens()) 375 v.addElement(t.nextToken()); 376 cmd = new String [v.size()]; 377 for (int i = 0; i < cmd.length; i++) 378 cmd[i] = (String )v.elementAt(i); 379 380 return cmd; 381 } 382 383 386 public static int howManyStringsWithinString (String toSearch,String toFind) { 387 try { 388 int startAt=0; 389 int howMany=0; 390 391 int fnd; 392 while ((fnd=toSearch.indexOf(toFind,startAt))!=-1) { 393 howMany++; 394 startAt=(fnd+toFind.length()); 395 } 396 return howMany; 397 } catch (Exception ex) { 398 return -1; 399 } 400 } 401 402 406 public static String makeTooltip(XMLElement[] elements){ 407 if (elements==null) return ""; 408 String s = HTML_OPEN; 409 for (int i=0; i<elements.length; i++) { 410 s+=makeAnotherHtmlLine(elements[i]); 411 } 412 s=s.substring(0, s.length() - LINE_BREAK.length()); 413 s+=HTML_CLOSE; 414 return s; 415 } 416 417 418 private static String makeAnotherHtmlLine(XMLElement el) { 419 int MAX_LENGTH=100; 420 int MAX_LINES_PER_TEXT=15; 421 String textToAppend=""; 422 textToAppend += STRONG_OPEN; 423 textToAppend += el.toLabel()+COLON_SPACE; 424 textToAppend += STRONG_CLOSE; 425 String val=el.toString(); 426 val=val.replaceAll("<","<"); 427 val=val.replaceAll(">",">"); 428 int vl=val.length(); 429 if (vl>MAX_LENGTH) { 430 String newVal=""; 431 int hm=vl/MAX_LENGTH; 432 for (int i=0; i<=hm; i++) { 433 int startI=i*MAX_LENGTH; 434 int endI=(i+1)*MAX_LENGTH; 435 if (endI>vl) { 436 endI=vl; 437 } 438 newVal=newVal+val.substring(startI,endI); 439 if (i==MAX_LINES_PER_TEXT) { 440 newVal=newVal+" ..."; 441 break; 442 } 443 if (i<hm) { 444 newVal+=LINE_BREAK; 445 newVal+=XMLUtil.makeEmptyHTMLText((el.toLabel()+COLON_SPACE).length()); 446 } 447 } 448 val=newVal; 449 } 450 textToAppend += val; 451 textToAppend += LINE_BREAK; 452 453 return textToAppend; 454 } 455 456 public static String getCanonicalPath (String path, boolean canBeDirectory) { 457 File f=new File(path); 458 if (!f.isAbsolute()) { 459 f=new File(System.getProperty("user.dir")+File.separator+path); 460 } 461 if (!f.exists() || (f.isDirectory() && !canBeDirectory)) { 462 System.err.println("The file "+f.getAbsolutePath()+" does not exist"); 463 return null; 464 } else { 465 return getCanonicalPath(f); 466 } 467 } 468 469 private static String getCanonicalPath (File f) { 470 try { 471 return f.getCanonicalPath(); 472 } catch (Exception ex) { 473 return f.getAbsolutePath(); 474 } 475 } 476 477 485 public static void parseElements (Node node,Collection complexStructure) { 486 String nameSpacePrefix=node.getPrefix(); 487 if (nameSpacePrefix!=null) { 488 nameSpacePrefix+=":"; 489 } else { 490 nameSpacePrefix=""; 491 } 492 Iterator it=complexStructure.iterator(); 494 while (it.hasNext()) { 495 XMLElement el=(XMLElement)it.next(); 496 if (!(el instanceof XMLAttribute) && !(el instanceof XMLComplexChoice) 497 && node!=null) { 498 Node child = getChildByName(node,nameSpacePrefix+el.name); 501 if (child!=null) { 502 el.fromXML(child); 503 } 504 } 505 else { 506 if (el instanceof XMLComplexChoice) { 507 Object [] ch=((XMLComplexChoice)el).getChoices(); 508 if (ch != null && node!=null) { 509 for (int i=0; i<ch.length; i++) { 510 XMLElement chc=(XMLElement)ch[i]; 511 Node child = getChildByName(node,nameSpacePrefix+chc.name); 513 if (child!=null) { 514 ((XMLComplexChoice)el).fromXML(chc.name,child); 515 } 516 } 517 } 518 } 519 } 520 } 521 } 522 523 public static Node getChildByName(Node parent,String childName) { 524 NodeList children = parent.getChildNodes(); 525 for (int i = 0; i < children.getLength(); ++i) { 526 Node child = (Node) children.item(i); 527 if (child.getNodeName().equals(childName)) { 528 return child; 529 } 530 } 531 return null; 532 } 533 534 public static String getID (Node node) { 535 try { 536 NamedNodeMap nnm=node.getAttributes(); 537 Node attrib=nnm.getNamedItem("Id"); 538 Object ID; 539 if (attrib.hasChildNodes()) { 540 ID=attrib.getChildNodes().item(0).getNodeValue(); 541 } else { 542 ID=attrib.getNodeValue(); 543 } 544 return ID.toString(); 545 } catch (Exception ex) { 546 return ""; 547 } 548 } 549 550 public static String getContent (Node node,boolean omitXMLDeclaration) { 551 try { 552 ByteArrayOutputStream baos=new ByteArrayOutputStream(); 553 554 TransformerFactory tFactory = 556 TransformerFactory.newInstance(); 557 Transformer transformer = tFactory.newTransformer(); 558 transformer.setOutputProperty("indent","yes"); 559 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); 560 transformer.setOutputProperty("encoding","UTF-8"); 561 if (omitXMLDeclaration) { 562 transformer.setOutputProperty("omit-xml-declaration","yes"); 563 } 564 565 DOMSource source = new DOMSource(node); 566 StreamResult result = new StreamResult(baos); 567 transformer.transform(source,result); 568 569 String cont=baos.toString("UTF8"); 570 571 baos.close(); 572 return cont; 573 } catch (Exception ex) { 574 return ""; 575 } 576 } 577 578 public static String getChildNodesContent (Node node) { 579 String txt=""; 580 if (node!=null) { 581 if (node.hasChildNodes()) { 582 txt=XMLUtil.getContent(node,true); 583 try { 584 Node fc=node.getFirstChild(); 585 String fcnc=XMLUtil.getContent(fc,true); 586 String closedTag="</"+node.getNodeName()+">"; 587 if (fcnc.trim().length()>0) { 588 fcnc=fcnc.trim(); 589 } 590 591 int i1,i2; 592 i1=txt.indexOf(fcnc); 593 i2=txt.lastIndexOf(closedTag); 594 txt=txt.substring(i1,i2).trim(); 595 } catch (Exception ex) { 596 NodeList nl=node.getChildNodes(); 597 txt=""; 598 try { 599 for (int i=0; i<nl.getLength(); i++) { 600 Node sn=nl.item(i); 601 if (sn instanceof Element) { 602 txt+=XMLUtil.getContent(sn,true); 603 } else { 604 String nv=sn.getNodeValue(); 605 if (i>0) { 607 txt+=nv.substring(1); 608 } else if (i==0 && nv.trim().length()==0) { 609 continue; 610 } else { 611 txt+=nv; 612 } 613 } 614 } 615 } catch (Exception ex2){} 616 } 617 } 618 } 619 return txt; 620 } 621 622 630 public static boolean isANDTypeSplitOrJoin (Activity act,int sOrJ) { 631 String splitOrJoin="Split"; 632 if (sOrJ!=0) { 633 splitOrJoin="Join"; 634 } 635 String sjType = "XOR"; Collection transitionRestrictions = 639 ((TransitionRestrictions)act.get("TransitionRestrictions")).toCollection(); 640 Iterator iter = transitionRestrictions.iterator(); 641 while(iter.hasNext()){ 642 TransitionRestriction restriction=(TransitionRestriction)iter.next(); 643 XMLComplexElement sj=(XMLComplexElement)restriction.get(splitOrJoin); 644 if(sj==null){ 645 continue; 647 } 648 sjType = sj.get("Type").toValue().toString(); 649 } 650 651 if (sjType.equals("AND")) { 652 return true; 653 } else { 654 return false; 655 } 656 } 657 658 665 public static Set getSplitOrJoinActivities (Collection acts,int sOrJ) { 666 Set sOrJactivities=new HashSet(); 667 if (acts==null) return sOrJactivities; 668 Transitions ts=null; 669 Iterator it=acts.iterator(); 670 while (it.hasNext()) { 671 Activity act=(Activity)it.next(); 672 if (ts==null) { 673 ts=(Transitions)act.getCollection().getOwner().get("Transitions"); 674 } 675 Iterator iter; 676 if (sOrJ==0) { 677 iter=ts.getTransitions(act.getID(),-1).iterator(); 678 } else { 679 iter=ts.getTransitions(act.getID(),1).iterator(); 680 } 681 int noOfTrans=0; 682 while (iter.hasNext()) { 683 Transition t = (Transition)iter.next(); 684 if (t!=null && t.getFrom()!=null && t.getTo()!=null) { 685 noOfTrans++; 686 } 687 } 688 if (noOfTrans>1) { 689 sOrJactivities.add(act); 690 } 691 } 692 693 return sOrJactivities; 694 } 695 696 703 public static Set getBlockActivities(XMLComplexElement wpOrAs,boolean recursivly) { 704 Collection allActs=((Activities)wpOrAs.get("Activities")).toCollection(); 705 Set bas=new HashSet(); 706 Iterator it=allActs.iterator(); 707 Activity act; 708 while (it.hasNext()) { 709 act=(Activity)it.next(); 710 BlockActivity ba=act.getBlockActivity(); 711 if (ba!=null) { 712 bas.add(act); 713 if (!recursivly) continue; 714 ActivitySets ass=(ActivitySets)act.getOwnerProcess().get("ActivitySets"); 715 String asId=ba.get("BlockId").toString(); 716 ActivitySet as=ass.getActivitySet(asId); 717 if (as!=null) { 718 bas.addAll(getBlockActivities(as,true)); 719 } 720 } 721 } 722 return bas; 723 } 724 725 726 732 public static int getConformanceClassNo (String conformanceClass) { 733 if (conformanceClass.equals("NON_BLOCKED")) { 734 return 0; 735 } else if (conformanceClass.equals("LOOP_BLOCKED")) { 736 return 1; 737 } else if (conformanceClass.equals("FULL_BLOCKED")) { 738 return 2; 739 } else { 740 return -1; 741 } 742 } 743 744 747 public static String fileToString (String fileName) { 748 if (fileName != null) { 749 byte[] utf8Bytes; 751 String sFile = new String (); 752 try { 754 FileInputStream fis=new FileInputStream(fileName); 755 int noOfBytes=fis.available(); 756 if (noOfBytes>0) { 757 utf8Bytes=new byte[noOfBytes]; 758 fis.read(utf8Bytes); 759 sFile=new String (utf8Bytes,"UTF8"); 760 } 761 } 762 catch (Exception ex) { 763 return null; 764 } 765 return sFile; 766 } 767 return null; 768 } 769 771 772 public static String getCurrentDateAndTime () { 773 String dateSeparator="-"; 774 String timeSeparator=":"; 775 Calendar cal=new GregorianCalendar(); 776 String dateTime=""; 777 dateTime=dateTime+String.valueOf(cal.get(Calendar.YEAR))+dateSeparator; 778 int mnth=cal.get(Calendar.MONTH)+1; 779 if (mnth<10) { 780 dateTime=dateTime+"0"; 781 } 782 dateTime=dateTime+String.valueOf(mnth)+dateSeparator; 783 int dayOfMnth=cal.get(Calendar.DAY_OF_MONTH); 784 if (dayOfMnth<10) { 785 dateTime=dateTime+"0"; 786 } 787 dateTime=dateTime+String.valueOf(dayOfMnth)+" "; 788 int hr=cal.get(Calendar.HOUR_OF_DAY); 789 int ampm=cal.get(Calendar.AM_PM); 790 if (ampm==Calendar.PM && hr<12) { 791 hr+=12; 792 } 793 if (hr<10) { 794 dateTime=dateTime+"0"; 795 } 796 dateTime=dateTime+String.valueOf(hr)+timeSeparator; 797 int min=cal.get(Calendar.MINUTE); 798 |