/** * Set the ErrorListener where errors and warnings are to be reported. * * @param listener A non-null ErrorListener reference. */ public void setErrorListener(ErrorListener listener) throws IllegalArgumentException { if (listener == null) throw new IllegalArgumentException( XSLMessages.createXPATHMessage( XPATHErrorResources.ER_NULL_ERROR_HANDLER, null)); // "Null error handler"); m_errorListener = listener; }
/** * Construct an XPath object. * * <p>(Needs review -sc) This method initializes an XPathParser/ Compiler and compiles the * expression. * * @param exprString The XPath expression. * @param locator The location of the expression, may be null. * @param prefixResolver A prefix resolver to use to resolve prefixes to namespace URIs. * @param type one of {@link #SELECT} or {@link #MATCH}. * @param errorListener The error listener, or null if default should be used. * @throws javax.xml.transform.TransformerException if syntax or other error. */ public XPath( String exprString, SourceLocator locator, PrefixResolver prefixResolver, int type, ErrorListener errorListener) throws javax.xml.transform.TransformerException { initFunctionTable(); if (null == errorListener) errorListener = new com.sun.org.apache.xml.internal.utils.DefaultErrorHandler(); m_patternString = exprString; XPathParser parser = new XPathParser(errorListener, locator); Compiler compiler = new Compiler(errorListener, locator, m_funcTable); if (SELECT == type) parser.initXPath(compiler, exprString, prefixResolver); else if (MATCH == type) parser.initMatchPattern(compiler, exprString, prefixResolver); else throw new RuntimeException( XSLMessages.createXPATHMessage( XPATHErrorResources.ER_CANNOT_DEAL_XPATH_TYPE, new Object[] {Integer.toString(type)})); // "Can not deal with XPath type: " + type); // System.out.println("----------------"); Expression expr = compiler.compile(0); // System.out.println("expr: "+expr); this.setExpression(expr); if ((null != locator) && locator instanceof ExpressionNode) { expr.exprSetParent((ExpressionNode) locator); } }
/** * Tell the user of an assertion error, and probably throw an exception. * * @param b If false, a runtime exception will be thrown. * @param msg The assertion message, which should be informative. * @throws RuntimeException if the b argument is false. */ public void assertion(boolean b, String msg) { if (!b) { String fMsg = XSLMessages.createXPATHMessage( XPATHErrorResources.ER_INCORRECT_PROGRAMMER_ASSERTION, new Object[] {msg}); throw new RuntimeException(fMsg); } }
/** * Given an expression and a context, evaluate the XPath and return the result. * * @param xctxt The execution context. * @param contextNode The node that "." expresses. * @param namespaceContext The context in which namespaces in the XPath are supposed to be * expanded. * @throws TransformerException thrown if the active ProblemListener decides the error condition * is severe enough to halt processing. * @throws javax.xml.transform.TransformerException * @xsl.usage experimental */ public XObject execute(XPathContext xctxt, int contextNode, PrefixResolver namespaceContext) throws javax.xml.transform.TransformerException { xctxt.pushNamespaceContext(namespaceContext); xctxt.pushCurrentNodeAndExpression(contextNode, contextNode); XObject xobj = null; try { xobj = m_mainExp.execute(xctxt); } catch (TransformerException te) { te.setLocator(this.getLocator()); ErrorListener el = xctxt.getErrorListener(); if (null != el) // defensive, should never happen. { el.error(te); } else throw te; } catch (Exception e) { while (e instanceof com.sun.org.apache.xml.internal.utils.WrappedRuntimeException) { e = ((com.sun.org.apache.xml.internal.utils.WrappedRuntimeException) e).getException(); } // e.printStackTrace(); String msg = e.getMessage(); if (msg == null || msg.length() == 0) { msg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_XPATH_ERROR, null); } TransformerException te = new TransformerException(msg, getLocator(), e); ErrorListener el = xctxt.getErrorListener(); // te.printStackTrace(); if (null != el) // defensive, should never happen. { el.fatalError(te); } else throw te; } finally { xctxt.popNamespaceContext(); xctxt.popCurrentNodeAndExpression(); } return xobj; }
/** * Set the root node of the TreeWalker. (Not part of the DOM2 TreeWalker interface). * * @param root The context node of this step. */ public void setRoot(int root) { // %OPT% Get this directly from the lpi. XPathContext xctxt = wi().getXPathContext(); m_dtm = xctxt.getDTM(root); m_traverser = m_dtm.getAxisTraverser(m_axis); m_isFresh = true; m_foundLast = false; m_root = root; m_currentNode = root; if (DTM.NULL == root) { throw new RuntimeException( XSLMessages.createXPATHMessage( XPATHErrorResources.ER_SETTING_WALKER_ROOT_TO_NULL, null)); // "\n !!!! Error! Setting the root of a walker to null!!!"); } resetProximityPositions(); }
/** * Tell the user of an error, and probably throw an exception. * * @param xctxt The XPath runtime context. * @param sourceNode Not used. * @param msg An error msgkey that corresponds to one of the constants found in {@link * com.sun.org.apache.xpath.internal.res.XPATHErrorResources}, which is a key for a format * string. * @param args An array of arguments represented in the format string, which may be null. * @throws TransformerException if the current ErrorListoner determines to throw an exception. */ public void error(XPathContext xctxt, int sourceNode, String msg, Object[] args) throws javax.xml.transform.TransformerException { String fmsg = XSLMessages.createXPATHMessage(msg, args); ErrorListener ehandler = xctxt.getErrorListener(); if (null != ehandler) { ehandler.fatalError(new TransformerException(fmsg, (SAXSourceLocator) xctxt.getSAXLocator())); } else { SourceLocator slocator = xctxt.getSAXLocator(); System.out.println( fmsg + "; file " + slocator.getSystemId() + "; line " + slocator.getLineNumber() + "; column " + slocator.getColumnNumber()); } }
/** * Constructs and throws a WrongNumberArgException with the appropriate message for this function * object. * * @throws WrongNumberArgsException */ protected void reportWrongNumberArgs() throws WrongNumberArgsException { throw new WrongNumberArgsException( XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ZERO_OR_ONE, null)); // "0 or 1"); }
/** * Constructs and throws a WrongNumberArgException with the appropriate message for this function * object. * * @throws WrongNumberArgsException */ protected void reportWrongNumberArgs() throws WrongNumberArgsException { throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("gtone", null)); }