📄 wbxmlparser.java
字号:
/* Copyright (c) 2002,2003,2004 Stefan Haustein, Oberhausen, Rhld., Germany
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE. */
// Contributors: Bjorn Aadland, Chris Bartley, Nicola Fankhauser,
// Victor Havin, Christian Kurzke, Bogdan Onoiu,
// Elias Ross, Jain Sanjay, David Santoro.
package org.kxml2.wap;
import java.io.*;
import java.util.Vector;
import java.util.Hashtable;
import org.xmlpull.v1.*;
public class WbxmlParser implements XmlPullParser {
static final String HEX_DIGITS = "0123456789abcdef";
/** Parser event type for Wbxml-specific events. The Wbxml event code can be
* accessed with getWapCode() */
public static final int WAP_EXTENSION = 64;
static final private String UNEXPECTED_EOF =
"Unexpected EOF";
static final private String ILLEGAL_TYPE =
"Wrong event type";
private InputStream in;
private int TAG_TABLE = 0;
private int ATTR_START_TABLE = 1;
private int ATTR_VALUE_TABLE = 2;
private String[] attrStartTable;
private String[] attrValueTable;
private String[] tagTable;
private byte[] stringTable;
private Hashtable cacheStringTable = null;
private boolean processNsp;
private int depth;
private String[] elementStack = new String[16];
private String[] nspStack = new String[8];
private int[] nspCounts = new int[4];
private int attributeCount;
private String[] attributes = new String[16];
private int nextId = -2;
private Vector tables = new Vector();
private int version;
private int publicIdentifierId;
// StartTag current;
// ParseEvent next;
private String prefix;
private String namespace;
private String name;
private String text;
private Object wapExtensionData;
private int wapCode;
private int type;
private boolean degenerated;
private boolean isWhitespace;
private String encoding;
public boolean getFeature(String feature) {
if (XmlPullParser
.FEATURE_PROCESS_NAMESPACES
.equals(feature))
return processNsp;
else
return false;
}
public String getInputEncoding() {
return encoding;
}
public void defineEntityReplacementText(
String entity,
String value)
throws XmlPullParserException {
// just ignore, has no effect
}
public Object getProperty(String property) {
return null;
}
public int getNamespaceCount(int depth) {
if (depth > this.depth)
throw new IndexOutOfBoundsException();
return nspCounts[depth];
}
public String getNamespacePrefix(int pos) {
return nspStack[pos << 1];
}
public String getNamespaceUri(int pos) {
return nspStack[(pos << 1) + 1];
}
public String getNamespace(String prefix) {
if ("xml".equals(prefix))
return "http://www.w3.org/XML/1998/namespace";
if ("xmlns".equals(prefix))
return "http://www.w3.org/2000/xmlns/";
for (int i = (getNamespaceCount(depth) << 1) - 2;
i >= 0;
i -= 2) {
if (prefix == null) {
if (nspStack[i] == null)
return nspStack[i + 1];
}
else if (prefix.equals(nspStack[i]))
return nspStack[i + 1];
}
return null;
}
public int getDepth() {
return depth;
}
public String getPositionDescription() {
StringBuffer buf =
new StringBuffer(
type < TYPES.length ? TYPES[type] : "unknown");
buf.append(' ');
if (type == START_TAG || type == END_TAG) {
if (degenerated)
buf.append("(empty) ");
buf.append('<');
if (type == END_TAG)
buf.append('/');
if (prefix != null)
buf.append("{" + namespace + "}" + prefix + ":");
buf.append(name);
int cnt = attributeCount << 2;
for (int i = 0; i < cnt; i += 4) {
buf.append(' ');
if (attributes[i + 1] != null)
buf.append(
"{"
+ attributes[i]
+ "}"
+ attributes[i
+ 1]
+ ":");
buf.append(
attributes[i
+ 2]
+ "='"
+ attributes[i
+ 3]
+ "'");
}
buf.append('>');
}
else if (type == IGNORABLE_WHITESPACE);
else if (type != TEXT)
buf.append(getText());
else if (isWhitespace)
buf.append("(whitespace)");
else {
String text = getText();
if (text.length() > 16)
text = text.substring(0, 16) + "...";
buf.append(text);
}
return buf.toString();
}
public int getLineNumber() {
return -1;
}
public int getColumnNumber() {
return -1;
}
public boolean isWhitespace()
throws XmlPullParserException {
if (type != TEXT
&& type != IGNORABLE_WHITESPACE
&& type != CDSECT)
exception(ILLEGAL_TYPE);
return isWhitespace;
}
public String getText() {
return text;
}
public char[] getTextCharacters(int[] poslen) {
if (type >= TEXT) {
poslen[0] = 0;
poslen[1] = text.length();
char[] buf = new char[text.length()];
text.getChars(0, text.length(), buf, 0);
return buf;
}
poslen[0] = -1;
poslen[1] = -1;
return null;
}
public String getNamespace() {
return namespace;
}
public String getName() {
return name;
}
public String getPrefix() {
return prefix;
}
public boolean isEmptyElementTag()
throws XmlPullParserException {
if (type != START_TAG)
exception(ILLEGAL_TYPE);
return degenerated;
}
public int getAttributeCount() {
return attributeCount;
}
public String getAttributeType(int index) {
return "CDATA";
}
public boolean isAttributeDefault(int index) {
return false;
}
public String getAttributeNamespace(int index) {
if (index >= attributeCount)
throw new IndexOutOfBoundsException();
return attributes[index << 2];
}
public String getAttributeName(int index) {
if (index >= attributeCount)
throw new IndexOutOfBoundsException();
return attributes[(index << 2) + 2];
}
public String getAttributePrefix(int index) {
if (index >= attributeCount)
throw new IndexOutOfBoundsException();
return attributes[(index << 2) + 1];
}
public String getAttributeValue(int index) {
if (index >= attributeCount)
throw new IndexOutOfBoundsException();
return attributes[(index << 2) + 3];
}
public String getAttributeValue(
String namespace,
String name) {
for (int i = (attributeCount << 2) - 4;
i >= 0;
i -= 4) {
if (attributes[i + 2].equals(name)
&& (namespace == null
|| attributes[i].equals(namespace)))
return attributes[i + 3];
}
return null;
}
public int getEventType() throws XmlPullParserException {
return type;
}
// TODO: Reuse resolveWapExtension here? Raw Wap extensions would still be accessible
// via nextToken(); ....?
public int next() throws XmlPullParserException, IOException {
isWhitespace = true;
int minType = 9999;
while (true) {
String save = text;
nextImpl();
if (type < minType)
minType = type;
if (minType > CDSECT) continue; // no "real" event so far
if (minType >= TEXT) { // text, see if accumulate
if (save != null) text = text == null ? save : save + text;
switch(peekId()) {
case Wbxml.ENTITY:
case Wbxml.STR_I:
case Wbxml.STR_T:
case Wbxml.LITERAL:
case Wbxml.LITERAL_C:
case Wbxml.LITERAL_A:
case Wbxml.LITERAL_AC: continue;
}
}
break;
}
type = minType;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -