📄 xmlparser.java
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither the name of Rice University (RICE) nor the names of itscontributors may be used to endorse or promote products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.p2p.util;import org.xmlpull.v1.*;import java.io.*;import java.util.*;/** * This class is a memory-efficient implementation of most of the XML pull * parsing API. * * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $ * @author jeffh */public class XMLParser implements XmlPullParser { /** * The internal reader used to read data */ protected Reader reader; /** * The internal buffer used to process data */ protected char[] buffer; /** * Internal pointers into the buffer */ protected int bufferPosition; /** * DESCRIBE THE FIELD */ protected int bufferLimit; /** * The StringCache used to reduce the memory requirements */ protected StringCache cache; /** * The internal stack of tags which have been read */ protected Stack tags; /** * If the tag parsed was a start/end, the name of the tag */ protected String name; /** * If the tag parsed was text, the text */ protected String text; /** * If the tag parsed was a start tag, the list of attribute-> value pairs */ protected String[] attributeKeys = new String[MAX_ATTRIBUTES]; /** * DESCRIBE THE FIELD */ protected String[] attributeValues = new String[MAX_ATTRIBUTES]; /** * DESCRIBE THE FIELD */ protected int numAttributes; /** * Whether or not we are currently in a tag... */ protected boolean inTag; /** * Internal variable which keeps track of the current mark */ protected int mark; /** * DESCRIBE THE FIELD */ protected CharArrayBuffer marked; /** * The size of the internal buffer to allocate */ public final static int BUFFER_SIZE = 32000; /** * DESCRIBE THE FIELD */ public final static int MAX_ATTRIBUTES = 100; /** * DESCRIBE THE FIELD */ public final static char[] QUOTE = new char[]{'\'', '"'}; /** * DESCRIBE THE FIELD */ public final static char[] TAG_END = new char[]{'>', '/', '?'}; /** * DESCRIBE THE FIELD */ public final static char[] WHITESPACE = new char[]{' ', '\t', '\n', '\r'}; /** * DESCRIBE THE FIELD */ public final static char[] WHITESPACE_OR_TAG_END = new char[]{' ', '\t', '\n', '\r', '>', '/', '?'}; /** * DESCRIBE THE FIELD */ public final static char[] WHITESPACE_OR_EQUALS = new char[]{' ', '\t', '\n', '\r', '='}; /** * DESCRIBE THE FIELD */ public final static char[] SINGLE = new char[1]; /** * DESCRIBE THE FIELD */ public final static String[][] ENTITIES = new String[][]{{"'", "'"}, {""", "\""}, {"<", "<"}, {">", ">"}, {" ", String.valueOf((char) 13)}, {" ", String.valueOf((char) 10)}, {"	", String.valueOf((char) 9)}, {"&", "&"}}; /** * Constructor */ public XMLParser() { this.buffer = new char[BUFFER_SIZE]; this.bufferPosition = 0; this.bufferLimit = 0; this.mark = -1; this.tags = new Stack(); this.cache = new StringCache(); this.inTag = false; this.numAttributes = 0; } /** * Returns the text content of the current event as String. The value returned * depends on current event type, for example for TEXT event it is element * content (this is typical case when next() is used). See description of * nextToken() for detailed description of possible returned values for * different types of events. <p> * * <strong>NOTE:</strong> in case of ENTITY_REF, this method returns the * entity replacement text (or null if not available). This is the only case * where getText() and getTextCharacters() return different values. * * @return The Text value * @see #getEventType * @see #next * @see #nextToken */ public String getText() { return this.text; } /** * For START_TAG or END_TAG events, the (local) name of the current element is * returned when namespaces are enabled. When namespace processing is * disabled, the raw name is returned. For ENTITY_REF events, the entity name * is returned. If the current event is not START_TAG, END_TAG, or ENTITY_REF, * null is returned. <p> * * <b>Please note:</b> To reconstruct the raw element name when namespaces are * enabled and the prefix is not null, you will need to add the prefix and a * colon to localName.. * * @return The Name value */ public String getName() { return this.name; } /** * Returns the attributes value identified by namespace URI and namespace * localName. If namespaces are disabled namespace must be null. If current * event type is not START_TAG then IndexOutOfBoundsException will be thrown. * <p> * * <strong>NOTE:</strong> attribute value must be normalized (including entity * replacement text if PROCESS_DOCDECL is false) as described in <a * href="http://www.w3.org/TR/REC-xml#AVNormalize">XML 1.0 section 3.3.3 * Attribute-Value Normalization</a> * * @param namespace Namespace of the attribute if namespaces are enabled * otherwise must be null * @param name If namespaces enabled local name of attribute otherwise just * attribute name * @return value of attribute or null if attribute with given name does not * exist * @see #defineEntityReplacementText */ public String getAttributeValue(String namespace, String name) { for (int i = 0; i < numAttributes; i++) { if (attributeKeys[i].equals(name)) { return attributeValues[i]; } } return null; } /** * Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.) * * @return The EventType value * @exception XmlPullParserException DESCRIBE THE EXCEPTION * @see #next() * @see #nextToken() */ public int getEventType() throws XmlPullParserException { return 0; } /** * Checks whether the current TEXT event contains only whitespace characters. * For IGNORABLE_WHITESPACE, this is always true. For TEXT and CDSECT, false * is returned when the current event text contains at least one non-white * space character. For any other event type an exception is thrown. <p> * * <b>Please note:</b> non-validating parsers are not able to distinguish * whitespace and ignorable whitespace, except from whitespace outside the * root element. Ignorable whitespace is reported as separate event, which is * exposed via nextToken only. * * @return The Whitespace value * @exception XmlPullParserException DESCRIBE THE EXCEPTION */ public boolean isWhitespace() throws XmlPullParserException { return isWhitespace(text); } /** * Internal method which checks for existence * * @param text DESCRIBE THE PARAMETER * @return The Whitespace value */ public boolean isWhitespace(String text) { for (int i = 0; i < text.length(); i++) { if (!contains(WHITESPACE, text.charAt(i))) { return false; } } return true; } /** * Gets the Feature attribute of the XMLParser object * * @param name DESCRIBE THE PARAMETER * @return The Feature value */ public boolean getFeature(String name) { throw new UnsupportedOperationException(); } /** * Gets the Property attribute of the XMLParser object * * @param name DESCRIBE THE PARAMETER * @return The Property value */ public Object getProperty(String name) { throw new UnsupportedOperationException(); } /** * Gets the InputEncoding attribute of the XMLParser object * * @return The InputEncoding value */ public String getInputEncoding() { throw new UnsupportedOperationException(); } /** * Gets the NamespaceCount attribute of the XMLParser object * * @param depth DESCRIBE THE PARAMETER * @return The NamespaceCount value * @exception XmlPullParserException DESCRIBE THE EXCEPTION */ public int getNamespaceCount(int depth) throws XmlPullParserException { throw new UnsupportedOperationException(); } /** * Gets the NamespacePrefix attribute of the XMLParser object * * @param pos DESCRIBE THE PARAMETER * @return The NamespacePrefix value * @exception XmlPullParserException DESCRIBE THE EXCEPTION */ public String getNamespacePrefix(int pos) throws XmlPullParserException { throw new UnsupportedOperationException(); } /** * Gets the NamespaceUri attribute of the XMLParser object * * @param pos DESCRIBE THE PARAMETER * @return The NamespaceUri value * @exception XmlPullParserException DESCRIBE THE EXCEPTION */ public String getNamespaceUri(int pos) throws XmlPullParserException { throw new UnsupportedOperationException(); } /** * Gets the Namespace attribute of the XMLParser object * * @param prefix DESCRIBE THE PARAMETER * @return The Namespace value */ public String getNamespace(String prefix) { throw new UnsupportedOperationException(); } /** * Gets the Depth attribute of the XMLParser object * * @return The Depth value */ public int getDepth() { throw new UnsupportedOperationException(); } /** * Gets the PositionDescription attribute of the XMLParser object * * @return The PositionDescription value */ public String getPositionDescription() { throw new UnsupportedOperationException(); } /** * Gets the LineNumber attribute of the XMLParser object * * @return The LineNumber value */ public int getLineNumber() { throw new UnsupportedOperationException(); } /** * Gets the ColumnNumber attribute of the XMLParser object * * @return The ColumnNumber value */ public int getColumnNumber() { throw new UnsupportedOperationException(); } /** * Gets the TextCharacters attribute of the XMLParser object * * @param holderForStartAndLength DESCRIBE THE PARAMETER * @return The TextCharacters value */ public char[] getTextCharacters(int[] holderForStartAndLength) { throw new UnsupportedOperationException(); } /** * Gets the Namespace attribute of the XMLParser object * * @return The Namespace value */ public String getNamespace() { throw new UnsupportedOperationException(); } /** * Gets the Prefix attribute of the XMLParser object * * @return The Prefix value */ public String getPrefix() { throw new UnsupportedOperationException(); } /** * Gets the EmptyElementTag attribute of the XMLParser object * * @return The EmptyElementTag value * @exception XmlPullParserException DESCRIBE THE EXCEPTION */ public boolean isEmptyElementTag() throws XmlPullParserException { throw new UnsupportedOperationException(); } /** * Gets the AttributeNamespace attribute of the XMLParser object * * @param index DESCRIBE THE PARAMETER * @return The AttributeNamespace value */ public String getAttributeNamespace(int index) { throw new UnsupportedOperationException(); } /** * Gets the AttributePrefix attribute of the XMLParser object * * @param index DESCRIBE THE PARAMETER * @return The AttributePrefix value */ public String getAttributePrefix(int index) { throw new UnsupportedOperationException(); } /** * Gets the AttributeType attribute of the XMLParser object * * @param index DESCRIBE THE PARAMETER * @return The AttributeType value */ public String getAttributeType(int index) { throw new UnsupportedOperationException(); } /** * Gets the AttributeDefault attribute of the XMLParser object * * @param index DESCRIBE THE PARAMETER * @return The AttributeDefault value */ public boolean isAttributeDefault(int index) { throw new UnsupportedOperationException(); } /** * Gets the AttributeCount attribute of the XMLParser object * * @return The AttributeCount value */ public int getAttributeCount() { throw new UnsupportedOperationException(); } /** * Gets the AttributeName attribute of the XMLParser object * * @param index DESCRIBE THE PARAMETER * @return The AttributeName value */ public String getAttributeName(int index) { throw new UnsupportedOperationException(); } /** * Gets the AttributeValue attribute of the XMLParser object * * @param index DESCRIBE THE PARAMETER * @return The AttributeValue value */ public String getAttributeValue(int index) { throw new UnsupportedOperationException(); } /** * Set the input source for parser to the given reader and resets the parser. * The event type is set to the initial value START_DOCUMENT. Setting the * reader to null will just stop parsing and reset parser state, allowing the * parser to free internal resources such as parsing buffers. * * @param in The new Input value * @exception XmlPullParserException DESCRIBE THE EXCEPTION */ public void setInput(Reader in) throws XmlPullParserException { this.reader = in; } /** * ----- UNSUPPORTED METHODS ----- * * @param name The new Feature value * @param state The new Feature value * @exception XmlPullParserException DESCRIBE THE EXCEPTION */ public void setFeature(String name, boolean state) throws XmlPullParserException { throw new UnsupportedOperationException(); } /** * Sets the Property attribute of the XMLParser object * * @param name The new Property value * @param value The new Property value * @exception XmlPullParserException DESCRIBE THE EXCEPTION */ public void setProperty(String name, Object value) throws XmlPullParserException { throw new UnsupportedOperationException(); } /** * Sets the Input attribute of the XMLParser object * * @param inputStream The new Input value * @param inputEncoding The new Input value * @exception XmlPullParserException DESCRIBE THE EXCEPTION */ public void setInput(InputStream inputStream, String inputEncoding) throws XmlPullParserException { throw new UnsupportedOperationException(); } /** * Get next parsing event - element content wil be coalesced and only one TEXT * event must be returned for whole element content (comments and processing * instructions will be ignored and emtity references must be expanded or * exception mus be thrown if entity reerence can not be exapnded). If element * content is empty (content is "") then no TEXT event will be reported. <p> * * <b>NOTE:</b> empty element (such as <tag/>) will be reported with two * separate events: START_TAG, END_TAG - it must be so to preserve parsing * equivalency of empty element to <tag></tag>. (see isEmptyElementTag * ()) * * @return DESCRIBE THE RETURN VALUE * @exception XmlPullParserException DESCRIBE THE EXCEPTION * @exception IOException DESCRIBE THE EXCEPTION * @see #isEmptyElementTag * @see #START_TAG * @see #TEXT * @see #END_TAG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -