1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.rngom.binary;
  27 
  28 import org.xml.sax.SAXException;
  29 
  30 import java.util.Collection;
  31 import java.util.List;
  32 import java.util.ArrayList;
  33 
  34 public abstract class BinaryPattern extends Pattern {
  35   protected final Pattern p1;
  36   protected final Pattern p2;
  37 
  38   BinaryPattern(boolean nullable, int hc, Pattern p1, Pattern p2) {
  39     super(nullable, Math.max(p1.getContentType(), p2.getContentType()), hc);
  40     this.p1 = p1;
  41     this.p2 = p2;
  42   }
  43 
  44   void checkRecursion(int depth) throws SAXException {
  45     p1.checkRecursion(depth);
  46     p2.checkRecursion(depth);
  47   }
  48 
  49   void checkRestrictions(int context, DuplicateAttributeDetector dad, Alphabet alpha)
  50     throws RestrictionViolationException {
  51     p1.checkRestrictions(context, dad, alpha);
  52     p2.checkRestrictions(context, dad, alpha);
  53   }
  54 
  55   boolean samePattern(Pattern other) {
  56     if (getClass() != other.getClass())
  57       return false;
  58     BinaryPattern b = (BinaryPattern)other;
  59     return p1 == b.p1 && p2 == b.p2;
  60   }
  61 
  62   public final Pattern getOperand1() {
  63     return p1;
  64   }
  65 
  66   public final Pattern getOperand2() {
  67     return p2;
  68   }
  69 
  70   /**
  71    * Adds all the children of this pattern to the given collection.
  72    *
  73    * <p>
  74    * For example, if this pattern is (A|B|C), it adds A, B, and C
  75    * to the collection, even though internally it's represented
  76    * as (A|(B|C)).
  77    */
  78   public final void fillChildren( Collection col ) {
  79     fillChildren(getClass(),p1,col);
  80     fillChildren(getClass(),p2,col);
  81   }
  82 
  83   /**
  84    * Same as {@link #fillChildren(Collection)} but returns an array.
  85    */
  86   public final Pattern[] getChildren() {
  87       List lst = new ArrayList();
  88       fillChildren(lst);
  89       return (Pattern[]) lst.toArray(new Pattern[lst.size()]);
  90   }
  91 
  92   private void fillChildren( Class c, Pattern p, Collection col ) {
  93     if(p.getClass()==c) {
  94       BinaryPattern bp = (BinaryPattern)p;
  95       bp.fillChildren(c,bp.p1,col);
  96       bp.fillChildren(c,bp.p2,col);
  97     } else {
  98       col.add(p);
  99     }
 100   }
 101 }