KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > PackageValidator


1 package org.enhydra.jawe.xml;
2
3 import java.util.*;
4 import java.io.*;
5
6 import org.enhydra.jawe.xml.*;
7 import org.enhydra.jawe.xml.elements.*;
8 import org.enhydra.jawe.xml.elements.Package;
9
10 import javax.xml.parsers.*;
11 import javax.xml.transform.*;
12 import javax.xml.transform.dom.*;
13 import javax.xml.transform.stream.*;
14 import org.w3c.dom.*;
15
16 /**
17  * Validates an XPDL package.
18  */

19 public class PackageValidator {
20
21    protected static final String JavaDoc CURRENT_XPDL_VERSION="1.0";
22
23    protected Package JavaDoc pkg;
24    protected boolean checkingByJaWE;
25    protected boolean checkingForJaWE;
26    protected boolean getExistingSchemaValidationErrors;
27    protected boolean checkExternalPackages;
28    protected boolean allowUndefinedStart;
29    protected boolean allowUndefinedEnd;
30
31    protected Map xpdlSchemaValidationErrors=new HashMap();
32    protected Map graphsConnectionErrors=new HashMap();
33    protected Map basicGraphConnectionErrors=new HashMap();
34    protected Map graphsConformanceErrors=new HashMap();
35    protected Map basicGraphsConformanceErrors=new HashMap();
36    protected Map logicErrors=new HashMap();
37    protected Map basicLogicErrors=new HashMap();
38
39    protected String JavaDoc encoding;
40
41    public Map getXPDLSchemaValidationErrors () {
42       return xpdlSchemaValidationErrors;
43    }
44
45    public Map getGraphsConnectionErrors (XMLComplexElement pkgOrWpOrAs) {
46       return (Map)graphsConnectionErrors.get(pkgOrWpOrAs);
47    }
48
49    public String JavaDoc getBasicGraphConnectionError (XMLComplexElement pkgOrWpOrAs) {
50       return (String JavaDoc)basicGraphConnectionErrors.get(pkgOrWpOrAs);
51    }
52
53    public Map getGraphConformanceErrors (XMLComplexElement pkgOrWpOrAs) {
54       return (Map)graphsConformanceErrors.get(pkgOrWpOrAs);
55    }
56
57    public List getBasicGraphConformanceErrors (XMLComplexElement pkgOrWpOrAs) {
58       return (List)basicGraphsConformanceErrors.get(pkgOrWpOrAs);
59    }
60
61    public Map getLogicErrors (XMLComplexElement pkgOrWpOrAs) {
62       return (Map)logicErrors.get(pkgOrWpOrAs);
63    }
64
65    public String JavaDoc getBasicLogicError (XMLComplexElement pkgOrWpOrAs) {
66       return (String JavaDoc)basicLogicErrors.get(pkgOrWpOrAs);
67    }
68
69    public PackageValidator(Package JavaDoc pkg,boolean checkingByJaWE,
70                            boolean checkingForJaWE, boolean getExistingSchemaValidationErrors,
71                            boolean checkExternalPackages,boolean allowUndefinedStart,
72                            boolean allowUndefinedEnd,String JavaDoc enc) {
73       this.pkg=pkg;
74       this.checkingByJaWE=checkingByJaWE;
75       this.checkingForJaWE=checkingForJaWE;
76       this.getExistingSchemaValidationErrors=getExistingSchemaValidationErrors;
77       this.checkExternalPackages=checkExternalPackages;
78       this.allowUndefinedStart=allowUndefinedStart;
79       this.allowUndefinedEnd=allowUndefinedEnd;
80       this.encoding=enc;
81    }
82
83    public boolean validateAll (boolean fullCheck) {
84       try {
85          boolean isValid=validateAgainstXPDLSchema();
86          if (fullCheck || isValid) {
87             isValid=checkPackage(fullCheck) && isValid;
88          }
89          if (fullCheck || isValid) {
90             isValid=checkGraphConnections(fullCheck) && isValid;
91          }
92          if (fullCheck || isValid) {
93             isValid=checkGraphConformance(fullCheck) && isValid;
94          }
95
96          return isValid;
97       } catch (Exception JavaDoc ex) {
98          //ex.printStackTrace();
99
return false;
100       }
101    }
102
103    //********************* validation against XPDL schema *********************
104
public boolean validateAgainstXPDLSchema () {
105       if (getExistingSchemaValidationErrors) {
106          xpdlSchemaValidationErrors=pkg.getXMLInterface().getParsingErrorMessages();
107          if (xpdlSchemaValidationErrors.size()>0) {
108             return false;
109          } else {
110             return true;
111          }
112       }
113       try {
114          Document document = null;
115
116          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
117          DocumentBuilder dbuilder = dbf.newDocumentBuilder();
118          document = dbuilder.newDocument();
119          ByteArrayOutputStream baos=new ByteArrayOutputStream();
120
121          // The extended attributes for all package elements must
122
// be updated if current package is not externally
123
// referenced package.
124
//Save.updateExtendedAttributesForWorkflowProcesses();
125

126          // Here we get all document elements
127
pkg.toXML(document);
128
129          // Use a Transformer for output
130
TransformerFactory tFactory =
131             TransformerFactory.newInstance();
132          Transformer transformer = tFactory.newTransformer();
133          transformer.setOutputProperty("indent","yes");
134          transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
135          transformer.setOutputProperty("encoding",encoding);
136          DOMSource source = new DOMSource(document);
137          StreamResult result = new StreamResult(baos);
138          transformer.transform(source,result);
139
140          pkg.getXMLInterface().clearParserErrorMessages();
141          pkg.getXMLInterface().parseDocument(baos.toString(encoding),false);
142          baos.close();
143          xpdlSchemaValidationErrors=pkg.getXMLInterface().getParsingErrorMessages();
144          if (xpdlSchemaValidationErrors.size()>0) {
145             return false;
146          }
147       } catch (Exception JavaDoc ex) {
148          return false;
149       }
150       return true;
151
152    }
153    //********************* Logic checking **************************************
154
public boolean checkPackage (boolean fullCheck) {
155       Map les=new HashMap();
156       //Map les=new OrderedHashMap();
157
//Map les=new LinkedHashMap();
158
logicErrors.put(pkg,les);
159       basicLogicErrors.remove(pkg);
160
161       boolean isPackageValid=true;
162       boolean invalidId=false;
163       if (!isIdValid(pkg.get("Id").toString())) {
164          isPackageValid=false;
165          invalidId=true;
166          les.put(pkg,XMLUtil.getLanguageDependentString("ErrorPackageIdIsNotValid"));
167       }
168       if (fullCheck || isPackageValid) {
169          checkPackageHeader(fullCheck);
170       }
171       if (fullCheck || isPackageValid) {
172          isPackageValid=checkRedefinableHeader(pkg,fullCheck) && isPackageValid;;
173       }
174       if (fullCheck || isPackageValid) {
175          isPackageValid=checkConformanceClass(fullCheck) && isPackageValid;;
176       }
177       if (fullCheck || isPackageValid) {
178          isPackageValid=checkScript(fullCheck) && isPackageValid;;
179       }
180       if ((fullCheck || isPackageValid) && checkExternalPackages) {
181          isPackageValid=checkExternalPackages(fullCheck) && isPackageValid;;
182       }
183       if (fullCheck || isPackageValid) {
184          isPackageValid=checkCollection("TypeDeclarations",pkg,fullCheck) && isPackageValid;;
185       }
186       if (fullCheck || isPackageValid) {
187          isPackageValid=checkCollection("Participants",pkg,fullCheck) && isPackageValid;;
188       }
189       if (fullCheck || isPackageValid) {
190          isPackageValid=checkCollection("Applications",pkg,fullCheck) && isPackageValid;;
191       }
192       if (fullCheck || isPackageValid) {
193          isPackageValid=checkCollection("DataFields",pkg,fullCheck) && isPackageValid;;
194       }
195       boolean areProcessesValid=true;
196       if (fullCheck || isPackageValid) {
197          areProcessesValid=checkCollection("WorkflowProcesses",pkg,fullCheck);
198          isPackageValid=areProcessesValid && isPackageValid;;
199       }
200       if (!isPackageValid) {
201          if (invalidId) {
202             basicLogicErrors.put(pkg,XMLUtil.getLanguageDependentString("ErrorPackageIdIsNotValid"));
203          } else if (!areProcessesValid){
204             basicLogicErrors.put(pkg,XMLUtil.getLanguageDependentString("ErrorOneOrMoreProcessesHaveLogicErrors"));
205          } else {
206             basicLogicErrors.put(pkg,les.values().toArray()[0]);
207          }
208       }
209       return isPackageValid;
210    }
211
212    public boolean checkPackageHeader (boolean fullCheck) {
213       PackageHeader phdr=(PackageHeader)pkg.get("PackageHeader");
214       String JavaDoc xpdlv="XPDLVersion";
215       if (!phdr.get(xpdlv).toString().trim().equals(CURRENT_XPDL_VERSION)) {
216          Map les=getLogicErrors(pkg);
217          les.put(phdr,XMLUtil.getLanguageDependentString("ErrorInvalidXPDLVersion"));
218          return false;
219       } else {
220          return true;
221       }
222    }
223
224    public boolean checkRedefinableHeader (XMLComplexElement pkgOrWp,boolean fullCheck) {
225       boolean isValid=true;
226       //RedefinableHeader rh=(RedefinableHeader)pkg.get("RedefinableHeader");
227
RedefinableHeader rh=(RedefinableHeader)pkgOrWp.get("RedefinableHeader"); //Harald Meister: use argument instead of package
228

229       Iterator rspns=((Responsibles)rh.get("Responsibles")).toCollection().iterator();
230       while (rspns.hasNext()) {
231          Responsible r=(Responsible)rspns.next();
232          if (!(r.toValue() instanceof Participant)) {
233             isValid=false;
234             Map les=getLogicErrors(pkgOrWp);
235             les.put(rh,XMLUtil.getLanguageDependentString("ErrorOneOrMoreResponsibleParticipantsDoNotExist"));
236             break;
237          }
238       }
239       return isValid;
240    }
241
242    public boolean checkConformanceClass (boolean fullCheck) {
243       //ConformanceClass cc=(ConformanceClass)pkg.get("ConformanceClass");
244
return true;
245    }
246
247    public boolean checkScript(boolean fullCheck) {
248       //
249
return true;
250    }
251
252    public boolean checkExternalPackages(boolean fullCheck) {
253       boolean isValid=true;
254       Map les=getLogicErrors(pkg);
255       Iterator it=pkg.getAllExternalPackages().iterator();
256       while (it.hasNext() && (fullCheck || isValid)) {
257          Package JavaDoc p=(Package JavaDoc)it.next();
258          PackageValidator pv=new PackageValidator(p,false,true,
259                                                   getExistingSchemaValidationErrors,false,allowUndefinedStart,
260                                                   allowUndefinedEnd,this.encoding);
261          if (!pv.validateAll(false)) {
262             isValid=false;
263             if (les!=null) {
264                les.put(p,XMLUtil.getLanguageDependentString("ErrorInvalidExternalPackage"));
265             }
266          }
267       }
268       return isValid;
269    }
270
271    public boolean checkCollection (String JavaDoc colName,XMLComplexElement cOwner,boolean fullCheck) {
272       boolean isValid=true;
273       Iterator it=((XMLCollection)cOwner.get(colName)).
274          toCollection().iterator();
275       while (it.hasNext() && (fullCheck || isValid)) {
276          isValid=checkCollectionElement((XMLCollectionElement)it.next(),fullCheck) && isValid;
277       }
278       return isValid;
279    }
280
281    public boolean checkCollectionElement (XMLCollectionElement ce,boolean fullCheck) {
282       boolean isValid=true;
283       if (!isIdValid(ce.getID())) {
284          isValid=false;
285          XMLComplexElement firstOwner=ce.getCollection().getOwner();
286          Map les;
287          if (firstOwner instanceof Application) {
288             les=getLogicErrors(((Application)firstOwner).getCollection().getOwner());
289          } else {
290             les=getLogicErrors(firstOwner);
291          }
292          les.put(ce,XMLUtil.getLanguageDependentString("ErrorIdIsNotValid"));
293       }
294       if (fullCheck || isValid) {
295          if (!isUniqueId(ce.getCollection(),ce.getID())) {
296             isValid=false;
297             XMLComplexElement firstOwner=ce.getCollection().getOwner();
298             Map les;
299             if (firstOwner instanceof Application) {
300                les=getLogicErrors(((Application)firstOwner).getCollection().getOwner());
301             } else {
302                les=getLogicErrors(firstOwner);
303             }
304             String JavaDoc msg=(String JavaDoc)les.get(ce);
305             msg=prepareMessageString(msg);
306             msg=msg+XMLUtil.getLanguageDependentString("ErrorIdIsNotUnique");
307             les.put(ce,msg);
308          }
309       }
310       if (fullCheck || isValid) {
311          if (ce instanceof TypeDeclaration) {
312             isValid=checkTypeDeclaration((TypeDeclaration)ce,fullCheck) && isValid;
313          } else if (ce instanceof Participant) {
314             isValid=checkParticipant((Participant)ce,fullCheck) && isValid;
315          } else if (ce instanceof Application) {
316             isValid=checkApplication((Application)ce,fullCheck) && isValid;
317          } else if (ce instanceof DataField) {
318             isValid=checkDataField((DataField)ce,fullCheck) && isValid;
319          } else if (ce instanceof FormalParameter) {
320             isValid=checkFormalParameter((FormalParameter)ce,fullCheck) && isValid;
321          } else if (ce instanceof WorkflowProcess) {
322             isValid=checkWorkflowProcess((WorkflowProcess)ce,!checkingForJaWE) && isValid;
323          } else if (ce instanceof ActivitySet) {
324             isValid=checkActivitySet((ActivitySet)ce,fullCheck) && isValid;
325          } else if (ce instanceof Activity) {
326             isValid=checkActivity((Activity)ce,fullCheck) && isValid;
327          } else if (ce instanceof Transition) {
328             isValid=checkTransition((Transition)ce,fullCheck) && isValid;
329          }
330       }
331       return isValid;
332    }
333
334    public boolean checkTypeDeclaration(TypeDeclaration td,boolean fullCheck) {
335       //
336
return true;
337    }
338
339    public boolean checkParticipant (Participant p,boolean fullCheck) {
340       //
341
return true;
342    }
343
344    public boolean checkApplication (Application app,boolean fullCheck) {
345       boolean isValid=true;
346       if (((XMLComplexChoice)app.get("Choice")).getChoosen() instanceof FormalParameters) {
347          FormalParameters fps=(FormalParameters)((XMLComplexChoice)app.
348                                                     get("Choice")).getChoices()[0];
349          Iterator it=fps.toCollection().iterator();
350          while (it.hasNext() && (fullCheck || isValid)) {
351             isValid=checkCollectionElement((XMLCollectionElement)it.next(),fullCheck) && isValid;
352          }
353       }
354       return isValid;
355    }
356
357    public boolean checkDataField (DataField df,boolean fullCheck) {
358       //
359
return checkDataType(df,fullCheck);
360    }
361
362    public boolean checkFormalParameter (FormalParameter fp,boolean fullCheck) {
363       //
364
return checkDataType(fp,fullCheck);
365    }
366
367    public boolean checkDataType (XMLCollectionElement dfOrFp,boolean fullCheck) {
368       boolean isValid=true;
369
370       DataType xpdlType=(DataType)dfOrFp.get("DataType");
371       Object JavaDoc type=xpdlType.get("Type").toValue();
372       if (type instanceof DeclaredType) {
373          TypeDeclarations tds=(TypeDeclarations)pkg.get("TypeDeclarations");
374          TypeDeclaration td=(TypeDeclaration)tds.getDeclaredType(
375                                                                    ((DeclaredType)type).get("Id").toString());
376          if (td==null) {
377             isValid=false;
378             XMLComplexElement firstOwner=dfOrFp.getCollection().getOwner();
379             Map les;
380             if (dfOrFp instanceof DataField) {
381                les=getLogicErrors(firstOwner);
382             } else {
383                if (firstOwner instanceof Application) {
384                   les=getLogicErrors(((Application)firstOwner).getCollection().getOwner());
385                } else {
386                   les=getLogicErrors(firstOwner);
387                }
388             }
389             String JavaDoc msg=(String JavaDoc)les.get(dfOrFp);
390             msg=prepareMessageString(msg);
391             msg=msg+XMLUtil.getLanguageDependentString("ErrorNonExistingDeclaredTypeReference");
392             les.put(dfOrFp,msg);
393          }
394       }
395       return isValid;
396    }
397
398    public boolean checkWorkflowProcess (WorkflowProcess wp,boolean fullCheck) {
399       Map les=new HashMap();
400       //Map les=new OrderedHashMap();
401
//Map les=new LinkedHashMap();
402
logicErrors.put(wp,les);
403       basicLogicErrors.remove(wp);
404
405       boolean notDefined=false;
406       boolean isValid=checkProcessHeader(wp,fullCheck);
407       if (fullCheck || isValid) {
408          isValid=checkRedefinableHeader(wp,fullCheck) && isValid;
409       }
410       if (fullCheck || isValid) {
411          isValid=checkCollection("FormalParameters",wp,fullCheck) && isValid;
412       }
413       if (fullCheck || isValid) {
414          isValid=checkCollection("DataFields",wp,fullCheck) && isValid;
415       }
416       if (fullCheck || isValid) {
417          isValid=checkCollection("Participants",wp,fullCheck) && isValid;
418       }
419       if (fullCheck || isValid) {
420          isValid=checkCollection("Applications",wp,fullCheck) && isValid;
421       }
422       if (fullCheck || isValid) {
423          isValid=checkCollection("ActivitySets",wp,fullCheck) && isValid;
424       }
425       if (fullCheck || isValid) {
426          if (((Activities)wp.get("Activities")).toCollection().size()==0) {
427             isValid=false;
428             notDefined=true;
429             les.put(wp,XMLUtil.getLanguageDependentString("ErrorProcessIsNotDefined"));
430          } else {
431             isValid=checkCollection("Activities",wp,fullCheck) && isValid;
432          }
433       }
434       if (fullCheck || isValid) {
435          isValid=checkCollection("Transitions",wp,fullCheck) && isValid;
436       }
437       if (!isValid) {
438          basicLogicErrors.put(wp,les.values().toArray()[0]);
439          Map pkgles=getLogicErrors(pkg);
440          if (pkgles!=null) {
441             if (notDefined) {
442                pkgles.put(wp,XMLUtil.getLanguageDependentString("ErrorProcessIsNotDefined"));
443             } else {
444                pkgles.put(wp,XMLUtil.getLanguageDependentString("ErrorProcessContainsOneOrMoreLogicErrors"));
445             }
446          }
447       }
448       return isValid;
449    }
450
451    public boolean checkProcessHeader(WorkflowProcess wp,boolean fullCheck) {
452       //
453
return true;
454    }
455
456    public boolean checkActivitySet (ActivitySet as,boolean fullCheck) {
457       Map les=new HashMap();
458       //Map les=new OrderedHashMap();
459
//Map les=new LinkedHashMap();
460
logicErrors.put(as,les);
461       basicLogicErrors.remove(as);
462       boolean isValid=true;
463       boolean notDefined=false;
464       if (((Activities)as.get("Activities")).toCollection().size()==0) {
465          isValid=false;
466          notDefined=true;
467          les.put(as,XMLUtil.getLanguageDependentString("ErrorBlockActivityIsNotDefined"));
468       } else {
469          isValid=checkCollection("Activities",as,fullCheck);
470       }
471       if (fullCheck || isValid) {
472          isValid=checkCollection("Transitions",as,fullCheck) && isValid;
473       }
474       if (!isValid) {
475          basicLogicErrors.put(as,getLogicErrors(as).values().toArray()[0]);
476          Map wples=getLogicErrors(as.getOwnerProcess());
477          Activity blockActivity=findBlockActivity(as);
478          if (!(wples==null || blockActivity==null)) {
479             if (notDefined) {
480                wples.put(blockActivity,XMLUtil.getLanguageDependentString("ErrorBlockActivityIsNotDefined"));
481             } else {
482                wples.put(blockActivity,XMLUtil.getLanguageDependentString("ErrorInnerLogicError"));
483             }
484          } else if (wples!=null) {
485             wples.put(as,XMLUtil.getLanguageDependentString("ErrorBlockActivityIsNotDefined"));
486          }
487       }
488       return isValid;
489    }
490
491    public boolean checkActivity (Activity act,boolean fullCheck) {
492       // check performer
493
boolean isValid=checkActivityPerformer(act,fullCheck);
494
495       if (!(fullCheck || isValid)) {
496          return false;
497       }
498
499       // if this is a block activity
500
int type=act.getType();
501       switch (type) {
502          case 0: // Route type
503
break;
504          case 1: // No type
505
break;
506          case 2: // Tool type
507
isValid=checkActivityTools(act,fullCheck) && isValid;
508             break;
509          case 3: // SubFlow type
510
isValid=checkActivitySubFlow(act,fullCheck) && isValid;
511             break;
512          case 4: // Block type
513
isValid=checkActivityBlock(act,fullCheck) && isValid;
514             break;
515       }
516
517       if (!(fullCheck || isValid)) {
518          return false;
519       }
520
521       Transitions trans=(Transitions)act.getCollection().getOwner().get("Transitions");
522       Set outTrans=trans.getTransitions(act.getID(),-1);
523       Set inTrans=trans.getTransitions(act.getID(),1);
524
525       // check deadlines
526
isValid=checkActivityDeadlines(act,fullCheck) && isValid;
527       if (!(fullCheck || isValid)) {
528          return false;
529       }
530
531       Map les=getLogicErrors(act.getCollection().getOwner());
532       String JavaDoc msg=(String JavaDoc)les.get(act);
533       // Split type and no. of outgoing transitions
534
Split split=act.getSplit();
535       if (split.get("Type").toValue().toString().trim().length()==0 && outTrans.size()>1) {
536          isValid=false;
537          msg=prepareMessageString(msg);
538          msg=msg+XMLUtil.getLanguageDependentString("ErrorMultipleOutgoingTransitionsWithoutSplitTypeDefined");
539          les.put(act,msg);
540       }
541
542       if (!(fullCheck || isValid)) {
543          return false;
544       }
545
546       if (!checkingByJaWE) {
547          // TransitionRefs size must be the same as the one of outgoing transitions
548
TransitionRefs tRfs=(TransitionRefs)split.get("TransitionRefs");
549          if ((tRfs.size()!=outTrans.size()) && outTrans.size()>1 && !split.get("Type").toValue().toString().equals("AND")) {
550             isValid=false;
551             msg=prepareMessageString(msg);
552             msg=msg+XMLUtil.getLanguageDependentString("ErrorNumberOfActivitiesOutgoingTransitionsAndTransitionRefsIsNotSame");
553             les.put(act,msg);
554          }
555          if (!(fullCheck || isValid)) {
556             return false;
557          }
558          // TransitionRefs must refer to valid transitions.
559
Iterator tRefs=tRfs.toCollection().iterator();
560          boolean invalidTref=false;
561          while (tRefs.hasNext()) {
562             String JavaDoc transitionId=tRefs.next().toString();
563             Transition t=trans.getTransition(transitionId);
564             if (t==null || !outTrans.contains(t)) {
565                isValid=false;
566                invalidTref=true;
567             }
568          }
569
570          if (invalidTref) {
571             msg=prepareMessageString(msg);
572             msg=msg+XMLUtil.getLanguageDependentString("ErrorTransitionRefIsNotValid");
573             les.put(act,msg);
574          }
575
576          if (!(fullCheck || isValid)) {
577             return false;
578          }
579       }
580
581       // Join type and no. of incoming transitions
582
Join join=act.getJoin();
583       if (join.get("Type").toValue().toString().trim().length()==0 && inTrans.size()>1) {
584          isValid=false;
585          msg=prepareMessageString(msg);
586          msg=msg+XMLUtil.getLanguageDependentString(
587             "ErrorMultipleIncomingTransitionsWithoutJoinTypeDefined");
588          les.put(act,msg);
589       }
590
591       if (!(fullCheck || isValid)) {
592          return false;
593       }
594
595       isValid=checkMultipleOtherwiseOrDefaultExceptionTransitions(act,fullCheck) && isValid;
596
597       return isValid;
598    }
599
600    public boolean checkActivityPerformer (Activity act,boolean fullCheck) {
601       boolean isValid=true;
602
603       // check performer
604
Object JavaDoc performer=act.get("Performer").toValue();
605       // if this is a block activity
606
int type=act.getType();
607       if (type!=1 && type !=2 && ((performer instanceof Participant) ||
608                                      performer.toString().trim().length()>0)) {
609          isValid=false;
610          Map les=getLogicErrors(act.getCollection().getOwner());
611          String JavaDoc msg=(String JavaDoc)les.get(act);
612          msg=prepareMessageString(msg);
613          msg=msg+XMLUtil.getLanguageDependentString("ErrorActivityCannotHavePerformer");
614          les.put(act,msg);
615       }
616       return isValid;
617    }
618
619    public boolean checkActivityTools (Activity act,boolean fullCheck) {
620       boolean isValid=true;
621       boolean nonExistingToolReference=false;
622
623       Tools tools=act.getTools();
624       if (tools!=null) {
625          Iterator it=tools.toCollection().iterator();
626          while (it.hasNext() && (fullCheck || isValid)) {
627             Tool tool=(Tool)it.next();
628             XMLComplexChoice apps=(XMLComplexChoice)tool.get("Application");
629             Object JavaDoc choosenApp=apps.getChoosen();
630             String JavaDoc toolID=null;
631             if (choosenApp!=null && choosenApp instanceof Application) {
632                toolID=((Application)choosenApp).getID();
633             }
634             if (toolID==null) {
635                isValid=false;
636                nonExistingToolReference=true;
637             }
638             if (!(isValid || fullCheck)) break;
639             try {
640                isValid=checkParameterMappings(tool,(Application)choosenApp,fullCheck) && isValid;
641             } catch (Exception JavaDoc ex) {}
642          }
643       }
644
645       if (!isValid) {
646          Map les=getLogicErrors(act.getCollection().getOwner());
647          String JavaDoc msg=(String JavaDoc)les.get(act);
648          msg=prepareMessageString(msg);
649          if (nonExistingToolReference) {
650             msg+=XMLUtil.getLanguageDependentString("ErrorNonExistingToolReference");
651          } else {
652             msg+=XMLUtil.getLanguageDependentString("ErrorToolsFormalAndActualParametersDoNotMatch");
653          }
654          les.put(act,msg);
655       }
656
657       return isValid;
658    }
659
660    public boolean checkActivitySubFlow (Activity act,boolean fullCheck) {
661       boolean isValid=true;
662       boolean nonExistingProcessReference=false;
663       boolean notAllowedProcessReference=false;
664       SubFlow s=act.getSubflow();
665       if (s!=null) {
666          XMLComplexChoice wp=(XMLComplexChoice)s.get("WorkflowProcess");
667          Object JavaDoc choosenWorkflow=wp.getChoosen();
668          String JavaDoc subflowID=null;
669          if (choosenWorkflow!=null) {
670             if (choosenWorkflow instanceof WorkflowProcess) {
671                subflowID=((WorkflowProcess)choosenWorkflow).getID();
672             } else {
673                subflowID=choosenWorkflow.toString();
674             }
675          }
676          if (subflowID==null || subflowID.trim().equals("")) {
677             isValid=false;
678             nonExistingProcessReference=true;
679          } /*else if (subflowID==act.getOwnerProcess().getID()) {
680           isValid=false;
681           notAllowedProcessReference=true;
682           }*/

683          if ((fullCheck || isValid) && (choosenWorkflow instanceof WorkflowProcess)) {
684             try {
685                isValid=checkParameterMappings(s,(WorkflowProcess)choosenWorkflow,fullCheck) && isValid;
686             } catch (Exception JavaDoc ex) {}
687          }
688       }
689       if (!isValid) {
690          Map les=getLogicErrors(act.getCollection().getOwner());
691          String JavaDoc msg=(String JavaDoc)les.get(act);
692          msg=prepareMessageString(msg);
693          if (nonExistingProcessReference) {
694             msg=msg+XMLUtil.getLanguageDependentString("ErrorNonExistingProcessReference");
695          } else if (nonExistingProcessReference) {
696             msg=msg+XMLUtil.getLanguageDependentString("ErrorNotAllowedProcessReference");
697          } else {
698             msg=msg+XMLUtil.getLanguageDependentString("ErrorSubFlowFormalAndActualParametersDoNotMatch");
699          }
700          les.put(act,msg);
701       }
702       return isValid;
703    }
704
705    public boolean checkActivityBlock (Activity act,boolean fullCheck) {
706       boolean isValid=true;
707       BlockActivity blk=act.getBlockActivity();
708       String JavaDoc blockId=blk.get("BlockId").toString();
709       // check if the activity set exists
710
ActivitySets ass=(ActivitySets)act.getOwnerProcess().get("ActivitySets");
711       ActivitySet as=ass.getActivitySet(blockId);
712       // check if there is activity set with the referenced id and if
713
// block activity is inside other block activity, and references it's owner
714
if (as==null || act.getCollection().getOwner().equals(as)) {
715          isValid=false;
716          Map les=getLogicErrors(act.getCollection().getOwner());
717          String JavaDoc msg=(String JavaDoc)les.get(act);
718          msg=prepareMessageString(msg);
719          if (as==null) {
720             msg=msg+XMLUtil.getLanguageDependentString("ErrorNonExistingActivitySetReference");
721          } else {
722             msg=msg+XMLUtil.getLanguageDependentString("ErrorNotAllowedActivitySetReference");
723          }
724          les.put(act,msg);
725       }
726       return isValid;
727    }
728
729    public boolean checkActivityDeadlines (Activity act,boolean fullCheck) {
730       boolean isValid=true;
731
732       Collection deadlines=((Deadlines)act.get("Deadlines")).toCollection();
733       if (deadlines.size()==0) return isValid;
734
735       Iterator dls=deadlines.iterator();
736       //Set deadlineExceptions = new HashSet();
737
int syncCount = 0;
738       while (dls.hasNext()) {
739          Deadline dl=(Deadline)dls.next();
740          // TODO: validate condition.
741
//XMLElement dc=dl.get("DeadlineCondition");
742
//XMLElement en=dl.get("ExceptionName");
743
//deadlineExceptions.add(en.toString().trim());
744
if (dl.get("Execution").toValue().toString().equals("SYNCHR")) {
745             syncCount++;
746          }
747       }
748
749       Map les=getLogicErrors(act.getCollection().getOwner());
750       String JavaDoc msg=(String JavaDoc)les.get(act);
751       if (syncCount>1) {
752          isValid=false;
753          msg=prepareMessageString(msg);
754          msg+=XMLUtil.getLanguageDependentString("ErrorActivityCanHaveOnlyOneSynchronousDeadline");
755          les.put(act,msg);
756       }
757       if (!(fullCheck || isValid)) {
758          return false;
759       }
760
761       Transitions trans=(Transitions)act.getCollection().getOwner().get("Transitions");
762       Set outTrans=trans.getTransitions(act.getID(),-1);
763
764       boolean hasExceptionTransition=false;
765       Iterator it=outTrans.iterator();
766       while (it.hasNext()) {
767          Transition t=(Transition)it.next();
768          Condition c=(Condition)t.get("Condition");
769          String JavaDoc ct=((Condition)t.get("Condition")).get("Type").
770             toValue().toString();
771          if (ct.equals("DEFAULTEXCEPTION") || ct.equals("EXCEPTION")) {
772             hasExceptionTransition=true;
773             break;
774          }
775       }
776       if (!hasExceptionTransition) {
777          isValid=false;
778          msg=prepareMessageString(msg);
779          msg+=XMLUtil.getLanguageDependentString(
780             "ErrorDeadlineExceptionIsNotHandledByAnyOutgoingTransitionWithExceptionOrDefaultExceptionConditionType");
781          les.put(act,msg);
782       }
783       return isValid;
784    }
785
786    public boolean checkMultipleOtherwiseOrDefaultExceptionTransitions (Activity act,boolean fullCheck) {
787       Transitions trans=(Transitions)act.getCollection().getOwner().get("Transitions");
788       Set outTrans=trans.getTransitions(act.getID(),-1);
789       // Check outgoing transitions
790
// do not allow more then 1 transitions of type otherwise or default_exception
791
boolean foundOtherwise=false;
792       boolean foundMultipleOtherwise=false;
793       boolean foundDefaultException=false;
794       boolean foundMultipleDefaultException=false;
795       Iterator ts=outTrans.iterator();
796       while (ts.hasNext()) {
797          Transition t=(Transition)ts.next();
798          String JavaDoc ct=((Condition)t.get("Condition")).get("Type").
799             toValue().toString();
800          if (ct.equals(Condition.CONDITION_TYPE_OTHERWISE)) {
801             if (foundOtherwise) {
802                foundMultipleOtherwise=true;
803                if (foundMultipleDefaultException || !fullCheck) break;
804             } else {
805                foundOtherwise=true;
806             }
807          } else if (ct.equals(Condition.CONDITION_TYPE_DEFAULTEXCEPTION)) {
808             if (foundDefaultException) {
809                foundMultipleDefaultException=true;
810                if (foundMultipleOtherwise || !fullCheck) break;
811             } else {
812                foundDefaultException=true;
813             }
814          }
815       }
816
817       if (foundMultipleOtherwise || foundMultipleDefaultException) {
818          Map les=getLogicErrors(act.getCollection().getOwner());
819          String JavaDoc msg=(String JavaDoc)les.get(act);
820          msg=prepareMessageString(msg);
821          if (foundMultipleDefaultException && foundMultipleOtherwise) {
822             msg=msg+XMLUtil.getLanguageDependentString(
823                "ErrorMoreThenOneOTHERWISEAndDEFAULTEXCEPTIONTypeOutgoingTransition");
824          } else if (foundMultipleOtherwise) {
825             msg=msg+XMLUtil.getLanguageDependentString(
826                "ErrorMoreThenOneOTHERWISETypeOutgoingTransition");
827          } else if (foundMultipleDefaultException) {
828             msg=msg+XMLUtil.getLanguageDependentString(
829                "ErrorMoreThenOneDEFAULTEXCEPTIONTypeOutgoingTransition");
830          }
831          les.put(act,msg);
832          return false;
833       } else {
834          return true;
835       }
836    }
837
838    public boolean checkParameterMappings (XMLComplexElement toolOrSbflw,
839                                           XMLComplexElement appOrWp,boolean fullCheck) {
840       FormalParameters fps;
841       if (appOrWp instanceof WorkflowProcess) {
842          fps=(FormalParameters)appOrWp.get("FormalParameters");
843       } else {
844          if (((XMLComplexChoice)appOrWp.get("Choice")).getChoosen() instanceof FormalParameters) {
845             fps=(FormalParameters)((XMLComplexChoice)appOrWp.get("Choice")).getChoices()[0];
846             // do not check if application is externally defined
847
} else {
848             return true;
849          }
850       }
851       ActualParameters aps=(ActualParameters)toolOrSbflw.get("ActualParameters");
852       int pm=XMLUtil.checkParameterMatching(fps,aps);
853       if (pm!=0) {
854          return false;
855       } else {
856          return true;
857       }
858    }
859
860    public boolean checkTransition (Transition transition,boolean fullCheck) {
861       boolean isValid=true;
862       Map les=getLogicErrors(transition.getCollection().getOwner());
863       String JavaDoc msg=(String JavaDoc)les.get(transition);
864
865       if (transition.getFrom()==null) {
866          isValid=false;
867          msg=prepareMessageString(msg);
868          msg+=XMLUtil.getLanguageDependentString("ErrorNonExistingFromActivityReference");
869       }
870       if (transition.getTo()==null) {
871          isValid=false;
872          msg=prepareMessageString(msg);
873          msg+=XMLUtil.getLanguageDependentString("ErrorNonExistingToActivityReference");
874       }
875       if (!isValid) {
876          les.put(transition,msg);
877       }
878       return isValid;
879    }
880
881    public boolean isIdValid (String JavaDoc id) {
882       return XMLCollection.isIdValid(id);
883    }
884
885    public static boolean isEmpty (String JavaDoc str) {
886       if (str==null || str.trim().length()==0) {
887          return true;
888       } else {
889          return false;
890       }
891    }
892
893    public static boolean isUniqueId (XMLCollection xmlCol,String JavaDoc id) {
894       if (xmlCol instanceof Activities || xmlCol instanceof Transitions) {
895          if (xmlCol instanceof Activities) {
896             return !((Activities)xmlCol).getWorkflowProcess().isActivityIdDuplicated(id);
897          } else {
898             return !((Transitions)xmlCol).getWorkflowProcess().isTransitionIdDuplicated(id);
899          }
900       } else {
901          int idCnt=0;
902          Iterator it=xmlCol.toCollection().iterator();
903          while (it.hasNext()) {
904             try {
905                XMLCollectionElement xmlce=(XMLCollectionElement)it.next();
906                String JavaDoc cId=xmlce.getID();
907                if (cId.equals(id)) {
908                   idCnt++;
909                   if (idCnt>1) {
910                      return false;
911                   }
912                }
913             } catch (ClassCastException JavaDoc cce) {
914                return true;
915             }
916          }
917       }
918       return true;
919    }
920
921    public static void main(String JavaDoc[] args) {
922       try {
923          XMLInterfaceForJDK13 xmlI=new XMLInterfaceForJDK13();
924          Package JavaDoc pkg=xmlI.parseDocument(args[0],true);
925          PackageValidator validator = new PackageValidator(pkg,false,true,false,
926                                                            true,false,false,"UTF-8");
927          if (validator.validateAll(false)) {
928             System.out.println(args[0]+" is a valid XPDL package");
929          } else {
930             System.out.println(args[0]+" is not a valid XPDL package");
931          }
932       } catch (Exception JavaDoc ex) {
933          ex.printStackTrace();
934          System.exit(1);
935       }
936    }
937
938    /** Used for debug only */
939    public static void printIM(boolean[][] im,java.util.List JavaDoc acts) {
940       if (im != null) {
941          for (int i=0; i<im.length; i++) {
942             for (int j=0; j<im[i].length; j++) {
943                System.out.print(acts.get(i)+"->"+acts.get(j)+"="+im[i][j]+" ");
944             }
945             System.out.println();
946          }
947       } else {
948          System.out.println("Passed array is null !!!");
949       }
950    }
951
952    /** Used for debug only */
953    public static void printIM2(boolean[][] im,java.util.List JavaDoc acts) {
954       System.out.println("Activities are"+acts);
955       if (im != null) {
956          for (int i=0; i<im.length; i++) {
957             for (int j=0; j<im[i].length; j++) {
958                System.out.print(((im[i][j]) ? "1":"0")+" ");
959             }
960             System.out.println();
961          }
962       } else {
963          System.out.println("Passed array is null !!!");
964       }
965    }
966
967
968    //************************** GRAPH CONFORMANCE CHECKING ****************************
969
public boolean checkGraphConformance (boolean fullCheck) {
970       boolean areGraphsConformant=true;
971
972       Map graphConformanceErrors=new HashMap();
973       //Map graphConformanceErrors=new OrderedHashMap();
974
//Map graphConformanceErrors=new LinkedHashMap();
975
List basicGraphConformanceErrors=new ArrayList();
976
977       Iterator procs=((WorkflowProcesses)pkg.get("WorkflowProcesses")).toCollection().iterator();
978       while (procs.hasNext()) {
979          WorkflowProcess wp=(WorkflowProcess)procs.next();
980          if (!checkGraphConformance(wp,fullCheck)) {
981             areGraphsConformant=false;
982             if (!fullCheck) {
983                break;
984             }
985             String JavaDoc msg="";
986             Iterator bces=getBasicGraphConformanceErrors(wp).iterator();
987             while (bces.hasNext()) {
988                msg=msg+bces.next().toString()+"<br>";
989             }
990             graphConformanceErrors.put(wp,msg);
991          }
992       }
993       if (!areGraphsConformant) {
994          basicGraphConformanceErrors.add(XMLUtil.
995                                             getLanguageDependentString("ErrorOneOrMoreProcessesDoNotSatisfyGraphConformance"));
996       }
997       basicGraphsConformanceErrors.put(pkg,basicGraphConformanceErrors);
998       graphsConformanceErrors.put(pkg,graphConformanceErrors);
999       return areGraphsConformant;
1000   }
1001   /**
1002    * Checks if graph conforms to the given conformance class.
1003    * @return true if graph is conformant, false otherwise
1004    */

1005   public boolean checkGraphConformance(XMLCollectionElement wpOrAs,boolean fullCheck) {
1006      Map graphConformanceErrors=new HashMap();
1007      //Map graphConformanceErrors=new OrderedHashMap();
1008
//Map graphConformanceErrors=new LinkedHashMap();
1009
List basicGraphConformanceErrors=new ArrayList();
1010
1011      Collection allActs=((XMLCollection)wpOrAs.get("Activities")).toCollection();
1012
1013      Package JavaDoc pkg;
1014      if (wpOrAs instanceof WorkflowProcess){
1015         pkg=((WorkflowProcess)wpOrAs).getPackage();
1016      } else {
1017         pkg=((WorkflowProcess)wpOrAs.getCollection().getOwner()).getPackage();
1018      }
1019      String JavaDoc conformanceClass=((ConformanceClass)pkg.get("ConformanceClass")).
1020         get("GraphConformance").toValue().toString();
1021      // ct=0->NON_BLOCKED, ct=1->LOOP_BLOCKED, ct=2->FULL_BLOCKED, ct=-1->default NON_BLOCKED
1022
int ct=XMLUtil.getConformanceClassNo(conformanceClass);
1023
1024      Activities acts=(Activities)wpOrAs.get("Activities");
1025      java.util.List JavaDoc activities=(java.util.List JavaDoc)acts.toCollection();
1026
1027      if (activities.size()==0) {
1028         graphsConformanceErrors.put(wpOrAs,graphConformanceErrors);
1029         basicGraphsConformanceErrors.put(wpOrAs,basicGraphConformanceErrors);
1030         return true;
1031      }
1032
1033      boolean isGraphConformant=true;
1034
1035      Set splitActs=XMLUtil.getSplitOrJoinActivities(activities,0);
1036      Set joinActs=XMLUtil.getSplitOrJoinActivities(activities,1);
1037
1038      Set noSplitActs=new HashSet(activities);
1039      noSplitActs.removeAll(splitActs);
1040
1041      GraphChecker gc=null;
1042      if (ct>0 && (isGraphConformant || fullCheck)) {
1043         boolean[][] incidenceMatrix=createIncidenceMatrix(activities);
1044         if (incidenceMatrix==null) {
1045            basicGraphConformanceErrors.add("Unexpected error");
1046            graphsConformanceErrors.put(wpOrAs,graphConformanceErrors);
1047            basicGraphsConformanceErrors.put(wpOrAs,basicGraphConformanceErrors);
1048            return false;
1049         }
1050
1051         gc=new GraphChecker(incidenceMatrix);
1052
1053         // call method to check loop cycling
1054
boolean loopError=false;
1055         if (fullCheck) {
1056            int[] loopNodes=gc.getCyclicNodes();
1057            if (loopNodes!=null) {
1058               isGraphConformant=false;
1059               loopError=true;
1060               for (int i=0; i<loopNodes.length; i++) {
1061                  Activity act=(Activity)activities.get(loopNodes[i]);
1062                  graphConformanceErrors.put(act,XMLUtil.
1063                                                getLanguageDependentString("ErrorLoopContainedActivity"));
1064               }
1065            }
1066         } else {
1067            loopError=gc.isGraphCyclic();
1068            if (loopError) {
1069               isGraphConformant=false;
1070            }
1071         }
1072         if (loopError) {
1073            basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString("ErrorTheGraphIsCyclic"));
1074         }
1075      }
1076      // Here we check FULL_BLOCK conformance
1077
if (ct==2 && (isGraphConformant || fullCheck)) {
1078         // check if there is more then one starting activity
1079
if (XMLUtil.getStartingActivities(wpOrAs).size()!=1) {
1080            isGraphConformant=false;
1081            basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1082                                               "ErrorThereMustBeExactlyOneStartingActivityInFullBlockedMode"));
1083         }
1084         // check if there is more then one ending activity
1085
if ((isGraphConformant || fullCheck) && XMLUtil.getEndingActivities(wpOrAs).size()!=1) {
1086            isGraphConformant=false;
1087            basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1088                                               "ErrorThereMustBeExactlyOneEndingActivityInFullBlockedMode"));
1089         }
1090
1091         // check if the number of splits and joins matches
1092
boolean smerr=false;
1093         if ((isGraphConformant || fullCheck) && splitActs.size()!=joinActs.size()) {
1094            if (splitActs.size()>joinActs.size()) {
1095               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1096                                                  "ErrorTheNumberOfSplitsAndJoinsIsNotTheSame-MoreSplits"));
1097            } else {
1098               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1099                                                  "ErrorTheNumberOfSplitsAndJoinsIsNotTheSame-MoreJoins"));
1100            }
1101            isGraphConformant=false;
1102            smerr=true;
1103         }
1104
1105         // check for split/join type mismatch
1106
if ((isGraphConformant || fullCheck) && !smerr) {
1107            if (getNoOfANDSplitsOrJoins(splitActs,0)!=getNoOfANDSplitsOrJoins(joinActs,1)) {
1108               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1109                                                  "ErrorOneOrMoreSplitsDoNotHaveCorrespondingJoinBecauseOfTypeMismatch"));
1110               isGraphConformant=false;
1111            }
1112         }
1113         // first check for correct outgoing transitions
1114
if (isGraphConformant || fullCheck) {
1115            Iterator it=splitActs.iterator();
1116            boolean andSplitError=false;
1117            boolean xorSplitError=false;
1118            while (it.hasNext()) {
1119               Activity act=(Activity)it.next();
1120               if (XMLUtil.isANDTypeSplitOrJoin(act,0)) {
1121                  if (!checkANDSplit(act)) {
1122                     isGraphConformant=false;
1123                     andSplitError=true;
1124                     String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1125                     msg=prepareMessageString(msg);
1126                     msg=msg+XMLUtil.getLanguageDependentString(
1127                        "ErrorOneOrMoreConditionalOutgoingTransitions");
1128                     graphConformanceErrors.put(act,msg);
1129                     if (!fullCheck) {
1130                        break;
1131                     }
1132                  }
1133               } else {
1134                  if (!checkXORSplit(act)) {
1135                     isGraphConformant=false;
1136                     xorSplitError=true;
1137                     String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1138                     msg=prepareMessageString(msg);
1139                     msg=msg+XMLUtil.getLanguageDependentString(
1140                        "ErrorMissingOTHERWISETypeOutgoingTransition");
1141                     graphConformanceErrors.put(act,msg);
1142                     if (!fullCheck) {
1143                        break;
1144                     }
1145                  }
1146               }
1147            }
1148
1149            // check activities that has only one outgoing transition, if
1150
// there is condition on it -> report XOR split with conditional
1151
// transition error
1152
it=noSplitActs.iterator();
1153            while (it.hasNext()) {
1154               Activity act=(Activity)it.next();
1155               if (!checkXORSplit(act)) {
1156                  isGraphConformant=false;
1157                  xorSplitError=true;
1158                  String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1159                  msg=prepareMessageString(msg);
1160                  msg=msg+XMLUtil.getLanguageDependentString(
1161                     "ErrorMissingOTHERWISETypeOutgoingTransition");
1162                  graphConformanceErrors.put(act,msg);
1163                  if (!fullCheck) {
1164                     break;
1165                  }
1166               }
1167            }
1168
1169            if (andSplitError) {
1170               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1171                                                  "ErrorOneOrMoreANDSplitsHaveConditionalOutgoingTransitions"));
1172            }
1173            if (xorSplitError) {
1174               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1175                                                  "ErrorOneOrMoreXORSplitsWithConditionalTransitionsDoNotHaveOTHERWISETransition"));
1176            }
1177         }
1178
1179         // now perform search on every split activity for corresponding join activity
1180
if (isGraphConformant || fullCheck) {
1181            boolean noCorrespondingJoinError=false;
1182            Iterator it=splitActs.iterator();
1183            while (it.hasNext()) {
1184               Activity act=(Activity)it.next();
1185               int splitIndex=activities.indexOf(act);
1186               if (splitIndex==-1) {
1187                  basicGraphConformanceErrors.add("Unexpected error");
1188                  isGraphConformant=false;
1189                  if (!fullCheck) {
1190                     break;
1191                  } else {
1192                     continue;
1193                  }
1194               }
1195               int ji=gc.getJoinIndex(splitIndex);
1196               // The correspondin join can't be found
1197
if (ji<0) {
1198                  isGraphConformant=false;
1199                  noCorrespondingJoinError=true;
1200                  String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1201                  msg=prepareMessageString(msg);
1202                  msg=msg+XMLUtil.getLanguageDependentString("ErrorThereIsNoCorrespondingJoinActivity");
1203                  graphConformanceErrors.put(act,msg);
1204                  if (!fullCheck) {
1205                     break;
1206                  }
1207                  // if the join is found and their types are different
1208
// the graph is not conformant
1209
} else {
1210                  if (XMLUtil.isANDTypeSplitOrJoin(act,0)!=
1211                      XMLUtil.isANDTypeSplitOrJoin((Activity)activities.get(ji),1)) {
1212                     isGraphConformant=false;
1213                     noCorrespondingJoinError=true;
1214                     String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(act);
1215                     msg=prepareMessageString(msg);
1216                     if (XMLUtil.isANDTypeSplitOrJoin((Activity)act,0)) {
1217                        msg=msg+XMLUtil.getLanguageDependentString(
1218                           "ErrorTheCorrespondingJoinActivityDoesNotHaveTheSameType-ANDXOR");
1219                     } else {
1220                        msg=msg+XMLUtil.getLanguageDependentString(
1221                           "ErrorTheCorrespondingJoinActivityDoesNotHaveTheSameType-XORAND");
1222                     }
1223                     graphConformanceErrors.put(act,msg);
1224                     if (!fullCheck) {
1225                        break;
1226                     }
1227                  }
1228               }
1229            }
1230            if (noCorrespondingJoinError) {
1231               basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1232                                                  "ErrorOneOrMoreSplitsDoNotHaveCorrespondingJoin"));
1233            }
1234         }
1235      }
1236
1237      // if so far the graph is conformant, or the full check is required,
1238
// check the graphs block activities
1239
if (isGraphConformant || fullCheck) {
1240         Set blockActivities=XMLUtil.getBlockActivities(wpOrAs,false);
1241         boolean innerConformanceError=false;
1242         Iterator it=blockActivities.iterator();
1243         while (it.hasNext()) {
1244            Activity act=(Activity)it.next();
1245            BlockActivity ba=act.getBlockActivity();
1246            ActivitySets ass=(ActivitySets)act.getOwnerProcess().get("ActivitySets");
1247            String JavaDoc asId=ba.get("BlockId").toString();
1248            ActivitySet as=ass.getActivitySet(asId);
1249            if (as!=null && !checkGraphConformance(as,false)) {
1250               isGraphConformant=false;
1251               innerConformanceError=true;
1252               String JavaDoc msg=(String JavaDoc)graphConformanceErrors.get(ba);
1253               msg=prepareMessageString(msg);
1254               msg=msg+XMLUtil.getLanguageDependentString("ErrorInnerGraphConformanceError");
1255               graphConformanceErrors.put(act,msg);
1256               graphConformanceErrors.put(act,msg);
1257               if (!fullCheck) {
1258                  break;
1259               }
1260            }
1261         }
1262         if (innerConformanceError) {
1263            basicGraphConformanceErrors.add(XMLUtil.getLanguageDependentString(
1264                                               "ErrorOneOrMoreBlockActivitiesAreNotValid"));
1265         }
1266      }
1267
1268      graphsConformanceErrors.put(wpOrAs,graphConformanceErrors);
1269      basicGraphsConformanceErrors.put(wpOrAs,basicGraphConformanceErrors);
1270      return isGraphConformant;
1271   }
1272
1273   protected boolean[][] createIncidenceMatrix (java.util.List JavaDoc activities) {
1274      int size=activities.size();
1275      boolean[][] incidenceMatrix=new boolean[size][size];
1276      Transitions ts=null;
1277      for (int indAct=0; indAct<size; indAct++) {
1278         Activity a=(Activity)activities.get(indAct);
1279         if (ts==null) {
1280            ts=(Transitions)a.getCollection().getOwner().get("Transitions");
1281         }
1282         Set oas=new HashSet();
1283         Iterator trs=ts.getTransitions(a.getID(),-1).iterator();
1284         while (trs.hasNext()) {
1285            Transition t=(Transition)trs.next();
1286            if (t!=null && t.getTo()!=null) {
1287               Object JavaDoc aOut=t.getTo();
1288               if (aOut instanceof Activity) {
1289                  int indOut=activities.indexOf(aOut);
1290                  if (indOut==-1) return null;
1291                  incidenceMatrix[indAct][indOut]=true;
1292               }
1293            }
1294         }
1295      }
1296      return incidenceMatrix;
1297   }
1298
1299   /**
1300    * Returns the number of activities in the given set that have
1301    * split or join, depending on second parameter.
1302    * @param acts The set of activities that are searched for split or join
1303    * @param sOrJ 0 -> searching for split, otherwise, searching for join
1304    */

1305   protected int getNoOfANDSplitsOrJoins (Set acts,int sOrJ) {
1306      int no=0;
1307      Iterator it=acts.iterator();
1308      while (it.hasNext()) {
1309         Activity act=(Activity)it.next();
1310         if (XMLUtil.isANDTypeSplitOrJoin(act,sOrJ)) {
1311            no++;
1312         }
1313      }
1314      return no;
1315   }
1316
1317   protected boolean checkANDSplit (Activity act) {
1318      return !hasAnyPostcondition(act);
1319   }
1320
1321   protected boolean checkXORSplit (Activity act) {
1322      // if activity has any postcondition, it must have an otherwise transition
1323
if (hasAnyPostcondition(act)) {
1324         Transitions ts=(Transitions)act.getCollection().getOwner().get("Transitions");
1325         Set ots=ts.getTransitions(act.getID(),-1);
1326         Iterator trs=ots.iterator();
1327         while (trs.hasNext()) {
1328            Transition t=(Transition)trs.next();
1329            if (t.getTo()!=null) {
1330               if (((XMLComplexElement)t.get("Condition")).get("Type").
1331                   toValue().toString().equals("OTHERWISE")) {
1332                  return true;
1333               }
1334            }
1335         }
1336         return false;
1337      } else {
1338         return true;
1339      }
1340   }
1341
1342   protected boolean hasAnyPostcondition (Activity act) {
1343      Transitions ts=(Transitions)act.getCollection().getOwner().get("Transitions");
1344      Set outL=ts.getTransitions(act.getID(),-1);
1345      Iterator it=outL.iterator();
1346      while (it.hasNext()) {
1347         if (((Transition)it.next()).hasCondition()) {
1348            return true;
1349         }
1350      }
1351      return false;
1352   }
1353
1354   //************************** GRAPH CONNECTIONS CHECKING ****************************
1355

1356   public boolean checkGraphConnections (boolean fullCheck) {
1357      basicGraphConnectionErrors.remove(pkg);
1358      graphsConnectionErrors.remove(pkg);
1359
1360      boolean areWellConnected=true;
1361      String JavaDoc basicGraphConnectionError;
1362      Map connectionErrorMessages=new HashMap();
1363      //Map connectionErrorMessages=new OrderedHashMap();
1364
//Map connectionErrorMessages=new LinkedHashMap();
1365

1366      Iterator procs=((WorkflowProcesses)pkg.get("WorkflowProcesses")).toCollection().iterator();
1367      while (procs.hasNext()) {
1368         WorkflowProcess wp=(WorkflowProcess)procs.next();
1369         if (!checkGraphConnections(wp,false)) {
1370            areWellConnected=false;
1371            if (!fullCheck) {
1372               break;
1373            }
1374            String JavaDoc msg=getBasicGraphConnectionError(wp);
1375            if (msg==null) {
1376               msg="";
1377            }
1378            connectionErrorMessages.put(wp,msg);
1379         }
1380      }
1381      if (!areWellConnected) {
1382         basicGraphConnectionError=
1383            XMLUtil.getLanguageDependentString("InformationOneOrMoreProcessesHaveImproperlyConnectedElements");
1384         basicGraphConnectionErrors.put(pkg,basicGraphConnectionError);
1385      }
1386      graphsConnectionErrors.put(pkg,connectionErrorMessages);
1387      return areWellConnected;
1388   }
1389
1390   public boolean checkGraphConnections (XMLCollectionElement wpOrAs,boolean fullCheck) {
1391      if (wpOrAs==null) return false;
1392      basicGraphConnectionErrors.remove(wpOrAs);
1393      graphsConnectionErrors.remove(wpOrAs);
1394
1395      boolean isWellConnected=true;
1396      boolean basicError=false;
1397      Map connectionErrorMessages=new HashMap();
1398      //Map connectionErrorMessages=new OrderedHashMap();
1399
//Map connectionErrorMessages=new LinkedHashMap();
1400

1401      Transitions ts=(Transitions)wpOrAs.get("Transitions");
1402      Collection acts=((Activities)wpOrAs.get("Activities")).toCollection();
1403      if (acts==null || acts.size()==0) {
1404         graphsConnectionErrors.put(wpOrAs,connectionErrorMessages);
1405         return true;
1406      }
1407
1408      Set startActs=null;
1409      Set endActs=null;
1410      if (fullCheck || isWellConnected) {
1411         startActs=XMLUtil.getStartingActivities(wpOrAs);
1412         if (startActs.size()==0 && (!allowUndefinedStart || (wpOrAs instanceof ActivitySet))) {
1413            isWellConnected=false;
1414            basicError=true;
1415            String JavaDoc msg=(String JavaDoc)connectionErrorMessages.get(wpOrAs);
1416            msg=prepareMessageString(msg);
1417            msg+=XMLUtil.getLanguageDependentString("ErrorStartingActivityDoesNotExist");
1418            connectionErrorMessages.put(wpOrAs,msg);
1419         }
1420      }
1421      if (fullCheck || isWellConnected) {
1422         endActs=XMLUtil.getEndingActivities(wpOrAs);
1423         if (endActs.size()==0 && (!allowUndefinedEnd || (wpOrAs instanceof ActivitySet))) {
1424            isWellConnected=false;
1425            basicError=true;
1426            String JavaDoc msg=(String JavaDoc)connectionErrorMessages.get(wpOrAs);
1427            msg=prepareMessageString(msg);
1428            msg+=XMLUtil.getLanguageDependentString("ErrorEndingActivityDoesNotExist");
1429            connectionErrorMessages.put(wpOrAs,msg);
1430         }
1431      }
1432      if (fullCheck || isWellConnected) {
1433         Iterator it=acts.iterator();
1434         while (it.hasNext()) {
1435            Activity act=(Activity)it.next();
1436            String JavaDoc cem=checkActivityConnection(act,ts,startActs,endActs,fullCheck);
1437            if (cem!=null) {
1438               connectionErrorMessages.put(act,cem);
1439               isWellConnected=false;
1440               if (!fullCheck) {
1441                  break;
1442               }
1443            }
1444
1445         }
1446      }
1447
1448      if (!isWellConnected) {
1449         if (basicError) {
1450            basicGraphConnectionErrors.put(wpOrAs,connectionErrorMessages.get(wpOrAs));
1451         } else {
1452            basicGraphConnectionErrors.put(wpOrAs,
1453                                           XMLUtil.getLanguageDependentString("InformationOneOrMoreElementsAreNotProperlyConnected"));
1454         }
1455      }
1456      graphsConnectionErrors.put(wpOrAs,connectionErrorMessages);
1457      return isWellConnected;
1458   }
1459
1460   /**
1461    * Checks if given activity is well connected.
1462    * @return String describing the error, or empty string if there is no connection
1463    * error for giving activity.
1464    */

1465   public String JavaDoc checkActivityConnection (Activity act,Transitions ts,
1466                                          Set startActs,Set endActs,boolean fullCheck) {
1467
1468      String JavaDoc connectionErrorMsg="";
1469
1470      // if this is a block activity, check inner transitions
1471
BlockActivity ba=act.getBlockActivity();
1472      if (ba!=null) {
1473         String JavaDoc asId=ba.get("BlockId").toString();
1474         ActivitySets ass=(ActivitySets)act.getOwnerProcess().get("ActivitySets");
1475         ActivitySet as=ass.getActivitySet(asId);
1476         if (as!=null) {
1477            if (!checkGraphConnections(as,false)) {
1478               connectionErrorMsg+=
1479                  XMLUtil.getLanguageDependentString("ErrorInnerTransitionError")+"; ";
1480            }
1481         }
1482      }
1483
1484      if (connectionErrorMsg.length()==0) connectionErrorMsg=null;
1485
1486      return connectionErrorMsg;
1487   }
1488
1489   public static String JavaDoc prepareMessageString (String JavaDoc msg) {
1490      if (msg!=null) {
1491         msg=msg+"; ";
1492      } else {
1493         msg="";
1494      }
1495      return msg;
1496   }
1497
1498
1499   protected Activity findBlockActivity (ActivitySet as) {
1500      String JavaDoc asId=as.getID();
1501      WorkflowProcess wp=as.getOwnerProcess();
1502      Set bas=XMLUtil.getBlockActivities(wp,true);
1503      Iterator it=bas.iterator();
1504      while (it.hasNext()) {
1505         Activity a=(Activity)it.next();
1506         String JavaDoc baId=a.getBlockActivity().get("BlockId").toString();
1507         if (baId.equals(asId)) {
1508            return a;
1509         }
1510      }
1511      return null;
1512   }
1513
1514}
1515
Popular Tags