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