/*
 * Copyright (C) 2009 JavaRosa
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.javarosa.xform.util;

import org.kxml2.io.KXmlSerializer;
import org.kxml2.kdom.Document;
import org.kxml2.kdom.Element;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

/* this is just a big dump of serialization-related code */

/* basically, anything that didn't belong in XFormParser */

public class XFormSerializer {

    public static ByteArrayOutputStream getStream(Document doc) {
        KXmlSerializer serializer = new KXmlSerializer();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        try {
            serializer.setOutput(dos, null);
            doc.write(serializer);
            serializer.flush();
            return bos;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String elementToString(Element e) {
        KXmlSerializer serializer = new KXmlSerializer();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        String s = null;
        try {
            serializer.setOutput(dos, null);
            e.write(serializer);
            serializer.flush();
            s = new String(bos.toByteArray(), "UTF-8");
            return s;
        } catch (UnsupportedEncodingException uce) {
            uce.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

        return null;

    }

    public static String getString(Document doc) {
        ByteArrayOutputStream bos = getStream(doc);

        byte[] byteArr = bos.toByteArray();
        char[] charArray = new char[byteArr.length];
        for (int i = 0; i < byteArr.length; i++)
            charArray[i] = (char)byteArr[i];

        return String.valueOf(charArray);
    }

    public static byte[] getUtfBytes(Document doc) {
        KXmlSerializer serializer = new KXmlSerializer();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            Writer osw = new OutputStreamWriter(bos, "UTF-8");
            serializer.setOutput(osw);
            doc.write(serializer);
            serializer.flush();
            return bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

@@ -1,1562 +1,343 @@
-/*%0A * Copyright (C) 2009 JavaRosa%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22); you may not%0A * use this file except in compliance with the License. You may obtain a copy of%0A * the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS, WITHOUT%0A * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the%0A * License for the specific language governing permissions and limitations under%0A * the License.%0A */%0A%0Apackage org.javarosa.xform.util;%0A%0Aimport org.kxml2.io.KXmlSerializer;%0Aimport org.kxml2.kdom.Document;%0Aimport org.kxml2.kdom.Element;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.DataOutputStream;%0Aimport java.io.OutputStreamWriter;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.io.Writer;%0A%0A/* this is just a big dump of serialization-related code */%0A%0A/* basically, anything that didn't belong in XFormParser */%0A%0Apublic class XFormSerializer %7B%0A%0A    public static ByteArrayOutputStream getStream(Document doc) %7B%0A        KXmlSerializer serializer = new KXmlSerializer();%0A        ByteArrayOutputStream bos = new ByteArrayOutputStream();%0A        DataOutputStream dos = new DataOutputStream(bos);%0A        try %7B%0A            serializer.setOutput(dos, null);%0A            doc.write(serializer);%0A            serializer.flush();%0A            return bos;%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A            return null;%0A        %7D%0A    %7D
+package org.javarosa.xform.util;%0A%0Aimport org.kxml2.io.KXmlSerializer;%0Aimport org.kxml2.kdom.Document;%0Aimport org.kxml2.kdom.Element;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.DataOutputStream;%0Aimport java.io.OutputStreamWriter;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.io.Writer;%0A%0Apublic class XFormSerializer %7B
 %0A%0A  
@@ -590,15 +590,8 @@
 ng s
- = null
 ;%0A  
@@ -1005,354 +1005,8 @@
 ull;
-%0A%0A    %7D%0A%0A    public static String getString(Document doc) %7B%0A        ByteArrayOutputStream bos = getStream(doc);%0A%0A        byte%5B%5D byteArr = bos.toByteArray();%0A        char%5B%5D charArray = new char%5BbyteArr.length%5D;%0A        for (int i = 0; i %3C byteArr.length; i++)%0A            charArray%5Bi%5D = (char)byteArr%5Bi%5D;%0A%0A        return String.valueOf(charArray);
 %0A   

Remove unused serialization code