KickJava   Java API By Example, From Geeks To Geeks.

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


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: DocumentCall.java,v 1.19 2004/02/16 22:24:29 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import java.util.Vector JavaDoc;
23
24 import org.apache.bcel.generic.ConstantPoolGen;
25 import org.apache.bcel.generic.GETFIELD;
26 import org.apache.bcel.generic.INVOKEINTERFACE;
27 import org.apache.bcel.generic.INVOKESTATIC;
28 import org.apache.bcel.generic.InstructionList;
29 import org.apache.bcel.generic.PUSH;
30 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
31 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
32 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
33 import org.apache.xalan.xsltc.compiler.util.Type;
34 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
35
36 /**
37  * @author Jacek Ambroziak
38  * @author Morten Jorgensen
39  */

40 final class DocumentCall extends FunctionCall {
41
42     private Expression _arg1 = null;
43     private Expression _arg2 = null;
44     private Type _arg1Type;
45
46     /**
47      * Default function call constructor
48      */

49     public DocumentCall(QName fname, Vector JavaDoc arguments) {
50         super(fname, arguments);
51     }
52
53     /**
54      * Type checks the arguments passed to the document() function. The first
55      * argument can be any type (we must cast it to a string) and contains the
56      * URI of the document
57      */

58     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
59         // At least one argument - two at most
60
final int ac = argumentCount();
61         if ((ac < 1) || (ac > 2)) {
62             ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_ARG_ERR, this);
63             throw new TypeCheckError(msg);
64         }
65         if (getStylesheet() == null) {
66             ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_ARG_ERR, this);
67             throw new TypeCheckError(msg);
68         }
69
70         // Parse the first argument
71
_arg1 = argument(0);
72
73         if (_arg1 == null) {// should not happened
74
ErrorMsg msg = new ErrorMsg(ErrorMsg.DOCUMENT_ARG_ERR, this);
75             throw new TypeCheckError(msg);
76         }
77
78         _arg1Type = _arg1.typeCheck(stable);
79         if ((_arg1Type != Type.NodeSet) && (_arg1Type != Type.String)) {
80             _arg1 = new CastExpr(_arg1, Type.String);
81         }
82
83         // Parse the second argument
84
if (ac == 2) {
85             _arg2 = argument(1);
86
87             if (_arg2 == null) {// should not happened
88
ErrorMsg msg = new ErrorMsg(ErrorMsg.DOCUMENT_ARG_ERR, this);
89                 throw new TypeCheckError(msg);
90             }
91
92             final Type arg2Type = _arg2.typeCheck(stable);
93
94             if (arg2Type.identicalTo(Type.Node)) {
95                 _arg2 = new CastExpr(_arg2, Type.NodeSet);
96             } else if (arg2Type.identicalTo(Type.NodeSet)) {
97                 // falls through
98
} else {
99                 ErrorMsg msg = new ErrorMsg(ErrorMsg.DOCUMENT_ARG_ERR, this);
100                 throw new TypeCheckError(msg);
101             }
102         }
103
104         return _type = Type.NodeSet;
105     }
106     
107     /**
108      * Translates the document() function call to a call to LoadDocument()'s
109      * static method document().
110      */

111     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
112         final ConstantPoolGen cpg = classGen.getConstantPool();
113         final InstructionList il = methodGen.getInstructionList();
114         final int ac = argumentCount();
115
116         final int domField = cpg.addFieldref(classGen.getClassName(),
117                                              DOM_FIELD,
118                                              DOM_INTF_SIG);
119           
120         String JavaDoc docParamList = null;
121         if (ac == 1) {
122            // documentF(Object,String,AbstractTranslet,DOM)
123
docParamList = "("+OBJECT_SIG+STRING_SIG+TRANSLET_SIG+DOM_INTF_SIG
124                          +")"+NODE_ITERATOR_SIG;
125         } else { //ac == 2; ac < 1 or as >2 was tested in typeChec()
126
// documentF(Object,DTMAxisIterator,String,AbstractTranslet,DOM)
127
docParamList = "("+OBJECT_SIG+NODE_ITERATOR_SIG+STRING_SIG
128                          +TRANSLET_SIG+DOM_INTF_SIG+")"+NODE_ITERATOR_SIG;
129         }
130         final int docIdx = cpg.addMethodref(LOAD_DOCUMENT_CLASS, "documentF",
131                                             docParamList);
132
133
134         // The URI can be either a node-set or something else cast to a string
135
_arg1.translate(classGen, methodGen);
136         if (_arg1Type == Type.NodeSet) {
137             _arg1.startIterator(classGen, methodGen);
138         }
139
140         if (ac == 2) {
141             //_arg2 == null was tested in typeChec()
142
_arg2.translate(classGen, methodGen);
143             _arg2.startIterator(classGen, methodGen);
144         }
145     
146         // Feck the rest of the parameters on the stack
147
il.append(new PUSH(cpg, getStylesheet().getSystemId()));
148         il.append(classGen.loadTranslet());
149         il.append(DUP);
150         il.append(new GETFIELD(domField));
151         il.append(new INVOKESTATIC(docIdx));
152     }
153
154 }
155
Popular Tags