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());
}
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;
}
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) {
}
}
}
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");
}
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();
}
Aggregations