View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-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   * $Id: AlternativePattern.java,v 1.2.4.1 2005/09/01 09:18:46 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import com.sun.org.apache.bcel.internal.generic.GOTO;
27  import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
28  import com.sun.org.apache.bcel.internal.generic.InstructionList;
29  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
30  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
31  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
32  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
33  
34  /**
35   * @author Jacek Ambroziak
36   * @author Santiago Pericas-Geertsen
37   */
38  final class AlternativePattern extends Pattern {
39      private final Pattern _left;
40      private final Pattern _right;
41  
42      /**
43       * Construct an alternative pattern. The method <code>setParent</code>
44       * should not be called in this case.
45       */
46      public AlternativePattern(Pattern left, Pattern right) {
47          _left = left;
48          _right = right;
49      }
50  
51      public void setParser(Parser parser) {
52          super.setParser(parser);
53          _left.setParser(parser);
54          _right.setParser(parser);
55      }
56  
57      public Pattern getLeft() {
58          return _left;
59      }
60  
61      public Pattern getRight() {
62          return _right;
63      }
64  
65      /**
66       * The type of an '|' is not really defined, hence null is returned.
67       */
68      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
69          _left.typeCheck(stable);
70          _right.typeCheck(stable);
71          return null;
72      }
73  
74      public double getPriority() {
75          double left = _left.getPriority();
76          double right = _right.getPriority();
77  
78          if (left < right)
79              return(left);
80          else
81              return(right);
82      }
83  
84      public String toString() {
85          return "alternative(" + _left + ", " + _right + ')';
86      }
87  
88      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
89          final InstructionList il = methodGen.getInstructionList();
90  
91          _left.translate(classGen, methodGen);
92          final InstructionHandle gotot = il.append(new GOTO(null));
93          il.append(methodGen.loadContextNode());
94          _right.translate(classGen, methodGen);
95  
96          _left._trueList.backPatch(gotot);
97          _left._falseList.backPatch(gotot.getNext());
98  
99          _trueList.append(_right._trueList.add(gotot));
100         _falseList.append(_right._falseList);
101     }
102 }