PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/bpel.mapper/src/org/netbeans/modules/bpel/mapper/predicates/PredicateFinderVisitor.java

https://bitbucket.org/rsaqc/netbeans-soa
Java | 240 lines | 154 code | 17 blank | 69 comment | 31 complexity | e381ccae6b3e543efd4df292d0ea8c9c MD5 | raw file
  1. /*
  2. * The contents of this file are subject to the terms of the Common Development
  3. * and Distribution License (the License). You may not use this file except in
  4. * compliance with the License.
  5. *
  6. * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
  7. * or http://www.netbeans.org/cddl.txt.
  8. *
  9. * When distributing Covered Code, include this CDDL Header Notice in each file
  10. * and include the License file at http://www.netbeans.org/cddl.txt.
  11. * If applicable, add the following below the CDDL Header, with the fields
  12. * enclosed by brackets [] replaced by your own identifying information:
  13. * "Portions Copyrighted [year] [name of copyright owner]"
  14. *
  15. * The Original Software is NetBeans. The Initial Developer of the Original
  16. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  17. * Microsystems, Inc. All Rights Reserved.
  18. */
  19. package org.netbeans.modules.bpel.mapper.predicates;
  20. import org.netbeans.modules.bpel.mapper.model.BpelPathConverter;
  21. import org.netbeans.modules.bpel.model.api.support.XPathBpelVariable;
  22. import org.netbeans.modules.soa.xpath.mapper.specstep.SpecialStepManager;
  23. import org.netbeans.modules.soa.xpath.mapper.tree.DirectedList;
  24. import org.netbeans.modules.xml.xpath.ext.AbstractLocationPath;
  25. import org.netbeans.modules.xml.xpath.ext.LocationStep;
  26. import org.netbeans.modules.xml.xpath.ext.StepNodeTest;
  27. import org.netbeans.modules.xml.xpath.ext.StepNodeTypeTest;
  28. import org.netbeans.modules.xml.xpath.ext.XPathCoreFunction;
  29. import org.netbeans.modules.xml.xpath.ext.XPathCoreOperation;
  30. import org.netbeans.modules.xml.xpath.ext.XPathExpression;
  31. import org.netbeans.modules.xml.xpath.ext.XPathExpressionPath;
  32. import org.netbeans.modules.xml.xpath.ext.XPathExtensionFunction;
  33. import org.netbeans.modules.xml.xpath.ext.XPathLocationPath;
  34. import org.netbeans.modules.xml.xpath.ext.XPathPredicateExpression;
  35. import org.netbeans.modules.xml.xpath.ext.schema.resolver.XPathSchemaContext;
  36. import org.netbeans.modules.xml.xpath.ext.XPathVariableReference;
  37. import org.netbeans.modules.xml.xpath.ext.schema.resolver.PredicatedSchemaContext;
  38. import org.netbeans.modules.xml.xpath.ext.schema.resolver.SpecialSchemaContext;
  39. import org.netbeans.modules.xml.xpath.ext.schema.resolver.WildcardSchemaContext;
  40. import org.netbeans.modules.xml.xpath.ext.schema.resolver.WrappingSchemaContext;
  41. import org.netbeans.modules.xml.xpath.ext.spi.XPathSpecialStep;
  42. import org.netbeans.modules.xml.xpath.ext.spi.XPathVariable;
  43. import org.netbeans.modules.xml.xpath.ext.visitor.XPathVisitorAdapter;
  44. /**
  45. * The visitor is intended to look for predicate expressions
  46. * inside of an XPath.
  47. *
  48. * @author nk160297
  49. */
  50. public class PredicateFinderVisitor extends XPathVisitorAdapter {
  51. private BpelPredicateManager mPredManager;
  52. private SpecialStepManager mSStepManager;
  53. private XPathVariableReference mXPathVarReference;
  54. private XPathBpelVariable mXPathVariable;
  55. public PredicateFinderVisitor(BpelPredicateManager predManager,
  56. SpecialStepManager sStepManager) {
  57. //
  58. assert predManager != null;
  59. assert sStepManager != null;
  60. //
  61. mPredManager = predManager;
  62. mSStepManager = sStepManager;
  63. }
  64. //---------------------------------------------------
  65. @Override
  66. public void visit(XPathLocationPath expr) {
  67. LocationPathProcessor converter = new LocationPathProcessor();
  68. converter.processLocationPath(expr);
  69. }
  70. @Override
  71. public void visit(XPathExpressionPath expressionPath) {
  72. XPathExpression rootExpr = expressionPath.getRootExpression();
  73. rootExpr.accept(this);
  74. //
  75. LocationPathProcessor processor = new LocationPathProcessor();
  76. processor.processLocationPath(expressionPath);
  77. //
  78. mXPathVariable = null; // discard the variable
  79. }
  80. @Override
  81. public void visit(XPathVariableReference vReference) {
  82. mXPathVarReference = vReference;
  83. }
  84. public XPathBpelVariable getVariable () {
  85. if (mXPathVariable == null) {
  86. if (mXPathVarReference == null) {
  87. return null;
  88. }
  89. //
  90. XPathVariable xPathVar = mXPathVarReference.getVariable();
  91. if (xPathVar != null && xPathVar instanceof XPathBpelVariable) {
  92. mXPathVariable = (XPathBpelVariable) xPathVar;
  93. }
  94. }
  95. return mXPathVariable;
  96. }
  97. //---------------------------------------------------
  98. @Override
  99. public void visit(XPathCoreOperation expr) {
  100. visitChildren(expr);
  101. }
  102. @Override
  103. public void visit(XPathCoreFunction expr) {
  104. visitChildren(expr);
  105. }
  106. @Override
  107. public void visit(XPathExtensionFunction expr) {
  108. visitChildren(expr);
  109. }
  110. /**
  111. * See the description of the method "processLocationPath"
  112. */
  113. private class LocationPathProcessor {
  114. // The list can contain objects of either SchemaComponent
  115. // or PredicatedScehmaComp type.
  116. private boolean processingAborted = false;
  117. /**
  118. * Resolves each LocationStep to object form and registers new
  119. * predicates in the BpelPredicateManager.
  120. */
  121. public void processLocationPath(AbstractLocationPath path) {
  122. processingAborted = false;
  123. //speed optimisation, see coments below
  124. // TODO: The path is a part of a big XPath expression or is
  125. // an atomic expression itself. The optimization tries evoiding
  126. // unnecessary resolving of the expression (calculation of XPathSchemaContex).
  127. // But actually the calculation is going to be done later anyway.
  128. // So the optimization is suspecious. It is necessary to convince
  129. // the resolving isn't done second time with the same expression.
  130. if (!needProcessing(path))
  131. return;
  132. //
  133. LocationStep[] stepArr = path.getSteps();
  134. for (int stepIndex = 0; stepIndex < stepArr.length; stepIndex++) {
  135. if (processingAborted) {
  136. break;
  137. }
  138. //
  139. LocationStep step = stepArr[stepIndex];
  140. XPathSchemaContext xPathContext = step.getSchemaContext();
  141. if (xPathContext != null) {
  142. processStep(xPathContext);
  143. }
  144. }
  145. }
  146. /**
  147. * AlexeyY:
  148. * speed optimisation: check if path contains predicates
  149. * before processing this path
  150. **/
  151. private boolean needProcessing(AbstractLocationPath path){
  152. for (LocationStep step : path.getSteps()) {
  153. XPathPredicateExpression[] predicates = step.getPredicates();
  154. if (predicates != null && predicates.length > 0) {
  155. return true;
  156. }
  157. StepNodeTest stepNodeTest = step.getNodeTest();
  158. if (stepNodeTest instanceof StepNodeTypeTest) {
  159. return true;
  160. }
  161. // else if (stepNodeTest instanceof StepNodeNameTest) {
  162. // // TODO: Process * and @* here
  163. // }
  164. }
  165. return false;
  166. }
  167. private void processStep(XPathSchemaContext stepContext) {
  168. if (stepContext instanceof PredicatedSchemaContext) {
  169. PredicatedSchemaContext predContext =
  170. (PredicatedSchemaContext)stepContext;
  171. if (predContext.hasUnknownVariable()) {
  172. processingAborted = true;
  173. return;
  174. }
  175. DirectedList<Object> predicatePath = BpelPathConverter.singleton().
  176. constructObjectLocationList(stepContext, true, true);
  177. //
  178. BpelMapperPredicate newPredSComp = new BpelMapperPredicate(predContext);
  179. mPredManager.addPredicate(predicatePath, newPredSComp);
  180. //
  181. XPathSchemaContext baseContext = predContext.getBaseContext();
  182. if (baseContext != null) {
  183. processStep(baseContext);
  184. }
  185. } else if (stepContext instanceof WildcardSchemaContext) {
  186. //
  187. // Registure special location step to the Special Step manager
  188. WildcardSchemaContext wContext = (WildcardSchemaContext)stepContext;
  189. XPathSpecialStep sStep = wContext.getSpecialStep();
  190. switch (sStep.getType()) {
  191. // case ALL_ELEMENTS:
  192. // case ALL_ATTRIBUTES:
  193. case NODE:
  194. DirectedList<Object> sStepPath = BpelPathConverter.singleton().
  195. constructObjectLocationList(stepContext, true, true);
  196. //
  197. mSStepManager.addStep(sStepPath, sStep);
  198. break;
  199. }
  200. } else if (stepContext instanceof SpecialSchemaContext) {
  201. //
  202. // Registure special location step to the Special Step manager
  203. SpecialSchemaContext wContext = (SpecialSchemaContext)stepContext;
  204. XPathSpecialStep sStep = wContext.getSpecialStep();
  205. switch (sStep.getType()) {
  206. // case ALL_ELEMENTS:
  207. // case ALL_ATTRIBUTES:
  208. case COMMENT:
  209. case TEXT:
  210. case PROCESSING_INSTR:
  211. DirectedList<Object> sStepPath = BpelPathConverter.singleton().
  212. constructObjectLocationList(stepContext, true, true);
  213. //
  214. mSStepManager.addStep(sStepPath, sStep);
  215. break;
  216. }
  217. } else if (stepContext instanceof WrappingSchemaContext) {
  218. processStep(WrappingSchemaContext.class.
  219. cast(stepContext).getBaseContext());
  220. }
  221. }
  222. }
  223. }