001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.tagging;
009:
010: //base classes
011: import java.util.ArrayList;
012: import java.util.HashMap;
013: import java.util.HashSet;
014: import java.util.Iterator;
015:
016: //project specific classes
017: import org.jfolder.common.StandardDataTypes;
018: import org.jfolder.common.UnexpectedSystemException;
019: import org.jfolder.common.utils.misc.MiscHelper;
020:
021: //other classes
022:
023: public class DynamicConceptTagValidator {
024:
025: private boolean andConjunction = false;
026: //private boolean resultInverted = false;
027: //
028: private boolean allowReturnableCt = false;
029: private boolean allowNonReturnableCt = false;
030:
031: //private ArrayList commands = null;
032: private HashSet commands = null;
033: //
034: private HashMap cachedQuickCheck = null;
035:
036: protected DynamicConceptTagValidator() {
037: reset();
038: }
039:
040: public int hashCode() {
041:
042: int outValue = 0;
043:
044: outValue += this .commands.hashCode();
045:
046: return outValue;
047: }
048:
049: public boolean equals(Object inObj) {
050:
051: boolean outValue = true;
052:
053: if (inObj instanceof DynamicConceptTagValidator) {
054: DynamicConceptTagValidator nextDctv = ((DynamicConceptTagValidator) inObj);
055: //
056: outValue &= (this .andConjunction == nextDctv.andConjunction);
057: outValue &= (this .allowReturnableCt == nextDctv.allowReturnableCt);
058: outValue &= (this .allowNonReturnableCt == nextDctv.allowNonReturnableCt);
059: outValue &= (this .commands.equals(nextDctv.commands));
060: } else {
061: outValue &= false;
062: }
063:
064: return outValue;
065: }
066:
067: public void reset() {
068: this .andConjunction = true;
069: //this.resultInverted = false;
070: //
071: //this.commands = new ArrayList();
072: this .commands = new HashSet();
073: //
074: this .allowReturnableCt = true;
075: this .allowNonReturnableCt = true;
076: //
077: this .cachedQuickCheck = null;
078: }
079:
080: private void addCommand(DynamicConceptTagValidatorPart inDctvp) {
081: this .commands.add(inDctvp);
082: this .cachedQuickCheck = null;
083: }
084:
085: //
086: //
087: //what is the role of inActualClass?
088: //what is the role of inStudioValidation?
089: public ArrayList validate(SelectionCriteriaForConceptTag inScfct,
090: Class inActualClass, boolean inStudioValidation) {
091: //
092: ArrayList outValue = null;
093:
094: //
095: if (this .cachedQuickCheck == null) {
096: this .cachedQuickCheck = new HashMap();
097: }
098: //
099: ArrayList key = new ArrayList();
100: key.add(inScfct);
101: key.add(inActualClass);
102: key.add(new Boolean(inStudioValidation));
103: //
104: if (this .cachedQuickCheck.containsKey(key)) {
105: Object o = this .cachedQuickCheck.get(key);
106: outValue = ((ArrayList) o);
107: } else {
108: outValue = subValidate(inScfct, inActualClass,
109: inStudioValidation);
110: this .cachedQuickCheck.put(key, outValue);
111: }
112: //
113: //
114: outValue = new ArrayList(outValue);//so edits won't affect original
115:
116: return outValue;
117: }
118:
119: //
120: //
121: //
122: public void allowReturnableOnly() {
123: this .allowReturnableCt = true;
124: this .allowNonReturnableCt = false;
125: //
126: this .cachedQuickCheck = null;
127: }
128:
129: public void allowNonReturnableOnly() {
130: this .allowReturnableCt = false;
131: this .allowNonReturnableCt = true;
132: //
133: this .cachedQuickCheck = null;
134: }
135:
136: public void allowBothReturnableAndNonReturnable() {
137: this .allowReturnableCt = true;
138: this .allowNonReturnableCt = true;
139: //
140: this .cachedQuickCheck = null;
141: }
142:
143: public boolean isReturnableAllowed() {
144: return this .allowReturnableCt;
145: }
146:
147: public boolean isNonReturnableAllowed() {
148: return this .allowNonReturnableCt;
149: }
150:
151: //
152: //
153: public ArrayList getMessages(String inStart, String inEnd,
154: String inIndent) {
155: //
156: ArrayList outValue = new ArrayList();
157:
158: //
159: if (isReturnableAllowed() && isNonReturnableAllowed()) {
160: outValue.add("Can Be Returnable And Non-Returnable");
161: } else if (isReturnableAllowed() && !isNonReturnableAllowed()) {
162: outValue.add("Must Be Returnable");
163: } else if (!isReturnableAllowed() && isNonReturnableAllowed()) {
164: outValue.add("Must Be Non-Returnable");
165: } else {
166: throw UnexpectedSystemException.unknownState();
167: }
168:
169: //
170: String conjLabel = inIndent + "AND ";
171: if (!this .andConjunction) {
172: conjLabel = inIndent + "OR ";
173: }
174: //
175: Iterator iter = this .commands.iterator();
176: while (iter.hasNext()) {
177: DynamicConceptTagValidatorPart nextDctvp = ((DynamicConceptTagValidatorPart) iter
178: .next());
179: //Object nextCommand[] = (Object[])this.commands.get(i);
180: //int nextCommandType = ((Integer)nextCommand[0]).intValue();
181: //String nextCommandError = (String)nextCommand[1];
182: //
183: if (nextDctvp.isCommandFlagPresent()) {
184: String flagNs = nextDctvp.getFlagNamespace();
185: String flagName = nextDctvp.getFlagName();
186: //String namespace = (String)nextCommand[2];
187: //String name = (String)nextCommand[3];
188: //
189: outValue.add(conjLabel + "Flag (" + flagNs + ", "
190: + flagName + ") must be present");
191: } else if (nextDctvp.isCommandFlagNotPresent()) {
192: String flagNs = nextDctvp.getFlagNamespace();
193: String flagName = nextDctvp.getFlagName();
194: //String namespace = (String)nextCommand[2];
195: //String name = (String)nextCommand[3];
196: //
197: outValue.add(conjLabel + "Flag (" + flagNs + ", "
198: + flagName + ") must not be present");
199: } else if (nextDctvp.isCommandFlagEqual()) {
200: //
201: String flagNs = nextDctvp.getFlagNamespace();
202: String flagName = nextDctvp.getFlagName();
203: Object flagValue = nextDctvp.getFlagValue();
204: //String namespace = (String)nextCommand[2];
205: //String name = (String)nextCommand[3];
206: //Object value = nextCommand[4];
207: outValue
208: .add(conjLabel + "Flag (" + flagNs + ", "
209: + flagName + ") must equal '"
210: + flagValue + "'");
211: } else if (nextDctvp.isCommandFlagNotEqual()) {
212: //
213: String flagNs = nextDctvp.getFlagNamespace();
214: String flagName = nextDctvp.getFlagName();
215: Object flagValue = nextDctvp.getFlagValue();
216: //String namespace = (String)nextCommand[2];
217: //String name = (String)nextCommand[3];
218: //Object value = nextCommand[4];
219: outValue.add(conjLabel + "Flag (" + flagNs + ", "
220: + flagName + ") must not equal '" + flagValue
221: + "'");
222: } else if (nextDctvp.isCommandFlagLessThan()) {
223: //
224: String flagNs = nextDctvp.getFlagNamespace();
225: String flagName = nextDctvp.getFlagName();
226: Object flagValue = nextDctvp.getFlagValue();
227: //String namespace = (String)nextCommand[2];
228: //String name = (String)nextCommand[3];
229: //Object value = nextCommand[4];
230: outValue.add(conjLabel + "Flag (" + flagNs + ", "
231: + flagName + ") must be less than '"
232: + flagValue + "'");
233: } else if (nextDctvp.isCommandFlagGreaterThan()) {
234: //
235: String flagNs = nextDctvp.getFlagNamespace();
236: String flagName = nextDctvp.getFlagName();
237: Object flagValue = nextDctvp.getFlagValue();
238: //String namespace = (String)nextCommand[2];
239: //String name = (String)nextCommand[3];
240: //Object value = nextCommand[4];
241: outValue.add(conjLabel + "Flag (" + flagNs + ", "
242: + flagName + ") must be greater than '"
243: + flagValue + "'");
244: } else if (nextDctvp.isCommandFlagGreaterThanOrEqual()) {
245: //
246: String flagNs = nextDctvp.getFlagNamespace();
247: String flagName = nextDctvp.getFlagName();
248: Object flagValue = nextDctvp.getFlagValue();
249: //String namespace = (String)nextCommand[2];
250: //String name = (String)nextCommand[3];
251: //Object value = nextCommand[4];
252: outValue.add(conjLabel + "Flag (" + flagNs + ", "
253: + flagName
254: + ") must be greater than or equal to '"
255: + flagValue + "'");
256: } else if (nextDctvp.isCommandFlagLessThanOrEqual()) {
257: //
258: String flagNs = nextDctvp.getFlagNamespace();
259: String flagName = nextDctvp.getFlagName();
260: Object flagValue = nextDctvp.getFlagValue();
261: //String namespace = (String)nextCommand[2];
262: //String name = (String)nextCommand[3];
263: //Object value = nextCommand[4];
264: outValue.add(conjLabel + "Flag (" + flagNs + ", "
265: + flagName
266: + ") must be less than or equal to '"
267: + flagValue + "'");
268: } else if (nextDctvp.isCommandIsClass()) {
269: Class c[] = nextDctvp.getClasses();//(Class[])nextCommand[2];
270: outValue.add(conjLabel
271: + "Concept Tag must be one of the following {");
272: for (int j = 0; j < c.length; j++) {
273: outValue.add(inIndent + (c[j]).getName());
274: }
275: outValue.add(inIndent + "}");
276: } else if (nextDctvp.isCommandContainsParentNode()) {
277: throw UnexpectedSystemException.unknownState();
278: } else if (nextDctvp.isCommandDoesNotContainParentNode()) {
279: throw UnexpectedSystemException.unknownState();
280: } else if (nextDctvp.isCommandDoesReturn()) {
281: Class c[] = nextDctvp.getClasses();//(Class[])nextCommand[2];
282: outValue
283: .add(conjLabel
284: + "Concept Tag must return one of the following {");
285: for (int j = 0; j < c.length; j++) {
286: outValue.add(inIndent + inIndent
287: + StandardDataTypes.getDisplayName(c[j]));
288: }
289: outValue.add(inIndent + inIndent + "}");
290: } else if (nextDctvp.isCommandDoesNotReturn()) {
291: Class c[] = nextDctvp.getClasses();//(Class[])nextCommand[2];
292: outValue
293: .add(conjLabel
294: + "Concept Tag must not return one of the following {");
295: for (int j = 0; j < c.length; j++) {
296: outValue.add(inIndent
297: + StandardDataTypes.getDisplayName(c[j]));
298: }
299: outValue.add(inIndent + "}");
300: } else if (nextDctvp.isCommandSubValidate()) {
301: DynamicConceptTagValidator subDctv = nextDctvp
302: .getSubValidator();
303: // (DynamicConceptTagValidator)nextCommand[2];
304: //
305: ArrayList subMessage = subDctv.getMessages(inIndent
306: + "(", ")", inIndent);
307: //
308: outValue.addAll(subMessage);
309: } else {
310: throw new UnexpectedSystemException("Unknown Command '"
311: + nextDctvp.getCommandType() + "'");
312: }
313: }
314:
315: //
316: if (outValue.size() > 0) {
317: String firstValue = outValue.get(0).toString();
318: outValue.remove(0);
319: outValue.add(0, inStart + firstValue);
320: }
321:
322: //
323: if (outValue.size() > 0) {
324: String lastValue = outValue.get(outValue.size() - 1)
325: .toString();
326: outValue.remove(outValue.size() - 1);
327: outValue.add(outValue.size(), lastValue + inEnd);
328: }
329:
330: //
331: for (int i = 0; i < outValue.size(); i++) {
332: String nextValue = outValue.get(i).toString();
333: outValue.remove(i);
334: outValue.add(i, (inIndent + nextValue));
335: }
336:
337: return outValue;
338: }
339:
340: //
341: public void allowOnlyTheseClasses(Class inClass, String inError) {
342: //
343: Class c[] = new Class[] { inClass };
344: allowOnlyTheseClasses(c, inError);
345: //
346: //addClassesMustNotBe(StandardDataTypes.getNothingClass(),
347: // "This tag does not return anything");
348: }
349:
350: public void allowOnlyTheseClasses(Class inClass[], String inError) {
351: addCommand(DynamicConceptTagValidatorPart.newCommandIsClass(
352: inClass, inError));
353: // new Object[]{new Integer(COMMAND_IS_CLASS), inError, inClass});
354: }
355:
356: //
357: public void allowReturnOfAnythingExceptNothing() {
358: allowReturnableOnly();
359: addReturnClassesMustBe(StandardDataTypes.getAnyClass(),
360: "Return value must be something");
361: addClassesMustNotBe(StandardDataTypes.getNothingClass(),
362: "This tag does not return anything");
363: }
364:
365: public void allowReturnOfAnythingIncludingNothing() {
366: allowReturnableOnly();
367: addReturnClassesMustBe(StandardDataTypes.getAnyClass(),
368: "Return value must be something");
369: }
370:
371: public void allowReturnOfOnlyBoolean() {
372: allowReturnableOnly();
373: addReturnClassesMustBe(StandardDataTypes.getBooleanClass(),
374: "This tag must return a Boolean");
375: }
376:
377: public void allowReturnOfOnlyDecimal() {
378: allowReturnableOnly();
379: addReturnClassesMustBe(StandardDataTypes.getDecimalClass(),
380: "This tag must return a Decimal");
381: }
382:
383: public void allowReturnOfOnlyString() {
384: allowReturnableOnly();
385: addReturnClassesMustBe(StandardDataTypes.getStringClass(),
386: "This tag must return a String");
387: }
388:
389: public void allowReturnOfOnlyBinary() {
390: allowReturnableOnly();
391: addReturnClassesMustBe(StandardDataTypes.getBinaryClass(),
392: "This tag must return a Binary");
393: }
394:
395: public void allowReturnOfOnlyTimestamp() {
396: allowReturnableOnly();
397: addReturnClassesMustBe(StandardDataTypes.getTimestampClass(),
398: "This tag must return a Timestamp");
399: }
400:
401: public void allowReturnOfOnlyStringOrDecimal() {
402: //
403: Class c[] = new Class[] { StandardDataTypes.getStringClass(),
404: StandardDataTypes.getDecimalClass() };
405: //
406: allowReturnableOnly();
407: addReturnClassesMustBe(c,
408: "This tag must return a String or a Decimal");
409: }
410:
411: public void allowReturnOfOnlyStringOrList() {
412: //
413: Class c[] = new Class[] { StandardDataTypes.getStringClass(),
414: StandardDataTypes.getListClass() };
415: //
416: allowReturnableOnly();
417: addReturnClassesMustBe(c,
418: "This tag must return a String or a List");
419: }
420:
421: //
422: public void allowReturnOfOnlyList() {
423: allowReturnableOnly();
424: addReturnClassesMustBe(StandardDataTypes.getListClass(),
425: "This tag must return a List");
426: }
427:
428: //
429: public void allowReturnOfOnlyMap() {
430: allowReturnableOnly();
431: addReturnClassesMustBe(StandardDataTypes.getMapClass(),
432: "This tag must return a Map");
433: }
434:
435: //
436: public boolean isAndConjunction() {
437: return this .andConjunction;
438: }
439:
440: public boolean isOrConjunction() {
441: return !isAndConjunction();
442: }
443:
444: public void useAndConjunction() {
445: this .andConjunction = true;
446: }
447:
448: public void useOrConjunction() {
449: this .andConjunction = false;
450: }
451:
452: //
453: //public void setResultInverted(boolean inResultInverted) {
454: // this.resultInverted = inResultInverted;
455: //}
456: //public boolean isResultInverted() {
457: // return this.resultInverted;
458: //}
459: //
460: public void addReturnClassesMustBe(Class inClass[], String inError) {
461: addCommand(DynamicConceptTagValidatorPart.newCommandDoesReturn(
462: inClass, inError));
463: // new Object[]{new Integer(COMMAND_DOES_RETURN), inError, inClass});
464: }
465:
466: public void addReturnClassesMustBe(Class inClass, String inError) {
467: addCommand(DynamicConceptTagValidatorPart.newCommandDoesReturn(
468: new Class[] { inClass }, inError));
469: // new Object[]{new Integer(COMMAND_DOES_RETURN), inError,
470: // new Class[]{inClass}});
471: }
472:
473: //
474: public void addClassesMustNotBe(Class inClass[], String inError) {
475: addCommand(DynamicConceptTagValidatorPart
476: .newCommandDoesNotReturn(inClass, inError));
477: // new Object[]{
478: // new Integer(COMMAND_DOES_NOT_RETURN), inError, inClass});
479: }
480:
481: public void addClassesMustNotBe(Class inClass, String inError) {
482: addCommand(DynamicConceptTagValidatorPart
483: .newCommandDoesNotReturn(new Class[] { inClass },
484: inError));
485: // new Object[]{new Integer(COMMAND_DOES_NOT_RETURN), inError,
486: // new Class[]{inClass}});
487: }
488:
489: //
490: public void addMainFlagMustBePresent(String inFlagName,
491: String inError) {
492: addCommand(DynamicConceptTagValidatorPart
493: .newCommandFlagPresent(
494: ConceptTagFlagsHelper.MAIN_NAMESPACE,
495: inFlagName, inError));
496: // new Object[]{new Integer(COMMAND_FLAG_PRESENT), inError,
497: // ConceptTagFlagsHelper.MAIN_NAMESPACE, inFlagName});
498: }
499:
500: public void addMainFlagMustNotBePresent(String inFlagName,
501: String inError) {
502: addCommand(DynamicConceptTagValidatorPart
503: .newCommandFlagNotPresent(
504: ConceptTagFlagsHelper.MAIN_NAMESPACE,
505: inFlagName, inError));
506: // new Object[]{new Integer(COMMAND_FLAG_NOT_PRESENT), inError,
507: // ConceptTagFlagsHelper.MAIN_NAMESPACE, inFlagName});
508: }
509:
510: //
511: public void assertCanBeDirectChildOfRoot() {
512: addMainFlagMustBePresent(
513: ConceptTagFlagsHelper.FLAG__CAN_BE_DIRECT_CHILD_OF_ROOT,
514: "Only tags that are allowed to be at the base are allowed here");
515: }
516:
517: public void assertCannotBeDirectChildOfRoot() {
518: addMainFlagMustNotBePresent(
519: ConceptTagFlagsHelper.FLAG__CAN_BE_DIRECT_CHILD_OF_ROOT,
520: "Only tags that are not allowed"
521: + " to be at the base are allowed here");
522: }
523:
524: //
525: public void assertCannotBeHtmlConceptTag() {
526: addMainFlagMustNotBePresent(
527: ConceptTagFlagsHelper.FLAG__HTML_CONCEPT_TAG,
528: "Only non-html tags are allowed");
529: }
530:
531: //
532: public void assertCanBeIterativeStructure() {
533: addMainFlagMustBePresent(
534: ConceptTagFlagsHelper.FLAG__CAN_BE_ITERATIVE_STRUCTURE,
535: "Only tags that are iterative structures are allowed here");
536: }
537:
538: //
539: public void filterDirectChildOfRoot() {
540: //
541: this .assertCannotBeDirectChildOfRoot();
542: }
543:
544: public void filterDirectChildOfRootAndHtml() {
545: //
546: this .assertCannotBeDirectChildOfRoot();
547: this .assertCannotBeHtmlConceptTag();
548: }
549:
550: //
551: private ArrayList subValidate(
552: SelectionCriteriaForConceptTag inScfct,
553: Class inActualReturnClass,
554: boolean inWideReturnInterpretation) {
555:
556: //boolean outValue = isAndConjunction();
557: ArrayList outValue = new ArrayList();
558:
559: ConceptTagFlags flags = inScfct.getFlags();
560: String ctClassName = inScfct.getCtClassName();
561: //
562: boolean positiveCaseFound = false;
563:
564: SelectionCriteriaForReturnableConceptTag scfrct = null;
565: if (inScfct instanceof SelectionCriteriaForReturnableConceptTag) {
566: scfrct = (SelectionCriteriaForReturnableConceptTag) inScfct;
567: }
568:
569: //
570: Iterator iter = this .commands.iterator();
571: //
572: while (iter.hasNext()
573: && !(positiveCaseFound && !this .andConjunction)) {
574: //
575: DynamicConceptTagValidatorPart nextDctvp = ((DynamicConceptTagValidatorPart) iter
576: .next());
577: //Object nextCommand[] = (Object[])this.commands.get(i);
578: //int nextCommandType = ((Integer)nextCommand[0]).intValue();
579: //String nextCommandError = (String)nextCommand[1];
580: String nextCommandError = nextDctvp.getErrorMessage();
581: //
582: if (nextDctvp.isCommandFlagPresent()) {
583: String flagNs = nextDctvp.getFlagNamespace();
584: String flagName = nextDctvp.getFlagName();
585: //String namespace = (String)nextCommand[2];
586: //String name = (String)nextCommand[3];
587: //
588: if (flags.isFlagPresent(flagNs, flagName)) {
589: positiveCaseFound = true;
590: } else {
591: outValue.add(nextCommandError);
592: }
593: //outValue = updateValidation(
594: // outValue, );
595: } else if (nextDctvp.isCommandFlagNotPresent()) {
596: String flagNs = nextDctvp.getFlagNamespace();
597: String flagName = nextDctvp.getFlagName();
598: //String namespace = (String)nextCommand[2];
599: //String name = (String)nextCommand[3];
600: //
601: if (!flags.isFlagPresent(flagNs, flagName)) {
602: positiveCaseFound = true;
603: } else {
604: outValue.add(nextCommandError);
605: }
606: //outValue = updateValidation(
607: // outValue, );
608: } else if (nextDctvp.isCommandFlagEqual()) {
609: //
610: if (conductFlagComparison(nextDctvp, flags)) {
611: positiveCaseFound = true;
612: } else {
613: outValue.add(nextCommandError);
614: }
615: //
616: //outValue = updateValidation(
617: // outValue, );
618: } else if (nextDctvp.isCommandFlagNotEqual()) {
619: //
620: if (conductFlagComparison(nextDctvp, flags)) {
621: positiveCaseFound = true;
622: } else {
623: outValue.add(nextCommandError);
624: }
625: //
626: //outValue = updateValidation(
627: // outValue, conductFlagComparison(nextCommand, flags));
628: } else if (nextDctvp.isCommandFlagLessThan()) {
629: //
630: if (conductFlagComparison(nextDctvp, flags)) {
631: positiveCaseFound = true;
632: } else {
633: outValue.add(nextCommandError);
634: }
635: //
636: //outValue = updateValidation(
637: // outValue, conductFlagComparison(nextCommand, flags));
638: } else if (nextDctvp.isCommandFlagGreaterThan()) {
639: //
640: if (conductFlagComparison(nextDctvp, flags)) {
641: positiveCaseFound = true;
642: } else {
643: outValue.add(nextCommandError);
644: }
645: //
646: //outValue = updateValidation(
647: // outValue, conductFlagComparison(nextCommand, flags));
648: } else if (nextDctvp.isCommandFlagGreaterThanOrEqual()) {
649: //
650: if (conductFlagComparison(nextDctvp, flags)) {
651: positiveCaseFound = true;
652: } else {
653: outValue.add(nextCommandError);
654: }
655: //
656: //outValue = updateValidation(
657: // outValue, conductFlagComparison(nextCommand, flags));
658: } else if (nextDctvp.isCommandFlagLessThanOrEqual()) {
659: //
660: if (conductFlagComparison(nextDctvp, flags)) {
661: positiveCaseFound = true;
662: } else {
663: outValue.add(nextCommandError);
664: }
665: //
666: //outValue = updateValidation(
667: // outValue, conductFlagComparison(nextCommand, flags));
668: } else if (nextDctvp.isCommandIsClass()) {
669: Class c[] = nextDctvp.getClasses();
670: boolean classFound = false;
671: //
672: for (int j = 0; j < c.length; j++) {
673: classFound |= ((c[j]).getName().equals(ctClassName));
674: }
675: //
676: if (!classFound) {
677: outValue.add(nextCommandError);
678: } else {
679: positiveCaseFound = true;
680: }
681: } else if (nextDctvp.isCommandContainsParentNode()) {
682: throw UnexpectedSystemException.unknownState();
683: } else if (nextDctvp.isCommandDoesNotContainParentNode()) {
684: throw UnexpectedSystemException.unknownState();
685: } else if (nextDctvp.isCommandDoesReturn()) {
686: //
687: if (!scfrct.isAutoValidateForTemplateInterface()) {
688: Class mustClasses[] = nextDctvp.getClasses();
689: Class candidateClasses[] = scfrct
690: .getPossibleReturnClasses();
691: if (inActualReturnClass != null) {
692: candidateClasses = new Class[] { inActualReturnClass };
693: }
694: //
695: if (scfrct != null) {
696: //
697: boolean manditoryCasting = determineIfClassIsReturned(
698: candidateClasses, mustClasses);
699: boolean possibleCasting = determineIfClassIsReturned(
700: mustClasses, candidateClasses);
701: //
702: if (inWideReturnInterpretation
703: && (manditoryCasting || possibleCasting)) {
704: //
705: positiveCaseFound = true;
706: } else if (!inWideReturnInterpretation
707: && manditoryCasting) {
708: //
709: positiveCaseFound = true;
710: } else {
711: outValue.add(nextCommandError);
712: }
713: //outValue = updateValidation(
714: // outValue, determineIfClassIsReturned(scfrct, c));
715: }
716: } else {
717: positiveCaseFound = true;
718: }
719: } else if (nextDctvp.isCommandDoesNotReturn()) {
720: Class mustNotClasses[] = nextDctvp.getClasses();
721: //
722: if (!scfrct.isAutoValidateForTemplateInterface()) {
723: if (inActualReturnClass != null) {
724: Class candidateClasses[] = new Class[] { inActualReturnClass };
725: if (determineIfClassIsReturned(
726: candidateClasses, mustNotClasses)) {
727: //
728: outValue.add(nextCommandError);
729: } else {
730: positiveCaseFound = true;
731: }
732: } else if (scfrct != null) {
733: if (!determineIfClassIsReturned(scfrct
734: .getImpossibleReturnClasses(),
735: mustNotClasses)
736: //
737: && determineIfClassIsReturned(scfrct
738: .getPossibleReturnClasses(),
739: mustNotClasses)) {
740: //
741: outValue.add(nextCommandError);
742: } else {
743: positiveCaseFound = true;
744: }
745: //outValue = updateValidation(
746: // outValue, !determineIfClassIsReturned(scfrct, c));
747: }
748: } else {
749: //
750: positiveCaseFound = true;
751: }
752: } else if (nextDctvp.isCommandSubValidate()) {
753: DynamicConceptTagValidator subDctv = nextDctvp
754: .getSubValidator();
755: //
756: ArrayList subValidationErrors = subDctv.validate(
757: inScfct, inActualReturnClass,
758: inWideReturnInterpretation);
759: if (subValidationErrors.size() > 0) {
760: outValue.add(nextCommandError);
761: } else {
762: //
763: positiveCaseFound = true;
764: }
765: //
766: //
767: //outValue = updateValidation(
768: // outValue, );
769: } else {
770: throw new UnexpectedSystemException("Unknown Command '"
771: + nextDctvp.getCommandType() + "'");
772: }
773: }
774: //
775: //if (this.commands.size() == 0) {
776: // outValue = true;
777: //}
778: //
779: //
780: //
781: //
782: //clear array if or check passes right here
783: if (positiveCaseFound && !this .andConjunction) {
784: outValue.clear();
785: }
786: //
787: //
788: //
789: if (scfrct != null) {
790: if (isReturnableAllowed()) {
791: } else {
792: outValue
793: .add("Tags that return something not allowed here");
794: }
795: //outValue = outValue&isReturnableAllowed();
796: } else {
797: if (isNonReturnableAllowed()) {
798: } else {
799: outValue
800: .add("Tags that do not return something not allowed here");
801: }
802: //outValue = outValue&isNonReturnableAllowed();
803: }
804: //
805:
806: //
807: //outValue = outValue^isResultInverted();
808:
809: return outValue;
810: }
811:
812: //
813: private boolean determineIfClassIsReturned(
814: Class inCandidateCtClasses[], Class inMustBeClass[]) {
815:
816: boolean outValue = false;
817:
818: //
819: // true if any possible class is a sub-class of inClass
820: //
821: for (int i = 0; i < inMustBeClass.length; i++) {
822: Class nextMustBeClass = inMustBeClass[i];
823: for (int j = 0; j < inCandidateCtClasses.length; j++) {
824: Class nextCandidateCtClass = inCandidateCtClasses[j];
825:
826: outValue |= isAssignableFrom(nextMustBeClass,
827: nextCandidateCtClass);
828: }
829: }
830:
831: return outValue;
832: }
833:
834: private final static boolean isAssignableFrom(Class inMustBeClass,
835: Class inCandidateCtClass) {
836: //
837: boolean outValue = false;
838:
839: outValue = (inMustBeClass).isAssignableFrom(inCandidateCtClass);
840:
841: if (inMustBeClass.equals(StandardDataTypes.getNothingClass())
842: && (inCandidateCtClass).equals(StandardDataTypes
843: .getNothingClass())) {
844: //
845: outValue = true;
846: }
847: //else if (inCandidateCtClass.equals(
848: // StandardDataTypes.getNothingClass())
849: // && (inMustBeClass).equals(
850: // StandardDataTypes.getAnyClass())) {
851: // //
852: // outValue = true;
853: //}
854: else if (inMustBeClass.equals(StandardDataTypes.getAnyClass())
855: && (inCandidateCtClass).equals(StandardDataTypes
856: .getNothingClass())) {
857: //
858: outValue = true;
859: }
860:
861: return outValue;
862: }
863:
864: //
865: private boolean conductNumberComparison(
866: DynamicConceptTagValidatorPart inDctvp, Object inValue1,
867: Object inValue2) {
868:
869: boolean outValue = false;
870:
871: if (inValue1 instanceof Double && inValue2 instanceof Double) {
872: Double value1 = (Double) inValue1;
873: Double value2 = (Double) inValue2;
874: if (inDctvp.isCommandFlagLessThan()) {
875: outValue = (value1.doubleValue()) < (value2
876: .doubleValue());
877: } else if (inDctvp.isCommandFlagGreaterThan()) {
878: outValue = (value1.doubleValue()) > (value2
879: .doubleValue());
880: } else if (inDctvp.isCommandFlagGreaterThanOrEqual()) {
881: //inCommand == COMMAND_FLAG_GREATER_THAN_OR_EQUAL) {
882: //
883: outValue = (value1.doubleValue()) >= (value2
884: .doubleValue());
885: } else if (inDctvp.isCommandFlagLessThanOrEqual()) {
886: //
887: outValue = (value1.doubleValue()) <= (value2
888: .doubleValue());
889: } else {
890: throw new UnexpectedSystemException("Unknown Command '"
891: + inDctvp.getCommandType() + "'");
892: }
893: }
894:
895: return outValue;
896: }
897:
898: private boolean conductFlagComparison(
899: DynamicConceptTagValidatorPart inDctvp,
900: ConceptTagFlags inCtf) {
901:
902: boolean outValue = false;
903:
904: //int commandName = ((Integer)inCommand[0]).intValue();
905: //String errorValidation = (String)inCommand[1];
906: //String namespace = (String)inCommand[2];
907: //String name = (String)inCommand[3];
908: //Object value = inCommand[4];
909: String dctvpFlagNamespace = inDctvp.getFlagNamespace();
910: String dctvpFlagName = inDctvp.getFlagName();
911: Object dctvpFlagValue = inDctvp.getFlagValue();
912: //
913: if (inCtf.isFlagPresent(dctvpFlagNamespace, dctvpFlagName)) {
914: Object flagValue = inCtf.getFlag(dctvpFlagNamespace,
915: dctvpFlagName);
916: if (dctvpFlagValue != null && flagValue != null) {
917: //
918: if (inDctvp.isCommandFlagEqual()) {
919: outValue = dctvpFlagValue.equals(flagValue);
920: } else if (inDctvp.isCommandFlagNotEqual()) {
921: outValue = !dctvpFlagValue.equals(flagValue);
922: } else {
923: if (inDctvp.isCommandFlagLessThan()) {
924: conductNumberComparison(inDctvp,
925: dctvpFlagValue, flagValue);
926: } else if (inDctvp.isCommandFlagGreaterThan()) {
927: conductNumberComparison(inDctvp,
928: dctvpFlagValue, flagValue);
929: } else if (inDctvp
930: .isCommandFlagGreaterThanOrEqual()) {
931: //commandName == COMMAND_FLAG_GREATER_THAN_OR_EQUAL) {
932: //
933: conductNumberComparison(inDctvp,
934: dctvpFlagValue, flagValue);
935: } else if (inDctvp.isCommandFlagLessThanOrEqual()) {
936: //
937: conductNumberComparison(inDctvp,
938: dctvpFlagValue, flagValue);
939: } else {
940: throw new UnexpectedSystemException(
941: "Unknown Command '"
942: + inDctvp.getCommandType()
943: + "'");
944: }
945: }
946: } else if (dctvpFlagValue == null && flagValue == null) {
947: outValue = true;
948: } else {
949: outValue = false;
950: }
951: } else {
952: outValue = false;
953: }
954:
955: return outValue;
956: }
957: //
958: //private boolean updateValidation(boolean inValue1, boolean inValue2) {
959: //
960: // boolean outValue = true;
961: //
962: // if (isAndConjunction()) {
963: // outValue = inValue1 & inValue2;
964: // }
965: // else if (isOrConjunction()) {
966: // outValue = inValue1 | inValue2;
967: // }
968: // else {
969: // throw UnexpectedSystemException.unknownState();
970: // }
971: //
972: // return outValue;
973: //}
974: //
975: //public String toString() {
976: // return ("This tag does not meet the validation criteria");
977: //}
978: }
|