286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001, 2002,2004,2005 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.impl.xs.identity;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.Constants;
286N/Aimport com.sun.org.apache.xerces.internal.impl.xpath.XPath;
286N/Aimport com.sun.org.apache.xerces.internal.util.IntStack;
286N/Aimport com.sun.org.apache.xerces.internal.xni.QName;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLAttributes;
286N/Aimport com.sun.org.apache.xerces.internal.xs.AttributePSVI;
286N/Aimport com.sun.org.apache.xerces.internal.xs.ShortList;
286N/Aimport com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
286N/Aimport org.xml.sax.SAXException;
286N/A
286N/A
286N/A/**
286N/A * XPath matcher.
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Andy Clark, IBM
286N/A *
286N/A */
286N/Apublic class XPathMatcher {
286N/A
286N/A //
286N/A // Constants
286N/A //
286N/A
286N/A // debugging
286N/A
286N/A /** Compile to true to debug everything. */
286N/A protected static final boolean DEBUG_ALL = false;
286N/A
286N/A /** Compile to true to debug method callbacks. */
286N/A protected static final boolean DEBUG_METHODS = false || DEBUG_ALL;
286N/A
286N/A /** Compile to true to debug important method callbacks. */
286N/A protected static final boolean DEBUG_METHODS2 = false || DEBUG_METHODS || DEBUG_ALL;
286N/A
286N/A /** Compile to true to debug the <em>really</em> important methods. */
286N/A protected static final boolean DEBUG_METHODS3 = false || DEBUG_METHODS || DEBUG_ALL;
286N/A
286N/A /** Compile to true to debug match. */
286N/A protected static final boolean DEBUG_MATCH = false || DEBUG_ALL;
286N/A
286N/A /** Compile to true to debug step index stack. */
286N/A protected static final boolean DEBUG_STACK = false || DEBUG_ALL;
286N/A
286N/A /** Don't touch this value unless you add more debug constants. */
286N/A protected static final boolean DEBUG_ANY = DEBUG_METHODS ||
286N/A DEBUG_METHODS2 ||
286N/A DEBUG_METHODS3 ||
286N/A DEBUG_MATCH ||
286N/A DEBUG_STACK;
286N/A
286N/A // constants describing whether a match was made,
286N/A // and if so how.
286N/A // matched any way
286N/A protected static final int MATCHED = 1;
286N/A // matched on the attribute axis
286N/A protected static final int MATCHED_ATTRIBUTE = 3;
286N/A // matched on the descendant-or-self axixs
286N/A protected static final int MATCHED_DESCENDANT = 5;
286N/A // matched some previous (ancestor) node on the descendant-or-self-axis, but not this node
286N/A protected static final int MATCHED_DESCENDANT_PREVIOUS = 13;
286N/A
286N/A //
286N/A // Data
286N/A //
286N/A
286N/A /** XPath location path. */
286N/A private XPath.LocationPath[] fLocationPaths;
286N/A
286N/A /** True if XPath has been matched. */
286N/A private int[] fMatched;
286N/A
286N/A /** The matching string. */
286N/A protected Object fMatchedString;
286N/A
286N/A /** Integer stack of step indexes. */
286N/A private IntStack[] fStepIndexes;
286N/A
286N/A /** Current step. */
286N/A private int[] fCurrentStep;
286N/A
286N/A /**
286N/A * No match depth. The value of this field will be zero while
286N/A * matching is successful for the given xpath expression.
286N/A */
286N/A private int [] fNoMatchDepth;
286N/A
286N/A final QName fQName = new QName();
286N/A
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /**
286N/A * Constructs an XPath matcher that implements a document fragment
286N/A * handler.
286N/A *
286N/A * @param xpath The xpath.
286N/A */
286N/A public XPathMatcher(XPath xpath) {
286N/A fLocationPaths = xpath.getLocationPaths();
286N/A fStepIndexes = new IntStack[fLocationPaths.length];
286N/A for(int i=0; i<fStepIndexes.length; i++) fStepIndexes[i] = new IntStack();
286N/A fCurrentStep = new int[fLocationPaths.length];
286N/A fNoMatchDepth = new int[fLocationPaths.length];
286N/A fMatched = new int[fLocationPaths.length];
286N/A } // <init>(XPath)
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A /**
286N/A * Returns value of first member of fMatched that
286N/A * is nonzero.
286N/A */
286N/A public boolean isMatched() {
286N/A // xpath has been matched if any one of the members of the union have matched.
286N/A for (int i=0; i < fLocationPaths.length; i++)
286N/A if (((fMatched[i] & MATCHED) == MATCHED)
286N/A && ((fMatched[i] & MATCHED_DESCENDANT_PREVIOUS) != MATCHED_DESCENDANT_PREVIOUS)
286N/A && ((fNoMatchDepth[i] == 0)
286N/A || ((fMatched[i] & MATCHED_DESCENDANT) == MATCHED_DESCENDANT)))
286N/A return true;
286N/A
286N/A return false;
286N/A } // isMatched():int
286N/A
286N/A //
286N/A // Protected methods
286N/A //
286N/A
286N/A // a place-holder method; to be overridden by subclasses
286N/A // that care about matching element content.
286N/A protected void handleContent(XSTypeDefinition type, boolean nillable, Object value, short valueType, ShortList itemValueType) {
286N/A }
286N/A
286N/A /**
286N/A * This method is called when the XPath handler matches the
286N/A * XPath expression. Subclasses can override this method to
286N/A * provide default handling upon a match.
286N/A */
286N/A protected void matched(Object actualValue, short valueType, ShortList itemValueType, boolean isNil) {
286N/A if (DEBUG_METHODS3) {
286N/A System.out.println(toString()+"#matched(\""+actualValue+"\")");
286N/A }
286N/A } // matched(String content, XSSimpleType val)
286N/A
286N/A //
286N/A // ~XMLDocumentFragmentHandler methods
286N/A //
286N/A
286N/A /**
286N/A * The start of the document fragment.
286N/A */
286N/A public void startDocumentFragment(){
286N/A if (DEBUG_METHODS) {
286N/A System.out.println(toString()+"#startDocumentFragment("+
286N/A ")");
286N/A }
286N/A
286N/A // reset state
286N/A fMatchedString = null;
286N/A for(int i = 0; i < fLocationPaths.length; i++) {
286N/A fStepIndexes[i].clear();
286N/A fCurrentStep[i] = 0;
286N/A fNoMatchDepth[i] = 0;
286N/A fMatched[i] = 0;
286N/A }
286N/A
286N/A
286N/A } // startDocumentFragment()
286N/A
286N/A /**
286N/A * The start of an element. If the document specifies the start element
286N/A * by using an empty tag, then the startElement method will immediately
286N/A * be followed by the endElement method, with no intervening methods.
286N/A *
286N/A * @param element The name of the element.
286N/A * @param attributes The element attributes.
286N/A *
286N/A * @throws SAXException Thrown by handler to signal an error.
286N/A */
286N/A public void startElement(QName element, XMLAttributes attributes){
286N/A if (DEBUG_METHODS2) {
286N/A System.out.println(toString()+"#startElement("+
286N/A "element={"+element+"},"+
286N/A "attributes=..."+attributes+
286N/A ")");
286N/A }
286N/A
286N/A for(int i = 0; i < fLocationPaths.length; i++) {
286N/A // push context
286N/A int startStep = fCurrentStep[i];
286N/A fStepIndexes[i].push(startStep);
286N/A
286N/A // try next xpath, if not matching
286N/A if ((fMatched[i] & MATCHED_DESCENDANT) == MATCHED || fNoMatchDepth[i] > 0) {
286N/A fNoMatchDepth[i]++;
286N/A continue;
286N/A }
286N/A if((fMatched[i] & MATCHED_DESCENDANT) == MATCHED_DESCENDANT) {
286N/A fMatched[i] = MATCHED_DESCENDANT_PREVIOUS;
286N/A }
286N/A
286N/A if (DEBUG_STACK) {
286N/A System.out.println(toString()+": "+fStepIndexes[i]);
286N/A }
286N/A
286N/A // consume self::node() steps
286N/A XPath.Step[] steps = fLocationPaths[i].steps;
286N/A while (fCurrentStep[i] < steps.length &&
286N/A steps[fCurrentStep[i]].axis.type == XPath.Axis.SELF) {
286N/A if (DEBUG_MATCH) {
286N/A XPath.Step step = steps[fCurrentStep[i]];
286N/A System.out.println(toString()+" [SELF] MATCHED!");
286N/A }
286N/A fCurrentStep[i]++;
286N/A }
286N/A if (fCurrentStep[i] == steps.length) {
286N/A if (DEBUG_MATCH) {
286N/A System.out.println(toString()+" XPath MATCHED!");
286N/A }
286N/A fMatched[i] = MATCHED;
286N/A continue;
286N/A }
286N/A
286N/A // now if the current step is a descendant step, we let the next
286N/A // step do its thing; if it fails, we reset ourselves
286N/A // to look at this step for next time we're called.
286N/A // so first consume all descendants:
286N/A int descendantStep = fCurrentStep[i];
286N/A while(fCurrentStep[i] < steps.length && steps[fCurrentStep[i]].axis.type == XPath.Axis.DESCENDANT) {
286N/A if (DEBUG_MATCH) {
286N/A XPath.Step step = steps[fCurrentStep[i]];
286N/A System.out.println(toString()+" [DESCENDANT] MATCHED!");
286N/A }
286N/A fCurrentStep[i]++;
286N/A }
286N/A boolean sawDescendant = fCurrentStep[i] > descendantStep;
286N/A if (fCurrentStep[i] == steps.length) {
286N/A if (DEBUG_MATCH) {
286N/A System.out.println(toString()+" XPath DIDN'T MATCH!");
286N/A }
286N/A fNoMatchDepth[i]++;
286N/A if (DEBUG_MATCH) {
286N/A System.out.println(toString()+" [CHILD] after NO MATCH");
286N/A }
286N/A continue;
286N/A }
286N/A
286N/A // match child::... step, if haven't consumed any self::node()
286N/A if ((fCurrentStep[i] == startStep || fCurrentStep[i] > descendantStep) &&
286N/A steps[fCurrentStep[i]].axis.type == XPath.Axis.CHILD) {
286N/A XPath.Step step = steps[fCurrentStep[i]];
286N/A XPath.NodeTest nodeTest = step.nodeTest;
286N/A if (DEBUG_MATCH) {
286N/A System.out.println(toString()+" [CHILD] before");
286N/A }
286N/A if (nodeTest.type == XPath.NodeTest.QNAME) {
286N/A if (!nodeTest.name.equals(element)) {
286N/A if(fCurrentStep[i] > descendantStep) {
286N/A fCurrentStep[i] = descendantStep;
286N/A continue;
286N/A }
286N/A fNoMatchDepth[i]++;
286N/A if (DEBUG_MATCH) {
286N/A System.out.println(toString()+" [CHILD] after NO MATCH");
286N/A }
286N/A continue;
286N/A }
286N/A }
286N/A fCurrentStep[i]++;
286N/A if (DEBUG_MATCH) {
286N/A System.out.println(toString()+" [CHILD] after MATCHED!");
286N/A }
286N/A }
286N/A if (fCurrentStep[i] == steps.length) {
286N/A if(sawDescendant) {
286N/A fCurrentStep[i] = descendantStep;
286N/A fMatched[i] = MATCHED_DESCENDANT;
286N/A } else {
286N/A fMatched[i] = MATCHED;
286N/A }
286N/A continue;
286N/A }
286N/A
286N/A // match attribute::... step
286N/A if (fCurrentStep[i] < steps.length &&
286N/A steps[fCurrentStep[i]].axis.type == XPath.Axis.ATTRIBUTE) {
286N/A if (DEBUG_MATCH) {
286N/A System.out.println(toString()+" [ATTRIBUTE] before");
286N/A }
286N/A int attrCount = attributes.getLength();
286N/A if (attrCount > 0) {
286N/A XPath.NodeTest nodeTest = steps[fCurrentStep[i]].nodeTest;
286N/A
286N/A for (int aIndex = 0; aIndex < attrCount; aIndex++) {
286N/A attributes.getName(aIndex, fQName);
286N/A if (nodeTest.type != XPath.NodeTest.QNAME ||
286N/A nodeTest.name.equals(fQName)) {
286N/A fCurrentStep[i]++;
286N/A if (fCurrentStep[i] == steps.length) {
286N/A fMatched[i] = MATCHED_ATTRIBUTE;
286N/A int j=0;
286N/A for(; j<i && ((fMatched[j] & MATCHED) != MATCHED); j++);
286N/A if(j==i) {
286N/A AttributePSVI attrPSVI = (AttributePSVI)attributes.getAugmentations(aIndex).getItem(Constants.ATTRIBUTE_PSVI);
286N/A fMatchedString = attrPSVI.getActualNormalizedValue();
286N/A matched(fMatchedString, attrPSVI.getActualNormalizedValueType(), attrPSVI.getItemValueTypes(), false);
286N/A }
286N/A }
286N/A break;
286N/A }
286N/A }
286N/A }
286N/A if ((fMatched[i] & MATCHED) != MATCHED) {
286N/A if(fCurrentStep[i] > descendantStep) {
286N/A fCurrentStep[i] = descendantStep;
286N/A continue;
286N/A }
286N/A fNoMatchDepth[i]++;
286N/A if (DEBUG_MATCH) {
286N/A System.out.println(toString()+" [ATTRIBUTE] after");
286N/A }
286N/A continue;
286N/A }
286N/A if (DEBUG_MATCH) {
286N/A System.out.println(toString()+" [ATTRIBUTE] after MATCHED!");
286N/A }
286N/A }
286N/A }
286N/A
286N/A }
286N/A // startElement(QName,XMLAttrList,int)
286N/A
286N/A /**
286N/A * @param element
286N/A * name of the element.
286N/A * @param type
286N/A * content type of this element. IOW, the XML schema type
286N/A * of the <tt>value</tt>. Note that this may not be the type declared
286N/A * in the element declaration, but it is "the actual type". For example,
286N/A * if the XML is &lt;foo xsi:type="xs:string">aaa&lt;/foo>, this
286N/A * parameter will be "xs:string".
286N/A * @param nillable - nillable
286N/A * true if the element declaration is nillable.
286N/A * @param value - actual value
286N/A * the typed value of the content of this element.
286N/A */
286N/A public void endElement(QName element, XSTypeDefinition type, boolean nillable, Object value, short valueType, ShortList itemValueType) {
286N/A if (DEBUG_METHODS2) {
286N/A System.out.println(toString()+"#endElement("+
286N/A "element={"+element+"},"+
286N/A ")");
286N/A }
286N/A for(int i = 0; i<fLocationPaths.length; i++) {
286N/A // go back a step
286N/A fCurrentStep[i] = fStepIndexes[i].pop();
286N/A
286N/A // don't do anything, if not matching
286N/A if (fNoMatchDepth[i] > 0) {
286N/A fNoMatchDepth[i]--;
286N/A }
286N/A
286N/A // signal match, if appropriate
286N/A else {
286N/A int j=0;
286N/A for(; j<i && ((fMatched[j] & MATCHED) != MATCHED); j++);
286N/A if ((j<i) || (fMatched[j] == 0) ||
286N/A ((fMatched[j] & MATCHED_ATTRIBUTE) == MATCHED_ATTRIBUTE)) {
286N/A continue;
286N/A }
286N/A // only certain kinds of matchers actually
286N/A // match element content. This permits
286N/A // them a way to override this to do nothing
286N/A // and hopefully save a few operations.
286N/A handleContent(type, nillable, value, valueType, itemValueType);
286N/A fMatched[i] = 0;
286N/A }
286N/A
286N/A if (DEBUG_STACK) {
286N/A System.out.println(toString()+": "+fStepIndexes[i]);
286N/A }
286N/A }
286N/A
286N/A } // endElement(QName)
286N/A
286N/A //
286N/A // Object methods
286N/A //
286N/A
286N/A /** Returns a string representation of this object. */
286N/A public String toString() {
286N/A /***
286N/A return fLocationPath.toString();
286N/A /***/
286N/A StringBuffer str = new StringBuffer();
286N/A String s = super.toString();
286N/A int index2 = s.lastIndexOf('.');
286N/A if (index2 != -1) {
286N/A s = s.substring(index2 + 1);
286N/A }
286N/A str.append(s);
286N/A for(int i =0;i<fLocationPaths.length; i++) {
286N/A str.append('[');
286N/A XPath.Step[] steps = fLocationPaths[i].steps;
286N/A for (int j = 0; j < steps.length; j++) {
286N/A if (j == fCurrentStep[i]) {
286N/A str.append('^');
286N/A }
286N/A str.append(steps[j].toString());
286N/A if (j < steps.length - 1) {
286N/A str.append('/');
286N/A }
286N/A }
286N/A if (fCurrentStep[i] == steps.length) {
286N/A str.append('^');
286N/A }
286N/A str.append(']');
286N/A str.append(',');
286N/A }
286N/A return str.toString();
286N/A } // toString():String
286N/A
286N/A //
286N/A // Private methods
286N/A //
286N/A
286N/A /** Normalizes text. */
286N/A private String normalize(String s) {
286N/A StringBuffer str = new StringBuffer();
286N/A int length = s.length();
286N/A for (int i = 0; i < length; i++) {
286N/A char c = s.charAt(i);
286N/A switch (c) {
286N/A case '\n': {
286N/A str.append("\\n");
286N/A break;
286N/A }
286N/A default: {
286N/A str.append(c);
286N/A }
286N/A }
286N/A }
286N/A return str.toString();
286N/A } // normalize(String):String
286N/A
286N/A //
286N/A // MAIN
286N/A //
286N/A
286N/A // NOTE: The main of this class is here for debugging purposes.
286N/A // However, javac (JDK 1.1.8) has an internal compiler
286N/A // error when compiling. Jikes has no problem, though.
286N/A //
286N/A // If you want to use this main, use Jikes to compile but
286N/A // *never* check in this code to CVS without commenting it
286N/A // out. -Ac
286N/A
286N/A /** Main program. */
286N/A /***
286N/A public static void main(String[] argv) throws XNIException {
286N/A
286N/A if (DEBUG_ANY) {
286N/A for (int i = 0; i < argv.length; i++) {
286N/A final String expr = argv[i];
286N/A final XPath xpath = new XPath(expr, symbols, null);
286N/A final XPathMatcher matcher = new XPathMatcher(xpath, true);
286N/A com.sun.org.apache.xerces.internal.parsers.SAXParser parser =
286N/A new com.sun.org.apache.xerces.internal.parsers.SAXParser(symbols) {
286N/A public void startDocument() throws XNIException {
286N/A matcher.startDocumentFragment(symbols, null);
286N/A }
286N/A public void startElement(QName element, XMLAttrList attributes, int handle) throws XNIException {
286N/A matcher.startElement(element, attributes, handle);
286N/A }
286N/A public void characters(char[] ch, int offset, int length) throws XNIException {
286N/A matcher.characters(ch, offset, length);
286N/A }
286N/A public void endElement(QName element) throws XNIException {
286N/A matcher.endElement(element);
286N/A }
286N/A public void endDocument() throws XNIException {
286N/A matcher.endDocumentFragment();
286N/A }
286N/A };
286N/A System.out.println("#### argv["+i+"]: \""+expr+"\" -> \""+xpath.toString()+'"');
286N/A final String uri = argv[++i];
286N/A System.out.println("#### argv["+i+"]: "+uri);
286N/A parser.parse(uri);
286N/A }
286N/A }
286N/A
286N/A } // main(String[])
286N/A /***/
286N/A
286N/A} // class XPathMatcher