📄 wbxmlwriter.java
字号:
package org.kxml.wap;import java.io.*;import java.util.*;import org.kxml.*;import org.kxml.io.*;//import com.sun.xml.parser.Parser;/** a class for converting ("binary encoding") XML to WBXML. * Todo: * <ul> * <li>Add support for processing instructions * <li>Add support for tag and attribute tables * <li>Add support for WBXML extensions * </ul> */public class WbxmlWriter extends AbstractXmlWriter { Hashtable stringTable = new Hashtable (); OutputStream out; ByteArrayOutputStream buf = new ByteArrayOutputStream (); ByteArrayOutputStream stringTableBuf = new ByteArrayOutputStream (); String pending; Vector attributes = new Vector (); public WbxmlWriter (OutputStream out) throws IOException { this.out = out; buf = new ByteArrayOutputStream (); stringTableBuf = new ByteArrayOutputStream (); // ok, write header out.write (0x01); // version out.write (0x01); // unknown or missing public identifier out.write (0x04); // iso-8859-1 } /** ATTENTION: flush cannot work since Wbxml documents cannot need buffering. Thus, this call does nothing. */ public void flush () { } public void close () throws IOException { writeInt (out, stringTableBuf.size ()); // write StringTable out.write (stringTableBuf.toByteArray ()); // write buf out.write (buf.toByteArray ()); // ready! out.flush (); } public void checkPending (boolean degenerated) throws IOException { if (pending == null) return; int len = attributes.size (); buf.write (len == 0 ? (degenerated ? Wbxml.LITERAL : Wbxml.LITERAL_C) : (degenerated ? Wbxml.LITERAL_A : Wbxml.LITERAL_AC)); writeStrT (pending); for (int i = 0; i < len;) { buf.write (Wbxml.LITERAL); writeStrT ((String) attributes.elementAt (i++)); buf.write (Wbxml.STR_I); writeStrI (buf, (String) attributes.elementAt (i++)); } pending = null; attributes.removeAllElements (); } public void startTag (PrefixMap prefixMap, String name) throws IOException { current = new State (current, prefixMap, name); checkPending (false); pending = name; } public void attribute (String name, String value) { attributes.addElement (name); attributes.addElement (value); } public void write (char [] chars, int start, int len) throws IOException { checkPending (false); buf.write (Wbxml.STR_I); writeStrI (buf, new String (chars, start, len)); } public void endTag () throws IOException { current = current.prev; if (pending != null) checkPending (true); else buf.write (Wbxml.END); } /** currently ignored! */ public void writeLegacy (int type, String data) { } // ------------- internal methods -------------------------- static void writeInt (OutputStream out, int i) throws IOException { byte [] buf = new byte [5]; int idx = 0; do { buf [idx++] = (byte) (i & 0x7f); i = i >> 7; } while (i != 0); while (idx > 1) { out.write (buf [--idx] | 0x80); } out.write (buf [0]); } static void writeStrI (OutputStream out, String s) throws IOException { for (int i=0; i < s.length (); i++) { out.write ((byte) s.charAt (i)); } out.write (0); } void writeStrT (String s) throws IOException { Integer idx = (Integer) stringTable.get (s); if (idx == null) { idx = new Integer (stringTableBuf.size ()); stringTable.put (s, idx); writeStrI (stringTableBuf, s); stringTableBuf.flush (); } writeInt (buf, idx.intValue ()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -