jaxpfunctionresolver.java
来自「bpel执行引擎用来执行bpel业务流程」· Java 代码 · 共 601 行 · 第 1/2 页
JAVA
601 行
} public class DomToString implements XPathFunction { public Object evaluate(List args) throws XPathFunctionException { if (args.size() != 1) throw new XPathFunctionException(new FaultException(new QName(Namespaces.ODE_EXTENSION_NS, "domToStringInvalidSource"), "Invalid arguments")); if (__log.isDebugEnabled()) { __log.debug("domToString call(context=" + _ectx + " args=" + args + ")"); } Element varElmt; try { if (args.get(0) instanceof List) { List elmts = (List) args.get(0); if (elmts.size() != 1) throw new XPathFunctionException( new FaultException(_oxpath.getOwner().constants.qnXsltInvalidSource, "The bpws:domToString function MUST be passed a single " + "element node.")); varElmt = (Element) elmts.get(0); } else { if (args.get(1) instanceof NodeWrapper) varElmt = (Element) ((NodeWrapper) args.get(1)).getUnderlyingNode(); else varElmt = (Element) args.get(1); } } catch (ClassCastException e) { throw new XPathFunctionException( new FaultException(_oxpath.getOwner().constants.qnXsltInvalidSource, "The bpws:domToString function MUST be passed a single " + "element node.")); } String result= DOMUtils.domToString(varElmt); return result; } } /** * Compile time checking for the non standard ode:splitToElements function. */ public class SplitToElements implements XPathFunction { public Object evaluate(List args) throws XPathFunctionException { if (args.size() <= 2 || args.size() >= 5) throw new XPathFunctionException(new FaultException(new QName(Namespaces.ODE_EXTENSION_NS, "splitInvalidSource"), "Invalid arguments")); if (__log.isDebugEnabled()) { __log.debug("splitToElements call(context=" + _ectx + " args=" + args + ")"); } String strToSplit; try { strToSplit = Helper.extractString(args.get(0)); } catch (IllegalArgumentException e) { throw new XPathFunctionException(new FaultException(new QName(Namespaces.ODE_EXTENSION_NS, "splitInvalidSource"), e)); } // Other parameters String separator = (String) args.get(1); String localName = (String) args.get(2); String namespace = args.size() == 4 ? (String) args.get(3) : null; // Preparing the result document Document doc = DOMUtils.newDocument(); Element wrapper = doc.createElement("wrapper"); doc.appendChild(wrapper); // Creating nodes for each string element of the split string and appending to result String[] strElmts = strToSplit.split(separator); for (String strElmt : strElmts) { Element elmt = doc.createElementNS(namespace, localName); elmt.setTextContent(strElmt.trim()); wrapper.appendChild(elmt); } return wrapper; } } /** * Takes the relative URL and combines it with the base URL to return a new absolute URL. * If the relative parameter is an absolute URL, returns it instead. * <p/> * As described in section 5 of <a href="http://www.ietf.org/rfc/rfc2396.txt">rfc2396</a>. * <p/> * This implementation relies heavily on {@link java.net.URL}. As thus, the same restrictions apply, especially regarding encoding. * <p/> * <i>"The URL class does not itself encode or decode any URL components according * to the escaping mechanism defined in RFC2396. It is the responsibility of the caller * to encode any fields, which need to be escaped prior to calling URL, and also to decode * any escaped fields, that are returned from URL."</i> * * @see java.net.URL * @see URL#URL(java.net.URL, String) */ public static class CombineUrl implements XPathFunction { public Object evaluate(List args) throws XPathFunctionException { final QName FAULT_QNAME = new QName(Namespaces.ODE_EXTENSION_NS, "combineUrlInvalidSource"); if (args.size() != 2) { throw new XPathFunctionException(new FaultException(FAULT_QNAME, "Invalid arguments")); } String base; try { base = Helper.extractString(args.get(0)); } catch (IllegalArgumentException e) { throw new XPathFunctionException(new FaultException(FAULT_QNAME, "Invalid argument: " + args.get(0), e)); } String relative; try { relative = Helper.extractString(args.get(1)); } catch (IllegalArgumentException e) { throw new XPathFunctionException(new FaultException(FAULT_QNAME, "Invalid argument: " + args.get(1), e)); } URL baseURL; try { baseURL = new URL(base); } catch (MalformedURLException e) { throw new XPathFunctionException(new FaultException(FAULT_QNAME, "First parameter [" + base + "] MUST point to a well-formed URL.", e)); } try { URL combined = new URL(baseURL, relative); return combined.toExternalForm(); } catch (MalformedURLException e) { throw new XPathFunctionException(new FaultException(FAULT_QNAME, e.getMessage(), e)); } } } public static class ComposeUrl implements XPathFunction { boolean preserveUndefinedVar = false; String faultLocalPart = "composeUrlInvalidSource"; QName faultQName; public ComposeUrl() { faultQName = new QName(Namespaces.ODE_EXTENSION_NS, faultLocalPart); } public ComposeUrl(boolean preserveUndefinedVar, String faultLocalPart) { this.preserveUndefinedVar = preserveUndefinedVar; this.faultLocalPart = faultLocalPart; faultQName = new QName(Namespaces.ODE_EXTENSION_NS, faultLocalPart); } public Object evaluate(List args) throws XPathFunctionException { // prepare these 2 arguments String uriTemplate; Map<String, String> pairs; boolean separareParameteters; if (args.size() == 2) { separareParameteters = false; } else if (args.size() > 2 && args.size() % 2 == 1) { separareParameteters = true; } else { throw new XPathFunctionException(new FaultException(faultQName, "Illegal Arguments")); } try { uriTemplate = Helper.extractString(args.get(0)); } catch (IllegalArgumentException e) { throw new XPathFunctionException(new FaultException(faultQName, "Invalid argument: URI Template expected. " + args.get(0), e)); } if (separareParameteters) { // /!\ Do NOT get the first element try { pairs = Helper.buildNameValueMap(args, 1); } catch (IllegalArgumentException e) { throw new XPathFunctionException(new FaultException(faultQName, "Invalid argument", e)); } } else { try { List elmts = (List) args.get(1); Element elt = (Element) elmts.get(0); pairs = Helper.extractNameValueMap(elt); } catch (ClassCastException e) { throw new XPathFunctionException(new FaultException(faultQName, "Expected an element similar too: <foo><name1>value1</name1>name2>value2</name2>...</foo>")); } } try { if (preserveUndefinedVar) { return URITemplate.expandLazily(uriTemplate, pairs); } else { return URITemplate.expand(uriTemplate, pairs); } } catch (URIException e) { throw new XPathFunctionException(new FaultException(faultQName, "Invalid argument", e)); } catch (UnsupportedOperationException e) { throw new XPathFunctionException(new FaultException(faultQName, "Invalid argument", e)); } } } public static class Helper { /** * Extract a string from the given parameter.<br/> * The parameter could be: * <ol> * <li>a {@link java.util.List} containing exactly one {@link org.w3c.dom.Node}</li> * <li>a {@link net.sf.saxon.dom.NodeWrapper}</li> * <li>a {@link org.w3c.dom.Node}</li> * <li>or a {@link String}</li> * </ol> * In the first 3 cases, if the {@linkplain org.w3c.dom.Node node} type is {@link Node#ELEMENT_NODE} the (trimmed) {@linkplain org.w3c.dom.Node#getTextContent() text content} is returned. * if the {@linkplain org.w3c.dom.Node node} type is {@link Node#TEXT_NODE} the (trimmed) {@linkplain org.w3c.dom.Text#getWholeText() text content} is returned. * <p/> * * @param arg * @return a string * @throws IllegalArgumentException if none of the conditions mentioned above are met */ public static String extractString(Object arg) throws IllegalArgumentException { // Checking the parameter, should be a proper element or a text node. Java verbosity at its best. String res = null; try { Node node = null; if (arg instanceof List) { List elmts = (List) arg; if (elmts.size() != 1) throw new IllegalArgumentException("Parameter MUST point to a string, single element or text node."); node = (Node) elmts.get(0); } else if (arg instanceof NodeWrapper) { node = (Node) ((NodeWrapper) arg).getUnderlyingNode(); } else if (arg instanceof Node) { node = (Node) arg; } else { res = (String) arg; } if (res == null) { if (Node.ELEMENT_NODE == node.getNodeType()) { res = node.getTextContent().trim(); } else if (Node.TEXT_NODE == node.getNodeType()) { res = ((Text) node).getWholeText().trim(); } } } catch (ClassCastException e) { throw new IllegalArgumentException("Parameter MUST point to a string, single element or text node.", e); } return res; } /** * Extract the name/value from an xml element similar too: * <br/> * {@literal <elt> * <foovar>bar</foovar> * <myvar>value1</myvar> * </elt>} * * <p/> * The local name of the element is the map key, the text content the associated value. * * @return a Map of name/value pair */ public static Map<String, String> extractNameValueMap(Element elt) { Map<String, String> pairs = new HashMap<String, String>(); for (int i = 0; i < elt.getChildNodes().getLength(); i++) { Node n = elt.getChildNodes().item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { pairs.put(n.getLocalName(), DOMUtils.getTextContent(n)); } } return pairs; } /** * Same as {@link #buildNameValueMap(java.util.List, int)} but index equals zero. * @see #buildNameValueMap(java.util.List, int) */ public static Map<String, String> buildNameValueMap(List args) { return buildNameValueMap(args, 0); } /** * {@linkplain #extractString(Object) Extract a string} from each list element and build a map with them. * <br/>Elements at even indices would be the keys, Elements at odd indices the values. * * @param args the list containing a serie of name, value, name, value, and so on * @param begin index of the first name to include in the map, (args.size - begin) must be an even number * or an IndexOutOfBoundsException will be thrown * @return a Map of name/value pairs * @throws IndexOutOfBoundsException * @see #extractString(Object) */ public static Map<String, String> buildNameValueMap(List args, int begin) { Map<String, String> pairs; pairs = new HashMap<String, String>(); for (int i = begin; i < args.size(); i = i + 2) { pairs.put(Helper.extractString(args.get(i)), Helper.extractString(args.get(i + 1))); } return pairs; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?