elementschemepointer.java
来自「JAVA 所有包」· Java 代码 · 共 874 行 · 第 1/3 页
JAVA
874 行
/* * 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 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.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.XNIException;import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;/** * <p> * Implements the XPointerPart interface for element() scheme specific processing. * </p> * * @xerces.internal * * @version $Id: ElementSchemePointer.java,v 1.1.4.1 2005/09/08 05:25:43 sunithareddy Exp $ * */class ElementSchemePointer implements XPointerPart { // Fields // The Scheme Name i.e element private String fSchemeName; // The scheme Data private String fSchemeData; // The scheme Data & child sequence private String fShortHandPointerName; // Should we attempt to resolve the ChildSequence from the // current element position. If a ShortHand Pointer is present // attempt to resolve relative to the short hand pointer. private boolean fIsResolveElement = false; // Has the element been found private boolean fIsElementFound = false; // Was only an empty element found private boolean fWasOnlyEmptyElementFound = false; // If a shorthand pointer is present and resolved boolean fIsShortHand = false; // The depth at which the element was found int fFoundDepth = 0; // The XPointer element child sequence private int fChildSequence[]; // The current child position private int fCurrentChildPosition = 1; // The current child depth private int fCurrentChildDepth = 0; // The current element's child sequence private int fCurrentChildSequence[];; // Stores if the Fragment was resolved by the pointer private boolean fIsFragmentResolved = false; // Stores if the Fragment was resolved by the pointer private ShortHandPointer fShortHandPointer; // The XPointer Error reporter protected XMLErrorReporter fErrorReporter; // The XPointer Error Handler protected XMLErrorHandler fErrorHandler; // private SymbolTable fSymbolTable; // ************************************************************************ // Constructors // ************************************************************************ public ElementSchemePointer() { } public ElementSchemePointer(SymbolTable symbolTable) { fSymbolTable = symbolTable; } public ElementSchemePointer(SymbolTable symbolTable, XMLErrorReporter errorReporter) { fSymbolTable = symbolTable; fErrorReporter = errorReporter; } // ************************************************************************ // XPointerPart implementation // ************************************************************************ /** * Parses the XPointer expression and tokenizes it into Strings * delimited by whitespace. * * @see com.sun.org.apache.xerces.internal.xpointer.XPointerProcessor#parseXPointer(java.lang.String) */ public void parseXPointer(String xpointer) throws XNIException { // 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_ELEM_CHILD || token == Tokens.XPTRTOKEN_ELEM_NCNAME) { super.addToken(tokens, token); return; } reportError("InvalidElementSchemeToken", new Object[] { tokens .getTokenString(token) }); } }; // scan the element() XPointer expression int length = xpointer.length(); boolean success = scanner.scanExpr(fSymbolTable, tokens, xpointer, 0, length); if (!success) { reportError("InvalidElementSchemeXPointer", new Object[] { xpointer }); } // Initialize a temp arrays to the size of token count which should // be atleast twice the size of child sequence, to hold the ChildSequence. int tmpChildSequence[] = new int[tokens.getTokenCount() / 2 + 1]; // the element depth int i = 0; // Traverse the scanned tokens while (tokens.hasMore()) { int token = tokens.nextToken(); switch (token) { case Tokens.XPTRTOKEN_ELEM_NCNAME: { // Note: Only a single ShortHand pointer can be present // The shortHand name token = tokens.nextToken(); fShortHandPointerName = tokens.getTokenString(token); // Create a new ShortHandPointer fShortHandPointer = new ShortHandPointer(fSymbolTable); fShortHandPointer.setSchemeName(fShortHandPointerName); break; } case Tokens.XPTRTOKEN_ELEM_CHILD: { tmpChildSequence[i] = tokens.nextToken(); i++; break; } default: reportError("InvalidElementSchemeXPointer", new Object[] { xpointer }); } } // Initialize the arrays to the number of elements in the ChildSequence. fChildSequence = new int[i]; fCurrentChildSequence = new int[i]; System.arraycopy(tmpChildSequence, 0, fChildSequence, 0, i); } /** * Returns the scheme name i.e element * @see com.sun.org.apache.xerces.internal.xpointer.XPointerPart#getSchemeName() */ public String getSchemeName() { return fSchemeName; } /** * Returns the scheme data * * @see com.sun.org.apache.xerces.internal.xpointer.XPointerPart#getSchemeData() */ public String getSchemeData() { return fSchemeData; } /** * Sets the scheme name * * @see com.sun.org.apache.xerces.internal.xpointer.XPointerPart#setSchemeName(java.lang.String) */ public void setSchemeName(String schemeName) { fSchemeName = schemeName; } /** * Sets the scheme data * * @see com.sun.org.apache.xerces.internal.xpointer.XPointerPart#setSchemeData(java.lang.String) */ public void setSchemeData(String schemeData) { fSchemeData = schemeData; } /** * Responsible for resolving the element() scheme XPointer. If a ShortHand * Pointer is present and it is successfully resolved and if a child * sequence is present, the child sequence is resolved relative to it. * * @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 isShortHandPointerResolved = false; // if a ChildSequence exisits, resolve child elements // if an element name exists if (fShortHandPointerName != null) { // resolve ShortHand Pointer isShortHandPointerResolved = fShortHandPointer.resolveXPointer( element, attributes, augs, event); if (isShortHandPointerResolved) { fIsResolveElement = true; fIsShortHand = true; } else { fIsResolveElement = false; } } else { fIsResolveElement = true; } // Added here to skip the ShortHand pointer corresponding to // an element if one exisits and start searching from its child if (fChildSequence.length > 0) { fIsFragmentResolved = matchChildSequence(element, event); } else if (isShortHandPointerResolved && fChildSequence.length <= 0) { // if only a resolved shorthand pointer exists fIsFragmentResolved = isShortHandPointerResolved; } else { fIsFragmentResolved = false; } return fIsFragmentResolved; } /** * Matches the current element position in the document tree with the * element position specified in the element XPointer scheme. * * @param event * @return boolean - true if the current element position in the document * tree matches theelement position specified in the element XPointer * scheme. */ protected boolean matchChildSequence(QName element, int event) throws XNIException { // need to resize fCurrentChildSequence if (fCurrentChildDepth >= fCurrentChildSequence.length) { int tmpCurrentChildSequence[] = new int[fCurrentChildSequence.length]; System.arraycopy(fCurrentChildSequence, 0, tmpCurrentChildSequence,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?