FileDocCategorySizeDatePackage
WbxmlSerializer.javaAPI DocAndroid 1.5 API14289Wed May 06 22:41:06 BST 2009org.kxml2.wap

WbxmlSerializer

public class WbxmlSerializer extends Object implements XmlSerializer
A class for writing WBXML.

Fields Summary
Hashtable
stringTable
OutputStream
out
ByteArrayOutputStream
buf
ByteArrayOutputStream
stringTableBuf
String
pending
int
depth
String
name
String
namespace
Vector
attributes
Hashtable
attrStartTable
Hashtable
attrValueTable
Hashtable
tagTable
private int
attrPage
private int
tagPage
private String
encoding
Constructors Summary
Methods Summary
public org.xmlpull.v1.XmlSerializerattribute(java.lang.String namespace, java.lang.String name, java.lang.String value)

    
    
            
        attributes.addElement(name);
        attributes.addElement(value);
        return this;
    
public voidcdsect(java.lang.String cdsect)

        text (cdsect);
    
public voidcheckPending(boolean degenerated)

        if (pending == null)
            return;
        
        int len = attributes.size();
        
        int[] idx = (int[]) tagTable.get(pending);
        
        // if no entry in known table, then add as literal
        if (idx == null) {
            buf.write(
            len == 0
            ? (degenerated ? Wbxml.LITERAL : Wbxml.LITERAL_C)
            : (degenerated ? Wbxml.LITERAL_A : Wbxml.LITERAL_AC));
            
            writeStrT(pending, false);
        }
        else {
            if(idx[0] != tagPage){
                tagPage=idx[0];
                buf.write(Wbxml.SWITCH_PAGE);
                buf.write(tagPage);
            }
            
            buf.write(
            len == 0
            ? (degenerated ? idx[1] : idx[1] | 64)
            : (degenerated
            ? idx[1] | 128
            : idx[1] | 192));
           
        }
        
        for (int i = 0; i < len;) {
            idx = (int[]) attrStartTable.get(attributes.elementAt(i));
            
            if (idx == null) {
                buf.write(Wbxml.LITERAL);
                writeStrT((String) attributes.elementAt(i), false);
            }
            else {
                if(idx[0] != attrPage){
                        attrPage = idx[0];
                    buf.write(0);
                    buf.write(attrPage);
                }
                buf.write(idx[1]);
            }
            idx = (int[]) attrValueTable.get(attributes.elementAt(++i));
            if (idx == null) {
                writeStr((String) attributes.elementAt(i));
            }
            else {
                if(idx[0] != attrPage){
                        attrPage = idx[0];
                    buf.write(0);
                    buf.write(attrPage);                    
                }
                buf.write(idx[1]);
            }
            ++i;
        }
        
        if (len > 0)
            buf.write(Wbxml.END);
        
        pending = null;
        attributes.removeAllElements();
    
public voidcomment(java.lang.String comment)

    
public voiddocdecl(java.lang.String docdecl)

        throw new RuntimeException ("Cannot write docdecl for WBXML");
    
public voidendDocument()

        writeInt(out, stringTableBuf.size());
        
        // write StringTable
        
        out.write(stringTableBuf.toByteArray());
        
        // write buf
        
        out.write(buf.toByteArray());
        
        // ready!
        
        out.flush();
    
public org.xmlpull.v1.XmlSerializerendTag(java.lang.String namespace, java.lang.String name)

        
        //        current = current.prev;
        
        if (pending != null)
            checkPending(true);
        else
            buf.write(Wbxml.END);
        
        depth--;
        
        return this;
    
public voidentityRef(java.lang.String er)

        throw new RuntimeException ("EntityReference not supported for WBXML");
    
public voidflush()
ATTENTION: flush cannot work since Wbxml documents require buffering. Thus, this call does nothing.

    
public intgetDepth()

        return depth;
    
public booleangetFeature(java.lang.String name)

        return false;
    
public java.lang.StringgetName()

        throw new RuntimeException("NYI");
    
public java.lang.StringgetNamespace()

        throw new RuntimeException("NYI");
    
public java.lang.StringgetPrefix(java.lang.String nsp, boolean create)

        throw new RuntimeException ("NYI");
    
public java.lang.ObjectgetProperty(java.lang.String name)

        return null;
    
public voidignorableWhitespace(java.lang.String sp)

    
public voidprocessingInstruction(java.lang.String pi)

        throw new RuntimeException ("PI NYI");
    
public voidsetAttrStartTable(int page, java.lang.String[] attrStartTable)
Sets the attribute start Table for a given page. The first string in the array defines attribute 5, the second attribute 6 etc. Please use the character '=' (without quote!) as delimiter between the attribute name and the (start of the) value

        
        for (int i = 0; i < attrStartTable.length; i++) {
            if (attrStartTable[i] != null) {
                Object idx = new int[] {page, i + 5};
                this.attrStartTable.put(attrStartTable[i], idx);
            }
        }
    
public voidsetAttrValueTable(int page, java.lang.String[] attrValueTable)
Sets the attribute value Table for a given page. The first string in the array defines attribute value 0x85, the second attribute value 0x86 etc.

        // clear entries in this.table!
        for (int i = 0; i < attrValueTable.length; i++) {
            if (attrValueTable[i] != null) {
                Object idx = new int[]{page, i + 0x085};
                this.attrValueTable.put(attrValueTable[i], idx);
            }
        }
    
public voidsetFeature(java.lang.String name, boolean value)

        throw new IllegalArgumentException ("unknown feature "+name);
    
public voidsetOutput(java.io.Writer writer)

        throw new RuntimeException ("Wbxml requires an OutputStream!");
    
public voidsetOutput(java.io.OutputStream out, java.lang.String encoding)

        
        this.encoding = encoding == null ? "UTF-8" : encoding;
        this.out = out;
        
        buf = new ByteArrayOutputStream();
        stringTableBuf = new ByteArrayOutputStream();
        
        // ok, write header
    
public voidsetPrefix(java.lang.String prefix, java.lang.String nsp)

        throw new RuntimeException("NYI");
    
public voidsetProperty(java.lang.String property, java.lang.Object value)

        throw new IllegalArgumentException ("unknown property "+property);
    
public voidsetTagTable(int page, java.lang.String[] tagTable)
Sets the tag table for a given page. The first string in the array defines tag 5, the second tag 6 etc.

        // TODO: clear entries in tagTable?
        
        for (int i = 0; i < tagTable.length; i++) {
            if (tagTable[i] != null) {
                Object idx = new int[]{page, i+5};
                this.tagTable.put(tagTable[i], idx);
            }
        }
    
public voidstartDocument(java.lang.String s, java.lang.Boolean b)

        out.write(0x03); // version 1.3
        // http://www.openmobilealliance.org/tech/omna/omna-wbxml-public-docid.htm
        out.write(0x01); // unknown or missing public identifier

        // default encoding is UTF-8
        
        if(s != null){
            encoding = s;
        }
        
        if (encoding.toUpperCase().equals("UTF-8")){
            out.write(106);
        }else if (encoding.toUpperCase().equals("ISO-8859-1")){
            out.write(0x04);
        }else{
            throw new UnsupportedEncodingException(s);
        }
    
public org.xmlpull.v1.XmlSerializerstartTag(java.lang.String namespace, java.lang.String name)

        
        if (namespace != null && !"".equals(namespace))
            throw new RuntimeException ("NSP NYI");
        
        //current = new State(current, prefixMap, name);
        
        checkPending(false);
        pending = name;
        depth++;
        
        return this;
    
public org.xmlpull.v1.XmlSerializertext(char[] chars, int start, int len)


        checkPending(false);
        
        writeStr(new String(chars, start, len));
        
        return this;
    
public org.xmlpull.v1.XmlSerializertext(java.lang.String text)

        
        checkPending(false);
        
        writeStr(text);
    
        return this;
    
static voidwriteInt(java.io.OutputStream out, int i)

        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]);
    
private voidwriteStr(java.lang.String text)
Used in text() and attribute() to write text

        int p0 = 0;
        int lastCut = 0;
        int len = text.length();
        
        while(p0 < len){
            while(p0 < len && text.charAt(p0) < 'A" ){ // skip interpunctation
                p0++;
            }
            int p1 = p0;
            while(p1 < len && text.charAt(p1) >= 'A"){
                p1++;
            }
            
            if(p1 - p0 > 10) {

                if(p0 > lastCut && text.charAt(p0-1) == ' " 
                    && stringTable.get(text.substring(p0, p1)) == null){
                    
                       buf.write(Wbxml.STR_T);
                       writeStrT(text.substring(lastCut, p1), false);
                }
                else {

                    if(p0 > lastCut && text.charAt(p0-1) == ' "){
                        p0--;
                    }

                    if(p0 > lastCut){
                        buf.write(Wbxml.STR_T);
                        writeStrT(text.substring(lastCut, p0), false);
                    }
                    buf.write(Wbxml.STR_T);
                    writeStrT(text.substring(p0, p1), true);
                }
                lastCut = p1;
            }
            p0 = p1;
        }

        if(lastCut < len){
            buf.write(Wbxml.STR_T);
            writeStrT(text.substring(lastCut, len), false);
        }
    
voidwriteStrI(java.io.OutputStream out, java.lang.String s)

        byte[] data = s.getBytes(encoding);
        out.write(data);
        out.write(0);
    
private final voidwriteStrT(java.lang.String s, boolean mayPrependSpace)

        
        Integer idx = (Integer) stringTable.get(s);
        
        if (idx != null) {
            writeInt(buf, idx.intValue());
        }
        else{
            int i = stringTableBuf.size();
            if(s.charAt(0) >= '0" && mayPrependSpace){
                s = ' " + s;
                writeInt(buf, i+1);
            }
            else{
                writeInt(buf, i);
            }
            
               stringTable.put(s, new Integer(i));
               if(s.charAt(0) == ' "){
                   stringTable.put(s.substring(1), new Integer(i+1));
               }
               int j = s.lastIndexOf(' ");
               if(j > 1){
                   stringTable.put(s.substring(j), new Integer(i+j));
                   stringTable.put(s.substring(j+1), new Integer(i+j+1));
               }
                
            writeStrI(stringTableBuf, s);
            stringTableBuf.flush();
        }
        
    
public voidwriteWapExtension(int type, java.lang.Object data)

throws
IOException

        checkPending(false);
        buf.write(type);
        switch(type){
        case Wbxml.EXT_0:
        case Wbxml.EXT_1:
        case Wbxml.EXT_2:
            break;
        
        case Wbxml.OPAQUE:
            byte[] bytes = (byte[]) data;
            writeInt(buf, bytes.length);
            buf.write(bytes);
            break;
            
        case Wbxml.EXT_I_0:
        case Wbxml.EXT_I_1:
        case Wbxml.EXT_I_2:
            writeStrI(buf, (String) data);
            break;

        case Wbxml.EXT_T_0:
        case Wbxml.EXT_T_1:
        case Wbxml.EXT_T_2:
            writeStrT((String) data, false);
            break;
            
        default: 
            throw new IllegalArgumentException();
        }