FileDocCategorySizeDatePackage
KXmlSerializer.javaAPI DocAndroid 1.5 API16302Wed May 06 22:41:06 BST 2009org.kxml2.io

KXmlSerializer

public class KXmlSerializer extends Object implements XmlSerializer

Fields Summary
private static final int
WRITE_BUFFER_SIZE
size (in characters) for the write buffer
private BufferedWriter
writer
private boolean
pending
private int
auto
private int
depth
private String[]
elementStack
private int[]
nspCounts
private String[]
nspStack
private boolean[]
indent
private boolean
unicode
private String
encoding
Constructors Summary
Methods Summary
public org.xmlpull.v1.XmlSerializerattribute(java.lang.String namespace, java.lang.String name, java.lang.String value)

        if (!pending)
            throw new IllegalStateException("illegal position for attribute");

        //        int cnt = nspCounts[depth];

        if (namespace == null)
            namespace = "";

        //        depth--;
        //        pending = false;

        String prefix =
            "".equals(namespace)
                ? ""
                : getPrefix(namespace, false, true);

        //        pending = true;
        //        depth++;

        /*        if (cnt != nspCounts[depth]) {
                    writer.write(' ');
                    writer.write("xmlns");
                    if (nspStack[cnt * 2] != null) {
                        writer.write(':');
                        writer.write(nspStack[cnt * 2]);
                    }
                    writer.write("=\"");
                    writeEscaped(nspStack[cnt * 2 + 1], '"');
                    writer.write('"');
                }
                */

        writer.write(' ");
        if (!"".equals(prefix)) {
            writer.write(prefix);
            writer.write(':");
        }
        writer.write(name);
        writer.write('=");
        char q = value.indexOf('"") == -1 ? '"" : '\'";
        writer.write(q);
        writeEscaped(value, q);
        writer.write(q);

        return this;
    
public voidcdsect(java.lang.String data)

        check(false);
        writer.write("<![CDATA[");
        writer.write(data);
        writer.write("]]>");
    
private final voidcheck(boolean close)


           
        if (!pending)
            return;

        depth++;
        pending = false;

        if (indent.length <= depth) {
            boolean[] hlp = new boolean[depth + 4];
            System.arraycopy(indent, 0, hlp, 0, depth);
            indent = hlp;
        }
        indent[depth] = indent[depth - 1];

        for (int i = nspCounts[depth - 1];
            i < nspCounts[depth];
            i++) {
            writer.write(' ");
            writer.write("xmlns");
            if (!"".equals(nspStack[i * 2])) {
                writer.write(':");
                writer.write(nspStack[i * 2]);
            }
            else if ("".equals(getNamespace()) && !"".equals(nspStack[i * 2 + 1]))
                throw new IllegalStateException("Cannot set default namespace for elements in no namespace");
            writer.write("=\"");
            writeEscaped(nspStack[i * 2 + 1], '"");
            writer.write('"");
        }

        if (nspCounts.length <= depth + 1) {
            int[] hlp = new int[depth + 8];
            System.arraycopy(nspCounts, 0, hlp, 0, depth + 1);
            nspCounts = hlp;
        }

        nspCounts[depth + 1] = nspCounts[depth];
        //   nspCounts[depth + 2] = nspCounts[depth];

        writer.write(close ? " />" : ">");
    
public voidcomment(java.lang.String comment)

        check(false);
        writer.write("<!--");
        writer.write(comment);
        writer.write("-->");
    
public voiddocdecl(java.lang.String dd)

        writer.write("<!DOCTYPE");
        writer.write(dd);
        writer.write(">");
    
public voidendDocument()

        while (depth > 0) {
            endTag(
                elementStack[depth * 3 - 3],
                elementStack[depth * 3 - 1]);
        }
        flush();
    
public org.xmlpull.v1.XmlSerializerendTag(java.lang.String namespace, java.lang.String name)


        if (!pending)
            depth--;
        //        if (namespace == null)
        //          namespace = "";

        if ((namespace == null
            && elementStack[depth * 3] != null)
            || (namespace != null
                && !namespace.equals(elementStack[depth * 3]))
            || !elementStack[depth * 3 + 2].equals(name))
            throw new IllegalArgumentException("</{"+namespace+"}"+name+"> does not match start");

        if (pending) {
            check(true);
            depth--;
        }
        else {
            if (indent[depth + 1]) {
                writer.write("\r\n");
                for (int i = 0; i < depth; i++)
                    writer.write("  ");
            }

            writer.write("</");
            String prefix = elementStack[depth * 3 + 1];
            if (!"".equals(prefix)) {
                writer.write(prefix);
                writer.write(':");
            }
            writer.write(name);
            writer.write('>");
        }

        nspCounts[depth + 1] = nspCounts[depth];
        return this;
    
public voidentityRef(java.lang.String name)

        check(false);
        writer.write('&");
        writer.write(name);
        writer.write(';");
    
public voidflush()

        check(false);
        writer.flush();
    
public intgetDepth()

        return pending ? depth + 1 : depth;
    
public booleangetFeature(java.lang.String name)

        //return false;
        return (
            "http://xmlpull.org/v1/doc/features.html#indent-output"
                .equals(
                name))
            ? indent[depth]
            : false;
    
public java.lang.StringgetName()

        return getDepth() == 0 ? null : elementStack[getDepth() * 3 - 1];
    
public java.lang.StringgetNamespace()

        return getDepth() == 0 ? null : elementStack[getDepth() * 3 - 3];
    
public java.lang.StringgetPrefix(java.lang.String namespace, boolean create)

        try {
            return getPrefix(namespace, false, create);
        }
        catch (IOException e) {
            throw new RuntimeException(e.toString());
        }
    
private final java.lang.StringgetPrefix(java.lang.String namespace, boolean includeDefault, boolean create)


        for (int i = nspCounts[depth + 1] * 2 - 2;
            i >= 0;
            i -= 2) {
            if (nspStack[i + 1].equals(namespace)
                && (includeDefault || !nspStack[i].equals(""))) {
                String cand = nspStack[i];
                for (int j = i + 2;
                    j < nspCounts[depth + 1] * 2;
                    j++) {
                    if (nspStack[j].equals(cand)) {
                        cand = null;
                        break;
                    }
                }
                if (cand != null)
                    return cand;
            }
        }

        if (!create)
            return null;

        String prefix;

        if ("".equals(namespace))
            prefix = "";
        else {
            do {
                prefix = "n" + (auto++);
                for (int i = nspCounts[depth + 1] * 2 - 2;
                    i >= 0;
                    i -= 2) {
                    if (prefix.equals(nspStack[i])) {
                        prefix = null;
                        break;
                    }
                }
            }
            while (prefix == null);
        }

        boolean p = pending;
        pending = false;
        setPrefix(prefix, namespace);
        pending = p;
        return prefix;
    
public java.lang.ObjectgetProperty(java.lang.String name)

        throw new RuntimeException("Unsupported property");
    
public voidignorableWhitespace(java.lang.String s)

        text(s);
    
public voidprocessingInstruction(java.lang.String pi)

        check(false);
        writer.write("<?");
        writer.write(pi);
        writer.write("?>");
    
public voidsetFeature(java.lang.String name, boolean value)

        if ("http://xmlpull.org/v1/doc/features.html#indent-output"
            .equals(name)) {
            indent[depth] = value;
        }
        else
            throw new RuntimeException("Unsupported Feature");
    
public voidsetOutput(java.io.Writer writer)

        // BEGIN android-changed
        // Guarantee that the writer is always buffered.
        if (writer instanceof BufferedWriter) {
            this.writer = (BufferedWriter) writer;
        } else {
            this.writer = new BufferedWriter(writer, WRITE_BUFFER_SIZE);
        }
        // END android-changed

        // elementStack = new String[12]; //nsp/prefix/name
        //nspCounts = new int[4];
        //nspStack = new String[8]; //prefix/nsp
        //indent = new boolean[4];

        nspCounts[0] = 2;
        nspCounts[1] = 2;
        nspStack[0] = "";
        nspStack[1] = "";
        nspStack[2] = "xml";
        nspStack[3] = "http://www.w3.org/XML/1998/namespace";
        pending = false;
        auto = 0;
        depth = 0;

        unicode = false;
    
public voidsetOutput(java.io.OutputStream os, java.lang.String encoding)

        if (os == null)
            throw new IllegalArgumentException();
        setOutput(
            encoding == null
                ? new OutputStreamWriter(os)
                : new OutputStreamWriter(os, encoding));
        this.encoding = encoding;
        if (encoding != null
            && encoding.toLowerCase().startsWith("utf"))
            unicode = true;
    
public voidsetPrefix(java.lang.String prefix, java.lang.String namespace)


        check(false);
        if (prefix == null)
            prefix = "";
        if (namespace == null)
            namespace = "";

        String defined = getPrefix(namespace, true, false);

        // boil out if already defined

        if (prefix.equals(defined))
            return;

        int pos = (nspCounts[depth + 1]++) << 1;

        if (nspStack.length < pos + 1) {
            String[] hlp = new String[nspStack.length + 16];
            System.arraycopy(nspStack, 0, hlp, 0, pos);
            nspStack = hlp;
        }

        nspStack[pos++] = prefix;
        nspStack[pos] = namespace;
    
public voidsetProperty(java.lang.String name, java.lang.Object value)

        throw new RuntimeException(
            "Unsupported Property:" + value);
    
public voidstartDocument(java.lang.String encoding, java.lang.Boolean standalone)

        writer.write("<?xml version='1.0' ");

        if (encoding != null) {
            this.encoding = encoding;
            if (encoding.toLowerCase().startsWith("utf"))
                unicode = true;
        }

        if (this.encoding != null) {
            writer.write("encoding='");
            writer.write(this.encoding);
            writer.write("' ");
        }

        if (standalone != null) {
            writer.write("standalone='");
            writer.write(
                standalone.booleanValue() ? "yes" : "no");
            writer.write("' ");
        }
        writer.write("?>");
    
public org.xmlpull.v1.XmlSerializerstartTag(java.lang.String namespace, java.lang.String name)

        check(false);

        //        if (namespace == null)
        //            namespace = "";

        if (indent[depth]) {
            writer.write("\r\n");
            for (int i = 0; i < depth; i++)
                writer.write("  ");
        }

        int esp = depth * 3;

        if (elementStack.length < esp + 3) {
            String[] hlp = new String[elementStack.length + 12];
            System.arraycopy(elementStack, 0, hlp, 0, esp);
            elementStack = hlp;
        }

        String prefix =
            namespace == null
                ? ""
                : getPrefix(namespace, true, true);

        if ("".equals(namespace)) {
            for (int i = nspCounts[depth];
                i < nspCounts[depth + 1];
                i++) {
                if ("".equals(nspStack[i * 2]) && !"".equals(nspStack[i * 2 + 1])) {
                    throw new IllegalStateException("Cannot set default namespace for elements in no namespace");
                }
            }
        }

        elementStack[esp++] = namespace;
        elementStack[esp++] = prefix;
        elementStack[esp] = name;

        writer.write('<");
        if (!"".equals(prefix)) {
            writer.write(prefix);
            writer.write(':");
        }

        writer.write(name);

        pending = true;

        return this;
    
public org.xmlpull.v1.XmlSerializertext(java.lang.String text)

        check(false);
        indent[depth] = false;
        writeEscaped(text, -1);
        return this;
    
public org.xmlpull.v1.XmlSerializertext(char[] text, int start, int len)

        text(new String(text, start, len));
        return this;
    
private final voidwriteEscaped(java.lang.String s, int quot)


        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case '\n":
                case '\r":
                case '\t":
                    if(quot == -1) 
                        writer.write(c);
                    else 
                        writer.write("&#"+((int) c)+';");
                    break;
                case '&" :
                    writer.write("&");
                    break;
                case '>" :
                    writer.write(">");
                    break;
                case '<" :
                    writer.write("<");
                    break;
                case '"" :
                case '\'" :
                    if (c == quot) {
                        writer.write(
                            c == '"" ? """ : "'");
                        break;
                    }
                default :
                    //if(c < ' ')
                    //    throw new IllegalArgumentException("Illegal control code:"+((int) c));

                    if (c >= ' " && c !='@" && (c < 127 || unicode))
                        writer.write(c);
                    else
                        writer.write("&#" + ((int) c) + ";");

            }
        }