xpathmatcher.java
来自「JAVA 所有包」· Java 代码 · 共 520 行 · 第 1/2 页
JAVA
520 行
boolean sawDescendant = fCurrentStep[i] > descendantStep; if (fCurrentStep[i] == steps.length) { if (DEBUG_MATCH) { System.out.println(toString()+" XPath DIDN'T MATCH!"); } fNoMatchDepth[i]++; if (DEBUG_MATCH) { System.out.println(toString()+" [CHILD] after NO MATCH"); } continue; } // match child::... step, if haven't consumed any self::node() if ((fCurrentStep[i] == startStep || fCurrentStep[i] > descendantStep) && steps[fCurrentStep[i]].axis.type == XPath.Axis.CHILD) { XPath.Step step = steps[fCurrentStep[i]]; XPath.NodeTest nodeTest = step.nodeTest; if (DEBUG_MATCH) { System.out.println(toString()+" [CHILD] before"); } if (nodeTest.type == XPath.NodeTest.QNAME) { if (!nodeTest.name.equals(element)) { if(fCurrentStep[i] > descendantStep) { fCurrentStep[i] = descendantStep; continue; } fNoMatchDepth[i]++; if (DEBUG_MATCH) { System.out.println(toString()+" [CHILD] after NO MATCH"); } continue; } } fCurrentStep[i]++; if (DEBUG_MATCH) { System.out.println(toString()+" [CHILD] after MATCHED!"); } } if (fCurrentStep[i] == steps.length) { if(sawDescendant) { fCurrentStep[i] = descendantStep; fMatched[i] = MATCHED_DESCENDANT; } else { fMatched[i] = MATCHED; } continue; } // match attribute::... step if (fCurrentStep[i] < steps.length && steps[fCurrentStep[i]].axis.type == XPath.Axis.ATTRIBUTE) { if (DEBUG_MATCH) { System.out.println(toString()+" [ATTRIBUTE] before"); } int attrCount = attributes.getLength(); if (attrCount > 0) { XPath.NodeTest nodeTest = steps[fCurrentStep[i]].nodeTest; for (int aIndex = 0; aIndex < attrCount; aIndex++) { attributes.getName(aIndex, fQName); if (nodeTest.type != XPath.NodeTest.QNAME || nodeTest.name.equals(fQName)) { fCurrentStep[i]++; if (fCurrentStep[i] == steps.length) { fMatched[i] = MATCHED_ATTRIBUTE; int j=0; for(; j<i && ((fMatched[j] & MATCHED) != MATCHED); j++); if(j==i) { AttributePSVI attrPSVI = (AttributePSVI)attributes.getAugmentations(aIndex).getItem(Constants.ATTRIBUTE_PSVI); fMatchedString = attrPSVI.getActualNormalizedValue(); matched(fMatchedString, attrPSVI.getActualNormalizedValueType(), attrPSVI.getItemValueTypes(), false); } } break; } } } if ((fMatched[i] & MATCHED) != MATCHED) { if(fCurrentStep[i] > descendantStep) { fCurrentStep[i] = descendantStep; continue; } fNoMatchDepth[i]++; if (DEBUG_MATCH) { System.out.println(toString()+" [ATTRIBUTE] after"); } continue; } if (DEBUG_MATCH) { System.out.println(toString()+" [ATTRIBUTE] after MATCHED!"); } } } } // startElement(QName,XMLAttrList,int) /** * @param element * name of the element. * @param type * content type of this element. IOW, the XML schema type * of the <tt>value</tt>. Note that this may not be the type declared * in the element declaration, but it is "the actual type". For example, * if the XML is <foo xsi:type="xs:string">aaa</foo>, this * parameter will be "xs:string". * @param nillable - nillable * true if the element declaration is nillable. * @param value - actual value * the typed value of the content of this element. */ public void endElement(QName element, XSTypeDefinition type, boolean nillable, Object value, short valueType, ShortList itemValueType) { if (DEBUG_METHODS2) { System.out.println(toString()+"#endElement("+ "element={"+element+"},"+ ")"); } for(int i = 0; i<fLocationPaths.length; i++) { // go back a step fCurrentStep[i] = fStepIndexes[i].pop(); // don't do anything, if not matching if (fNoMatchDepth[i] > 0) { fNoMatchDepth[i]--; } // signal match, if appropriate else { int j=0; for(; j<i && ((fMatched[j] & MATCHED) != MATCHED); j++); if ((j<i) || (fMatched[j] == 0) || ((fMatched[j] & MATCHED_ATTRIBUTE) == MATCHED_ATTRIBUTE)) { continue; } // only certain kinds of matchers actually // match element content. This permits // them a way to override this to do nothing // and hopefully save a few operations. handleContent(type, nillable, value, valueType, itemValueType); fMatched[i] = 0; } if (DEBUG_STACK) { System.out.println(toString()+": "+fStepIndexes[i]); } } } // endElement(QName) // // Object methods // /** Returns a string representation of this object. */ public String toString() { /*** return fLocationPath.toString(); /***/ StringBuffer str = new StringBuffer(); String s = super.toString(); int index2 = s.lastIndexOf('.'); if (index2 != -1) { s = s.substring(index2 + 1); } str.append(s); for(int i =0;i<fLocationPaths.length; i++) { str.append('['); XPath.Step[] steps = fLocationPaths[i].steps; for (int j = 0; j < steps.length; j++) { if (j == fCurrentStep[i]) { str.append('^'); } str.append(steps[j].toString()); if (j < steps.length - 1) { str.append('/'); } } if (fCurrentStep[i] == steps.length) { str.append('^'); } str.append(']'); str.append(','); } return str.toString(); } // toString():String // // Private methods // /** Normalizes text. */ private String normalize(String s) { StringBuffer str = new StringBuffer(); int length = s.length(); for (int i = 0; i < length; i++) { char c = s.charAt(i); switch (c) { case '\n': { str.append("\\n"); break; } default: { str.append(c); } } } return str.toString(); } // normalize(String):String // // MAIN // // NOTE: The main of this class is here for debugging purposes. // However, javac (JDK 1.1.8) has an internal compiler // error when compiling. Jikes has no problem, though. // // If you want to use this main, use Jikes to compile but // *never* check in this code to CVS without commenting it // out. -Ac /** Main program. */ /*** public static void main(String[] argv) throws XNIException { if (DEBUG_ANY) { for (int i = 0; i < argv.length; i++) { final String expr = argv[i]; final XPath xpath = new XPath(expr, symbols, null); final XPathMatcher matcher = new XPathMatcher(xpath, true); com.sun.org.apache.xerces.internal.parsers.SAXParser parser = new com.sun.org.apache.xerces.internal.parsers.SAXParser(symbols) { public void startDocument() throws XNIException { matcher.startDocumentFragment(symbols, null); } public void startElement(QName element, XMLAttrList attributes, int handle) throws XNIException { matcher.startElement(element, attributes, handle); } public void characters(char[] ch, int offset, int length) throws XNIException { matcher.characters(ch, offset, length); } public void endElement(QName element) throws XNIException { matcher.endElement(element); } public void endDocument() throws XNIException { matcher.endDocumentFragment(); } }; System.out.println("#### argv["+i+"]: \""+expr+"\" -> \""+xpath.toString()+'"'); final String uri = argv[++i]; System.out.println("#### argv["+i+"]: "+uri); parser.parse(uri); } } } // main(String[]) /***/} // class XPathMatcher
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?