1 package org.enhydra.shark.xpdl; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileOutputStream ; 8 import java.io.ObjectInputStream ; 9 import java.io.ObjectOutputStream ; 10 import java.io.OutputStream ; 11 import java.io.StringReader ; 12 import java.net.URL ; 13 import java.util.ArrayList ; 14 import java.util.Calendar ; 15 import java.util.Collection ; 16 import java.util.GregorianCalendar ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.MissingResourceException ; 23 import java.util.Properties ; 24 import java.util.ResourceBundle ; 25 import java.util.Set ; 26 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.transform.Transformer ; 30 import javax.xml.transform.TransformerFactory ; 31 import javax.xml.transform.dom.DOMSource ; 32 import javax.xml.transform.stream.StreamResult ; 33 34 import org.apache.xerces.util.XMLChar; 35 import org.enhydra.shark.xpdl.elements.Activities; 36 import org.enhydra.shark.xpdl.elements.Activity; 37 import org.enhydra.shark.xpdl.elements.ActivitySet; 38 import org.enhydra.shark.xpdl.elements.ActivitySets; 39 import org.enhydra.shark.xpdl.elements.ActualParameter; 40 import org.enhydra.shark.xpdl.elements.ActualParameters; 41 import org.enhydra.shark.xpdl.elements.BasicType; 42 import org.enhydra.shark.xpdl.elements.BlockActivity; 43 import org.enhydra.shark.xpdl.elements.Condition; 44 import org.enhydra.shark.xpdl.elements.DataType; 45 import org.enhydra.shark.xpdl.elements.DataTypes; 46 import org.enhydra.shark.xpdl.elements.Deadlines; 47 import org.enhydra.shark.xpdl.elements.DeclaredType; 48 import org.enhydra.shark.xpdl.elements.EnumerationType; 49 import org.enhydra.shark.xpdl.elements.EnumerationValue; 50 import org.enhydra.shark.xpdl.elements.ExtendedAttribute; 51 import org.enhydra.shark.xpdl.elements.ExtendedAttributes; 52 import org.enhydra.shark.xpdl.elements.FinishMode; 53 import org.enhydra.shark.xpdl.elements.FormalParameter; 54 import org.enhydra.shark.xpdl.elements.FormalParameters; 55 import org.enhydra.shark.xpdl.elements.Join; 56 import org.enhydra.shark.xpdl.elements.Manual; 57 import org.enhydra.shark.xpdl.elements.Namespace; 58 import org.enhydra.shark.xpdl.elements.Namespaces; 59 import org.enhydra.shark.xpdl.elements.Package; 60 import org.enhydra.shark.xpdl.elements.RedefinableHeader; 61 import org.enhydra.shark.xpdl.elements.Responsible; 62 import org.enhydra.shark.xpdl.elements.Responsibles; 63 import org.enhydra.shark.xpdl.elements.SchemaType; 64 import org.enhydra.shark.xpdl.elements.Split; 65 import org.enhydra.shark.xpdl.elements.StartMode; 66 import org.enhydra.shark.xpdl.elements.SubFlow; 67 import org.enhydra.shark.xpdl.elements.Tools; 68 import org.enhydra.shark.xpdl.elements.Transition; 69 import org.enhydra.shark.xpdl.elements.TransitionRef; 70 import org.enhydra.shark.xpdl.elements.TransitionRefs; 71 import org.enhydra.shark.xpdl.elements.TransitionRestriction; 72 import org.enhydra.shark.xpdl.elements.TransitionRestrictions; 73 import org.enhydra.shark.xpdl.elements.Transitions; 74 import org.enhydra.shark.xpdl.elements.WorkflowProcess; 75 import org.w3c.dom.Attr ; 76 import org.w3c.dom.Document ; 77 import org.w3c.dom.Element ; 78 import org.w3c.dom.NamedNodeMap ; 79 import org.w3c.dom.Node ; 80 import org.w3c.dom.NodeList ; 81 import org.xml.sax.InputSource ; 82 83 88 public class XMLUtil { 89 90 public final static String XMLNS = "http://www.wfmc.org/2002/XPDL1.0"; 91 public final static String XMLNS_XPDL = "http://www.wfmc.org/2002/XPDL1.0"; 92 public final static String XMLNS_XSI = "http://www.w3.org/2001/XMLSchema-instance"; 93 public final static String XSI_SCHEMA_LOCATION = "http://www.wfmc.org/2002/XPDL1.0 http://wfmc.org/standards/docs/TC-1025_schema_10_xpdl.xsd"; 94 95 private static ResourceBundle defaultResources; 96 private static ResourceBundle choosenResources; 97 private static Properties properties; 98 99 public static void setDefaultResources (ResourceBundle defaultR) { 100 defaultResources=defaultR; 101 } 102 103 public static void setChoosenResources (ResourceBundle choosenR) { 104 choosenResources=choosenR; 105 } 106 107 public static void setProperties (Properties props) { 108 properties=props; 109 } 110 111 121 public static String getLanguageDependentString(String nm) { 122 String str; 123 try { 124 str=choosenResources.getString(nm); 125 } catch (MissingResourceException mre) { 126 try { 127 str=defaultResources.getString(nm); 128 } catch (MissingResourceException mre1) { 129 str = null; 130 } catch (NullPointerException npe) { 131 str=null; 132 } 133 } catch (NullPointerException npe) { 134 136 ResourceBundle orig=ResourceBundle. 137 getBundle("org.enhydra.shark.xpdl.resources.SharkXPDL"); 138 try { 139 str=orig.getString(nm); 140 } catch (Exception ex) { 141 str=null; 142 } 143 } 144 return str; 145 } 146 147 153 public static URL getResource(String key) { 154 try { 155 String name=properties.getProperty(key); 156 if (name != null) { 157 URL url = XMLUtil.class.getClassLoader().getResource(name); 158 return url; 159 } 160 } catch (Exception ex) {} 161 return null; 162 } 163 164 167 public static int howManyStringsWithinString (String toSearch,String toFind) { 168 try { 169 int startAt=0; 170 int howMany=0; 171 172 int fnd; 173 while ((fnd=toSearch.indexOf(toFind,startAt))!=-1) { 174 howMany++; 175 startAt=(fnd+toFind.length()); 176 } 177 return howMany; 178 } catch (Exception ex) { 179 return -1; 180 } 181 } 182 183 public static String getCanonicalPath (String relpath,String basedir,boolean canBeDirectory) { 184 File f=new File (relpath); 185 if (!f.isAbsolute()) { 186 f=f.getAbsoluteFile(); 187 if (!f.exists()) { 188 f=new File (XMLUtil.createPath(basedir,relpath)); 189 } 190 } 191 if (!f.exists() || (f.isDirectory() && !canBeDirectory)) { 192 System.err.println("The file "+f.getAbsolutePath()+" does not exist"); 193 return null; 194 } else { 195 return getCanonicalPath(f); 196 } 197 } 198 199 public static String getCanonicalPath (String path, boolean canBeDirectory) { 200 File f=new File (path); 201 if (!f.isAbsolute()) { 202 f=new File (System.getProperty("user.dir")+File.separator+path); 203 } 204 if (!f.exists() || (f.isDirectory() && !canBeDirectory)) { 205 System.err.println("The file "+f.getAbsolutePath()+" does not exist"); 206 return null; 207 } else { 208 return getCanonicalPath(f); 209 } 210 } 211 212 private static String getCanonicalPath (File f) { 213 try { 214 return f.getCanonicalPath(); 215 } catch (Exception ex) { 216 return f.getAbsolutePath(); 217 } 218 } 219 220 private static boolean logging=false; 221 222 public static void fromXML (Element node,Package pkg) { 223 NamedNodeMap attribs=node.getAttributes(); 226 Namespaces nss=pkg.getNamespaces(); 227 for (int i=0; i<attribs.getLength(); i++) { 228 Node n=attribs.item(i); 229 String nn=n.getNodeName(); 230 if (nn.startsWith("xmlns:") && !nn.equals("xmlns:xsi")) { 231 Namespace ns=(Namespace)nss.generateNewElement(); 232 ns.setName(nn.substring(6,nn.length())); 233 fromXML(n,(XMLAttribute)ns.get("location")); 234 nss.add(ns); 235 } 236 } 237 fromXML(node,(XMLComplexElement)pkg); 238 } 241 242 public static void fromXML (Node node,XMLCollection cel) { 243 if ( node == null || !node.hasChildNodes()) return; 244 String nameSpacePrefix=getNameSpacePrefix(node); 245 246 XMLElement newOne=cel.generateNewElement(); 247 String elName=newOne.toName(); 248 249 NodeList children = node.getChildNodes(); 250 int lng=children.getLength(); 251 if (logging) System.out.println("FROMXML for "+cel.toName()+", c="+cel.getClass().getName()); 252 for (int i = 0; i<lng; i++) { 253 Node child=children.item(i); 254 if (child.getNodeName().equals(nameSpacePrefix+elName)) { 255 newOne = cel.generateNewElement(); 257 if (newOne instanceof XMLComplexElement) { 259 fromXML(children.item(i),(XMLComplexElement)newOne); 260 } else { 261 fromXML(children.item(i),(XMLSimpleElement)newOne); 262 } 263 cel.add(newOne); 264 } else { 265 } 267 } 268 } 270 271 public static void fromXML (Node node,XMLComplexElement cel) { 272 if ( node == null || (!node.hasChildNodes() && !node.hasAttributes())) return; 273 274 String nameSpacePrefix=node.getPrefix(); 275 if (nameSpacePrefix!=null) { 276 nameSpacePrefix+=":"; 277 } else { 278 nameSpacePrefix=""; 279 } 280 if (logging) System.out.println("FROMXML for "+cel.toName()+", c="+cel.getClass().getName()); 281 282 if ( node.hasAttributes() ) { 283 NamedNodeMap attribs = node.getAttributes(); 284 for ( int i = 0; i < attribs.getLength(); ++i ) { 285 Node attrib = ( Node ) attribs.item( i ); 286 try { 287 fromXML(attrib,(XMLAttribute)cel.get( attrib.getNodeName() )); 289 } catch ( NullPointerException npe ) { 290 294 } 295 } 296 } 297 if (node.hasChildNodes()) { 299 if (cel instanceof Condition) { 301 String newVal=node.getChildNodes().item(0).getNodeValue(); 302 if (newVal!=null) { 303 cel.setValue(newVal); 304 } 305 } 306 if (cel instanceof SchemaType) { 308 NodeList nl=node.getChildNodes(); 309 for (int j=0; j<nl.getLength(); j++) { 310 Node sn=nl.item(j); 311 if (sn instanceof Element ) { 312 cel.setValue(XMLUtil.getContent(sn,true)); 313 break; 314 } 315 } 316 } 317 if (cel instanceof ExtendedAttribute) { 319 cel.setValue(XMLUtil.getChildNodesContent(node)); 320 } 321 Iterator it=cel.getXMLElements().iterator(); 322 while (it.hasNext()) { 323 XMLElement el=(XMLElement)it.next(); 324 String elName=el.toName(); 325 if (el instanceof XMLComplexElement) { 326 Node child = getChildByName(node,nameSpacePrefix+elName); 327 fromXML(child, (XMLComplexElement)el); 328 } else if (el instanceof Deadlines) { 330 fromXML(node, (XMLCollection)el); 331 } else if (el instanceof XMLCollection) { 332 Node child = getChildByName(node,nameSpacePrefix+elName); 333 fromXML(child, (XMLCollection)el); 334 } else if (el instanceof XMLComplexChoice) { 335 fromXML(node, (XMLComplexChoice)el); 336 } else if (el instanceof XMLSimpleElement) { 337 Node child = getChildByName(node,nameSpacePrefix+elName); 338 fromXML(child,(XMLSimpleElement)el); 339 } 340 } 341 } 342 } 343 344 public static void fromXML (Node node,XMLComplexChoice el) { 345 String nameSpacePrefix=getNameSpacePrefix(node); 346 List ch=el.getChoices(); 347 if (logging) System.out.println("FROMXML for "+el.toName()+", c="+el.getClass().getName()); 348 for (int i=0; i<ch.size(); i++) { 349 XMLElement chc=(XMLElement)ch.get(i); 350 String chname=chc.toName(); 351 if (chname.equals("Tools")) { 353 chname="Tool"; 354 } 355 Node child = getChildByName(node,nameSpacePrefix+chname); 356 if (child!=null) { 357 if (chc instanceof XMLComplexElement) { 358 fromXML(child,(XMLComplexElement)chc); 359 } else { if (chc instanceof Tools) { 362 fromXML(node, (XMLCollection)chc); 363 } else { 364 fromXML(child, (XMLCollection)chc); 365 } 366 } 367 el.setChoosen(chc); 368 break; 369 } 370 } 371 } 372 373 public static void fromXML (Node node,XMLSimpleElement el) { 374 fromXMLBasic(node, el); 375 } 376 377 public static void fromXML (Node node,XMLAttribute el) { 378 fromXMLBasic(node,el); 379 } 380 381 public static void fromXMLBasic (Node node,XMLElement el) { 382 if (node!=null) { 383 if (logging) System.out.println("FROMXML for "+el.toName()+", c="+el.getClass().getName()); 384 String newVal; 385 if (node.hasChildNodes()) { 386 newVal=node.getChildNodes().item(0).getNodeValue(); 387 if (logging) System.out.println("11111"); 388 } else { 390 if (logging) System.out.println("22222"); 391 newVal=node.getNodeValue(); 392 } 393 if (logging) System.out.println("NV="+newVal); 394 395 if (newVal!=null) { 396 el.setValue(newVal); 397 } 398 } 399 } 400 401 402 public static void toXML (Document parent,Package pkg) { 403 Node node = ((Document ) parent).createElement(pkg.toName()); 404 ((Element ) node).setAttribute("xmlns", XMLNS); 405 Iterator itNs=pkg.getNamespaces().toElements().iterator(); 408 while (itNs.hasNext()) { 409 Namespace ns=(Namespace)itNs.next(); 410 ((Element ) node).setAttribute("xmlns:"+ns.getName(), 411 ns.getLocation()); 412 } 413 ((Element ) node).setAttribute("xmlns:xsi", XMLNS_XSI); 414 ((Element ) node).setAttribute("xsi:schemaLocation", XSI_SCHEMA_LOCATION); 415 416 toXML(node,(XMLComplexElement)pkg); 417 parent.appendChild( node ); 418 } 419 420 public static void toXML (Node parent,XMLCollection cel) { 421 if (!cel.isEmpty() || cel.isRequired()) { 422 if (parent!=null) { 423 String elName=cel.toName(); 424 Node node = parent; 425 if (!(elName.equals("Deadlines") || elName.equals("Tools"))) { 427 node=(parent.getOwnerDocument()).createElement(elName); 428 } 429 for (Iterator it = cel.toElements().iterator(); it.hasNext();) { 430 XMLElement el=(XMLElement)it.next(); 431 if (el instanceof XMLSimpleElement) { 432 toXML(node,(XMLSimpleElement)el); 433 } else { 434 toXML(node,(XMLComplexElement)el); 435 } 436 } 437 if (node!=parent) { 439 parent.appendChild(node); 440 } 441 } 442 } 443 } 444 445 public static void toXML (Node parent,XMLComplexElement cel) { 446 if ( cel.isEmpty() && !cel.isRequired() ) 447 return; 448 if ( parent != null ) { 449 if (logging) System.out.println("TOXML for "+cel.toName()+", c="+cel.getClass().getName()); 450 Node node=parent; 451 if (!(cel instanceof Package )) { 453 node = ( parent.getOwnerDocument() ).createElement( cel.toName() ); 454 } 455 if (cel.toValue()!=null && cel.toValue().length()>0) { 456 if (cel instanceof Condition) { 458 if (!cel.toValue().equals("")) { 459 Node textNode=node.getOwnerDocument().createTextNode(cel.toValue()); 460 node.appendChild(textNode); 461 } 462 } 463 if (cel instanceof SchemaType) { 465 Node schema=XMLUtil.parseSchemaNode(cel.toValue(), false); 466 if (schema!=null) { 467 node.appendChild(node.getOwnerDocument().importNode(schema,true)); 468 } 469 } 470 if (cel instanceof ExtendedAttribute) { 472 try { 473 Node n=XMLUtil.parseExtendedAttributeContent(cel.toValue()); 474 NodeList nl=n.getChildNodes(); 475 for (int i=0; i<nl.getLength(); i++) { 476 node.appendChild(parent.getOwnerDocument().importNode(nl.item(i),true)); 477 } 478 } catch (Exception ex) {} 479 } 480 } 481 for ( Iterator it = cel.toElements().iterator(); it.hasNext(); ) { 482 XMLElement el = ( XMLElement ) it.next(); 483 if (el instanceof XMLComplexElement) { 484 toXML(node, (XMLComplexElement)el); 485 } else if (el instanceof XMLCollection) { 486 toXML(node, (XMLCollection)el); 487 } else if (el instanceof XMLComplexChoice) { 488 toXML(node, (XMLComplexChoice)el); 489 } else if (el instanceof XMLSimpleElement) { 490 toXML(node,(XMLSimpleElement)el); 491 } else { toXML(node,(XMLAttribute)el); 493 } 494 } 495 if (node!=parent) { 497 parent.appendChild( node ); 498 } 499 } 500 } 501 502 public static void toXML (Node parent,XMLComplexChoice el) { 503 XMLElement choosen=el.getChoosen(); 504 if (choosen!=null) { 505 if (choosen instanceof XMLComplexElement) { 506 toXML(parent,(XMLComplexElement)choosen); 507 } else { 508 toXML(parent,(XMLCollection)choosen); 509 } 510 } 511 } 512 513 public static void toXML (Node parent,XMLSimpleElement el) { 514 if (!el.isEmpty() || el.isRequired()) { 515 if (parent!=null) { 516 Node node = (parent.getOwnerDocument()).createElement(el.toName()); 517 node.appendChild(parent.getOwnerDocument().createTextNode(el.toValue().trim())); 518 parent.appendChild(node); 519 } 520 } 521 } 522 523 public static void toXML (Node parent,XMLAttribute el) { 524 if (!el.isEmpty() || el.isRequired()) { 525 if (parent!=null) { 526 Attr node = (parent.getOwnerDocument()).createAttribute(el.toName()); 527 node.setValue(el.toValue().trim()); 528 ((Element ) parent).setAttributeNode(node); 529 } 530 } 531 } 532 533 public static String getNameSpacePrefix (Node node) { 534 String nameSpacePrefix=node.getPrefix(); 535 if (nameSpacePrefix!=null) { 536 nameSpacePrefix+=":"; 537 } else { 538 nameSpacePrefix=""; 539 } 540 return nameSpacePrefix; 541 } 542 543 public static Node getChildByName(Node parent,String childName) { 544 NodeList children = parent.getChildNodes(); 545 for (int i = 0; i < children.getLength(); ++i) { 546 Node child = (Node ) children.item(i); 547 if (child.getNodeName().equals(childName)) { 548 return child; 549 } 550 } 551 return null; 552 } 553 554 public static String getId (Node node) { 555 try { 556 NamedNodeMap nnm=node.getAttributes(); 557 Node attrib=nnm.getNamedItem("Id"); 558 Object ID; 559 if (attrib.hasChildNodes()) { 560 ID=attrib.getChildNodes().item(0).getNodeValue(); 561 } else { 562 ID=attrib.getNodeValue(); 563 } 564 return ID.toString(); 565 } catch (Exception ex) { 566 return ""; 567 } 568 } 569 570 public static String getContent (Node node,boolean omitXMLDeclaration) { 571 try { 572 ByteArrayOutputStream baos=new ByteArrayOutputStream (); 573 574 TransformerFactory tFactory = 576 TransformerFactory.newInstance(); 577 Transformer transformer = tFactory.newTransformer(); 578 transformer.setOutputProperty("indent","yes"); 579 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); 580 transformer.setOutputProperty("encoding","UTF-8"); 581 if (omitXMLDeclaration) { 582 transformer.setOutputProperty("omit-xml-declaration","yes"); 583 } 584 585 DOMSource source = new DOMSource (node); 586 StreamResult result = new StreamResult (baos); 587 transformer.transform(source,result); 588 589 String cont=baos.toString("UTF8"); 590 591 baos.close(); 592 return cont; 593 } catch (Exception ex) { 594 return ""; 595 } 596 } 597 598 public static String getChildNodesContent (Node node) { 599 String txt=""; 600 if (node!=null) { 601 if (node.hasChildNodes()) { 602 txt=XMLUtil.getContent(node,true); 603 try { 604 Node fc=node.getFirstChild(); 605 String fcnc=XMLUtil.getContent(fc,true); 606 String closedTag="</"+node.getNodeName()+">"; 607 if (fcnc.trim().length()>0) { 608 fcnc=fcnc.trim(); 609 } 610 611 int i1,i2; 612 i1=txt.indexOf(fcnc); 613 i2=txt.lastIndexOf(closedTag); 614 txt=txt.substring(i1,i2).trim(); 615 } catch (Exception ex) { 616 NodeList nl=node.getChildNodes(); 617 txt=""; 618 try { 619 for (int i=0; i<nl.getLength(); i++) { 620 Node sn=nl.item(i); 621 if (sn instanceof Element ) { 622 txt+=XMLUtil.getContent(sn,true); 623 } else { 624 String nv=sn.getNodeValue(); 625 if (i>0) { 627 txt+=nv.substring(1); 628 } else if (i==0 && nv.trim().length()==0) { 629 continue; 630 } else { 631 txt+=nv; 632 } 633 } 634 } 635 } catch (Exception ex2){} 636 } 637 } 638 } 639 return txt; 640 } 641 642 public static String getShortClassName (String fullClassName) { 643 int lastDot=fullClassName.lastIndexOf("."); 644 if (lastDot>=0) { 645 return fullClassName.substring(lastDot+1,fullClassName.length()); 646 } 647 return fullClassName; 648 } 649 650 public static String getExternalPackageId (String extPkgHref) { 651 int indBSL=extPkgHref.lastIndexOf("\\"); 653 int indSL=extPkgHref.lastIndexOf("/"); 654 int indDotXPDL=extPkgHref.lastIndexOf(".xpdl"); 655 if (indSL!=-1 || indBSL!=-1) { 656 int ind=indSL; 657 if (indBSL>indSL) { 658 ind=indBSL; 659 } 660 extPkgHref=extPkgHref.substring(indSL+1); 661 } 662 if (indDotXPDL!=-1) { 663 extPkgHref=extPkgHref.substring(0,extPkgHref.length()-5); 664 } 665 return extPkgHref; 667 } 668 669 public static Node parseSchemaNode (String toParse,boolean isFile) { 670 Document document=null; 671 672 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 673 factory.setValidating(false); 674 675 try { 677 ParsingErrors pErrors=new ParsingErrors(); 678 679 DocumentBuilder parser = factory.newDocumentBuilder(); 680 parser.setErrorHandler(pErrors); 681 if (isFile) { 683 File f=new File (toParse); 684 if (!f.exists()) { 685 throw new Exception (); 686 } else { 688 document=parser.parse(new InputSource (new FileInputStream (f))); } 690 } else { 691 document=parser.parse(new InputSource (new StringReader (toParse))); 692 } 693 694 695 Set errorMessages = pErrors.getErrorMessages(); 696 if (errorMessages.size()>0) { 697 System.err.println("Errors in schema type"); 698 } 699 } catch (Exception ex) { 700 System.err.println("Fatal error while parsing xml schema document"); 701 return null; 702 } 703 if (document!=null) { 704 return document.getDocumentElement(); 705 } else { 706 return null; 707 } 708 } 709 710 public static String stringifyExtendedAttributes(ExtendedAttributes extAttribs) throws Exception { 711 try { 712 ExtendedAttributes easclone=(ExtendedAttributes)extAttribs.clone(); 713 easclone.setParent(null); 714 Iterator it=easclone.toElements().iterator(); 715 while (it.hasNext()) { 716 ExtendedAttribute ea=(ExtendedAttribute)it.next(); 717 ea.setParent(null); 718 ea.get("Name").setParent(null); 719 ea.get("Value").setParent(null); 720 } 721 722 return XMLUtil.getExtendedAttributesString(easclone); 723 } catch (Throwable thr) { 726 throw new Exception ("Can't stringify extended attributes, error="+thr.getMessage()+" !"); 727 } 728 } 729 730 public static ExtendedAttributes destringyfyExtendedAttributes(String extAttribs) throws Exception { 731 ExtendedAttributes extAttr=null; 732 if (extAttribs != null && !extAttribs.trim().equals("")) { 733 try { 734 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 735 factory.setNamespaceAware(true); 736 737 DocumentBuilder parser = factory.newDocumentBuilder(); 738 739 Document document=null; 740 document=parser.parse(new InputSource (new StringReader (extAttribs))); 741 742 extAttr=new ExtendedAttributes(null); 743 if (document!=null) { 744 fromXML(document.getDocumentElement(),extAttr); 745 } 746 747 return extAttr; 750 } catch (Throwable thr) { 751 thr.printStackTrace(); 752 throw new Exception ("Failed to destringify extended attributes, error="+thr.getMessage()+" !"); 753 } 754 } 755 756 return extAttr; 757 } 758 759 public static Node parseExtendedAttributeContent (String toParse) { 760 Document document=null; 761 762 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 763 factory.setValidating(false); 764 765 try { 766 ParsingErrors pErrors=new ParsingErrors(); 767 768 DocumentBuilder parser = factory.newDocumentBuilder(); 769 parser.setErrorHandler(pErrors); 770 toParse="<ExtAttribsAddition>"+toParse+"</ExtAttribsAddition>"; 773 document=parser.parse(new InputSource (new StringReader (toParse))); 774 Set errorMessages = pErrors.getErrorMessages(); 775 if (errorMessages.size()>0) { 776 System.err.println("Errors in ext attribs complex content"); 777 } 778 } catch (Exception ex) { 779 System.err.println("Fatal error while parsing ext. attributes complex content "+toParse); 780 return null; 781 } 782 if (document!=null) { 783 return document.getDocumentElement(); 784 } else { 785 return null; 786 } 787 788 } 789 790 public static String getExtendedAttributeValue(String [][] extendedAttributes, String extAttrName) { 791 if (extendedAttributes != null) { 792 for (int i = 0; i < extendedAttributes.length; i++) { 793 if (extendedAttributes[i][0].equals(extAttrName)) { 794 return extendedAttributes[i][1]; 795 } 796 } 797 } 798 return null; 799 } 800 801 public static Package getPackage(XMLElement el) { 802 while (!(el instanceof Package )) { 803 el = el.getParent(); 804 if (el == null) 805 break; 806 } 807 return (Package ) el; 808 } 809 810 public static WorkflowProcess getWorkflowProcess(XMLElement el) { 811 while (!(el instanceof WorkflowProcess)) { 812 el = el.getParent(); 813 if (el == null) 814 break; 815 } 816 return (WorkflowProcess) el; 817 } 818 819 public static Activity getActivity(XMLElement el) { 820 while (!(el instanceof Activity)) { 821 el = el.getParent(); 822 if (el == null) 823 break; 824 } 825 return (Activity) el; 826 } 827 828 public static Join getJoin (Activity act) { 829 Join j=null; 830 TransitionRestrictions trs=act.getTransitionRestrictions(); 831 if (trs.size()>0) { 832 j=((TransitionRestriction)trs.get(0)).getJoin(); 833 } 834 return j; 835 } 836 837 public static Split getSplit (Activity act) { 838 Split s=null; 839 TransitionRestrictions trs=act.getTransitionRestrictions(); 840 if (trs.size()>0) { 841 s=((TransitionRestriction)trs.get(0)).getSplit(); 842 } 843 return s; 844 } 845 846 public static Set getOutgoingTransitions (Activity act) { 847 Set s=new HashSet (); 848 Iterator it=((Transitions)((XMLCollectionElement)act.getParent().getParent()).get("Transitions")).toElements().iterator(); 849 while (it.hasNext()) { 850 Transition t=(Transition)it.next(); 851 if (t.getFrom().equals(act.getId())) { 852 s.add(t); 853 } 854 } 855 return s; 856 } 857 858 public static Set getExceptionalOutgoingTransitions (Activity act) { 859 Set s=new HashSet (); 860 Iterator it=((Transitions)((XMLCollectionElement)act.getParent().getParent()).get("Transitions")).toElements().iterator(); 861 while (it.hasNext()) { 862 Transition t=(Transition)it.next(); 863 if (t.getFrom().equals(act.getId())) { 864 String ct=t.getCondition().getType(); 865 if (ct.equals(XPDLConstants.CONDITION_TYPE_EXCEPTION) || 866 ct.equals(XPDLConstants.CONDITION_TYPE_DEFAULTEXCEPTION)) { 867 s.add(t); 868 } 869 } 870 } 871 return s; 872 } 873 874 public static Set getNonExceptionalOutgoingTransitions (Activity act) { 875 Set s=new HashSet (); 876 Iterator it=((Transitions)((XMLCollectionElement)act.getParent().getParent()).get("Transitions")).toElements().iterator(); 877 while (it.hasNext()) { 878 Transition t=(Transition)it.next(); 879 if (t.getFrom().equals(act.getId())) { 880 String ct=t.getCondition().getType(); 881 if (!(ct.equals(XPDLConstants.CONDITION_TYPE_EXCEPTION) || 882 ct.equals(XPDLConstants.CONDITION_TYPE_DEFAULTEXCEPTION))) { 883 s.add(t); 884 } 885 } 886 } 887 return s; 888 } 889 890 public static Set getIncomingTransitions (Activity act) { 891 Set s=new HashSet (); 892 Iterator it=((Transitions)((XMLCollectionElement)act.getParent().getParent()).get("Transitions")).toElements().iterator(); 893 while (it.hasNext()) { 894 Transition t=(Transition)it.next(); 895 if (t.getTo().equals(act.getId())) { 896 s.add(t); 897 } 898 } 899 return s; 900 } 901 902 public static Activity getFromActivity (Transition t) { 903 return ((Activities)((XMLCollectionElement)t.getParent().getParent()).get("Activities")).getActivity(t.getFrom()); 904 } 905 906 public static Activity getToActivity (Transition t) { 907 return ((Activities)((XMLCollectionElement)t.getParent().getParent()).get("Activities")).getActivity(t.getTo()); 908 } 909 910 913 public static boolean isIdValid (String id) { 914 return XMLChar.isValidNmtoken(id); 915 } 916 917 918 public static WorkflowProcess getSubflowProcess (XMLInterface xmlInterface,Activity sbflwAct) { 919 if (sbflwAct.getActivityType()!=XPDLConstants.ACTIVITY_TYPE_SUBFLOW) return null; 920 Package pkg=XMLUtil.getPackage(sbflwAct); 921 SubFlow s=sbflwAct.getActivityTypes().getImplementation().getImplementationTypes().getSubFlow(); 922 String subflowID=s.getId(); 923 924 WorkflowProcess wp=pkg.getWorkflowProcess(subflowID); 925 if (wp==null) { 926 List l=XMLUtil.getAllExternalPackageIds(xmlInterface, pkg); 927 Iterator it=l.iterator(); 928 while (it.hasNext()) { 929 Package p=xmlInterface.getPackageById((String )it.next()); 930 if (p!=null) { 931 wp=p.getWorkflowProcess(subflowID); 932 if (wp!=null) { 933 break; 934 } 935 } 936 } 937 } 938 return wp; 940 } 941 942 public static List getAllExternalPackageIds (XMLInterface xmli,Package pkg) { 943 List l=new ArrayList (); 944 List workingList=new ArrayList (pkg.getExternalPackageIds()); 945 Iterator it=workingList.iterator(); 946 while (it.hasNext()) { 947 Package p=xmli.getPackageById((String )it.next()); 948 if (p!=null) { 949 l.add(p.getId()); 950 l.addAll(getAllExternalPackageIds(xmli, p)); 951 } 952 } 953 return l; 954 } 955 956 963 public static Set getSplitOrJoinActivities (Collection acts,int sOrJ) { 964 Set sOrJactivities=new HashSet (); 965 if (acts==null) return sOrJactivities; 966 Iterator it=acts.iterator(); 967 while (it.hasNext()) { 968 Activity act=(Activity)it.next(); 969 Iterator iter; 970 if (sOrJ==0) { 971 iter=getOutgoingTransitions(act).iterator(); 972 } else { 973 iter=getIncomingTransitions(act).iterator(); 974 } 975 int noOfTrans=0; 976 while (iter.hasNext()) { 977 Transition t = (Transition)iter.next(); 978 noOfTrans++; 979 } 980 if (noOfTrans>1) { 981 sOrJactivities.add(act); 982 } 983 } 984 985 return sOrJactivities; 986 } 987 988 995 public static Set getBlockActivities(XMLComplexElement wpOrAs,boolean recursivly) { 996 Collection allActs=((Activities)wpOrAs.get("Activities")).toElements(); 997 Set bas=new HashSet (); 998 Iterator it=allActs.iterator(); 999 Activity act; 1000 while (it.hasNext()) { 1001 act=(Activity)it.next(); 1002 BlockActivity ba=act.getActivityTypes().getBlockActivity(); 1003 if (ba!=null) { 1004 bas.add(act); 1005 if (!recursivly) continue; 1006 ActivitySets ass=getWorkflowProcess(act).getActivitySets(); 1007 String asId=ba.getBlockId(); 1008 ActivitySet as=ass.getActivitySet(asId); 1009 if (as!=null) { 1010 bas.addAll(getBlockActivities(as,true)); 1011 } 1012 } 1013 } 1014 return bas; 1015 } 1016 1017 1018 1024 public static int getConformanceClassNo (String conformanceClass) { 1025 if (conformanceClass.equals(XPDLConstants.GRAPH_CONFORMANCE_NON_BLOCKED)) { 1026 return 0; 1027 } else if (conformanceClass.equals(XPDLConstants.GRAPH_CONFORMANCE_LOOP_BLOCKED)) { 1028 return 1; 1029 } else if (conformanceClass.equals(XPDLConstants.GRAPH_CONFORMANCE_FULL_BLOCKED)) { 1030 return 2; 1031 } else { 1032 return -1; 1033 } 1034 } 1035 1036 1039 public static String fileToString (String fileName) { 1040 if (fileName != null) { 1041 byte[] utf8Bytes; 1043 String sFile = new String (); 1044 try { 1046 FileInputStream fis=new FileInputStream (fileName); 1047 int noOfBytes=fis.available(); 1048 if (noOfBytes>0) { 1049 utf8Bytes=new byte[noOfBytes]; 1050 fis.read(utf8Bytes); 1051 sFile=new String (utf8Bytes,"UTF8"); 1052 } 1053 } 1054 catch (Exception ex) { 1055 return null; 1056 } 1057 return sFile; 1058 } 1059 return null; 1060 } 1061 1063 1064 public static String getCurrentDateAndTime () { 1065 String dateSeparator="-"; 1066 String timeSeparator=":"; 1067 Calendar cal=new GregorianCalendar (); 1068 String dateTime=""; 1069 dateTime=dateTime+String.valueOf(cal.get(Calendar.YEAR))+dateSeparator; 1070 int mnth=cal.get(Calendar.MONTH)+1; 1071 if (mnth<10) { 1072 dateTime=dateTime+"0"; 1073 } 1074 dateTime=dateTime+String.valueOf(mnth)+dateSeparator; 1075 int dayOfMnth=cal.get(Calendar.DAY_OF_MONTH); 1076 if (dayOfMnth<10) { 1077 dateTime=dateTime+"0"; 1078 } 1079 dateTime=dateTime+String.valueOf(dayOfMnth)+" "; 1080 int hr=cal.get(Calendar.HOUR_OF_DAY); 1081 int ampm=cal.get(Calendar.AM_PM); 1082 if (ampm==Calendar.PM && hr<12) { 1083 hr+=12; 1084 } 1085 if (hr<10) { 1086 dateTime=dateTime+"0"; 1087 } 1088 dateTime=dateTime+String.valueOf(hr)+timeSeparator; 1089 int min=cal.get(Calendar.MINUTE); 1090 if (min<10) { 1091 dateTime=dateTime+"0"; 1092 } 1093 dateTime=dateTime+String.valueOf(min)+timeSeparator; 1094 int sec=cal.get(Calendar.SECOND); 1095 if (sec<10) { 1096 dateTime=dateTime+"0"; 1097 } 1098 dateTime=dateTime+String.valueOf(sec); 1099 1100 return dateTime; 1101 } 1102 1103 1109 public static int checkParameterMatching (FormalParameters fps,ActualParameters aps) { 1110 if (fps==null || aps==null || fps.size()!=aps.size()) { 1111 return 1; 1112 } 1113 1114 for (int i=0; i<fps.size(); i++) { 1115 FormalParameter fp=(FormalParameter)fps.get(i); 1116 ActualParameter ap=(ActualParameter)aps.get(i); 1117 1118 String fpMode=fp.getMode(); 1119 1120 if (fpMode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_IN)) continue; 1123 1124 DataType fpdt=fp.getDataType(); 1126 DataTypes fpdtt=fpdt.getDataTypes(); 1127 XMLElement fpType=fpdtt.getChoosen(); 1128 1129 Map idToDFOrFP=XMLUtil.getWorkflowProcess(aps).getAllVariables(); 1131 String apWRD=ap.toValue(); 1132 XMLCollectionElement ce=(XMLCollectionElement)idToDFOrFP.get(apWRD); 1133 if (ce==null) { 1136 return 2; 1137 } 1138 XMLElement apType=null; 1139 DataType apdt=(DataType)ce.get("DataType"); 1140 DataTypes apdtt=apdt.getDataTypes(); 1141 apType=apdtt.getChoosen(); 1142 1143 if (fpType.getClass().equals(apType.getClass())) { 1144 if (fpType instanceof BasicType) { 1146 String fpAT=((BasicType)fpType).getType(); 1147 String apAT=((BasicType)apType).getType(); 1148 if (!fpAT.equals(apAT)) { 1149 return 2; 1150 } 1151 } 1152 if (fpType instanceof EnumerationType) { 1154 if (((EnumerationType)fpType).size()!=((EnumerationType)apType).size()) { 1156 return 2; 1157 } 1158 for (int j=0; j<((EnumerationType)fpType).size(); j++) { 1160 EnumerationValue evFP=(EnumerationValue)((EnumerationType)fpType).get(j); 1161 EnumerationValue evAP=(EnumerationValue)((EnumerationType)apType).get(j); 1162 if (!evFP.getName().equals(evAP.getName())) { 1163 return 2; 1164 } 1165 } 1166 } 1167 if (fpType instanceof DeclaredType) { 1169 if (!((DeclaredType)fpType).getId(). 1170 equals(((DeclaredType)apType).getId())) { 1171 return 2; 1172 } 1173 } 1174 1175 } else { 1176 return 2; 1177 } 1178 } 1179 return 0; 1180 } 1181 1182 public static String replaceBackslashesWithSlashes (String repBS) { 1183 if (repBS!=null) { 1184 int ind=-1; 1185 while ((ind=repBS.indexOf("\\"))!=-1) { 1186 repBS=repBS.substring(0,ind)+"/"+repBS.substring(ind+1); 1187 } 1188 } 1189 return repBS; 1190 } 1191 1192 public static String getExtendedAttributesString (ExtendedAttributes eas) throws Exception { 1193 Document document = null; 1194 1195 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 1196 DocumentBuilder dbuilder = dbf.newDocumentBuilder(); 1197 document = dbuilder.newDocument(); 1198 Node eac=document.createElement("EAC"); 1199 toXML(eac,eas); 1200 1201 String cont=XMLUtil.getContent(eac.getFirstChild(),true); 1202 return XMLUtil.getContent(eac.getFirstChild(),true); 1203 } 1204 1205 public static String getExtendedAttributesString (Node node) { 1206 String nameSpacePrefix=node.getPrefix(); 1207 if (nameSpacePrefix!=null) { 1208 nameSpacePrefix+=":"; 1209 } else { 1210 nameSpacePrefix=""; 1211 } 1212 Node eas=getChildByName(node,nameSpacePrefix+"ExtendedAttributes"); 1213 return XMLUtil.getContent(eas,true); 1214 } 1215 1216 public static Set getStartingActivities (XMLCollectionElement procOrASDef) { 1217 Activities acts=((Activities)procOrASDef.get("Activities")); 1218 Set starts=new HashSet (); 1219 Iterator it=acts.toElements().iterator(); 1220 Transitions ts=(Transitions)procOrASDef.get("Transitions"); 1221 while (it.hasNext()) { 1222 Activity act=(Activity)it.next(); 1223 Set trs=getIncomingTransitions(act); 1224 if (trs.size()==0) { 1226 starts.add(act); 1227 } else if (trs.size()==1) { 1229 Transition t=(Transition)trs.toArray()[0]; 1230 if (t.getFrom().equals(t.getTo())) { 1231 starts.add(act); 1232 } 1233 } 1234 } 1235 return starts; 1236 } 1237 1238 public static Set getEndingActivities (XMLCollectionElement procOrASDef) { 1239 Activities acts=((Activities)procOrASDef.get("Activities")); 1240 Set ends=new HashSet (); 1241 Iterator it=acts.toElements().iterator(); 1242 Transitions ts=(Transitions)procOrASDef.get("Transitions"); 1243 while (it.hasNext()) { 1244 Activity act=(Activity)it.next(); 1245 Set trs=getNonExceptionalOutgoingTransitions(act); 1246 if (trs.size()==0) { 1248 ends.add(act); 1249 } else if (trs.size()==1) { 1251 Transition t=(Transition)trs.toArray()[0]; 1252 if (t.getFrom().equals(t.getTo())) { 1253 ends.add(act); 1254 } 1255 } 1256 } 1257 1258 return ends; 1259 } 1260 1261 1262 1266 public static List getResponsibles (WorkflowProcess wp) { 1267 List resp=new ArrayList (); 1268 RedefinableHeader rh=wp.getRedefinableHeader(); 1269 Responsibles rsps=rh.getResponsibles(); 1270 Iterator it=rsps.toElements().iterator(); 1271 while (it.hasNext()) { 1272 Responsible rsp=(Responsible)it.next(); 1273 if (!resp.contains(rsp)) { 1274 resp.add(rsp); 1275 } 1276 } 1277 rh=getPackage(wp).getRedefinableHeader(); 1279 rsps=rh.getResponsibles(); 1280 it=rsps.toElements().iterator(); 1281 while (it.hasNext()) { 1282 Responsible rsp=(Responsible)it.next(); 1283 if (!resp.contains(rsp)) { 1284 resp.add(rsp); 1285 } 1286 } 1287 return resp; 1288 } 1289 1290 1298 public static boolean isANDTypeSplitOrJoin (Activity act,int sOrJ) { 1299 String sjType = XPDLConstants.JOIN_SPLIT_TYPE_XOR; 1301 if (sOrJ==0) { Split s=getSplit(act); 1303 if (s!=null) { 1304 sjType=s.getType(); 1305 } 1306 } else { Join j=getJoin(act); 1308 if (j!=null) { 1309 sjType=j.getType(); 1310 } 1311 } 1312 1313 if (sjType.equals(XPDLConstants.JOIN_SPLIT_TYPE_AND)) { 1314 return true; 1315 } else { 1316 return false; 1317 } 1318 } 1319 1320 public static boolean isSubflowSynchronous (Activity sbflwActivityDefinition) { 1321 String type = XPDLConstants.EXECUTION_SYNCHR; 1322 SubFlow subflow=sbflwActivityDefinition.getActivityTypes().getImplementation().getImplementationTypes().getSubFlow(); 1324 type = subflow.getExecution(); 1325 1326 if (type.equals(XPDLConstants.EXECUTION_ASYNCHR)) { 1327 return false; 1328 } else { 1329 return true; 1330 } 1331 } 1332 1333 public static int getStartMode (Activity act) { 1334 int ret=XPDLConstants.ACTIVITY_MODE_AUTOMATIC; 1335 StartMode startMode = act.getStartMode(); 1336 Object mode=startMode.getStartFinishModes().getChoosen(); 1337 if(mode instanceof Manual) { 1338 ret=XPDLConstants.ACTIVITY_MODE_MANUAL; 1339 } 1340 return ret; 1341 } 1342 1343 public static int getFinishMode (Activity act) { 1344 int ret=XPDLConstants.ACTIVITY_MODE_AUTOMATIC; 1345 FinishMode finishMode = act.getFinishMode(); 1346 Object mode=finishMode.getStartFinishModes().getChoosen(); 1347 if(mode instanceof Manual) { 1348 ret=XPDLConstants.ACTIVITY_MODE_MANUAL; 1349 } 1350 return ret; 1351 } 1352 1353 public static OutputStream packageToStream ( 1354 org.enhydra.shark.xpdl.elements.Package pkg,OutputStream os) { 1355 try { 1356 Document document = null; 1357 1358 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 1359 DocumentBuilder dbuilder = dbf.newDocumentBuilder(); 1360 document = dbuilder.newDocument(); 1361 1362 toXML(document,pkg); 1364 1365 TransformerFactory tFactory = 1367 TransformerFactory.newInstance(); 1368 Transformer transformer = tFactory.newTransformer(); 1369 transformer.setOutputProperty("indent","yes"); 1370 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); 1371 transformer.setOutputProperty("encoding","UTF-8"); 1372 DOMSource source = new DOMSource (document); 1373 StreamResult result = new StreamResult (os); 1374 transformer.transform(source,result); 1375 return os; 1376 } catch (Exception ex) { 1377 ex.printStackTrace(); 1378 return null; 1379 } 1380 } 1381 1382 public static List getOrderedOutgoingTransitions (Activity fromActDef, 1383 Set outTransitions) { 1384 Set otCopy=new HashSet (outTransitions); 1387 List orderedOutTransitions=new ArrayList (); 1388 Map trIdToTr=new HashMap (); 1389 Iterator it=outTransitions.iterator(); 1390 while (it.hasNext()) { 1391 Transition trans=(Transition)it.next(); 1392 trIdToTr.put(trans.getId(),trans); 1393 } 1394 Split s=getSplit(fromActDef); 1395 if (s!=null) { 1396 TransitionRefs trfs=s.getTransitionRefs(); 1397 Iterator trefs=trfs.toElements().iterator(); 1398 while (trefs.hasNext()) { 1399 TransitionRef tref=(TransitionRef)trefs.next(); 1400 Transition trans=(Transition)trIdToTr.get(tref.getId()); 1401 if (trans==null) continue; 1402 orderedOutTransitions.add(trans); 1403 otCopy.remove(trans); 1404 } 1405 } 1406 orderedOutTransitions.addAll(otCopy); 1409 return orderedOutTransitions; 1410 } 1411 1412 public static String createPath (String basedir,String relpath) { 1413 basedir=XMLUtil.convertToSystemPath(basedir); 1414 relpath=XMLUtil.convertToSystemPath(relpath); 1415 return (basedir+File.separator+relpath); 1416 } 1417 1418 public static String convertToSystemPath (String path) { 1419 char separatorChar=File.separatorChar; 1420 char charToReplace; 1421 if (separatorChar=='\\') { 1422 charToReplace='/'; 1423 } else { 1424 charToReplace='\\'; 1425 } 1426 1427 String systemPath=path.replace(charToReplace,separatorChar); 1428 return systemPath; 1429 } 1430 1431 1432 public static void main (String [] args) throws Throwable { 1433 long t1,t2,t3, ts, te; 1434 ts=System.currentTimeMillis(); 1435 boolean readExt=false; 1436 readExt=new Boolean (args[0]).booleanValue(); 1437 for (int i=1; i<args.length; i++) { 1438 String inputFile=args[i]; 1439System.out.println("Handling file "+inputFile); 1440 t1=System.currentTimeMillis(); 1441 XMLInterface xmli=new XMLInterfaceForJDK13(); 1442 Package pkg=readFromFile(xmli,inputFile,readExt); 1443Package p1=pkg; 1444 PackageValidator pv=new PackageValidator(xmli,pkg,true,readExt,true,true); 1445 System.out.println("VALIDATING ..."); 1446 boolean valid=pv.validateAll(true); 1447 System.out.println("VALID="+valid); 1448 t2=System.currentTimeMillis(); 1449 pkg.setReadOnly(true); 1450 writeToFile(xmli,inputFile+"r", pkg); 1451 t3=System.currentTimeMillis(); 1452 pkg=clonePackage(pkg); 1453 pkg.initCaches(); 1455 Package p2=pkg; 1457 long t4=System.currentTimeMillis(); 1458 writeToFile(xmli,inputFile+"rr", pkg); 1459 long t5=System.currentTimeMillis(); 1460 pkg=clonePackageBySerialization(pkg); 1461 System.out.println("P1=P2="+p1.equals(p2)+", P2=P3="+p2.equals(pkg)); 1462 long t6=System.currentTimeMillis(); 1463 writeToFile(xmli,inputFile+"rrr", pkg); 1464 long t7=System.currentTimeMillis(); 1465 System.out.println("TOverall ="+(t6-t1)); 1466 System.out.println("TOpenPKG "+(t2-t1)); 1467 System.out.println("TSavePKG1 ="+(t3-t2)); 1468 System.out.println("TClonePkg1 ="+(t4-t3)); 1469 System.out.println("TSavePKG2 ="+(t5-t3)); 1470 System.out.println("TClonePkg2 ="+(t6-t5)); 1471 System.out.println("TSavePKG4 ="+(t7-t6)); 1472 } 1473 te=System.currentTimeMillis(); 1474 System.out.println("Handling of "+(args.length-1)+" XPDLs lasted "+((te-ts)/1000)+" sec"); 1475 } 1476 1477 public static Package readFromFile (XMLInterface xmli,String inputFile,boolean readExt) throws Exception { 1478 return xmli.openPackage(inputFile, readExt); 1479 } 1480 1481 public static void writeToFile (XMLInterface xmli,String outputFile,Package pkg) throws Exception { 1482 System.out.println("PKGEPS="+pkg.getExternalPackageIds()); 1483 Document document = null; 1484 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 1485 DocumentBuilder dbuilder = dbf.newDocumentBuilder(); 1486 document = dbuilder.newDocument(); 1487 FileOutputStream os; 1491 os=new FileOutputStream (outputFile); 1494 1495 toXML(document,pkg); 1497 1498 TransformerFactory tFactory = 1500 TransformerFactory.newInstance(); 1501 Transformer transformer = tFactory.newTransformer(); 1502 transformer.setOutputProperty("indent","yes"); 1503 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); 1504 transformer.setOutputProperty("encoding","UTF8"); 1505 DOMSource source = new DOMSource (document); 1506 StreamResult result = new StreamResult (os); 1507 transformer.transform(source,result); 1508 1509 os.close(); 1510 } 1511 1512 public static Package clonePackage (Package pkg) throws Exception { 1513 Package cloned=(Package )pkg.clone(); 1514 return cloned; 1515 } 1516 1517 public static Package clonePackageBySerialization (Package pkg) throws Exception { 1518 byte[] ser=serialize(pkg); 1519 Package cloned=(Package )deserialize(ser); 1520 return cloned; 1521 } 1522 1523 public static byte[] serialize(Object obj) throws Exception { 1524 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 1526 ObjectOutputStream oout = new ObjectOutputStream (bout); 1527 oout.writeObject(obj); 1528 oout.flush(); 1529 byte array[] = bout.toByteArray(); 1530 oout.close(); 1531 bout.close(); 1532 return array; 1534 } 1535 1536 public static Object deserialize(byte[]array) throws Exception { 1537 ObjectInputStream rin = new ObjectInputStream 1539 (new ByteArrayInputStream (array)); 1540 Object obj = rin.readObject(); 1541 rin.close(); 1542 return obj; 1544 } 1545 1546} 1547 1548 | Popular Tags |