Search in sources :

Example 31 with XmlSerializer

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

the class AtomGenerator method writeFeed.

@Override
public void writeFeed(Feed feed, OutputStream outputStream, String encoding, long flags) throws IOException {
    if (feed == null)
        throw new IllegalArgumentException("feed = null");
    if (outputStream == null)
        throw new IllegalArgumentException("outputStream = null");
    if (encoding == null)
        throw new IllegalArgumentException("encoding = null");
    XmlSerializer xml = Xml.newSerializer();
    xml.setOutput(outputStream, encoding);
    xml.startDocument(encoding, null);
    xml.startTag(null, "feed");
    xml.attribute(null, "xmlns", NS_ATOM);
    // Write Feed data
    if (feed.getIdentifyingValue() != null) {
        xml.startTag(null, "id");
        xml.text(feed.getIdentifyingValue());
        xml.endTag(null, "id");
    }
    if (feed.getTitle() != null) {
        xml.startTag(null, "title");
        xml.text(feed.getTitle());
        xml.endTag(null, "title");
    }
    if (feed.getLink() != null) {
        xml.startTag(null, "link");
        xml.attribute(null, "rel", "alternate");
        xml.attribute(null, "href", feed.getLink());
        xml.endTag(null, "link");
    }
    if (feed.getDescription() != null) {
        xml.startTag(null, "subtitle");
        xml.text(feed.getDescription());
        xml.endTag(null, "subtitle");
    }
    if (feed.getPaymentLink() != null) {
        GeneratorUtil.addPaymentLink(xml, feed.getPaymentLink(), false);
    }
    // Write FeedItem data
    if (feed.getItems() != null) {
        for (FeedItem item : feed.getItems()) {
            xml.startTag(null, "entry");
            if (item.getIdentifyingValue() != null) {
                xml.startTag(null, "id");
                xml.text(item.getIdentifyingValue());
                xml.endTag(null, "id");
            }
            if (item.getTitle() != null) {
                xml.startTag(null, "title");
                xml.text(item.getTitle());
                xml.endTag(null, "title");
            }
            if (item.getLink() != null) {
                xml.startTag(null, "link");
                xml.attribute(null, "rel", "alternate");
                xml.attribute(null, "href", item.getLink());
                xml.endTag(null, "link");
            }
            if (item.getPubDate() != null) {
                xml.startTag(null, "published");
                if ((flags & FEATURE_USE_RFC3339LOCAL) != 0) {
                    xml.text(DateUtils.formatRFC3339Local(item.getPubDate()));
                } else {
                    xml.text(DateUtils.formatRFC3339UTC(item.getPubDate()));
                }
                xml.endTag(null, "published");
            }
            if (item.getDescription() != null) {
                xml.startTag(null, "content");
                xml.text(item.getDescription());
                xml.endTag(null, "content");
            }
            if (item.getMedia() != null) {
                FeedMedia media = item.getMedia();
                xml.startTag(null, "link");
                xml.attribute(null, "rel", "enclosure");
                xml.attribute(null, "href", media.getDownload_url());
                xml.attribute(null, "type", media.getMime_type());
                xml.attribute(null, "length", String.valueOf(media.getSize()));
                xml.endTag(null, "link");
            }
            if (item.getPaymentLink() != null) {
                GeneratorUtil.addPaymentLink(xml, item.getPaymentLink(), false);
            }
            xml.endTag(null, "entry");
        }
    }
    xml.endTag(null, "feed");
    xml.endDocument();
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 32 with XmlSerializer

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

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 33 with XmlSerializer

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

the class AppOpsService method writeState.

void writeState() {
    synchronized (mFile) {
        FileOutputStream stream;
        try {
            stream = mFile.startWrite();
        } catch (IOException e) {
            Slog.w(TAG, "Failed to write state: " + e);
            return;
        }
        SparseArray<UidState> outUidStates = null;
        synchronized (this) {
            final int uidStateCount = mUidStates.size();
            for (int i = 0; i < uidStateCount; i++) {
                UidState uidState = mUidStates.valueAt(i);
                SparseIntArray opModes = uidState.opModes;
                if (opModes != null && opModes.size() > 0) {
                    UidState outUidState = new UidState(uidState.uid);
                    outUidState.opModes = opModes.clone();
                    if (outUidStates == null) {
                        outUidStates = new SparseArray<>();
                    }
                    outUidStates.put(mUidStates.keyAt(i), outUidState);
                }
            }
        }
        List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
        try {
            XmlSerializer out = new FastXmlSerializer();
            out.setOutput(stream, StandardCharsets.UTF_8.name());
            out.startDocument(null, true);
            out.startTag(null, "app-ops");
            final int uidStateCount = outUidStates != null ? outUidStates.size() : 0;
            for (int i = 0; i < uidStateCount; i++) {
                UidState uidState = outUidStates.valueAt(i);
                if (uidState.opModes != null && uidState.opModes.size() > 0) {
                    out.startTag(null, "uid");
                    out.attribute(null, "n", Integer.toString(uidState.uid));
                    SparseIntArray uidOpModes = uidState.opModes;
                    final int opCount = uidOpModes.size();
                    for (int j = 0; j < opCount; j++) {
                        final int op = uidOpModes.keyAt(j);
                        final int mode = uidOpModes.valueAt(j);
                        out.startTag(null, "op");
                        out.attribute(null, "n", Integer.toString(op));
                        out.attribute(null, "m", Integer.toString(mode));
                        out.endTag(null, "op");
                    }
                    out.endTag(null, "uid");
                }
            }
            if (allOps != null) {
                String lastPkg = null;
                for (int i = 0; i < allOps.size(); i++) {
                    AppOpsManager.PackageOps pkg = allOps.get(i);
                    if (!pkg.getPackageName().equals(lastPkg)) {
                        if (lastPkg != null) {
                            out.endTag(null, "pkg");
                        }
                        lastPkg = pkg.getPackageName();
                        out.startTag(null, "pkg");
                        out.attribute(null, "n", lastPkg);
                    }
                    out.startTag(null, "uid");
                    out.attribute(null, "n", Integer.toString(pkg.getUid()));
                    synchronized (this) {
                        Ops ops = getOpsRawLocked(pkg.getUid(), pkg.getPackageName(), false);
                        // from Ops.
                        if (ops != null) {
                            out.attribute(null, "p", Boolean.toString(ops.isPrivileged));
                        } else {
                            out.attribute(null, "p", Boolean.toString(false));
                        }
                    }
                    List<AppOpsManager.OpEntry> ops = pkg.getOps();
                    for (int j = 0; j < ops.size(); j++) {
                        AppOpsManager.OpEntry op = ops.get(j);
                        out.startTag(null, "op");
                        out.attribute(null, "n", Integer.toString(op.getOp()));
                        if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
                            out.attribute(null, "m", Integer.toString(op.getMode()));
                        }
                        long time = op.getTime();
                        if (time != 0) {
                            out.attribute(null, "t", Long.toString(time));
                        }
                        time = op.getRejectTime();
                        if (time != 0) {
                            out.attribute(null, "r", Long.toString(time));
                        }
                        int dur = op.getDuration();
                        if (dur != 0) {
                            out.attribute(null, "d", Integer.toString(dur));
                        }
                        int proxyUid = op.getProxyUid();
                        if (proxyUid != -1) {
                            out.attribute(null, "pu", Integer.toString(proxyUid));
                        }
                        String proxyPackageName = op.getProxyPackageName();
                        if (proxyPackageName != null) {
                            out.attribute(null, "pp", proxyPackageName);
                        }
                        out.endTag(null, "op");
                    }
                    out.endTag(null, "uid");
                }
                if (lastPkg != null) {
                    out.endTag(null, "pkg");
                }
            }
            out.endTag(null, "app-ops");
            out.endDocument();
            mFile.finishWrite(stream);
        } catch (IOException e) {
            Slog.w(TAG, "Failed to write state, restoring backup.", e);
            mFile.failWrite(stream);
        }
    }
}
Also used : IOException(java.io.IOException) AppOpsManager(android.app.AppOpsManager) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) SparseIntArray(android.util.SparseIntArray) FileOutputStream(java.io.FileOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 34 with XmlSerializer

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

the class DevicePolicyManagerService method saveSettingsLocked.

private void saveSettingsLocked(int userHandle) {
    DevicePolicyData policy = getUserData(userHandle);
    JournaledFile journal = makeJournaledFile(userHandle);
    FileOutputStream stream = null;
    try {
        stream = new FileOutputStream(journal.chooseForWrite(), false);
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(stream, StandardCharsets.UTF_8.name());
        out.startDocument(null, true);
        out.startTag(null, "policies");
        if (policy.mRestrictionsProvider != null) {
            out.attribute(null, ATTR_PERMISSION_PROVIDER, policy.mRestrictionsProvider.flattenToString());
        }
        if (policy.mUserSetupComplete) {
            out.attribute(null, ATTR_SETUP_COMPLETE, Boolean.toString(true));
        }
        if (policy.mPaired) {
            out.attribute(null, ATTR_DEVICE_PAIRED, Boolean.toString(true));
        }
        if (policy.mDeviceProvisioningConfigApplied) {
            out.attribute(null, ATTR_DEVICE_PROVISIONING_CONFIG_APPLIED, Boolean.toString(true));
        }
        if (policy.mUserProvisioningState != DevicePolicyManager.STATE_USER_UNMANAGED) {
            out.attribute(null, ATTR_PROVISIONING_STATE, Integer.toString(policy.mUserProvisioningState));
        }
        if (policy.mPermissionPolicy != DevicePolicyManager.PERMISSION_POLICY_PROMPT) {
            out.attribute(null, ATTR_PERMISSION_POLICY, Integer.toString(policy.mPermissionPolicy));
        }
        if (policy.mDelegatedCertInstallerPackage != null) {
            out.attribute(null, ATTR_DELEGATED_CERT_INSTALLER, policy.mDelegatedCertInstallerPackage);
        }
        if (policy.mApplicationRestrictionsManagingPackage != null) {
            out.attribute(null, ATTR_APPLICATION_RESTRICTIONS_MANAGER, policy.mApplicationRestrictionsManagingPackage);
        }
        final int N = policy.mAdminList.size();
        for (int i = 0; i < N; i++) {
            ActiveAdmin ap = policy.mAdminList.get(i);
            if (ap != null) {
                out.startTag(null, "admin");
                out.attribute(null, "name", ap.info.getComponent().flattenToString());
                ap.writeToXml(out);
                out.endTag(null, "admin");
            }
        }
        if (policy.mPasswordOwner >= 0) {
            out.startTag(null, "password-owner");
            out.attribute(null, "value", Integer.toString(policy.mPasswordOwner));
            out.endTag(null, "password-owner");
        }
        if (policy.mFailedPasswordAttempts != 0) {
            out.startTag(null, "failed-password-attempts");
            out.attribute(null, "value", Integer.toString(policy.mFailedPasswordAttempts));
            out.endTag(null, "failed-password-attempts");
        }
        // Don't save metrics for FBE devices
        if (!mInjector.storageManagerIsFileBasedEncryptionEnabled() && (policy.mActivePasswordQuality != 0 || policy.mActivePasswordLength != 0 || policy.mActivePasswordUpperCase != 0 || policy.mActivePasswordLowerCase != 0 || policy.mActivePasswordLetters != 0 || policy.mActivePasswordNumeric != 0 || policy.mActivePasswordSymbols != 0 || policy.mActivePasswordNonLetter != 0)) {
            out.startTag(null, "active-password");
            out.attribute(null, "quality", Integer.toString(policy.mActivePasswordQuality));
            out.attribute(null, "length", Integer.toString(policy.mActivePasswordLength));
            out.attribute(null, "uppercase", Integer.toString(policy.mActivePasswordUpperCase));
            out.attribute(null, "lowercase", Integer.toString(policy.mActivePasswordLowerCase));
            out.attribute(null, "letters", Integer.toString(policy.mActivePasswordLetters));
            out.attribute(null, "numeric", Integer.toString(policy.mActivePasswordNumeric));
            out.attribute(null, "symbols", Integer.toString(policy.mActivePasswordSymbols));
            out.attribute(null, "nonletter", Integer.toString(policy.mActivePasswordNonLetter));
            out.endTag(null, "active-password");
        }
        for (int i = 0; i < policy.mAcceptedCaCertificates.size(); i++) {
            out.startTag(null, TAG_ACCEPTED_CA_CERTIFICATES);
            out.attribute(null, ATTR_NAME, policy.mAcceptedCaCertificates.valueAt(i));
            out.endTag(null, TAG_ACCEPTED_CA_CERTIFICATES);
        }
        for (int i = 0; i < policy.mLockTaskPackages.size(); i++) {
            String component = policy.mLockTaskPackages.get(i);
            out.startTag(null, TAG_LOCK_TASK_COMPONENTS);
            out.attribute(null, "name", component);
            out.endTag(null, TAG_LOCK_TASK_COMPONENTS);
        }
        if (policy.mStatusBarDisabled) {
            out.startTag(null, TAG_STATUS_BAR);
            out.attribute(null, ATTR_DISABLED, Boolean.toString(policy.mStatusBarDisabled));
            out.endTag(null, TAG_STATUS_BAR);
        }
        if (policy.doNotAskCredentialsOnBoot) {
            out.startTag(null, DO_NOT_ASK_CREDENTIALS_ON_BOOT_XML);
            out.endTag(null, DO_NOT_ASK_CREDENTIALS_ON_BOOT_XML);
        }
        for (String id : policy.mAffiliationIds) {
            out.startTag(null, TAG_AFFILIATION_ID);
            out.attribute(null, "id", id);
            out.endTag(null, TAG_AFFILIATION_ID);
        }
        if (policy.mAdminBroadcastPending) {
            out.startTag(null, TAG_ADMIN_BROADCAST_PENDING);
            out.attribute(null, ATTR_VALUE, Boolean.toString(policy.mAdminBroadcastPending));
            out.endTag(null, TAG_ADMIN_BROADCAST_PENDING);
        }
        if (policy.mInitBundle != null) {
            out.startTag(null, TAG_INITIALIZATION_BUNDLE);
            policy.mInitBundle.saveToXml(out);
            out.endTag(null, TAG_INITIALIZATION_BUNDLE);
        }
        out.endTag(null, "policies");
        out.endDocument();
        stream.flush();
        FileUtils.sync(stream);
        stream.close();
        journal.commit();
        sendChangedNotification(userHandle);
    } catch (XmlPullParserException | IOException e) {
        Slog.w(LOG_TAG, "failed writing file", e);
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException ex) {
        // Ignore
        }
        journal.rollback();
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) JournaledFile(com.android.internal.util.JournaledFile) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ParcelableString(com.android.internal.util.ParcelableString) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 35 with XmlSerializer

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

the class UserManagerService method writeUserListLP.

/*
     * Writes the user list file in this format:
     *
     * <users nextSerialNumber="3">
     *   <user id="0"></user>
     *   <user id="2"></user>
     * </users>
     */
private void writeUserListLP() {
    if (DBG) {
        debug("writeUserList");
    }
    FileOutputStream fos = null;
    AtomicFile userListFile = new AtomicFile(mUserListFile);
    try {
        fos = userListFile.startWrite();
        final BufferedOutputStream bos = new BufferedOutputStream(fos);
        // XmlSerializer serializer = XmlUtils.serializerInstance();
        final XmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(bos, StandardCharsets.UTF_8.name());
        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, TAG_USERS);
        serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
        serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
        serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
        synchronized (mGuestRestrictions) {
            UserRestrictionsUtils.writeRestrictions(serializer, mGuestRestrictions, TAG_RESTRICTIONS);
        }
        serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
        synchronized (mRestrictionsLock) {
            UserRestrictionsUtils.writeRestrictions(serializer, mDevicePolicyGlobalUserRestrictions, TAG_DEVICE_POLICY_RESTRICTIONS);
        }
        serializer.startTag(null, TAG_GLOBAL_RESTRICTION_OWNER_ID);
        serializer.attribute(null, ATTR_ID, Integer.toString(mGlobalRestrictionOwnerUserId));
        serializer.endTag(null, TAG_GLOBAL_RESTRICTION_OWNER_ID);
        int[] userIdsToWrite;
        synchronized (mUsersLock) {
            userIdsToWrite = new int[mUsers.size()];
            for (int i = 0; i < userIdsToWrite.length; i++) {
                UserInfo user = mUsers.valueAt(i).info;
                userIdsToWrite[i] = user.id;
            }
        }
        for (int id : userIdsToWrite) {
            serializer.startTag(null, TAG_USER);
            serializer.attribute(null, ATTR_ID, Integer.toString(id));
            serializer.endTag(null, TAG_USER);
        }
        serializer.endTag(null, TAG_USERS);
        serializer.endDocument();
        userListFile.finishWrite(fos);
    } catch (Exception e) {
        userListFile.failWrite(fos);
        Slog.e(LOG_TAG, "Error writing user list");
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) UserInfo(android.content.pm.UserInfo) BufferedOutputStream(java.io.BufferedOutputStream) ErrnoException(android.system.ErrnoException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

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