View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 1999-2002,2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.impl.xs.models;
22  
23  import com.sun.org.apache.xerces.internal.impl.dtd.models.CMNode;
24  import com.sun.org.apache.xerces.internal.impl.dtd.models.CMStateSet;
25  import com.sun.org.apache.xerces.internal.impl.xs.XSModelGroupImpl;
26  
27  /**
28   *
29   * Content model Bin-Op node.
30   *
31   * @xerces.internal
32   *
33   * @author Neil Graham, IBM
34   */
35  public class XSCMBinOp extends CMNode {
36      // -------------------------------------------------------------------
37      //  Constructors
38      // -------------------------------------------------------------------
39      public XSCMBinOp(int type, CMNode leftNode, CMNode rightNode)
40      {
41          super(type);
42  
43          // Insure that its one of the types we require
44          if ((type() != XSModelGroupImpl.MODELGROUP_CHOICE)
45          &&  (type() != XSModelGroupImpl.MODELGROUP_SEQUENCE)) {
46              throw new RuntimeException("ImplementationMessages.VAL_BST");
47          }
48  
49          // Store the nodes and init any data that needs it
50          fLeftChild = leftNode;
51          fRightChild = rightNode;
52      }
53  
54  
55      // -------------------------------------------------------------------
56      //  Package, final methods
57      // -------------------------------------------------------------------
58      final CMNode getLeft() {
59          return fLeftChild;
60      }
61  
62      final CMNode getRight() {
63          return fRightChild;
64      }
65  
66  
67      // -------------------------------------------------------------------
68      //  Package, inherited methods
69      // -------------------------------------------------------------------
70      public boolean isNullable() {
71          //
72          //  If its an alternation, then if either child is nullable then
73          //  this node is nullable. If its a concatenation, then both of
74          //  them have to be nullable.
75          //
76          if (type() == XSModelGroupImpl.MODELGROUP_CHOICE)
77              return (fLeftChild.isNullable() || fRightChild.isNullable());
78          else if (type() == XSModelGroupImpl.MODELGROUP_SEQUENCE)
79              return (fLeftChild.isNullable() && fRightChild.isNullable());
80          else
81              throw new RuntimeException("ImplementationMessages.VAL_BST");
82      }
83  
84  
85      // -------------------------------------------------------------------
86      //  Protected, inherited methods
87      // -------------------------------------------------------------------
88      protected void calcFirstPos(CMStateSet toSet) {
89          if (type() == XSModelGroupImpl.MODELGROUP_CHOICE) {
90              // Its the the union of the first positions of our children.
91              toSet.setTo(fLeftChild.firstPos());
92              toSet.union(fRightChild.firstPos());
93          }
94           else if (type() == XSModelGroupImpl.MODELGROUP_SEQUENCE) {
95              //
96              //  If our left child is nullable, then its the union of our
97              //  children's first positions. Else is our left child's first
98              //  positions.
99              //
100             toSet.setTo(fLeftChild.firstPos());
101             if (fLeftChild.isNullable())
102                 toSet.union(fRightChild.firstPos());
103         }
104          else {
105             throw new RuntimeException("ImplementationMessages.VAL_BST");
106         }
107     }
108 
109     protected void calcLastPos(CMStateSet toSet) {
110         if (type() == XSModelGroupImpl.MODELGROUP_CHOICE) {
111             // Its the the union of the first positions of our children.
112             toSet.setTo(fLeftChild.lastPos());
113             toSet.union(fRightChild.lastPos());
114         }
115         else if (type() == XSModelGroupImpl.MODELGROUP_SEQUENCE) {
116             //
117             //  If our right child is nullable, then its the union of our
118             //  children's last positions. Else is our right child's last
119             //  positions.
120             //
121             toSet.setTo(fRightChild.lastPos());
122             if (fRightChild.isNullable())
123                 toSet.union(fLeftChild.lastPos());
124         }
125         else {
126             throw new RuntimeException("ImplementationMessages.VAL_BST");
127         }
128     }
129 
130 
131     // -------------------------------------------------------------------
132     //  Private data members
133     //
134     //  fLeftChild
135     //  fRightChild
136     //      These are the references to the two nodes that are on either
137     //      side of this binary operation.
138     // -------------------------------------------------------------------
139     private CMNode  fLeftChild;
140     private CMNode  fRightChild;
141 } // XSCMBinOp