📄 compiler.java
字号:
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: Compiler.java,v 1.2.4.1 2005/09/14 19:47:10 jeffsuttor Exp $ */package com.sun.org.apache.xpath.internal.compiler;import javax.xml.transform.ErrorListener;import javax.xml.transform.SourceLocator;import javax.xml.transform.TransformerException;import com.sun.org.apache.xalan.internal.res.XSLMessages;import com.sun.org.apache.xml.internal.dtm.Axis;import com.sun.org.apache.xml.internal.dtm.DTMFilter;import com.sun.org.apache.xml.internal.dtm.DTMIterator;import com.sun.org.apache.xml.internal.utils.PrefixResolver;import com.sun.org.apache.xml.internal.utils.QName;import com.sun.org.apache.xml.internal.utils.SAXSourceLocator;import com.sun.org.apache.xpath.internal.Expression;import com.sun.org.apache.xpath.internal.axes.UnionPathIterator;import com.sun.org.apache.xpath.internal.axes.WalkerFactory;import com.sun.org.apache.xpath.internal.functions.FuncExtFunction;import com.sun.org.apache.xpath.internal.functions.FuncExtFunctionAvailable;import com.sun.org.apache.xpath.internal.functions.Function;import com.sun.org.apache.xpath.internal.functions.WrongNumberArgsException;import com.sun.org.apache.xpath.internal.objects.XNumber;import com.sun.org.apache.xpath.internal.objects.XString;import com.sun.org.apache.xpath.internal.operations.And;import com.sun.org.apache.xpath.internal.operations.Div;import com.sun.org.apache.xpath.internal.operations.Equals;import com.sun.org.apache.xpath.internal.operations.Gt;import com.sun.org.apache.xpath.internal.operations.Gte;import com.sun.org.apache.xpath.internal.operations.Lt;import com.sun.org.apache.xpath.internal.operations.Lte;import com.sun.org.apache.xpath.internal.operations.Minus;import com.sun.org.apache.xpath.internal.operations.Mod;import com.sun.org.apache.xpath.internal.operations.Mult;import com.sun.org.apache.xpath.internal.operations.Neg;import com.sun.org.apache.xpath.internal.operations.NotEquals;import com.sun.org.apache.xpath.internal.operations.Operation;import com.sun.org.apache.xpath.internal.operations.Or;import com.sun.org.apache.xpath.internal.operations.Plus;import com.sun.org.apache.xpath.internal.operations.UnaryOperation;import com.sun.org.apache.xpath.internal.operations.Variable;import com.sun.org.apache.xpath.internal.patterns.FunctionPattern;import com.sun.org.apache.xpath.internal.patterns.NodeTest;import com.sun.org.apache.xpath.internal.patterns.StepPattern;import com.sun.org.apache.xpath.internal.patterns.UnionPattern;import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;/** * An instance of this class compiles an XPath string expression into * a Expression object. This class compiles the string into a sequence * of operation codes (op map) and then builds from that into an Expression * tree. * @xsl.usage advanced */public class Compiler extends OpMap{ /** * Construct a Compiler object with a specific ErrorListener and * SourceLocator where the expression is located. * * @param errorHandler Error listener where messages will be sent, or null * if messages should be sent to System err. * @param locator The location object where the expression lives, which * may be null, but which, if not null, must be valid over * the long haul, in other words, it will not be cloned. * @param fTable The FunctionTable object where the xpath build-in * functions are stored. */ public Compiler(ErrorListener errorHandler, SourceLocator locator, FunctionTable fTable) { m_errorHandler = errorHandler; m_locator = locator; m_functionTable = fTable; } /** * Construct a Compiler instance that has a null error listener and a * null source locator. */ public Compiler() { m_errorHandler = null; m_locator = null; } /** * Execute the XPath object from a given opcode position. * @param opPos The current position in the xpath.m_opMap array. * @return The result of the XPath. * * @throws TransformerException if there is a syntax or other error. * @xsl.usage advanced */ public Expression compile(int opPos) throws TransformerException { int op = getOp(opPos); Expression expr = null; // System.out.println(getPatternString()+"op: "+op); switch (op) { case OpCodes.OP_XPATH : expr = compile(opPos + 2); break; case OpCodes.OP_OR : expr = or(opPos); break; case OpCodes.OP_AND : expr = and(opPos); break; case OpCodes.OP_NOTEQUALS : expr = notequals(opPos); break; case OpCodes.OP_EQUALS : expr = equals(opPos); break; case OpCodes.OP_LTE : expr = lte(opPos); break; case OpCodes.OP_LT : expr = lt(opPos); break; case OpCodes.OP_GTE : expr = gte(opPos); break; case OpCodes.OP_GT : expr = gt(opPos); break; case OpCodes.OP_PLUS : expr = plus(opPos); break; case OpCodes.OP_MINUS : expr = minus(opPos); break; case OpCodes.OP_MULT : expr = mult(opPos); break; case OpCodes.OP_DIV : expr = div(opPos); break; case OpCodes.OP_MOD : expr = mod(opPos); break;// case OpCodes.OP_QUO :// expr = quo(opPos); break; case OpCodes.OP_NEG : expr = neg(opPos); break; case OpCodes.OP_STRING : expr = string(opPos); break; case OpCodes.OP_BOOL : expr = bool(opPos); break; case OpCodes.OP_NUMBER : expr = number(opPos); break; case OpCodes.OP_UNION : expr = union(opPos); break; case OpCodes.OP_LITERAL : expr = literal(opPos); break; case OpCodes.OP_VARIABLE : expr = variable(opPos); break; case OpCodes.OP_GROUP : expr = group(opPos); break; case OpCodes.OP_NUMBERLIT : expr = numberlit(opPos); break; case OpCodes.OP_ARGUMENT : expr = arg(opPos); break; case OpCodes.OP_EXTFUNCTION : expr = compileExtension(opPos); break; case OpCodes.OP_FUNCTION : expr = compileFunction(opPos); break; case OpCodes.OP_LOCATIONPATH : expr = locationPath(opPos); break; case OpCodes.OP_PREDICATE : expr = null; break; // should never hit this here. case OpCodes.OP_MATCHPATTERN : expr = matchPattern(opPos + 2); break; case OpCodes.OP_LOCATIONPATHPATTERN : expr = locationPathPattern(opPos); break; case OpCodes.OP_QUO: error(XPATHErrorResources.ER_UNKNOWN_OPCODE, new Object[]{ "quo" }); //"ERROR! Unknown op code: "+m_opMap[opPos]); break; default : error(XPATHErrorResources.ER_UNKNOWN_OPCODE, new Object[]{ Integer.toString(getOp(opPos)) }); //"ERROR! Unknown op code: "+m_opMap[opPos]); }// if(null != expr)// expr.setSourceLocator(m_locator); return expr; } /** * Bottle-neck compilation of an operation with left and right operands. * * @param operation non-null reference to parent operation. * @param opPos The op map position of the parent operation. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Operation} instance. * * @throws TransformerException if there is a syntax or other error. */ private Expression compileOperation(Operation operation, int opPos) throws TransformerException { int leftPos = getFirstChildPos(opPos); int rightPos = getNextOpPos(leftPos); operation.setLeftRight(compile(leftPos), compile(rightPos)); return operation; } /** * Bottle-neck compilation of a unary operation. * * @param unary The parent unary operation. * @param opPos The position in the op map of the parent operation. * * @return The unary argument. * * @throws TransformerException if syntax or other error occurs. */ private Expression compileUnary(UnaryOperation unary, int opPos) throws TransformerException { int rightPos = getFirstChildPos(opPos); unary.setRight(compile(rightPos)); return unary; } /** * Compile an 'or' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Or} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression or(int opPos) throws TransformerException { return compileOperation(new Or(), opPos); } /** * Compile an 'and' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.And} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression and(int opPos) throws TransformerException { return compileOperation(new And(), opPos); } /** * Compile a '!=' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.NotEquals} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression notequals(int opPos) throws TransformerException { return compileOperation(new NotEquals(), opPos); } /** * Compile a '=' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Equals} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression equals(int opPos) throws TransformerException { return compileOperation(new Equals(), opPos); } /** * Compile a '<=' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Lte} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression lte(int opPos) throws TransformerException { return compileOperation(new Lte(), opPos); } /** * Compile a '<' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Lt} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression lt(int opPos) throws TransformerException { return compileOperation(new Lt(), opPos); } /** * Compile a '>=' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Gte} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression gte(int opPos) throws TransformerException { return compileOperation(new Gte(), opPos); } /** * Compile a '>' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Gt} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression gt(int opPos) throws TransformerException { return compileOperation(new Gt(), opPos); } /** * Compile a '+' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Plus} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression plus(int opPos) throws TransformerException { return compileOperation(new Plus(), opPos); } /** * Compile a '-' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Minus} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression minus(int opPos) throws TransformerException { return compileOperation(new Minus(), opPos); } /** * Compile a '*' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Mult} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression mult(int opPos) throws TransformerException { return compileOperation(new Mult(), opPos); } /** * Compile a 'div' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Div} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression div(int opPos) throws TransformerException { return compileOperation(new Div(), opPos); } /** * Compile a 'mod' operation. * * @param opPos The current position in the m_opMap array. * * @return reference to {@link com.sun.org.apache.xpath.internal.operations.Mod} instance. * * @throws TransformerException if a error occurs creating the Expression. */ protected Expression mod(int opPos) throws TransformerException { return compileOperation(new Mod(), opPos); } /* * Compile a 'quo' operation.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -