?? abstractxmlparser.java
字號(hào):
package org.kxml.parser;import java.io.IOException;import java.util.*;import org.kxml.*;import org.kxml.io.*;/** An abstract base class for the XML and WBXML parsers. Of course, you can implement your own subclass with additional features, e.g. a validating parser. Attention: This class has been renamed from Parser for consistency with the writer classes in the org.kxml.io package. */public abstract class AbstractXmlParser { static final int FORCED_EVENTS = Xml.TEXT | Xml.END_DOCUMENT | Xml.START_TAG | Xml.END_TAG; protected boolean processNamespaces = true; /** The next event. May be null at startup. */ protected ParseEvent next; /** Ignores a complete element tree. The next event must be a start tag. */ public void ignoreTree () throws IOException { StartTag start = (StartTag) read (); while (true) { ParseEvent event = peek (); switch (event.getType ()) { case Xml.START_TAG: ignoreTree (); break; case Xml.END_TAG: case Xml.END_DOCUMENT: read (); return; default: read (); } } } /** Returns the current line number; -1 if unknown. Convenience method for peek ().getLineNumber (). */ public int getLineNumber () throws IOException { return peek ().getLineNumber (); } /** Concrete parser implementations need to implement this method at least. */ protected abstract ParseEvent readImpl () throws IOException; /** reads the next event available from the parser. If the end of the parsed stream has been reached, null is returned. */ public ParseEvent read () throws IOException { if (next == null) return readImpl (); ParseEvent result = next; next = null; return result; } /** Reads an event of the given type. If the type is START_TAG or END_TAG, namespace and name are tested, otherwise ignored. Throws a ParseException if the actual event does not match the given parameters. */ public ParseEvent read (int type, String namespace, String name) throws IOException { if (!peek (type, namespace, name)) throw new ParseException ("unexpected event: "+peek (), null, peek ().getLineNumber (), -1); return read (); } /** Convenience method for read (type, Xml.NO_NAMESPACE, name). */ public ParseEvent read (int type, String name) throws IOException { return read (type, Xml.NO_NAMESPACE, name); } /** Convenience Method for skip (Xml.COMMENT | Xml.DOCTYPE | Xml.PROCESSING_INSTRUCTION | Xml.WHITESPACE) */ public void skip () throws IOException { skip (Xml.COMMENT | Xml.DOCTYPE | Xml.PROCESSING_INSTRUCTION | Xml.WHITESPACE); } /** This method skips all subsequent events of the given type(s) */ public void skip (int types) throws IOException { while ((peek ().getType () & types) != 0) read (); } /** reads the next event available from the parser without consuming it */ public ParseEvent peek () throws IOException { if (next == null) next = readImpl (); return next; } /** peeks for the given type, namespace and name. Returns true if matched, false otherwise. If the type is not START_TAG or END_TAG, namespace and name are ignored. */ public boolean peek (int type, String namespace, String name) throws IOException { ParseEvent event = peek (); if (namespace == null) namespace = Xml.NO_NAMESPACE; if (event.getType () != type) return false; if (type == Xml.START_TAG || type == Xml.END_TAG) return event.getName ().equals (name) && event.getNamespace ().equals (namespace); return true; } /** Convenience method for peek (type, Xml.NO_NAMESPACE, name). */ public boolean peek (int type, String name) throws IOException { return peek (type, Xml.NO_NAMESPACE, name); } /** tells the parser if it shall resolve namespace prefixes to namespaces. Default is true */ public void setProcessNamespaces (boolean processNamespaces) { this.processNamespaces = processNamespaces; } /** Convenience method for reading text content. Reads text until an EndTag or EndDocument is reached. The EndTag is NOT consumed. The concatenated text String is returned. If the method reaches an StartTag, an Exception is thrown */ public String readText () throws IOException { StringBuffer buf = new StringBuffer (); while (true) { ParseEvent event = peek (); switch (event.getType ()) { case Xml.START_TAG: throw new RuntimeException ("Illegal StartTag in readText: "+event); case Xml.WHITESPACE: case Xml.TEXT: read (); buf.append (((TextEvent) event).getText ()); break; case Xml.END_TAG: case Xml.END_DOCUMENT: return buf.toString (); } } }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -