PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/jbpm-bpmn2/src/main/java/org/jbpm/bpmn2/xpath/XPATHExpressionModifier.java

https://github.com/mariofusco/jbpm
Java | 253 lines | 3 code | 4 blank | 246 comment | 0 complexity | 98edd474a3dea26afc511d5808b8c59d MD5 | raw file
  1. /*
  2. Copyright 2010 Intalio Inc
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. package org.jbpm.bpmn2.xpath;
  14. /**
  15. * A helper utility that modifies XPath Expression in-place.
  16. */
  17. public class XPATHExpressionModifier {
  18. // /**
  19. // * Creates a new XPATHExpressionModifier object.
  20. // *
  21. // * @param contextUris
  22. // * @param namePool
  23. // */
  24. // public XPATHExpressionModifier() {
  25. // }
  26. //
  27. // /**
  28. // * Insert nodes into the specified XPath expression wherever
  29. // * required
  30. // * <p>
  31. // * To be precise, a node is added to its parent if:
  32. // * a) the node is an element...
  33. // * b) that corresponds to an step...
  34. // * c) that has a child axis...
  35. // * d) whose parent had no children with its name...
  36. // * e) and all preceding steps are element name tests.
  37. // * </p>
  38. // * @param xpathExpr
  39. // * @param namePool
  40. // *
  41. // * @throws DOMException
  42. // * @throws TransformerException
  43. // * @throws XPathExpressionException
  44. // */
  45. // public Node insertMissingData(String xpath, Node contextNode)
  46. // throws DOMException, TransformerException, XPathExpressionException {
  47. // if (xpath.startsWith("/") && contextNode == null) {
  48. // xpath = xpath.substring(1);
  49. // }
  50. // Node rootNode = contextNode;
  51. // if (contextNode != null) {
  52. // contextNode = contextNode.getOwnerDocument();
  53. // }
  54. //
  55. // XPathFactory xpf = new XPathFactoryImpl();
  56. // XPath xpe = xpf.newXPath();
  57. // XPathExpression xpathExpr = xpe.compile(xpath);
  58. //
  59. //// if (contextNode == null || !(contextNode instanceof Element) ||
  60. //// !(xpathExpr instanceof XPathExpressionImpl)) {
  61. //// return;
  62. //// }
  63. //
  64. // Expression expression = ((XPathExpressionImpl) xpathExpr).getInternalExpression();
  65. //
  66. // Document document = toDOMDocument(contextNode);
  67. //
  68. // PathExpression pathExpr = null;
  69. // Expression step = null;
  70. //
  71. // if (expression instanceof PathExpression) {
  72. // pathExpr = (PathExpression) expression;
  73. // step = pathExpr.getFirstStep();
  74. // } else if (expression instanceof AxisExpression) {
  75. // pathExpr = null;
  76. // step = (AxisExpression) expression;
  77. // } else {
  78. // return contextNode;
  79. // }
  80. //
  81. // while (step != null) {
  82. // if (step instanceof AxisExpression) {
  83. // AxisExpression axisExpr = (AxisExpression) step;
  84. //
  85. // NodeTest nodeTest = axisExpr.getNodeTest();
  86. //
  87. // if (!(nodeTest instanceof NameTest)) {
  88. // break;
  89. // }
  90. //
  91. // NameTest nameTest = (NameTest) nodeTest;
  92. //
  93. // QName childName = getQualifiedName(nameTest.getFingerprint(),
  94. // ((XPathFactoryImpl) xpf).getConfiguration().getNamePool()/*, contextUris*/);
  95. //
  96. // if (Axis.CHILD == axisExpr.getAxis()) {
  97. // if (NodeKindTest.ELEMENT.getNodeKindMask() != nameTest.getNodeKindMask()) {
  98. // break;
  99. // }
  100. // if (contextNode == null) {
  101. // contextNode = document.createElementNS(childName.getNamespaceURI(), childName.getLocalPart());
  102. // document.appendChild(contextNode);
  103. // rootNode = contextNode;
  104. // } else {
  105. //
  106. // NodeList children = null;
  107. // if (contextNode instanceof Element) {
  108. // children = ((Element) contextNode).getElementsByTagNameNS(childName.getNamespaceURI(),
  109. // childName.getLocalPart());
  110. // } else if (contextNode instanceof Document) {
  111. // children = ((Document) contextNode).getElementsByTagNameNS(childName.getNamespaceURI(),
  112. // childName.getLocalPart());
  113. // } else {
  114. // throw new IllegalArgumentException(contextNode + " is of unsupported type");
  115. // }
  116. // if ((children == null) || (children.getLength() == 0)) {
  117. // Node child = document.createElementNS(childName.getNamespaceURI(),
  118. // getQualifiedName(childName));
  119. // Document currentDoc = (Document) (contextNode instanceof Document ? contextNode : contextNode.getOwnerDocument());
  120. //
  121. // contextNode.appendChild(currentDoc.importNode(child, true));
  122. // contextNode = child;
  123. // } else if (children.getLength() == 1) {
  124. // contextNode = children.item(0);
  125. // } else {
  126. // break;
  127. // }
  128. // }
  129. // } else if (Axis.ATTRIBUTE == axisExpr.getAxis()) {
  130. // if (NodeKindTest.ATTRIBUTE.getNodeKindMask() != nameTest.getNodeKindMask()) {
  131. // break;
  132. // }
  133. //
  134. // Attr attribute = ((Element) contextNode).getAttributeNodeNS(childName.getNamespaceURI(), childName.getLocalPart());
  135. // if (attribute == null) {
  136. // attribute = document.createAttributeNS(childName.getNamespaceURI(), childName.getLocalPart());
  137. // ((Element) contextNode).setAttributeNode(attribute);
  138. // contextNode = attribute;
  139. // } else {
  140. // break;
  141. // }
  142. //
  143. // } else {
  144. // break;
  145. // }
  146. //
  147. //
  148. // } else if (step instanceof ItemChecker) {
  149. // ItemChecker itemChecker = (ItemChecker) step;
  150. // Expression baseExpr = itemChecker.getBaseExpression();
  151. //
  152. // if (!(baseExpr instanceof VariableReference)) {
  153. // break;
  154. // }
  155. // } else {
  156. // break;
  157. // }
  158. //
  159. // if (pathExpr != null) {
  160. // Expression remainingSteps = pathExpr.getRemainingSteps();
  161. //
  162. // if (remainingSteps instanceof PathExpression) {
  163. // pathExpr = (PathExpression) remainingSteps;
  164. // step = pathExpr.getFirstStep();
  165. // } else if (remainingSteps instanceof AxisExpression) {
  166. // pathExpr = null;
  167. // step = (AxisExpression) remainingSteps;
  168. // } else {
  169. // throw new RuntimeException("Not supported step " + remainingSteps + " in expression " + expression);
  170. // }
  171. // } else {
  172. // break;
  173. // }
  174. // }
  175. // return rootNode;
  176. // }
  177. //
  178. // /**
  179. // * Create the QName by running the given finger print against the
  180. // * given context
  181. // *
  182. // * @param fingerprint
  183. // * @param namePool
  184. // * @param nsContext
  185. // *
  186. // * @return The QName corresponding to the finger print
  187. // */
  188. // private QName getQualifiedName(int fingerprint, NamePool namePool) {
  189. // String localName = namePool.getLocalName(fingerprint);
  190. // String prefix = namePool.getPrefix(fingerprint);
  191. // String uri = namePool.getURI(fingerprint);
  192. //
  193. // // Unfortunately, NSContext.getPrefix(String URI) doesn't always work
  194. // // So, we need to find the prefix for the URI the hard way
  195. // // if ((prefix == null) || "".equals(prefix)) {
  196. // // for (String nsPrefix : nsContext.getPrefixes()) {
  197. // // String nsUri = nsContext.getNamespaceURI(nsPrefix);
  198. // //
  199. // // if (nsUri.equals(uri)) {
  200. // // prefix = nsPrefix;
  201. // // }
  202. // // }
  203. // // }
  204. //
  205. // return new QName(uri, localName, prefix);
  206. // }
  207. //
  208. // public static Document toDOMDocument(Node node) throws TransformerException {
  209. // // If the node is the document, just cast it
  210. // if (node instanceof Document) {
  211. // return newDocument();
  212. // // If the node is an element
  213. // } else if (node instanceof Element) {
  214. // Element elem = (Element) node;
  215. // // If this is the root element, return its owner document
  216. // // if (elem.getOwnerDocument().getDocumentElement() == elem) {
  217. // return elem.getOwnerDocument();
  218. // // else, create a new doc and copy the element inside it
  219. // // } else {
  220. // // Document doc = newDocument();
  221. // // doc.appendChild(doc.importNode(node, true));
  222. // // return doc;
  223. // // }
  224. // // other element types are not handled
  225. // } else if (node == null) {
  226. // return newDocument();
  227. // } else {
  228. // throw new TransformerException("Unable to convert DOM node to a Document");
  229. // }
  230. // }
  231. //
  232. // public static Document newDocument() {
  233. // try {
  234. // DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  235. // return db.newDocument();
  236. // } catch (ParserConfigurationException e) {
  237. // // __log.error(e);
  238. // throw new RuntimeException(e);
  239. // }
  240. // }
  241. //
  242. // public static String getQualifiedName(QName qName) {
  243. // String prefix = qName.getPrefix(), localPart = qName.getLocalPart();
  244. // return (prefix == null || "".equals(prefix)) ? localPart : (prefix + ":" + localPart);
  245. // }
  246. }