| 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 i
|