KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > AlternativePattern


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: AlternativePattern.java,v 1.5 2004/02/16 22:24:29 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import org.apache.bcel.generic.GOTO;
23 import org.apache.bcel.generic.InstructionHandle;
24 import org.apache.bcel.generic.InstructionList;
25 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
26 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
27 import org.apache.xalan.xsltc.compiler.util.Type;
28 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
29
30 /**
31  * @author Jacek Ambroziak
32  * @author Santiago Pericas-Geertsen
33  */

34 final class AlternativePattern extends Pattern {
35     private final Pattern _left;
36     private final Pattern _right;
37         
38     /**
39      * Construct an alternative pattern. The method <code>setParent</code>
40      * should not be called in this case.
41      */

42     public AlternativePattern(Pattern left, Pattern right) {
43     _left = left;
44     _right = right;
45     }
46         
47     public void setParser(Parser parser) {
48     super.setParser(parser);
49     _left.setParser(parser);
50     _right.setParser(parser);
51     }
52     
53     public Pattern getLeft() {
54     return _left;
55     }
56
57     public Pattern getRight() {
58     return _right;
59     }
60
61     /**
62      * The type of an '|' is not really defined, hence null is returned.
63      */

64     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
65     _left.typeCheck(stable);
66     _right.typeCheck(stable);
67     return null;
68     }
69
70     public double getPriority() {
71     double left = _left.getPriority();
72     double right = _right.getPriority();
73     
74     if (left < right)
75         return(left);
76     else
77         return(right);
78     }
79
80     public String JavaDoc toString() {
81     return "alternative(" + _left + ", " + _right + ')';
82     }
83
84     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
85     final InstructionList il = methodGen.getInstructionList();
86
87     _left.translate(classGen, methodGen);
88     final InstructionHandle gotot = il.append(new GOTO(null));
89     il.append(methodGen.loadContextNode());
90     _right.translate(classGen, methodGen);
91
92     _left._trueList.backPatch(gotot);
93     _left._falseList.backPatch(gotot.getNext());
94
95     _trueList.append(_right._trueList.add(gotot));
96     _falseList.append(_right._falseList);
97     }
98 }
99
Popular Tags