xpointerhandler.java
来自「JAVA 所有包」· Java 代码 · 共 1,245 行 · 第 1/4 页
JAVA
1,245 行
/* * Copyright 2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xerces.internal.xpointer;import java.util.Hashtable;import java.util.Vector;import com.sun.org.apache.xerces.internal.impl.Constants;import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;import com.sun.org.apache.xerces.internal.util.SymbolTable;import com.sun.org.apache.xerces.internal.util.XMLChar;import com.sun.org.apache.xerces.internal.util.XMLSymbols;import com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler;import com.sun.org.apache.xerces.internal.xinclude.XIncludeNamespaceSupport;import com.sun.org.apache.xerces.internal.xni.Augmentations;import com.sun.org.apache.xerces.internal.xni.QName;import com.sun.org.apache.xerces.internal.xni.XMLAttributes;import com.sun.org.apache.xerces.internal.xni.XMLString;import com.sun.org.apache.xerces.internal.xni.XNIException;import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;/** * <p> * This is a pipeline component which extends the XIncludeHandler to perform * XPointer specific processing specified in the W3C XPointerFramework and * element() Scheme Recommendations. * </p> * * <p> * This component analyzes each event in the pipeline, looking for an element * that matches a PointerPart in the parent XInclude element's xpointer attribute * value. If the match succeeds, all children are passed by this component. * </p> * * <p> * See the <a href="http://www.w3.org/TR/xptr-framework//">XPointer Framework Recommendation</a> for * more information on the XPointer Framework and ShortHand Pointers. * See the <a href="http://www.w3.org/TR/xptr-element/">XPointer element() Scheme Recommendation</a> for * more information on the XPointer element() Scheme. * </p> * * @xerces.internal * * @version $Id: XPointerHandler.java,v 1.1.4.1 2005/09/08 05:25:44 sunithareddy Exp $ */public final class XPointerHandler extends XIncludeHandler implements XPointerProcessor { // Fields // A Vector of XPointerParts protected Vector fXPointerParts = null; // The current XPointerPart protected XPointerPart fXPointerPart = null; // Has the fXPointerPart resolved successfully protected boolean fFoundMatchingPtrPart = false; // The XPointer Error reporter protected XMLErrorReporter fXPointerErrorReporter; // The XPointer Error Handler protected XMLErrorHandler fErrorHandler; // XPointerFramework symbol table protected SymbolTable fSymbolTable = null; // Supported schemes private final String ELEMENT_SCHEME_NAME = "element"; // Has the XPointer resolved the subresource protected boolean fIsXPointerResolved = false; // Fixup xml:base and xml:lang attributes protected boolean fFixupBase = false; protected boolean fFixupLang = false; // ************************************************************************ // Constructors // ************************************************************************ /** * */ public XPointerHandler() { super(); fXPointerParts = new Vector(); fSymbolTable = new SymbolTable(); } public XPointerHandler(SymbolTable symbolTable, XMLErrorHandler errorHandler, XMLErrorReporter errorReporter) { super(); fXPointerParts = new Vector(); fSymbolTable = symbolTable; fErrorHandler = errorHandler; fXPointerErrorReporter = errorReporter; //fErrorReporter = errorReporter; // The XInclude ErrorReporter } // ************************************************************************ // Implementation of the XPointerProcessor interface. // ************************************************************************ /** * Parses the XPointer framework expression and delegates scheme specific parsing. * * @see com.sun.org.apache.xerces.internal.xpointer.XPointerProcessor#parseXPointer(java.lang.String) */ public void parseXPointer(String xpointer) throws XNIException { // Initialize init(); // tokens final Tokens tokens = new Tokens(fSymbolTable); // scanner Scanner scanner = new Scanner(fSymbolTable) { protected void addToken(Tokens tokens, int token) throws XNIException { if (token == Tokens.XPTRTOKEN_OPEN_PAREN || token == Tokens.XPTRTOKEN_CLOSE_PAREN || token == Tokens.XPTRTOKEN_SCHEMENAME || token == Tokens.XPTRTOKEN_SCHEMEDATA || token == Tokens.XPTRTOKEN_SHORTHAND) { super.addToken(tokens, token); return; } reportError("InvalidXPointerToken", new Object[] { tokens .getTokenString(token) }); } }; // scan the XPointer expression int length = xpointer.length(); boolean success = scanner.scanExpr(fSymbolTable, tokens, xpointer, 0, length); if (!success) reportError("InvalidXPointerExpression", new Object[] { xpointer }); while (tokens.hasMore()) { int token = tokens.nextToken(); switch (token) { case Tokens.XPTRTOKEN_SHORTHAND: { // The shortHand name token = tokens.nextToken(); String shortHandPointerName = tokens.getTokenString(token); if (shortHandPointerName == null) { reportError("InvalidXPointerExpression", new Object[] { xpointer }); } XPointerPart shortHandPointer = new ShortHandPointer( fSymbolTable); shortHandPointer.setSchemeName(shortHandPointerName); fXPointerParts.add(shortHandPointer); break; } case Tokens.XPTRTOKEN_SCHEMENAME: { // Retreive the local name and prefix to form the scheme name token = tokens.nextToken(); String prefix = tokens.getTokenString(token); token = tokens.nextToken(); String localName = tokens.getTokenString(token); String schemeName = prefix + localName; // The next character should be an open parenthesis int openParenCount = 0; int closeParenCount = 0; token = tokens.nextToken(); String openParen = tokens.getTokenString(token); if (openParen != "XPTRTOKEN_OPEN_PAREN") { // can not have more than one ShortHand Pointer if (token == Tokens.XPTRTOKEN_SHORTHAND) { reportError("MultipleShortHandPointers", new Object[] { xpointer }); } else { reportError("InvalidXPointerExpression", new Object[] { xpointer }); } } openParenCount++; // followed by zero or more ( and the schemeData String schemeData = null; while (tokens.hasMore()) { token = tokens.nextToken(); schemeData = tokens.getTokenString(token); if (schemeData != "XPTRTOKEN_OPEN_PAREN") { break; } openParenCount++; } token = tokens.nextToken(); schemeData = tokens.getTokenString(token); // followed by the same number of ) token = tokens.nextToken(); String closeParen = tokens.getTokenString(token); if (closeParen != "XPTRTOKEN_CLOSE_PAREN") { reportError("SchemeDataNotFollowedByCloseParenthesis", new Object[] { xpointer }); } closeParenCount++; while (tokens.hasMore()) { if (tokens.getTokenString(tokens.peekToken()) != "XPTRTOKEN_OPEN_PAREN") { break; } closeParenCount++; } // check if the number of open parenthesis are equal to the number of close parenthesis if (openParenCount != closeParenCount) { reportError("UnbalancedParenthesisInXPointerExpression", new Object[] { xpointer, new Integer(openParenCount), new Integer(closeParenCount) }); } // Perform scheme specific parsing of the pointer part if (schemeName.equals(ELEMENT_SCHEME_NAME)) { XPointerPart elementSchemePointer = new ElementSchemePointer( fSymbolTable, fErrorReporter); elementSchemePointer.setSchemeName(schemeName); elementSchemePointer.setSchemeData(schemeData); // If an exception occurs while parsing the element() scheme expression // ignore it and move on to the next pointer part try { elementSchemePointer.parseXPointer(schemeData); fXPointerParts.add(elementSchemePointer); } catch (XNIException e) { // Re-throw the XPointer element() scheme syntax error. throw new XNIException (e); } } else { // ???? reportWarning("SchemeUnsupported", new Object[] { schemeName }); } break; } default: reportError("InvalidXPointerExpression", new Object[] { xpointer }); } } } /** * * @see com.sun.org.apache.xerces.internal.xpointer.XPointerProcessor#resolveXPointer(com.sun.org.apache.xerces.internal.xni.QName, com.sun.org.apache.xerces.internal.xni.XMLAttributes, com.sun.org.apache.xerces.internal.xni.Augmentations, int event) */ public boolean resolveXPointer(QName element, XMLAttributes attributes, Augmentations augs, int event) throws XNIException { boolean resolved = false; // The result of the first pointer part whose evaluation identifies // one or more subresources is reported by the XPointer processor as the // result of the pointer as a whole, and evaluation stops. // In our implementation, typically the first xpointer scheme that // matches an element is the document is considered. // If the pointer part resolved then use it, else search for the fragment // using next pointer part from lef-right. if (!fFoundMatchingPtrPart) { // for each element, attempt to resolve it against each pointer part // in the XPointer expression until a matching element is found. for (int i = 0; i < fXPointerParts.size(); i++) { fXPointerPart = (XPointerPart) fXPointerParts.get(i); if (fXPointerPart.resolveXPointer(element, attributes, augs, event)) { fFoundMatchingPtrPart = true; resolved = true; } } } else { if (fXPointerPart.resolveXPointer(element, attributes, augs, event)) { resolved = true; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?