01: package org.kohsuke.rngom.binary;
02:
03: import org.kohsuke.rngom.binary.visitor.PatternFunction;
04: import org.kohsuke.rngom.binary.visitor.PatternVisitor;
05:
06: public class ChoicePattern extends BinaryPattern {
07: ChoicePattern(Pattern p1, Pattern p2) {
08: super (p1.isNullable() || p2.isNullable(), combineHashCode(
09: CHOICE_HASH_CODE, p1.hashCode(), p2.hashCode()), p1, p2);
10: }
11:
12: Pattern expand(SchemaPatternBuilder b) {
13: Pattern ep1 = p1.expand(b);
14: Pattern ep2 = p2.expand(b);
15: if (ep1 != p1 || ep2 != p2)
16: return b.makeChoice(ep1, ep2);
17: else
18: return this ;
19: }
20:
21: boolean containsChoice(Pattern p) {
22: return p1.containsChoice(p) || p2.containsChoice(p);
23: }
24:
25: public void accept(PatternVisitor visitor) {
26: visitor.visitChoice(p1, p2);
27: }
28:
29: public Object apply(PatternFunction f) {
30: return f.caseChoice(this );
31: }
32:
33: void checkRestrictions(int context, DuplicateAttributeDetector dad,
34: Alphabet alpha) throws RestrictionViolationException {
35: if (dad != null)
36: dad.startChoice();
37: p1.checkRestrictions(context, dad, alpha);
38: if (dad != null)
39: dad.alternative();
40: p2.checkRestrictions(context, dad, alpha);
41: if (dad != null)
42: dad.endChoice();
43: }
44:
45: }
|