01: package org.kohsuke.rngom.binary;
02:
03: import org.xml.sax.SAXException;
04:
05: import java.util.Collection;
06: import java.util.List;
07: import java.util.ArrayList;
08:
09: public abstract class BinaryPattern extends Pattern {
10: protected final Pattern p1;
11: protected final Pattern p2;
12:
13: BinaryPattern(boolean nullable, int hc, Pattern p1, Pattern p2) {
14: super (nullable, Math.max(p1.getContentType(), p2
15: .getContentType()), hc);
16: this .p1 = p1;
17: this .p2 = p2;
18: }
19:
20: void checkRecursion(int depth) throws SAXException {
21: p1.checkRecursion(depth);
22: p2.checkRecursion(depth);
23: }
24:
25: void checkRestrictions(int context, DuplicateAttributeDetector dad,
26: Alphabet alpha) throws RestrictionViolationException {
27: p1.checkRestrictions(context, dad, alpha);
28: p2.checkRestrictions(context, dad, alpha);
29: }
30:
31: boolean samePattern(Pattern other) {
32: if (getClass() != other.getClass())
33: return false;
34: BinaryPattern b = (BinaryPattern) other;
35: return p1 == b.p1 && p2 == b.p2;
36: }
37:
38: public final Pattern getOperand1() {
39: return p1;
40: }
41:
42: public final Pattern getOperand2() {
43: return p2;
44: }
45:
46: /**
47: * Adds all the children of this pattern to the given collection.
48: *
49: * <p>
50: * For example, if this pattern is (A|B|C), it adds A, B, and C
51: * to the collection, even though internally it's represented
52: * as (A|(B|C)).
53: */
54: public final void fillChildren(Collection col) {
55: fillChildren(getClass(), p1, col);
56: fillChildren(getClass(), p2, col);
57: }
58:
59: /**
60: * Same as {@link #fillChildren(Collection)} but returns an array.
61: */
62: public final Pattern[] getChildren() {
63: List lst = new ArrayList();
64: fillChildren(lst);
65: return (Pattern[]) lst.toArray(new Pattern[lst.size()]);
66: }
67:
68: private void fillChildren(Class c, Pattern p, Collection col) {
69: if (p.getClass() == c) {
70: BinaryPattern bp = (BinaryPattern) p;
71: bp.fillChildren(c, bp.p1, col);
72: bp.fillChildren(c, bp.p2, col);
73: } else {
74: col.add(p);
75: }
76: }
77: }
|