Search in sources :

Example 26 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 27 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 28 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 29 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 30 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project AntennaPod by AntennaPod.

the class OpmlWriter method writeDocument.

/**
	 * Takes a list of feeds and a writer and writes those into an OPML
	 * document.
	 * 
	 * @throws IOException
	 * @throws IllegalStateException
	 * @throws IllegalArgumentException
	 */
@Override
public void writeDocument(List<Feed> feeds, Writer writer) throws IllegalArgumentException, IllegalStateException, IOException {
    Log.d(TAG, "Starting to write document");
    XmlSerializer xs = Xml.newSerializer();
    xs.setFeature(OpmlSymbols.XML_FEATURE_INDENT_OUTPUT, true);
    xs.setOutput(writer);
    xs.startDocument(ENCODING, false);
    xs.startTag(null, OpmlSymbols.OPML);
    xs.attribute(null, OpmlSymbols.VERSION, OPML_VERSION);
    xs.startTag(null, OpmlSymbols.HEAD);
    xs.startTag(null, OpmlSymbols.TITLE);
    xs.text(OPML_TITLE);
    xs.endTag(null, OpmlSymbols.TITLE);
    xs.startTag(null, OpmlSymbols.DATE_CREATED);
    xs.text(DateUtils.formatRFC822Date(new Date()));
    xs.endTag(null, OpmlSymbols.DATE_CREATED);
    xs.endTag(null, OpmlSymbols.HEAD);
    xs.startTag(null, OpmlSymbols.BODY);
    for (Feed feed : feeds) {
        xs.startTag(null, OpmlSymbols.OUTLINE);
        xs.attribute(null, OpmlSymbols.TEXT, feed.getTitle());
        xs.attribute(null, OpmlSymbols.TITLE, feed.getTitle());
        if (feed.getType() != null) {
            xs.attribute(null, OpmlSymbols.TYPE, feed.getType());
        }
        xs.attribute(null, OpmlSymbols.XMLURL, feed.getDownload_url());
        if (feed.getLink() != null) {
            xs.attribute(null, OpmlSymbols.HTMLURL, feed.getLink());
        }
        xs.endTag(null, OpmlSymbols.OUTLINE);
    }
    xs.endTag(null, OpmlSymbols.BODY);
    xs.endTag(null, OpmlSymbols.OPML);
    xs.endDocument();
    Log.d(TAG, "Finished writing document");
}
Also used : Date(java.util.Date) XmlSerializer(org.xmlpull.v1.XmlSerializer) Feed(de.danoeh.antennapod.core.feed.Feed)

Aggregations

XmlSerializer (org.xmlpull.v1.XmlSerializer)307 IOException (java.io.IOException)171 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)157 FileOutputStream (java.io.FileOutputStream)156 BufferedOutputStream (java.io.BufferedOutputStream)61 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)56 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 AtomicFile (android.util.AtomicFile)39 File (java.io.File)32 FileNotFoundException (java.io.FileNotFoundException)32 StringWriter (java.io.StringWriter)30 RemoteException (android.os.RemoteException)29 Map (java.util.Map)26 ErrnoException (android.system.ErrnoException)20 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)15 JournaledFile (com.android.internal.util.JournaledFile)15 HashMap (java.util.HashMap)14 FileWriter (java.io.FileWriter)12 UserInfo (android.content.pm.UserInfo)9 SendIntentException (android.content.IntentSender.SendIntentException)8