001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.dtd.models;
019:
020: import org.apache.xerces.impl.dtd.XMLContentSpec;
021:
022: /**
023: * Content model Bin-Op node.
024: *
025: * @xerces.internal
026: *
027: * @version $Id: CMBinOp.java 572057 2007-09-02 18:03:20Z mrglavas $
028: */
029: public class CMBinOp extends CMNode {
030: // -------------------------------------------------------------------
031: // Constructors
032: // -------------------------------------------------------------------
033: public CMBinOp(int type, CMNode leftNode, CMNode rightNode) {
034: super (type);
035:
036: // Insure that its one of the types we require
037: if ((type() != XMLContentSpec.CONTENTSPECNODE_CHOICE)
038: && (type() != XMLContentSpec.CONTENTSPECNODE_SEQ)) {
039: throw new RuntimeException("ImplementationMessages.VAL_BST");
040: }
041:
042: // Store the nodes and init any data that needs it
043: fLeftChild = leftNode;
044: fRightChild = rightNode;
045: }
046:
047: // -------------------------------------------------------------------
048: // Package, final methods
049: // -------------------------------------------------------------------
050: final CMNode getLeft() {
051: return fLeftChild;
052: }
053:
054: final CMNode getRight() {
055: return fRightChild;
056: }
057:
058: // -------------------------------------------------------------------
059: // Package, inherited methods
060: // -------------------------------------------------------------------
061: public boolean isNullable() {
062: //
063: // If its an alternation, then if either child is nullable then
064: // this node is nullable. If its a concatenation, then both of
065: // them have to be nullable.
066: //
067: if (type() == XMLContentSpec.CONTENTSPECNODE_CHOICE)
068: return (fLeftChild.isNullable() || fRightChild.isNullable());
069: else if (type() == XMLContentSpec.CONTENTSPECNODE_SEQ)
070: return (fLeftChild.isNullable() && fRightChild.isNullable());
071: else
072: throw new RuntimeException("ImplementationMessages.VAL_BST");
073: }
074:
075: // -------------------------------------------------------------------
076: // Protected, inherited methods
077: // -------------------------------------------------------------------
078: protected void calcFirstPos(CMStateSet toSet) {
079: if (type() == XMLContentSpec.CONTENTSPECNODE_CHOICE) {
080: // Its the the union of the first positions of our children.
081: toSet.setTo(fLeftChild.firstPos());
082: toSet.union(fRightChild.firstPos());
083: } else if (type() == XMLContentSpec.CONTENTSPECNODE_SEQ) {
084: //
085: // If our left child is nullable, then its the union of our
086: // children's first positions. Else is our left child's first
087: // positions.
088: //
089: toSet.setTo(fLeftChild.firstPos());
090: if (fLeftChild.isNullable())
091: toSet.union(fRightChild.firstPos());
092: } else {
093: throw new RuntimeException("ImplementationMessages.VAL_BST");
094: }
095: }
096:
097: protected void calcLastPos(CMStateSet toSet) {
098: if (type() == XMLContentSpec.CONTENTSPECNODE_CHOICE) {
099: // Its the the union of the first positions of our children.
100: toSet.setTo(fLeftChild.lastPos());
101: toSet.union(fRightChild.lastPos());
102: } else if (type() == XMLContentSpec.CONTENTSPECNODE_SEQ) {
103: //
104: // If our right child is nullable, then its the union of our
105: // children's last positions. Else is our right child's last
106: // positions.
107: //
108: toSet.setTo(fRightChild.lastPos());
109: if (fRightChild.isNullable())
110: toSet.union(fLeftChild.lastPos());
111: } else {
112: throw new RuntimeException("ImplementationMessages.VAL_BST");
113: }
114: }
115:
116: // -------------------------------------------------------------------
117: // Private data members
118: //
119: // fLeftChild
120: // fRightChild
121: // These are the references to the two nodes that are on either
122: // side of this binary operation.
123: // -------------------------------------------------------------------
124: private final CMNode fLeftChild;
125: private final CMNode fRightChild;
126: };
|