wbxmlparser.java

来自「Mofire的JAR压缩包」· Java 代码 · 共 330 行

JAVA
330
字号
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   WbxmlParser.java

package org.kxml.wap;

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import org.kxml.Attribute;
import org.kxml.io.ParseException;
import org.kxml.parser.*;

// Referenced classes of package org.kxml.wap:
//			WapExtensionEvent

public class WbxmlParser extends AbstractXmlParser
{

	InputStream in;
	String attrStartTable[];
	String attrValueTable[];
	String tagTable[];
	String stringTable;
	int version;
	int publicIdentifierId;
	int charSet;
	StartTag current;
	ParseEvent next;
	boolean whitespace;

	public WbxmlParser(InputStream in)
		throws IOException
	{
		this.in = in;
		version = readByte();
		publicIdentifierId = readInt();
		if (publicIdentifierId == 0)
			readInt();
		charSet = readInt();
		int strTabSize = readInt();
		StringBuffer buf = new StringBuffer(strTabSize);
		for (int i = 0; i < strTabSize; i++)
			buf.append((char)readByte());

		stringTable = buf.toString();
	}

	public ParseEvent peek()
		throws IOException
	{
		if (next != null)
			return next;
		if (current != null && current.getDegenerated())
		{
			next = new Tag(16, current, current.getNamespace(), current.getName());
			current = current.getParent();
			return next;
		}
		ParseEvent result = null;
		do
		{
			int id = in.read();
			switch (id)
			{
			case -1: 
				if (current != null)
					throw new RuntimeException("unclosed elements: " + current);
				next = new ParseEvent(8, null);
				break;

			case 0: // '\0'
				if (readByte() != 0)
					throw new IOException("Curr. only CP0 supported");
				break;

			case 1: // '\001'
				next = new Tag(16, current, current.getNamespace(), current.getName());
				current = current.getParent();
				break;

			case 2: // '\002'
				next = new ParseEvent(128, (char)readInt());
				break;

			case 3: // '\003'
				String s = readStrI();
				next = new ParseEvent(whitespace ? 256 : 128, s);
				break;

			case 64: // '@'
			case 65: // 'A'
			case 66: // 'B'
			case 128: 
			case 129: 
			case 130: 
			case 192: 
			case 193: 
			case 194: 
			case 195: 
				next = parseWapExtension(id);
				break;

			case 67: // 'C'
				throw new RuntimeException("PI curr. not supp.");

			case 131: 
				int pos = readInt();
				int end = stringTable.indexOf('\0', pos);
				next = new ParseEvent(128, stringTable.substring(pos, end));
				break;

			default:
				next = parseElement(id);
				break;
			}
		} while (next == null);
		return next;
	}

	public ParseEvent read()
		throws IOException
	{
		if (next == null)
			peek();
		ParseEvent result = next;
		next = null;
		return result;
	}

	public ParseEvent parseWapExtension(int id)
		throws IOException
	{
		switch (id)
		{
		case 64: // '@'
		case 65: // 'A'
		case 66: // 'B'
			return new WapExtensionEvent(id, readStrI());

		case 128: 
		case 129: 
		case 130: 
			return new WapExtensionEvent(id, new Integer(readInt()));

		case 192: 
		case 193: 
		case 194: 
			return new WapExtensionEvent(id, null);

		case 195: 
			int len = readInt();
			byte buf[] = new byte[len];
			for (int i = 0; i < len; i++)
				buf[i] = (byte)readByte();

			return new WapExtensionEvent(id, buf);
		}
		throw new IOException("illegal id!");
	}

	public Vector readAttr()
		throws IOException
	{
		Vector result = new Vector();
		for (int id = readByte(); id != 1;)
		{
			String name = resolveId(attrStartTable, id);
			int cut = name.indexOf('=');
			StringBuffer value;
			if (cut == -1)
			{
				value = new StringBuffer();
			} else
			{
				value = new StringBuffer(name.substring(cut + 1));
				name = name.substring(0, cut);
			}
			for (id = readByte(); id > 128 || id == 2 || id == 3 || id == 131 || id >= 64 && id <= 66 || id >= 128 && id <= 130; id = readByte())
				switch (id)
				{
				case 2: // '\002'
					value.append((char)readInt());
					break;

				case 3: // '\003'
					value.append(readStrI());
					break;

				case 64: // '@'
				case 65: // 'A'
				case 66: // 'B'
				case 128: 
				case 129: 
				case 130: 
				case 192: 
				case 193: 
				case 194: 
				case 195: 
					ParseEvent e = parseWapExtension(id);
					if (e.getType() == 128 || e.getType() == 256)
						throw new RuntimeException("parse WapExtension must return Text Event in order to work inside Attributes!");
					value.append(e.getText());
					// fall through

				case 131: 
					value.append(readStrT());
					break;

				default:
					value.append(resolveId(attrValueTable, id));
					break;
				}

			result.addElement(new Attribute(null, name, value.toString()));
		}

		return result;
	}

	String resolveId(String tab[], int id)
		throws IOException
	{
		int idx = (id & 0x7f) - 5;
		if (idx == -1)
			return readStrT();
		if (idx < 0 || tab == null || idx >= tab.length || tab[idx] == null)
			throw new IOException("id " + id + " undef.");
		else
			return tab[idx];
	}

	StartTag parseElement(int id)
		throws IOException
	{
		String tag = resolveId(tagTable, id & 0x3f);
		try
		{
			current = new StartTag(current, null, tag, (id & 0x80) == 0 ? null : readAttr(), (id & 0x40) == 0, super.processNamespaces);
		}
		catch (Exception e)
		{
			throw new ParseException(null, e, -1, -1);
		}
		return current;
	}

	int readByte()
		throws IOException
	{
		int i = in.read();
		if (i == -1)
			throw new IOException("Unexpected EOF");
		else
			return i;
	}

	int readInt()
		throws IOException
	{
		int result = 0;
		int i;
		do
		{
			i = readByte();
			result = result << 7 | i & 0x7f;
		} while ((i & 0x80) != 0);
		return result;
	}

	String readStrI()
		throws IOException
	{
		StringBuffer buf = new StringBuffer();
		boolean wsp = true;
		do
		{
			int i = in.read();
			if (i == -1)
				throw new IOException("Unexpected EOF");
			if (i != 0)
			{
				if (i > 32)
					wsp = false;
				buf.append((char)i);
			} else
			{
				whitespace = wsp;
				return buf.toString();
			}
		} while (true);
	}

	String readStrT()
		throws IOException
	{
		int pos = readInt();
		int end = stringTable.indexOf('\0', pos);
		return stringTable.substring(pos, end);
	}

	public void setTagTable(int page, String tagTable[])
	{
		this.tagTable = tagTable;
		if (page != 0)
			throw new RuntimeException("code pages curr. not supp.");
		else
			return;
	}

	public void setAttrStartTable(int page, String attrStartTable[])
	{
		this.attrStartTable = attrStartTable;
		if (page != 0)
			throw new RuntimeException("code pages curr. not supp.");
		else
			return;
	}

	public void setAttrValueTable(int page, String attrStartTable[])
	{
		attrValueTable = attrStartTable;
		if (page != 0)
			throw new RuntimeException("code pages curr. not supp.");
		else
			return;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?