286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: Key.java,v 1.6 2006/04/25 02:25:08 jeffsuttor Exp $
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.compiler;
286N/A
286N/Aimport java.util.Vector;
286N/A
286N/Aimport com.sun.org.apache.bcel.internal.generic.BranchHandle;
286N/Aimport com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
286N/Aimport com.sun.org.apache.bcel.internal.generic.GOTO;
286N/Aimport com.sun.org.apache.bcel.internal.generic.IFEQ;
286N/Aimport com.sun.org.apache.bcel.internal.generic.IFGE;
286N/Aimport com.sun.org.apache.bcel.internal.generic.IFGT;
286N/Aimport com.sun.org.apache.bcel.internal.generic.ILOAD;
286N/Aimport com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
286N/Aimport com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
286N/Aimport com.sun.org.apache.bcel.internal.generic.ISTORE;
286N/Aimport com.sun.org.apache.bcel.internal.generic.InstructionHandle;
286N/Aimport com.sun.org.apache.bcel.internal.generic.InstructionList;
286N/Aimport com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
286N/Aimport com.sun.org.apache.bcel.internal.generic.PUSH;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.StringType;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
286N/Aimport com.sun.org.apache.xml.internal.dtm.Axis;
286N/Aimport com.sun.org.apache.xml.internal.utils.XML11Char;
286N/A
286N/A/**
286N/A * @author Morten Jorgensen
286N/A * @author Santiago Pericas-Geertsen
286N/A */
286N/Afinal class Key extends TopLevelElement {
286N/A
286N/A /**
286N/A * The name of this key as defined in xsl:key.
286N/A */
286N/A private QName _name;
286N/A
286N/A /**
286N/A * The pattern to match starting at the root node.
286N/A */
286N/A private Pattern _match;
286N/A
286N/A /**
286N/A * The expression that generates the values for this key.
286N/A */
286N/A private Expression _use;
286N/A
286N/A /**
286N/A * The type of the _use expression.
286N/A */
286N/A private Type _useType;
286N/A
286N/A /**
286N/A * Parse the <xsl:key> element and attributes
286N/A * @param parser A reference to the stylesheet parser
286N/A */
286N/A public void parseContents(Parser parser) {
286N/A
286N/A // Get the required attributes and parser XPath expressions
286N/A final String name = getAttribute("name");
286N/A if (!XML11Char.isXML11ValidQName(name)){
286N/A ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this);
286N/A parser.reportError(Constants.ERROR, err);
286N/A }
286N/A
286N/A // Parse key name and add to symbol table
286N/A _name = parser.getQNameIgnoreDefaultNs(name);
286N/A getSymbolTable().addKey(_name, this);
286N/A
286N/A _match = parser.parsePattern(this, "match", null);
286N/A _use = parser.parseExpression(this, "use", null);
286N/A
286N/A // Make sure required attribute(s) have been set
286N/A if (_name == null) {
286N/A reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "name");
286N/A return;
286N/A }
286N/A if (_match.isDummy()) {
286N/A reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "match");
286N/A return;
286N/A }
286N/A if (_use.isDummy()) {
286N/A reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "use");
286N/A return;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Returns a String-representation of this key's name
286N/A * @return The key's name (from the <xsl:key> elements 'name' attribute).
286N/A */
286N/A public String getName() {
286N/A return _name.toString();
286N/A }
286N/A
286N/A public Type typeCheck(SymbolTable stable) throws TypeCheckError {
286N/A // Type check match pattern
286N/A _match.typeCheck(stable);
286N/A
286N/A // Cast node values to string values (except for nodesets)
286N/A _useType = _use.typeCheck(stable);
286N/A if (_useType instanceof StringType == false &&
286N/A _useType instanceof NodeSetType == false)
286N/A {
286N/A _use = new CastExpr(_use, Type.String);
286N/A }
286N/A
286N/A return Type.Void;
286N/A }
286N/A
286N/A /**
286N/A * This method is called if the "use" attribute of the key contains a
286N/A * node set. In this case we must traverse all nodes in the set and
286N/A * create one entry in this key's index for each node in the set.
286N/A */
286N/A public void traverseNodeSet(ClassGenerator classGen,
286N/A MethodGenerator methodGen,
286N/A int buildKeyIndex) {
286N/A final ConstantPoolGen cpg = classGen.getConstantPool();
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A
286N/A // DOM.getStringValueX(nodeIndex) => String
286N/A final int getNodeValue = cpg.addInterfaceMethodref(DOM_INTF,
286N/A GET_NODE_VALUE,
286N/A "(I)"+STRING_SIG);
286N/A
286N/A final int getNodeIdent = cpg.addInterfaceMethodref(DOM_INTF,
286N/A "getNodeIdent",
286N/A "(I)"+NODE_SIG);
286N/A
286N/A // AbstractTranslet.SetKeyIndexDom(name, Dom) => void
286N/A final int keyDom = cpg.addMethodref(TRANSLET_CLASS,
286N/A "setKeyIndexDom",
286N/A "("+STRING_SIG+DOM_INTF_SIG+")V");
286N/A
286N/A
286N/A // This variable holds the id of the node we found with the "match"
286N/A // attribute of xsl:key. This is the id we store, with the value we
286N/A // get from the nodes we find here, in the index for this key.
286N/A final LocalVariableGen parentNode =
286N/A methodGen.addLocalVariable("parentNode",
286N/A Util.getJCRefType("I"),
286N/A null, null);
286N/A
286N/A // Get the 'parameter' from the stack and store it in a local var.
286N/A parentNode.setStart(il.append(new ISTORE(parentNode.getIndex())));
286N/A
286N/A // Save current node and current iterator on the stack
286N/A il.append(methodGen.loadCurrentNode());
286N/A il.append(methodGen.loadIterator());
286N/A
286N/A // Overwrite current iterator with one that gives us only what we want
286N/A _use.translate(classGen, methodGen);
286N/A _use.startIterator(classGen, methodGen);
286N/A il.append(methodGen.storeIterator());
286N/A
286N/A final BranchHandle nextNode = il.append(new GOTO(null));
286N/A final InstructionHandle loop = il.append(NOP);
286N/A
286N/A // Prepare to call buildKeyIndex(String name, int node, String value);
286N/A il.append(classGen.loadTranslet());
286N/A il.append(new PUSH(cpg, _name.toString()));
286N/A parentNode.setEnd(il.append(new ILOAD(parentNode.getIndex())));
286N/A
286N/A // Now get the node value and push it on the parameter stack
286N/A il.append(methodGen.loadDOM());
286N/A il.append(methodGen.loadCurrentNode());
286N/A il.append(new INVOKEINTERFACE(getNodeValue, 2));
286N/A
286N/A // Finally do the call to add an entry in the index for this key.
286N/A il.append(new INVOKEVIRTUAL(buildKeyIndex));
286N/A
286N/A il.append(classGen.loadTranslet());
286N/A il.append(new PUSH(cpg, getName()));
286N/A il.append(methodGen.loadDOM());
286N/A il.append(new INVOKEVIRTUAL(keyDom));
286N/A
286N/A nextNode.setTarget(il.append(methodGen.loadIterator()));
286N/A il.append(methodGen.nextNode());
286N/A
286N/A il.append(DUP);
286N/A il.append(methodGen.storeCurrentNode());
286N/A il.append(new IFGE(loop)); // Go on to next matching node....
286N/A
286N/A // Restore current node and current iterator from the stack
286N/A il.append(methodGen.storeIterator());
286N/A il.append(methodGen.storeCurrentNode());
286N/A }
286N/A
286N/A /**
286N/A * Gather all nodes that match the expression in the attribute "match"
286N/A * and add one (or more) entries in this key's index.
286N/A */
286N/A public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
286N/A
286N/A final ConstantPoolGen cpg = classGen.getConstantPool();
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A final int current = methodGen.getLocalIndex("current");
286N/A
286N/A // AbstractTranslet.buildKeyIndex(name,node_id,value) => void
286N/A final int key = cpg.addMethodref(TRANSLET_CLASS,
286N/A "buildKeyIndex",
286N/A "("+STRING_SIG+"I"+OBJECT_SIG+")V");
286N/A
286N/A // AbstractTranslet.SetKeyIndexDom(name, Dom) => void
286N/A final int keyDom = cpg.addMethodref(TRANSLET_CLASS,
286N/A "setKeyIndexDom",
286N/A "("+STRING_SIG+DOM_INTF_SIG+")V");
286N/A
286N/A final int getNodeIdent = cpg.addInterfaceMethodref(DOM_INTF,
286N/A "getNodeIdent",
286N/A "(I)"+NODE_SIG);
286N/A
286N/A // DOM.getAxisIterator(root) => NodeIterator
286N/A final int git = cpg.addInterfaceMethodref(DOM_INTF,
286N/A "getAxisIterator",
286N/A "(I)"+NODE_ITERATOR_SIG);
286N/A
286N/A il.append(methodGen.loadCurrentNode());
286N/A il.append(methodGen.loadIterator());
286N/A
286N/A // Get an iterator for all nodes in the DOM
286N/A il.append(methodGen.loadDOM());
286N/A il.append(new PUSH(cpg,Axis.DESCENDANT));
286N/A il.append(new INVOKEINTERFACE(git, 2));
286N/A
286N/A // Reset the iterator to start with the root node
286N/A il.append(methodGen.loadCurrentNode());
286N/A il.append(methodGen.setStartNode());
286N/A il.append(methodGen.storeIterator());
286N/A
286N/A // Loop for traversing all nodes in the DOM
286N/A final BranchHandle nextNode = il.append(new GOTO(null));
286N/A final InstructionHandle loop = il.append(NOP);
286N/A
286N/A // Check if the current node matches the pattern in "match"
286N/A il.append(methodGen.loadCurrentNode());
286N/A _match.translate(classGen, methodGen);
286N/A _match.synthesize(classGen, methodGen); // Leaves 0 or 1 on stack
286N/A final BranchHandle skipNode = il.append(new IFEQ(null));
286N/A
286N/A // If this is a node-set we must go through each node in the set
286N/A if (_useType instanceof NodeSetType) {
286N/A // Pass current node as parameter (we're indexing on that node)
286N/A il.append(methodGen.loadCurrentNode());
286N/A traverseNodeSet(classGen, methodGen, key);
286N/A }
286N/A else {
286N/A il.append(classGen.loadTranslet());
286N/A il.append(DUP);
286N/A il.append(new PUSH(cpg, _name.toString()));
286N/A il.append(DUP_X1);
286N/A il.append(methodGen.loadCurrentNode());
286N/A _use.translate(classGen, methodGen);
286N/A il.append(new INVOKEVIRTUAL(key));
286N/A
286N/A il.append(methodGen.loadDOM());
286N/A il.append(new INVOKEVIRTUAL(keyDom));
286N/A }
286N/A
286N/A // Get the next node from the iterator and do loop again...
286N/A final InstructionHandle skip = il.append(NOP);
286N/A
286N/A il.append(methodGen.loadIterator());
286N/A il.append(methodGen.nextNode());
286N/A il.append(DUP);
286N/A il.append(methodGen.storeCurrentNode());
286N/A il.append(new IFGT(loop));
286N/A
286N/A // Restore current node and current iterator from the stack
286N/A il.append(methodGen.storeIterator());
286N/A il.append(methodGen.storeCurrentNode());
286N/A
286N/A nextNode.setTarget(skip);
286N/A skipNode.setTarget(skip);
286N/A }
286N/A}