List of usage examples for org.apache.commons.jxpath JXPathContext getVariables
public Variables getVariables()
From source file:org.xchain.example.namespaces.xhtml.SelectCommand.java
public boolean execute(JXPathContext context) throws Exception { // get the request and the name from the context. HttpServletRequest request = getRequest(context); String name = getName(context); // get the original value of the name. String currentValue = request.getParameter(name); // get the xhtml prefix and make sure that there is a mapping for it. String xhtmlPrefix = context.getPrefix(XHTML_NAMESPACE); if (xhtmlPrefix == null) { throw new IllegalStateException("There is no mapping defined for " + XHTML_NAMESPACE); }//from www . j a va 2 s . c o m // create the attributes for the element we are going to output. AttributesImpl attributes = new AttributesImpl(); if (hasId()) { attributes.addAttribute("", ID_LOCAL_NAME, ID_LOCAL_NAME, "CDATA", getId(context)); } attributes.addAttribute("", NAME_LOCAL_NAME, NAME_LOCAL_NAME, "CDATA", getName(context)); ((ScopedQNameVariables) context.getVariables()).declareVariable(SELECTED_VARIABLE_NAME, currentValue, Scope.execution); ContentHandler handler = getContentHandler(); handler.startElement(XHTML_NAMESPACE, SELECT_LOCAL_NAME, qNameString(xhtmlPrefix, SELECT_LOCAL_NAME), attributes); boolean result = false; Exception exception = null; // execute the children and catch any exceptions. try { result = super.execute(context); } catch (Exception e) { exception = e; } // if there was not an exception, or the exception is not a sax exception, then we need to finish the output. if (exception == null || !(exception instanceof SAXException)) { handler.endElement(XHTML_NAMESPACE, SELECT_LOCAL_NAME, qNameString(xhtmlPrefix, SELECT_LOCAL_NAME)); } // rethrow the exception. if (exception != null) { throw exception; } return result; }
From source file:org.xchain.framework.jxpath.JXPathContextTest.java
@Test public void testBindMethodIgnore() throws Exception { JXPathContext context = JXPathContext.newContext(new Object()); context.getVariables().declareVariable("sub-foo", new SubFoo()); context.getVariables().declareVariable("bar", new SubBar()); // This will throw a JXPathException, due to an ambiguous method lookup, unless // GenericsWisePackageFunctions is used. String result = (String) context.getValue("take($sub-foo, $bar)", String.class); assertEquals("SubFoo", result); }
From source file:org.xchain.framework.jxpath.ScopedJXPathContextImpl.java
/** * Creates the proper qName variables for this parent context. If the given parentContext is * a LocalJXPathContext then the QNameVariables will be shared with the parentContext. If the * given parentContext is not a LocalJXPathContext then the QNameVariables will be able to reference * variables in the parent context but values in the create QNameVariables will not be available to * the parent context./*w w w . j av a2s. co m*/ * * @param parentContext The parent context to build from. * * @return The ScopedQNameVariables for this JXPathContext. */ private ScopedQNameVariables createQNameVariables(JXPathContext parentContext) { ScopedQNameVariables variables = null; if (parentContext != null) { // Create a new instance of the ScopedQNameVariables with a reference to the parent context's variables. variables = new ScopedQNameVariablesImpl(this, ((ScopedQNameVariables) parentContext.getVariables()), scope); } else { // Create a new instance of the ScopedQNameVariables with no reference to the parent context. variables = new ScopedQNameVariablesImpl(this, null, scope); } return variables; }
From source file:org.xchain.framework.quartz.CommandJob.java
/** * <p>Creates a JXPathContext for a JobExecutionContext.</p> * @param jobContext the job execution context for which the jxpath context will be created. * @return the JXPathContext created for the job execution context. *///from w w w .j av a 2s .c om private JXPathContext createJXPathContext(JobExecutionContext jobContext) { JXPathContext commandContext = JXPathContext.newContext(new HashMap()); ((QNameVariables) commandContext.getVariables()) .declareVariable(new QName(Constants.LIFECYCLE_URI, Constants.JOB_EXECUTION_CONTEXT), jobContext); return commandContext; }
From source file:org.xchain.framework.servlet.CatalogServlet.java
/** * Build a new JXPathContext and populate it with the given request and response. * //from w w w .j ava 2 s . c o m * @param request The incoming HttpServletRequest. * @param response The outgoing HttpServletResponse. * * @return A JXPath which contains the servlet request and response. */ protected JXPathContext jXPathContext(HttpServletRequest request, HttpServletResponse response) { JXPathContext context = JXPathContext.newContext(new HashMap()); ((QNameVariables) context.getVariables()).declareVariable(new QName(Constants.URI, Constants.CONTEXT), getServletConfig().getServletContext()); ((QNameVariables) context.getVariables()).declareVariable(new QName(Constants.URI, Constants.REQUEST), request); ((QNameVariables) context.getVariables()).declareVariable(new QName(Constants.URI, Constants.RESPONSE), response); return context; }
From source file:org.xchain.javassist.InnerClassCommand.java
public boolean execute(JXPathContext context) throws Exception { String testNamespace = context.getNamespaceURI("test"); context.getVariables().declareVariable("namespace", testNamespace); staticInnerCommand.execute(context); innerCommand.execute(context);// w w w.j ava2s. c o m return true; }
From source file:org.xchain.javassist.SimpleCommand.java
public boolean execute(JXPathContext context) throws Exception { String testNamespace = context.getNamespaceURI("test"); context.getVariables().declareVariable("namespace", testNamespace); return true;//from w ww . jav a2s . c o m }
From source file:org.xchain.namespaces.core.FilterVariableCommand.java
public boolean execute(JXPathContext context) throws Exception { QName variableName = getName(context); Object variableValue = null;// w w w .java 2 s.co m if (hasSelect()) { variableValue = getSelect(context); } else if (hasSelectNodes()) { variableValue = getSelectNodes(context); } else if (hasSelectSingleNode()) { variableValue = getSelectSingleNode(context); } else { throw new Exception("Variable '" + variableName + "' must have a select attribute (select, select-nodes, or select-single-node)"); } // get the scope. Scope scope = getScope(context); if (log.isDebugEnabled()) { log.debug("Setting variable name '" + variableName + "' to value '" + variableValue + "' in scope '" + scope + "'."); } ScopedQNameVariables variables = (ScopedQNameVariables) context.getVariables(); QName name = getName(context); if (variables.isDeclaredVariable(name, scope)) { pushValue(variables.getVariable(name, scope)); } else { pushValue(new UndeclaredVariable()); } // declare the variable. ((ScopedQNameVariables) context.getVariables()).declareVariable(variableName, variableValue, scope); // return false and allow other chains to execute. return false; }
From source file:org.xchain.namespaces.core.FilterVariableCommand.java
public boolean postProcess(JXPathContext context, Exception e) { QName name = getName(context); // get the scope. Scope scope = getScope(context);// w w w . j av a 2s.com ScopedQNameVariables variables = (ScopedQNameVariables) context.getVariables(); Object value = popValue(); if (value instanceof UndeclaredVariable) { variables.undeclareVariable(name, scope); } else { variables.declareVariable(name, value, scope); } // since we didn't handle the exception, just return false. return false; }
From source file:org.xchain.namespaces.core.IterateCommand.java
public boolean execute(JXPathContext context) throws Exception { boolean result = false; try {/*from ww w. j a va2 s. c om*/ if (log.isDebugEnabled()) { log.debug("Iterate command called. Has iterator:" + hasIterator() + " Has select:" + hasSelect()); } Iterator iterator = null; Enumeration enumeration = null; Object[] objectArray = null; int index = -1; QName variableName = getName(context); if (log.isDebugEnabled()) { log.debug("Iterate variable:" + variableName); } // get the scope. Scope scope = getScope(context); ScopedQNameVariables variables = (ScopedQNameVariables) context.getVariables(); if (log.isDebugEnabled()) { log.debug("Starting iteration process."); } if (hasIterator()) { iterator = getIterator(context); } else if (hasSelect()) { Object object = getSelect(context); if (object == null) { if (log.isDebugEnabled()) { log.debug("The selected iteration object is null."); } } else if (object instanceof Iterator) { if (log.isDebugEnabled()) { log.debug("Iterator selected."); } iterator = (Iterator) object; } else if (object instanceof Enumeration) { if (log.isDebugEnabled()) { log.debug("Enumeration selected."); } enumeration = (Enumeration) object; } else if (object instanceof Collection) { if (log.isDebugEnabled()) { log.debug("Collection selected."); } iterator = ((Collection) object).iterator(); } else if (object instanceof Map) { if (log.isDebugEnabled()) { log.debug("Map selected."); } iterator = ((Map) object).entrySet().iterator(); } else if (object instanceof Object[]) { objectArray = (Object[]) object; index = 0; } else { if (log.isDebugEnabled()) { log.debug("The selected iteration object is not a supported type: " + object.getClass().getName()); } } } if (iterator != null || enumeration != null || objectArray != null) { // if the variable has a value, then get it. while (((iterator != null && iterator.hasNext()) || (enumeration != null && enumeration.hasMoreElements()) || (objectArray != null && index < objectArray.length)) && !result) { Object value = null; if (iterator != null) { value = iterator.next(); } else if (enumeration != null) { value = enumeration.nextElement(); } else { value = objectArray[index++]; } if (log.isDebugEnabled()) { log.debug("Setting variable name '" + variableName + "' to value '" + value + "' in scope '" + scope + "'."); } variables.declareVariable(variableName, value, scope); result = super.execute(context); } } else { if (log.isDebugEnabled()) { log.debug("Skipping iteration due to a null iterator."); } } if (log.isDebugEnabled()) { log.debug("Iteration complete."); } } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("Could not iterate due to exception.", e); } throw e; } catch (Error err) { if (log.isDebugEnabled()) { log.debug("Could not iterate due to exception.", err); } throw err; } // return false and allow other chains to execute. return result; }