001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: ForEach.java,v 1.19 2004/02/16 22:24:29 minchau Exp $
018: */
019:
020: package org.apache.xalan.xsltc.compiler;
021:
022: import java.util.Enumeration;
023: import java.util.Vector;
024:
025: import org.apache.bcel.generic.BranchHandle;
026: import org.apache.bcel.generic.ConstantPoolGen;
027: import org.apache.bcel.generic.GOTO;
028: import org.apache.bcel.generic.IFGT;
029: import org.apache.bcel.generic.InstructionHandle;
030: import org.apache.bcel.generic.InstructionList;
031: import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
032: import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
033: import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
034: import org.apache.xalan.xsltc.compiler.util.NodeSetType;
035: import org.apache.xalan.xsltc.compiler.util.NodeType;
036: import org.apache.xalan.xsltc.compiler.util.ReferenceType;
037: import org.apache.xalan.xsltc.compiler.util.ResultTreeType;
038: import org.apache.xalan.xsltc.compiler.util.Type;
039: import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
040: import org.apache.xalan.xsltc.compiler.util.Util;
041:
042: /**
043: * @author Jacek Ambroziak
044: * @author Santiago Pericas-Geertsen
045: * @author Morten Jorgensen
046: */
047: final class ForEach extends Instruction {
048:
049: private Expression _select;
050: private Type _type;
051:
052: public void display(int indent) {
053: indent(indent);
054: Util.println("ForEach");
055: indent(indent + IndentIncrement);
056: Util.println("select " + _select.toString());
057: displayContents(indent + IndentIncrement);
058: }
059:
060: public void parseContents(Parser parser) {
061: _select = parser.parseExpression(this , "select", null);
062:
063: parseChildren(parser);
064:
065: // make sure required attribute(s) have been set
066: if (_select.isDummy()) {
067: reportError(this , parser, ErrorMsg.REQUIRED_ATTR_ERR,
068: "select");
069: }
070: }
071:
072: public Type typeCheck(SymbolTable stable) throws TypeCheckError {
073: _type = _select.typeCheck(stable);
074:
075: if (_type instanceof ReferenceType || _type instanceof NodeType) {
076: _select = new CastExpr(_select, Type.NodeSet);
077: typeCheckContents(stable);
078: return Type.Void;
079: }
080: if (_type instanceof NodeSetType
081: || _type instanceof ResultTreeType) {
082: typeCheckContents(stable);
083: return Type.Void;
084: }
085: throw new TypeCheckError(this );
086: }
087:
088: public void translate(ClassGenerator classGen,
089: MethodGenerator methodGen) {
090: final ConstantPoolGen cpg = classGen.getConstantPool();
091: final InstructionList il = methodGen.getInstructionList();
092:
093: // Save current node and current iterator on the stack
094: il.append(methodGen.loadCurrentNode());
095: il.append(methodGen.loadIterator());
096:
097: // Collect sort objects associated with this instruction
098: final Vector sortObjects = new Vector();
099: Enumeration children = elements();
100: while (children.hasMoreElements()) {
101: final Object child = children.nextElement();
102: if (child instanceof Sort) {
103: sortObjects.addElement(child);
104: }
105: }
106:
107: if ((_type != null) && (_type instanceof ResultTreeType)) {
108: // Store existing DOM on stack - must be restored when loop is done
109: il.append(methodGen.loadDOM());
110:
111: // <xsl:sort> cannot be applied to a result tree - issue warning
112: if (sortObjects.size() > 0) {
113: ErrorMsg msg = new ErrorMsg(
114: ErrorMsg.RESULT_TREE_SORT_ERR, this );
115: getParser().reportError(WARNING, msg);
116: }
117:
118: // Put the result tree on the stack (DOM)
119: _select.translate(classGen, methodGen);
120: // Get an iterator for the whole DOM - excluding the root node
121: _type.translateTo(classGen, methodGen, Type.NodeSet);
122: // Store the result tree as the default DOM
123: il.append(SWAP);
124: il.append(methodGen.storeDOM());
125: } else {
126: // Compile node iterator
127: if (sortObjects.size() > 0) {
128: Sort.translateSortIterator(classGen, methodGen,
129: _select, sortObjects);
130: } else {
131: _select.translate(classGen, methodGen);
132: }
133:
134: if (_type instanceof ReferenceType == false) {
135: il.append(methodGen.loadContextNode());
136: il.append(methodGen.setStartNode());
137: }
138: }
139:
140: // Overwrite current iterator
141: il.append(methodGen.storeIterator());
142:
143: // Give local variables (if any) default values before starting loop
144: initializeVariables(classGen, methodGen);
145:
146: final BranchHandle nextNode = il.append(new GOTO(null));
147: final InstructionHandle loop = il.append(NOP);
148:
149: translateContents(classGen, methodGen);
150:
151: nextNode.setTarget(il.append(methodGen.loadIterator()));
152: il.append(methodGen.nextNode());
153: il.append(DUP);
154: il.append(methodGen.storeCurrentNode());
155: il.append(new IFGT(loop));
156:
157: // Restore current DOM (if result tree was used instead for this loop)
158: if ((_type != null) && (_type instanceof ResultTreeType)) {
159: il.append(methodGen.storeDOM());
160: }
161:
162: // Restore current node and current iterator from the stack
163: il.append(methodGen.storeIterator());
164: il.append(methodGen.storeCurrentNode());
165: }
166:
167: /**
168: * The code that is generated by nested for-each loops can appear to some
169: * JVMs as if it is accessing un-initialized variables. We must add some
170: * code that pushes the default variable value on the stack and pops it
171: * into the variable slot. This is done by the Variable.initialize()
172: * method. The code that we compile for this loop looks like this:
173: *
174: * initialize iterator
175: * initialize variables <-- HERE!!!
176: * goto Iterate
177: * Loop: :
178: * : (code for <xsl:for-each> contents)
179: * :
180: * Iterate: node = iterator.next();
181: * if (node != END) goto Loop
182: */
183: public void initializeVariables(ClassGenerator classGen,
184: MethodGenerator methodGen) {
185: final int n = elementCount();
186: for (int i = 0; i < n; i++) {
187: final Object child = getContents().elementAt(i);
188: if (child instanceof Variable) {
189: Variable var = (Variable) child;
190: var.initialize(classGen, methodGen);
191: }
192: }
193: }
194:
195: }
|