Search in sources :

Example 56 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class FastXmlSerializerTest method testEmptyText.

public void testEmptyText() throws Exception {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    final XmlSerializer out = new FastXmlSerializer();
    out.setOutput(stream, "utf-8");
    out.startDocument(null, true);
    out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
    out.startTag(null, "string");
    out.attribute(null, "name", "meow");
    out.text("");
    out.endTag(null, "string");
    out.endDocument();
    assertEquals("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" + "<string name=\"meow\"></string>\n", stream.toString());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 57 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class FastXmlSerializerTest method checkPreserved.

private boolean checkPreserved(String description, String str) {
    boolean ok = true;
    byte[] data;
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final XmlSerializer out = new FastXmlSerializer();
        out.setOutput(baos, StandardCharsets.UTF_16.name());
        out.startDocument(null, true);
        out.startTag(null, ROOT_TAG);
        out.attribute(null, ATTR, str);
        out.text(str);
        out.endTag(null, ROOT_TAG);
        out.endDocument();
        baos.flush();
        data = baos.toByteArray();
    } catch (Exception e) {
        Log.e(TAG, "Unable to serialize: " + description, e);
        return false;
    }
    if (ENABLE_DUMP) {
        Log.d(TAG, "Dump:");
        Log.d(TAG, new String(data));
    }
    try (final ByteArrayInputStream baos = new ByteArrayInputStream(data)) {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(baos, StandardCharsets.UTF_16.name());
        int type;
        String tag = null;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type == XmlPullParser.START_TAG) {
                tag = parser.getName();
                if (ROOT_TAG.equals(tag)) {
                    String read = parser.getAttributeValue(null, ATTR);
                    if (!str.equals(read)) {
                        Log.e(TAG, "Attribute not preserved: " + description + " input=\"" + str + "\", but read=\"" + read + "\"");
                        ok = false;
                    }
                }
            }
            if (type == XmlPullParser.TEXT && ROOT_TAG.equals(tag)) {
                String read = parser.getText();
                if (!str.equals(parser.getText())) {
                    Log.e(TAG, "Text not preserved: " + description + " input=\"" + str + "\", but read=\"" + read + "\"");
                    ok = false;
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Unable to parse: " + description, e);
        return false;
    }
    return ok;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 58 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class BatteryStatsImpl method recordDailyStatsLocked.

public void recordDailyStatsLocked() {
    DailyItem item = new DailyItem();
    item.mStartTime = mDailyStartTime;
    item.mEndTime = System.currentTimeMillis();
    boolean hasData = false;
    if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
        hasData = true;
        item.mDischargeSteps = new LevelStepTracker(mDailyDischargeStepTracker.mNumStepDurations, mDailyDischargeStepTracker.mStepDurations);
    }
    if (mDailyChargeStepTracker.mNumStepDurations > 0) {
        hasData = true;
        item.mChargeSteps = new LevelStepTracker(mDailyChargeStepTracker.mNumStepDurations, mDailyChargeStepTracker.mStepDurations);
    }
    if (mDailyPackageChanges != null) {
        hasData = true;
        item.mPackageChanges = mDailyPackageChanges;
        mDailyPackageChanges = null;
    }
    mDailyDischargeStepTracker.init();
    mDailyChargeStepTracker.init();
    updateDailyDeadlineLocked();
    if (hasData) {
        mDailyItems.add(item);
        while (mDailyItems.size() > MAX_DAILY_ITEMS) {
            mDailyItems.remove(0);
        }
        final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
        try {
            XmlSerializer out = new FastXmlSerializer();
            out.setOutput(memStream, StandardCharsets.UTF_8.name());
            writeDailyItemsLocked(out);
            BackgroundThread.getHandler().post(new Runnable() {

                @Override
                public void run() {
                    synchronized (mCheckinFile) {
                        FileOutputStream stream = null;
                        try {
                            stream = mDailyFile.startWrite();
                            memStream.writeTo(stream);
                            stream.flush();
                            FileUtils.sync(stream);
                            stream.close();
                            mDailyFile.finishWrite(stream);
                        } catch (IOException e) {
                            Slog.w("BatteryStats", "Error writing battery daily items", e);
                            mDailyFile.failWrite(stream);
                        }
                    }
                }
            });
        } catch (IOException e) {
        }
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 59 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class AccessibilityNodeInfoDumper method dumpWindowToFile.

/**
     * Using {@link AccessibilityNodeInfo} this method will walk the layout hierarchy
     * and generates an xml dump to the location specified by <code>dumpFile</code>
     * @param root The root accessibility node.
     * @param dumpFile The file to dump to.
     * @param rotation The rotaion of current display
     * @param width The pixel width of current display
     * @param height The pixel height of current display
     */
public static void dumpWindowToFile(AccessibilityNodeInfo root, File dumpFile, int rotation, int width, int height) {
    if (root == null) {
        return;
    }
    final long startTime = SystemClock.uptimeMillis();
    try {
        FileWriter writer = new FileWriter(dumpFile);
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter stringWriter = new StringWriter();
        serializer.setOutput(stringWriter);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "hierarchy");
        serializer.attribute("", "rotation", Integer.toString(rotation));
        dumpNodeRec(root, serializer, 0, width, height);
        serializer.endTag("", "hierarchy");
        serializer.endDocument();
        writer.write(stringWriter.toString());
        writer.close();
    } catch (IOException e) {
        Log.e(LOGTAG, "failed to dump window to file", e);
    }
    final long endTime = SystemClock.uptimeMillis();
    Log.w(LOGTAG, "Fetch time: " + (endTime - startTime) + "ms");
}
Also used : StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 60 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project AndroidChromium by JackyAndroid.

the class RequestGenerator method generateXML.

/**
     * Generates the XML for the current request.
     * Follows the format laid out at https://github.com/google/omaha/blob/wiki/ServerProtocolV3.md
     * with some additional dummy values supplied.
     */
public String generateXML(String sessionID, String versionName, long installAge, RequestData data) throws RequestFailureException {
    XmlSerializer serializer = Xml.newSerializer();
    StringWriter writer = new StringWriter();
    try {
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        // Set up <request protocol=3.0 ...>
        serializer.startTag(null, "request");
        serializer.attribute(null, "protocol", "3.0");
        serializer.attribute(null, "version", "Android-1.0.0.0");
        serializer.attribute(null, "ismachine", "1");
        serializer.attribute(null, "requestid", "{" + data.getRequestID() + "}");
        serializer.attribute(null, "sessionid", "{" + sessionID + "}");
        serializer.attribute(null, "installsource", data.getInstallSource());
        appendExtraAttributes("request", serializer);
        // Set up <os platform="android"... />
        serializer.startTag(null, "os");
        serializer.attribute(null, "platform", "android");
        serializer.attribute(null, "version", Build.VERSION.RELEASE);
        serializer.attribute(null, "arch", "arm");
        serializer.endTag(null, "os");
        // Set up <app version="" ...>
        serializer.startTag(null, "app");
        serializer.attribute(null, "brand", getBrand());
        serializer.attribute(null, "client", getClient());
        serializer.attribute(null, "appid", getAppId());
        serializer.attribute(null, "version", versionName);
        serializer.attribute(null, "nextversion", "");
        serializer.attribute(null, "lang", getLanguage());
        serializer.attribute(null, "installage", String.valueOf(installAge));
        serializer.attribute(null, "ap", getAdditionalParameters());
        appendExtraAttributes("app", serializer);
        if (data.isSendInstallEvent()) {
            // Set up <event eventtype="2" eventresult="1" />
            serializer.startTag(null, "event");
            serializer.attribute(null, "eventtype", "2");
            serializer.attribute(null, "eventresult", "1");
            serializer.endTag(null, "event");
        } else {
            // Set up <updatecheck />
            serializer.startTag(null, "updatecheck");
            serializer.endTag(null, "updatecheck");
            // Set up <ping active="1" />
            serializer.startTag(null, "ping");
            serializer.attribute(null, "active", "1");
            serializer.endTag(null, "ping");
        }
        serializer.endTag(null, "app");
        serializer.endTag(null, "request");
        serializer.endDocument();
    } catch (IOException e) {
        throw new RequestFailureException("Caught an IOException creating the XML: ", e);
    } catch (IllegalArgumentException e) {
        throw new RequestFailureException("Caught an IllegalArgumentException creating the XML: ", e);
    } catch (IllegalStateException e) {
        throw new RequestFailureException("Caught an IllegalStateException creating the XML: ", e);
    }
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Aggregations

XmlSerializer (org.xmlpull.v1.XmlSerializer)268 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)156 IOException (java.io.IOException)151 FileOutputStream (java.io.FileOutputStream)144 BufferedOutputStream (java.io.BufferedOutputStream)57 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 AtomicFile (android.util.AtomicFile)39 RemoteException (android.os.RemoteException)29 File (java.io.File)26 FileNotFoundException (java.io.FileNotFoundException)26 StringWriter (java.io.StringWriter)26 Map (java.util.Map)23 ErrnoException (android.system.ErrnoException)20 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)15 JournaledFile (com.android.internal.util.JournaledFile)15 UserInfo (android.content.pm.UserInfo)9 HashMap (java.util.HashMap)9 SendIntentException (android.content.IntentSender.SendIntentException)8 PackageParserException (android.content.pm.PackageParser.PackageParserException)8