001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.rngom.binary;
027:
028: import org.xml.sax.SAXException;
029:
030: import java.util.Collection;
031: import java.util.List;
032: import java.util.ArrayList;
033:
034: public abstract class BinaryPattern extends Pattern {
035: protected final Pattern p1;
036: protected final Pattern p2;
037:
038: BinaryPattern(boolean nullable, int hc, Pattern p1, Pattern p2) {
039: super (nullable, Math.max(p1.getContentType(), p2
040: .getContentType()), hc);
041: this .p1 = p1;
042: this .p2 = p2;
043: }
044:
045: void checkRecursion(int depth) throws SAXException {
046: p1.checkRecursion(depth);
047: p2.checkRecursion(depth);
048: }
049:
050: void checkRestrictions(int context, DuplicateAttributeDetector dad,
051: Alphabet alpha) throws RestrictionViolationException {
052: p1.checkRestrictions(context, dad, alpha);
053: p2.checkRestrictions(context, dad, alpha);
054: }
055:
056: boolean samePattern(Pattern other) {
057: if (getClass() != other.getClass())
058: return false;
059: BinaryPattern b = (BinaryPattern) other;
060: return p1 == b.p1 && p2 == b.p2;
061: }
062:
063: public final Pattern getOperand1() {
064: return p1;
065: }
066:
067: public final Pattern getOperand2() {
068: return p2;
069: }
070:
071: /**
072: * Adds all the children of this pattern to the given collection.
073: *
074: * <p>
075: * For example, if this pattern is (A|B|C), it adds A, B, and C
076: * to the collection, even though internally it's represented
077: * as (A|(B|C)).
078: */
079: public final void fillChildren(Collection col) {
080: fillChildren(getClass(), p1, col);
081: fillChildren(getClass(), p2, col);
082: }
083:
084: /**
085: * Same as {@link #fillChildren(Collection)} but returns an array.
086: */
087: public final Pattern[] getChildren() {
088: List lst = new ArrayList();
089: fillChildren(lst);
090: return (Pattern[]) lst.toArray(new Pattern[lst.size()]);
091: }
092:
093: private void fillChildren(Class c, Pattern p, Collection col) {
094: if (p.getClass() == c) {
095: BinaryPattern bp = (BinaryPattern) p;
096: bp.fillChildren(c, bp.p1, col);
097: bp.fillChildren(c, bp.p2, col);
098: } else {
099: col.add(p);
100: }
101: }
102: }
|