Search in sources :

Example 16 with START_TAG

use of org.xmlpull.v1.XmlPullParser.START_TAG in project platform_frameworks_base by android.

the class BridgeXmlBlockParser method nextText.

@Override
public String nextText() throws XmlPullParserException, IOException {
    if (getEventType() != START_TAG) {
        throw new XmlPullParserException(getPositionDescription() + ": parser must be on START_TAG to read next text", this, null);
    }
    int eventType = next();
    if (eventType == TEXT) {
        String result = getText();
        eventType = next();
        if (eventType != END_TAG) {
            throw new XmlPullParserException(getPositionDescription() + ": event TEXT it must be immediately followed by END_TAG", this, null);
        }
        return result;
    } else if (eventType == END_TAG) {
        return "";
    } else {
        throw new XmlPullParserException(getPositionDescription() + ": parser must be on START_TAG or TEXT to read text", this, null);
    }
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 17 with START_TAG

use of org.xmlpull.v1.XmlPullParser.START_TAG in project AndroidSDK-RecipeBook by gabu.

the class Recipe016 method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // テストデータ
    String xml = "<user id=\"1\" name=\"gabu\"><age>25</age></user>";
    // パーサのインスタンスを取得
    XmlPullParser parser = Xml.newPullParser();
    try {
        // パーサにXMLをセット
        parser.setInput(new StringReader(xml));
        // イベントタイプを初期化
        int eventType = parser.getEventType();
        // イベントタイプがEND_DOCUMENTになるまでループ
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch(eventType) {
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.END_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    // アトリビュートの数を取得
                    int count = parser.getAttributeCount();
                    // アトリビュートの数だけループ
                    for (int i = 0; i < count; i++) {
                        // アトリビュートの名前を取得
                        String name = parser.getAttributeName(i);
                        Log.d(TAG, "name=" + name);
                        // アトリビュートの値を取得
                        String value = parser.getAttributeValue(i);
                        Log.d(TAG, "value=" + value);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    break;
                case XmlPullParser.TEXT:
                    // START_TAG -> TEXT とイベントが発生する
                    // 今回の例だと<age>25</age>の25の部分が現在の位置
                    // この状態でgetTextメソッドでテキストを取得する
                    Log.d(TAG, "text=" + parser.getText());
                    break;
                default:
                    break;
            }
            // パーサを次のイベントまで進める
            eventType = parser.next();
        }
    } catch (XmlPullParserException e) {
        // when setInput()
        e.printStackTrace();
    } catch (IOException e) {
        // when next()
        e.printStackTrace();
    }
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 18 with START_TAG

use of org.xmlpull.v1.XmlPullParser.START_TAG in project Smack by igniterealtime.

the class PacketParserUtils method newXmppParser.

/**
     * Creates a new XmlPullParser suitable for parsing XMPP. This means in particular that
     * FEATURE_PROCESS_NAMESPACES is enabled.
     * <p>
     * Note that not all XmlPullParser implementations will return a String on
     * <code>getText()</code> if the parser is on START_TAG or END_TAG. So you must not rely on this
     * behavior when using the parser.
     * </p>
     * 
     * @return A suitable XmlPullParser for XMPP parsing
     * @throws XmlPullParserException
     */
public static XmlPullParser newXmppParser() throws XmlPullParserException {
    XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    if (XML_PULL_PARSER_SUPPORTS_ROUNDTRIP) {
        try {
            parser.setFeature(FEATURE_XML_ROUNDTRIP, true);
        } catch (XmlPullParserException e) {
            LOGGER.log(Level.SEVERE, "XmlPullParser does not support XML_ROUNDTRIP, although it was first determined to be supported", e);
        }
    }
    return parser;
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 19 with START_TAG

use of org.xmlpull.v1.XmlPullParser.START_TAG in project android_frameworks_base by ParanoidAndroid.

the class NotificationManagerService method readPolicy.

private int readPolicy(AtomicFile file, String lookUpTag, HashSet<String> db, String resultTag, int defaultResult) {
    int result = defaultResult;
    FileInputStream infile = null;
    try {
        infile = file.openRead();
        final XmlPullParser parser = Xml.newPullParser();
        parser.setInput(infile, null);
        int type;
        String tag;
        int version = DB_VERSION;
        while ((type = parser.next()) != END_DOCUMENT) {
            tag = parser.getName();
            if (type == START_TAG) {
                if (TAG_BODY.equals(tag)) {
                    version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
                    if (resultTag != null) {
                        String attribValue = parser.getAttributeValue(null, resultTag);
                        result = Integer.parseInt((attribValue != null ? attribValue : "0"));
                    }
                } else if (lookUpTag.equals(tag)) {
                    while ((type = parser.next()) != END_DOCUMENT) {
                        tag = parser.getName();
                        if (TAG_PACKAGE.equals(tag)) {
                            db.add(parser.getAttributeValue(null, ATTR_NAME));
                        } else if (lookUpTag.equals(tag) && type == END_TAG) {
                            break;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
    // Unable to read
    } finally {
        IoUtils.closeQuietly(infile);
    }
    return result;
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser) FileInputStream(java.io.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 20 with START_TAG

use of org.xmlpull.v1.XmlPullParser.START_TAG in project android_frameworks_base by ParanoidAndroid.

the class NetworkPolicyManagerService method readPolicyLocked.

private void readPolicyLocked() {
    if (LOGV)
        Slog.v(TAG, "readPolicyLocked()");
    // clear any existing policy and read from disk
    mNetworkPolicy.clear();
    mUidPolicy.clear();
    FileInputStream fis = null;
    try {
        fis = mPolicyFile.openRead();
        final XmlPullParser in = Xml.newPullParser();
        in.setInput(fis, null);
        int type;
        int version = VERSION_INIT;
        while ((type = in.next()) != END_DOCUMENT) {
            final String tag = in.getName();
            if (type == START_TAG) {
                if (TAG_POLICY_LIST.equals(tag)) {
                    version = readIntAttribute(in, ATTR_VERSION);
                    if (version >= VERSION_ADDED_RESTRICT_BACKGROUND) {
                        mRestrictBackground = readBooleanAttribute(in, ATTR_RESTRICT_BACKGROUND);
                    } else {
                        mRestrictBackground = false;
                    }
                } else if (TAG_NETWORK_POLICY.equals(tag)) {
                    final int networkTemplate = readIntAttribute(in, ATTR_NETWORK_TEMPLATE);
                    final String subscriberId = in.getAttributeValue(null, ATTR_SUBSCRIBER_ID);
                    final String networkId;
                    if (version >= VERSION_ADDED_NETWORK_ID) {
                        networkId = in.getAttributeValue(null, ATTR_NETWORK_ID);
                    } else {
                        networkId = null;
                    }
                    final int cycleDay = readIntAttribute(in, ATTR_CYCLE_DAY);
                    final String cycleTimezone;
                    if (version >= VERSION_ADDED_TIMEZONE) {
                        cycleTimezone = in.getAttributeValue(null, ATTR_CYCLE_TIMEZONE);
                    } else {
                        cycleTimezone = Time.TIMEZONE_UTC;
                    }
                    final long warningBytes = readLongAttribute(in, ATTR_WARNING_BYTES);
                    final long limitBytes = readLongAttribute(in, ATTR_LIMIT_BYTES);
                    final long lastLimitSnooze;
                    if (version >= VERSION_SPLIT_SNOOZE) {
                        lastLimitSnooze = readLongAttribute(in, ATTR_LAST_LIMIT_SNOOZE);
                    } else if (version >= VERSION_ADDED_SNOOZE) {
                        lastLimitSnooze = readLongAttribute(in, ATTR_LAST_SNOOZE);
                    } else {
                        lastLimitSnooze = SNOOZE_NEVER;
                    }
                    final boolean metered;
                    if (version >= VERSION_ADDED_METERED) {
                        metered = readBooleanAttribute(in, ATTR_METERED);
                    } else {
                        switch(networkTemplate) {
                            case MATCH_MOBILE_3G_LOWER:
                            case MATCH_MOBILE_4G:
                            case MATCH_MOBILE_ALL:
                                metered = true;
                                break;
                            default:
                                metered = false;
                        }
                    }
                    final long lastWarningSnooze;
                    if (version >= VERSION_SPLIT_SNOOZE) {
                        lastWarningSnooze = readLongAttribute(in, ATTR_LAST_WARNING_SNOOZE);
                    } else {
                        lastWarningSnooze = SNOOZE_NEVER;
                    }
                    final boolean inferred;
                    if (version >= VERSION_ADDED_INFERRED) {
                        inferred = readBooleanAttribute(in, ATTR_INFERRED);
                    } else {
                        inferred = false;
                    }
                    final NetworkTemplate template = new NetworkTemplate(networkTemplate, subscriberId, networkId);
                    mNetworkPolicy.put(template, new NetworkPolicy(template, cycleDay, cycleTimezone, warningBytes, limitBytes, lastWarningSnooze, lastLimitSnooze, metered, inferred));
                } else if (TAG_UID_POLICY.equals(tag)) {
                    final int uid = readIntAttribute(in, ATTR_UID);
                    final int policy = readIntAttribute(in, ATTR_POLICY);
                    if (UserHandle.isApp(uid)) {
                        setUidPolicyUnchecked(uid, policy, false);
                    } else {
                        Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring");
                    }
                } else if (TAG_APP_POLICY.equals(tag)) {
                    final int appId = readIntAttribute(in, ATTR_APP_ID);
                    final int policy = readIntAttribute(in, ATTR_POLICY);
                    // TODO: set for other users during upgrade
                    final int uid = UserHandle.getUid(UserHandle.USER_OWNER, appId);
                    if (UserHandle.isApp(uid)) {
                        setUidPolicyUnchecked(uid, policy, false);
                    } else {
                        Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring");
                    }
                }
            }
        }
    } catch (FileNotFoundException e) {
        // missing policy is okay, probably first boot
        upgradeLegacyBackgroundData();
    } catch (IOException e) {
        Log.wtf(TAG, "problem reading network policy", e);
    } catch (XmlPullParserException e) {
        Log.wtf(TAG, "problem reading network policy", e);
    } finally {
        IoUtils.closeQuietly(fis);
    }
}
Also used : NetworkTemplate(android.net.NetworkTemplate) NetworkPolicy(android.net.NetworkPolicy) XmlPullParser(org.xmlpull.v1.XmlPullParser) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

XmlPullParserException (org.xmlpull.v1.XmlPullParserException)41 XmlPullParser (org.xmlpull.v1.XmlPullParser)28 IOException (java.io.IOException)25 FileNotFoundException (java.io.FileNotFoundException)24 FileInputStream (java.io.FileInputStream)19 NetworkPolicy (android.net.NetworkPolicy)6 NetworkTemplate (android.net.NetworkTemplate)6 TaskDescription (android.app.ActivityManager.TaskDescription)5 PendingIntent (android.app.PendingIntent)5 Intent (android.content.Intent)5 ActivityInfo (android.content.pm.ActivityInfo)5 IPackageInstallerSession (android.content.pm.IPackageInstallerSession)5 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)5 PersistableBundle (android.os.PersistableBundle)5 VolumeRecord (android.os.storage.VolumeRecord)5 ArraySet (android.util.ArraySet)5 AtomicFile (android.util.AtomicFile)5 ReferrerIntent (com.android.internal.content.ReferrerIntent)5 BufferedReader (java.io.BufferedReader)5 File (java.io.File)5