List of usage examples for org.apache.commons.jxpath JXPathContext getPrefix
public String getPrefix(String namespaceURI)
From source file:org.xchain.example.namespaces.xhtml.OptionCommand.java
public boolean execute(JXPathContext context) throws Exception { String value = getValue(context); String currentValue = (String) ((ScopedQNameVariables) context.getVariables()) .getVariable(SelectCommand.SELECTED_VARIABLE_NAME, Scope.execution); // 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); }/* w ww . j a va 2 s . c o m*/ AttributesImpl attributes = new AttributesImpl(); if (value != null) { attributes.addAttribute("", VALUE_LOCAL_NAME, VALUE_LOCAL_NAME, "CDATA", value); } if (value != null && value.equals(currentValue)) { attributes.addAttribute("", SELECTED_LOCAL_NAME, SELECTED_LOCAL_NAME, "CDATA", SELECTED_VALUE); } ContentHandler handler = getContentHandler(); handler.startElement(XHTML_NAMESPACE, OPTION_LOCAL_NAME, qNameString(xhtmlPrefix, OPTION_LOCAL_NAME), attributes); boolean result = false; Exception exception = null; try { result = super.execute(context); } catch (Exception e) { exception = e; } if (exception == null || !(exception instanceof SAXException)) { handler.endElement(XHTML_NAMESPACE, OPTION_LOCAL_NAME, qNameString(xhtmlPrefix, OPTION_LOCAL_NAME)); } if (exception != null) { throw exception; } return result; }
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 w ww . jav a2 s . co 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.examples.tutorial.TraceUtil.java
public static String xhtmlPrefix(JXPathContext context) throws SAXException { String prefix = context.getPrefix(XHTML_NAMESPACE_URI); if (prefix == null) { throw new SAXException("The namespace '" + XHTML_NAMESPACE_URI + "' must be bound to a prefix."); }//from www.j a va2 s. co m return prefix; }