001: /**
002: *
003: */package newprocess.validation;
004:
005: import newprocess.Actor;
006: import newprocess.AsyncActivity;
007: import newprocess.Conclusion;
008: import newprocess.Condition;
009: import newprocess.Element;
010: import newprocess.Event;
011: import newprocess.Expansion;
012: import newprocess.Globals;
013: import newprocess.Listener;
014: import newprocess.Loader;
015: import newprocess.SyncActivity;
016:
017: import org.eclipse.emf.ecore.EObject;
018: import org.eclipse.jdt.core.JavaConventions;
019:
020: /**
021: * this validator is used by other validators to determinate the correctness of
022: * implementation properties
023: *
024: * a implementation must be fully qualified if there is no valid namespace set in process.
025: * otherwise a implementation can be a simple identifier.
026: * @author sh
027: *
028: */
029: public class ImplementationValidator {
030:
031: // singleton
032: public static final ImplementationValidator INSTANCE = new ImplementationValidator();
033:
034: /**
035: * takes any object of following types: Process, Actor,
036: * Conditions and Activities
037: *
038: */
039: public boolean validate(EObject obj) {
040: newprocess.Process proc = getProcess(obj);
041: if (proc == null)
042: return false;
043: String impl = getImplementation(obj);
044: if (impl == null || impl == "")
045: return false;
046: if (!JavaConventions.validatePackageName(impl, null, null)
047: .isOK())
048: return false;
049: if (qualifiedRequired(proc) && !isQualified(impl))
050: return false;
051: return true;
052: }
053:
054: /**
055: * get the process object.
056: * @param obj
057: * @return
058: */
059: private newprocess.Process getProcess(EObject obj) {
060: if (obj instanceof newprocess.Process)
061: return (newprocess.Process) obj;
062: if (obj instanceof Actor || obj instanceof SyncActivity
063: || obj instanceof AsyncActivity || obj instanceof Event
064: || obj instanceof Listener || obj instanceof Conclusion
065: || obj instanceof Expansion)
066: return (newprocess.Process) obj.eContainer();
067: if (obj instanceof Condition)
068: return (newprocess.Process) ((Globals) ((Condition) obj)
069: .eContainer()).eContainer();
070: if (obj instanceof Loader)
071: return (newprocess.Process) ((Globals) ((Loader) obj)
072: .eContainer()).eContainer();
073: return null;
074: }
075:
076: /**
077: * get the tmplementation property as string
078: * @param obj
079: * @return
080: */
081: private String getImplementation(EObject obj) {
082: if (obj instanceof newprocess.Process)
083: return ((Element) obj).getImplementation();
084: if (obj instanceof Actor)
085: return ((Element) obj).getImplementation();
086: if (obj instanceof SyncActivity)
087: return ((Element) obj).getImplementation();
088: if (obj instanceof AsyncActivity)
089: return ((Element) obj).getImplementation();
090: if (obj instanceof Event)
091: return ((Element) obj).getImplementation();
092: if (obj instanceof Listener)
093: return ((Element) obj).getImplementation();
094: if (obj instanceof Conclusion)
095: return ((Element) obj).getImplementation();
096: if (obj instanceof Expansion)
097: return ((Element) obj).getImplementation();
098: if (obj instanceof Condition)
099: return ((Element) obj).getImplementation();
100: if (obj instanceof Loader)
101: return ((Element) obj).getImplementation();
102: return null;
103: }
104:
105: /**
106: * finds out if a String has a qualfied syntax by looking if there is a "." in it.
107: * @param identifier
108: * @return
109: */
110: private boolean isQualified(String identifier) {
111: return identifier.contains(".");
112: }
113:
114: /**
115: * Qualified Identifiers are required, if there is a valid namespace
116: * @param proc
117: * @return
118: */
119: private boolean qualifiedRequired(newprocess.Process proc) {
120: String name = proc.getNamespace();
121: return (name != null && name != "" && ProcessValidatorImpl.INSTANCE
122: .validateNamespace(proc));
123: }
124:
125: } // ImplementationValidator
|