1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 2002 The Apache Software Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.impl.xs.identity;
  58. import com.sun.org.apache.xerces.internal.impl.Constants;
  59. import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
  60. import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  61. import com.sun.org.apache.xerces.internal.util.IntStack;
  62. import com.sun.org.apache.xerces.internal.xni.QName;
  63. import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
  64. import com.sun.org.apache.xerces.internal.xs.AttributePSVI;
  65. /**
  66. * XPath matcher.
  67. *
  68. * @author Andy Clark, IBM
  69. *
  70. * @version $Id: XPathMatcher.java,v 1.22 2004/02/10 21:26:03 kohsuke Exp $
  71. */
  72. public class XPathMatcher {
  73. //
  74. // Constants
  75. //
  76. // debugging
  77. /** Compile to true to debug everything. */
  78. protected static final boolean DEBUG_ALL = false;
  79. /** Compile to true to debug method callbacks. */
  80. protected static final boolean DEBUG_METHODS = false || DEBUG_ALL;
  81. /** Compile to true to debug important method callbacks. */
  82. protected static final boolean DEBUG_METHODS2 = false || DEBUG_METHODS || DEBUG_ALL;
  83. /** Compile to true to debug the <em>really</em> important methods. */
  84. protected static final boolean DEBUG_METHODS3 = false || DEBUG_METHODS || DEBUG_ALL;
  85. /** Compile to true to debug match. */
  86. protected static final boolean DEBUG_MATCH = false || DEBUG_ALL;
  87. /** Compile to true to debug step index stack. */
  88. protected static final boolean DEBUG_STACK = false || DEBUG_ALL;
  89. /** Don't touch this value unless you add more debug constants. */
  90. protected static final boolean DEBUG_ANY = DEBUG_METHODS ||
  91. DEBUG_METHODS2 ||
  92. DEBUG_METHODS3 ||
  93. DEBUG_MATCH ||
  94. DEBUG_STACK;
  95. // constants describing whether a match was made,
  96. // and if so how.
  97. // matched any way
  98. protected static final int MATCHED = 1;
  99. // matched on the attribute axis
  100. protected static final int MATCHED_ATTRIBUTE = 3;
  101. // matched on the descendant-or-self axixs
  102. protected static final int MATCHED_DESCENDANT = 5;
  103. // matched some previous (ancestor) node on the descendant-or-self-axis, but not this node
  104. protected static final int MATCHED_DESCENDANT_PREVIOUS = 13;
  105. //
  106. // Data
  107. //
  108. /** XPath location path. */
  109. private XPath.LocationPath[] fLocationPaths;
  110. /** True if XPath has been matched. */
  111. private int[] fMatched;
  112. /** The matching string. */
  113. protected Object fMatchedString;
  114. /** Integer stack of step indexes. */
  115. private IntStack[] fStepIndexes;
  116. /** Current step. */
  117. private int[] fCurrentStep;
  118. /**
  119. * No match depth. The value of this field will be zero while
  120. * matching is successful for the given xpath expression.
  121. */
  122. private int [] fNoMatchDepth;
  123. final QName fQName = new QName();
  124. XSTypeDefinition fCurrMatchedType = null;
  125. //
  126. // Constructors
  127. //
  128. /**
  129. * Constructs an XPath matcher that implements a document fragment
  130. * handler.
  131. *
  132. * @param xpath The xpath.
  133. */
  134. public XPathMatcher(XPath xpath) {
  135. fLocationPaths = xpath.getLocationPaths();
  136. fStepIndexes = new IntStack[fLocationPaths.length];
  137. for(int i=0; i<fStepIndexes.length; i++) fStepIndexes[i] = new IntStack();
  138. fCurrentStep = new int[fLocationPaths.length];
  139. fNoMatchDepth = new int[fLocationPaths.length];
  140. fMatched = new int[fLocationPaths.length];
  141. } // <init>(XPath)
  142. //
  143. // Public methods
  144. //
  145. /**
  146. * Returns value of first member of fMatched that
  147. * is nonzero.
  148. */
  149. public boolean isMatched() {
  150. // xpath has been matched if any one of the members of the union have matched.
  151. for (int i=0; i < fLocationPaths.length; i++)
  152. if (((fMatched[i] & MATCHED) == MATCHED)
  153. && ((fMatched[i] & MATCHED_DESCENDANT_PREVIOUS) != MATCHED_DESCENDANT_PREVIOUS)
  154. && ((fNoMatchDepth[i] == 0)
  155. || ((fMatched[i] & MATCHED_DESCENDANT) == MATCHED_DESCENDANT)))
  156. return true;
  157. return false;
  158. } // isMatched():int
  159. //
  160. // Protected methods
  161. //
  162. // a place-holder method; to be overridden by subclasses
  163. // that care about matching element content.
  164. protected void handleContent(XSTypeDefinition type, boolean nillable, Object value) {
  165. }
  166. /**
  167. * This method is called when the XPath handler matches the
  168. * XPath expression. Subclasses can override this method to
  169. * provide default handling upon a match.
  170. */
  171. protected void matched(Object actualValue, boolean isNil) {
  172. if (DEBUG_METHODS3) {
  173. System.out.println(toString()+"#matched(\""+actualValue+"\")");
  174. }
  175. } // matched(String content, XSSimpleType val)
  176. //
  177. // ~XMLDocumentFragmentHandler methods
  178. //
  179. /**
  180. * The start of the document fragment.
  181. *
  182. * @param context The namespace scope in effect at the
  183. * start of this document fragment.
  184. */
  185. public void startDocumentFragment(){
  186. if (DEBUG_METHODS) {
  187. System.out.println(toString()+"#startDocumentFragment("+
  188. ")");
  189. }
  190. // reset state
  191. fMatchedString = null;
  192. for(int i = 0; i < fLocationPaths.length; i++) {
  193. fStepIndexes[i].clear();
  194. fCurrentStep[i] = 0;
  195. fNoMatchDepth[i] = 0;
  196. fMatched[i] = 0;
  197. }
  198. } // startDocumentFragment(SymbolTable)
  199. /**
  200. * The start of an element. If the document specifies the start element
  201. * by using an empty tag, then the startElement method will immediately
  202. * be followed by the endElement method, with no intervening methods.
  203. *
  204. * @param element The name of the element.
  205. * @param attributes The element attributes.
  206. * @param type: The element's type
  207. *
  208. * @throws SAXException Thrown by handler to signal an error.
  209. */
  210. public void startElement(QName element, XMLAttributes attributes){
  211. if (DEBUG_METHODS2) {
  212. System.out.println(toString()+"#startElement("+
  213. "element={"+element+"},"+
  214. "attributes=..."+attributes+
  215. ")");
  216. }
  217. for(int i = 0; i < fLocationPaths.length; i++) {
  218. // push context
  219. int startStep = fCurrentStep[i];
  220. fStepIndexes[i].push(startStep);
  221. // try next xpath, if not matching
  222. if ((fMatched[i] & MATCHED_DESCENDANT) == MATCHED || fNoMatchDepth[i] > 0) {
  223. fNoMatchDepth[i]++;
  224. continue;
  225. }
  226. if((fMatched[i] & MATCHED_DESCENDANT) == MATCHED_DESCENDANT) {
  227. fMatched[i] = MATCHED_DESCENDANT_PREVIOUS;
  228. }
  229. if (DEBUG_STACK) {
  230. System.out.println(toString()+": "+fStepIndexes[i]);
  231. }
  232. // consume self::node() steps
  233. XPath.Step[] steps = fLocationPaths[i].steps;
  234. while (fCurrentStep[i] < steps.length &&
  235. steps[fCurrentStep[i]].axis.type == XPath.Axis.SELF) {
  236. if (DEBUG_MATCH) {
  237. XPath.Step step = steps[fCurrentStep[i]];
  238. System.out.println(toString()+" [SELF] MATCHED!");
  239. }
  240. fCurrentStep[i]++;
  241. }
  242. if (fCurrentStep[i] == steps.length) {
  243. if (DEBUG_MATCH) {
  244. System.out.println(toString()+" XPath MATCHED!");
  245. }
  246. fMatched[i] = MATCHED;
  247. continue;
  248. }
  249. // now if the current step is a descendant step, we let the next
  250. // step do its thing; if it fails, we reset ourselves
  251. // to look at this step for next time we're called.
  252. // so first consume all descendants:
  253. int descendantStep = fCurrentStep[i];
  254. while(fCurrentStep[i] < steps.length && steps[fCurrentStep[i]].axis.type == XPath.Axis.DESCENDANT) {
  255. if (DEBUG_MATCH) {
  256. XPath.Step step = steps[fCurrentStep[i]];
  257. System.out.println(toString()+" [DESCENDANT] MATCHED!");
  258. }
  259. fCurrentStep[i]++;
  260. }
  261. boolean sawDescendant = fCurrentStep[i] > descendantStep;
  262. if (fCurrentStep[i] == steps.length) {
  263. if (DEBUG_MATCH) {
  264. System.out.println(toString()+" XPath DIDN'T MATCH!");
  265. }
  266. fNoMatchDepth[i]++;
  267. if (DEBUG_MATCH) {
  268. System.out.println(toString()+" [CHILD] after NO MATCH");
  269. }
  270. continue;
  271. }
  272. // match child::... step, if haven't consumed any self::node()
  273. if ((fCurrentStep[i] == startStep || fCurrentStep[i] > descendantStep) &&
  274. steps[fCurrentStep[i]].axis.type == XPath.Axis.CHILD) {
  275. XPath.Step step = steps[fCurrentStep[i]];
  276. XPath.NodeTest nodeTest = step.nodeTest;
  277. if (DEBUG_MATCH) {
  278. System.out.println(toString()+" [CHILD] before");
  279. }
  280. if (nodeTest.type == XPath.NodeTest.QNAME) {
  281. if (!nodeTest.name.equals(element)) {
  282. if(fCurrentStep[i] > descendantStep) {
  283. fCurrentStep[i] = descendantStep;
  284. continue;
  285. }
  286. fNoMatchDepth[i]++;
  287. if (DEBUG_MATCH) {
  288. System.out.println(toString()+" [CHILD] after NO MATCH");
  289. }
  290. continue;
  291. }
  292. }
  293. fCurrentStep[i]++;
  294. if (DEBUG_MATCH) {
  295. System.out.println(toString()+" [CHILD] after MATCHED!");
  296. }
  297. }
  298. if (fCurrentStep[i] == steps.length) {
  299. if(sawDescendant) {
  300. fCurrentStep[i] = descendantStep;
  301. fMatched[i] = MATCHED_DESCENDANT;
  302. } else {
  303. fMatched[i] = MATCHED;
  304. }
  305. continue;
  306. }
  307. // match attribute::... step
  308. if (fCurrentStep[i] < steps.length &&
  309. steps[fCurrentStep[i]].axis.type == XPath.Axis.ATTRIBUTE) {
  310. if (DEBUG_MATCH) {
  311. System.out.println(toString()+" [ATTRIBUTE] before");
  312. }
  313. int attrCount = attributes.getLength();
  314. if (attrCount > 0) {
  315. XPath.NodeTest nodeTest = steps[fCurrentStep[i]].nodeTest;
  316. for (int aIndex = 0; aIndex < attrCount; aIndex++) {
  317. attributes.getName(aIndex, fQName);
  318. if (nodeTest.type != XPath.NodeTest.QNAME ||
  319. nodeTest.name.equals(fQName)) {
  320. fCurrentStep[i]++;
  321. if (fCurrentStep[i] == steps.length) {
  322. fMatched[i] = MATCHED_ATTRIBUTE;
  323. int j=0;
  324. for(; j<i && ((fMatched[j] & MATCHED) != MATCHED); j++);
  325. if(j==i) {
  326. AttributePSVI attrPSVI = (AttributePSVI)attributes.getAugmentations(aIndex).getItem(Constants.ATTRIBUTE_PSVI);
  327. fMatchedString = attrPSVI.getActualNormalizedValue();
  328. fCurrMatchedType = attrPSVI.getTypeDefinition();
  329. matched(fMatchedString, false);
  330. }
  331. }
  332. break;
  333. }
  334. }
  335. }
  336. if ((fMatched[i] & MATCHED) != MATCHED) {
  337. if(fCurrentStep[i] > descendantStep) {
  338. fCurrentStep[i] = descendantStep;
  339. continue;
  340. }
  341. fNoMatchDepth[i]++;
  342. if (DEBUG_MATCH) {
  343. System.out.println(toString()+" [ATTRIBUTE] after");
  344. }
  345. continue;
  346. }
  347. if (DEBUG_MATCH) {
  348. System.out.println(toString()+" [ATTRIBUTE] after MATCHED!");
  349. }
  350. }
  351. }
  352. }
  353. // startElement(QName,XMLAttrList,int)
  354. /**
  355. * @param element
  356. * name of the element.
  357. * @param type
  358. * content type of this element. IOW, the XML schema type
  359. * of the <tt>value</tt>. Note that this may not be the type declared
  360. * in the element declaration, but it is "the actual type". For example,
  361. * if the XML is <foo xsi:type="xs:string">aaa</foo>, this
  362. * parameter will be "xs:string".
  363. * @param nillable - nillable
  364. * true if the element declaration is nillable.
  365. * @param value - actual value
  366. * the typed value of the content of this element.
  367. */
  368. public void endElement(QName element, XSTypeDefinition type, boolean nillable, Object value ) {
  369. if (DEBUG_METHODS2) {
  370. System.out.println(toString()+"#endElement("+
  371. "element={"+element+"},"+
  372. ")");
  373. }
  374. for(int i = 0; i<fLocationPaths.length; i++) {
  375. // go back a step
  376. fCurrentStep[i] = fStepIndexes[i].pop();
  377. // don't do anything, if not matching
  378. if (fNoMatchDepth[i] > 0) {
  379. fNoMatchDepth[i]--;
  380. }
  381. // signal match, if appropriate
  382. else {
  383. int j=0;
  384. for(; j<i && ((fMatched[j] & MATCHED) != MATCHED); j++);
  385. if ((j<i) || (fMatched[j] == 0) ||
  386. ((fMatched[j] & MATCHED_ATTRIBUTE) == MATCHED_ATTRIBUTE)) {
  387. continue;
  388. }
  389. // only certain kinds of matchers actually
  390. // match element content. This permits
  391. // them a way to override this to do nothing
  392. // and hopefully save a few operations.
  393. handleContent(type, nillable, value);
  394. fMatched[i] = 0;
  395. }
  396. if (DEBUG_STACK) {
  397. System.out.println(toString()+": "+fStepIndexes[i]);
  398. }
  399. }
  400. } // endElement(QName)
  401. //
  402. // Object methods
  403. //
  404. /** Returns a string representation of this object. */
  405. public String toString() {
  406. /***
  407. return fLocationPath.toString();
  408. /***/
  409. StringBuffer str = new StringBuffer();
  410. String s = super.toString();
  411. int index2 = s.lastIndexOf('.');
  412. if (index2 != -1) {
  413. s = s.substring(index2 + 1);
  414. }
  415. str.append(s);
  416. for(int i =0;i<fLocationPaths.length; i++) {
  417. str.append('[');
  418. XPath.Step[] steps = fLocationPaths[i].steps;
  419. for (int j = 0; j < steps.length; j++) {
  420. if (j == fCurrentStep[i]) {
  421. str.append('^');
  422. }
  423. str.append(steps[j].toString());
  424. if (j < steps.length - 1) {
  425. str.append('/');
  426. }
  427. }
  428. if (fCurrentStep[i] == steps.length) {
  429. str.append('^');
  430. }
  431. str.append(']');
  432. str.append(',');
  433. }
  434. return str.toString();
  435. } // toString():String
  436. //
  437. // Private methods
  438. //
  439. /** Normalizes text. */
  440. private String normalize(String s) {
  441. StringBuffer str = new StringBuffer();
  442. int length = s.length();
  443. for (int i = 0; i < length; i++) {
  444. char c = s.charAt(i);
  445. switch (c) {
  446. case '\n': {
  447. str.append("\\n");
  448. break;
  449. }
  450. default: {
  451. str.append(c);
  452. }
  453. }
  454. }
  455. return str.toString();
  456. } // normalize(String):String
  457. public XSTypeDefinition getCurrentMatchedType(){
  458. return fCurrMatchedType;
  459. }
  460. //
  461. // MAIN
  462. //
  463. // NOTE: The main of this class is here for debugging purposes.
  464. // However, javac (JDK 1.1.8) has an internal compiler
  465. // error when compiling. Jikes has no problem, though.
  466. //
  467. // If you want to use this main, use Jikes to compile but
  468. // *never* check in this code to CVS without commenting it
  469. // out. -Ac
  470. /** Main program. */
  471. /***
  472. public static void main(String[] argv) throws XNIException {
  473. if (DEBUG_ANY) {
  474. for (int i = 0; i < argv.length; i++) {
  475. final String expr = argv[i];
  476. final XPath xpath = new XPath(expr, symbols, null);
  477. final XPathMatcher matcher = new XPathMatcher(xpath, true);
  478. com.sun.org.apache.xerces.internal.parsers.SAXParser parser =
  479. new com.sun.org.apache.xerces.internal.parsers.SAXParser(symbols) {
  480. public void startDocument() throws XNIException {
  481. matcher.startDocumentFragment(symbols, null);
  482. }
  483. public void startElement(QName element, XMLAttrList attributes, int handle) throws XNIException {
  484. matcher.startElement(element, attributes, handle);
  485. }
  486. public void characters(char[] ch, int offset, int length) throws XNIException {
  487. matcher.characters(ch, offset, length);
  488. }
  489. public void endElement(QName element) throws XNIException {
  490. matcher.endElement(element);
  491. }
  492. public void endDocument() throws XNIException {
  493. matcher.endDocumentFragment();
  494. }
  495. };
  496. System.out.println("#### argv["+i+"]: \""+expr+"\" -> \""+xpath.toString()+'"');
  497. final String uri = argv[++i];
  498. System.out.println("#### argv["+i+"]: "+uri);
  499. parser.parse(uri);
  500. }
  501. }
  502. } // main(String[])
  503. /***/
  504. } // class XPathMatcher