Java Code Examples for org.xmlpull.v1.XmlPullParser#next()

The following examples show how to use org.xmlpull.v1.XmlPullParser#next() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlUtils.java    From Android-PreferencesManager with Apache License 2.0 6 votes vote down vote up
/**
 * Read a HashMap object from an XmlPullParser. The XML data could
 * previously have been generated by writeMapXml(). The XmlPullParser must
 * be positioned <em>after</em> the tag that begins the map.
 *
 * @param parser The XmlPullParser from which to read the map data.
 * @param endTag Name of the tag that will end the map, usually "map".
 * @param name   An array of one string, used to return the name attribute of
 *               the map's tag.
 * @return HashMap The newly generated map.
 * @see #readMapXml
 */
public static final HashMap readThisMapXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, java.io.IOException {
    HashMap map = new HashMap();

    int eventType = parser.getEventType();
    do {
        if (eventType == parser.START_TAG) {
            Object val = readThisValueXml(parser, name);
            if (name[0] != null) {
                // System.out.println("Adding to map: " + name + " -> " +
                // val);
                map.put(name[0], val);
            } else {
                throw new XmlPullParserException("Map value without name attribute: " + parser.getName());
            }
        } else if (eventType == parser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return map;
            }
            throw new XmlPullParserException("Expected " + endTag + " end tag at: " + parser.getName());
        }
        eventType = parser.next();
    } while (eventType != parser.END_DOCUMENT);

    throw new XmlPullParserException("Document ended before " + endTag + " end tag");
}
 
Example 2
Source File: XmlUtils.java    From HeadsUp with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Read an ArrayList object from an XmlPullParser.  The XML data could
 * previously have been generated by writeListXml().  The XmlPullParser
 * must be positioned <em>after</em> the tag that begins the list.
 *
 * @param parser The XmlPullParser from which to read the list data.
 * @param endTag Name of the tag that will end the list, usually "list".
 * @param name   An array of one string, used to return the name attribute
 *               of the list's tag.
 * @return HashMap The newly generated list.
 * @see #readListXml
 */
private static ArrayList readThisListXml(XmlPullParser parser, String endTag,
                                         String[] name, ReadMapCallback callback)
        throws XmlPullParserException, java.io.IOException {
    ArrayList list = new ArrayList();

    int eventType = parser.getEventType();
    do {
        if (eventType == XmlPullParser.START_TAG) {
            Object val = readThisValueXml(parser, name, callback);
            list.add(val);
            //System.out.println("Adding to list: " + val);
        } else if (eventType == XmlPullParser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return list;
            }
            throw new XmlPullParserException(
                    "Expected " + endTag + " end tag at: " + parser.getName());
        }
        eventType = parser.next();
    } while (eventType != XmlPullParser.END_DOCUMENT);

    throw new XmlPullParserException(
            "Document ended before " + endTag + " end tag");
}
 
Example 3
Source File: KmlFeatureParser.java    From android-maps-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new KmlLineString object
 *
 * @return KmlLineString object
 */
private static KmlLineString createLineString(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    ArrayList<LatLng> coordinates = new ArrayList<LatLng>();
    ArrayList<Double> altitudes = new ArrayList<Double>();
    int eventType = parser.getEventType();
    while (!(eventType == END_TAG && parser.getName().equals("LineString"))) {
        if (eventType == START_TAG && parser.getName().equals("coordinates")) {
            List<LatLngAlt> latLngAlts = convertToLatLngAltArray(parser.nextText());
            for (LatLngAlt latLngAlt : latLngAlts) {
                coordinates.add(latLngAlt.latLng);
                if (latLngAlt.altitude != null) {
                    altitudes.add(latLngAlt.altitude);
                }
            }
        }
        eventType = parser.next();
    }
    return new KmlLineString(coordinates, altitudes);
}
 
Example 4
Source File: XmlUtils.java    From JobSchedulerCompat with Apache License 2.0 6 votes vote down vote up
public static final void beginDocument(XmlPullParser parser, String firstElementName) throws XmlPullParserException, IOException
{
    int type;
    while ((type=parser.next()) != parser.START_TAG
               && type != parser.END_DOCUMENT) {
        ;
    }

    if (type != parser.START_TAG) {
        throw new XmlPullParserException("No start tag found");
    }

    if (!parser.getName().equals(firstElementName)) {
        throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
                ", expected " + firstElementName);
    }
}
 
Example 5
Source File: Tasks.java    From Spark with Apache License 2.0 6 votes vote down vote up
@Override
public PrivateData parsePrivateData(XmlPullParser parser) throws XmlPullParserException, IOException {
          boolean done = false;
          while (!done) {
              int eventType = parser.next();
              if (eventType == XmlPullParser.START_TAG && "tasks".equals(parser.getName())) {
                  String showAll = parser.getAttributeValue("", "showAll");
                  ScratchPadPlugin.SHOW_ALL_TASKS = Boolean.parseBoolean(showAll);
              }

              if (eventType == XmlPullParser.START_TAG && "task".equals(parser.getName())) {
                  tasks.addTask(getTask(parser));
              }
              else if (eventType == XmlPullParser.END_TAG) {
                  if ("scratchpad".equals(parser.getName())) {
                      done = true;
                  }
              }
          }


          return tasks;
      }
 
Example 6
Source File: XmlUtils.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
public static void skipEntry(XmlPullParser parser) throws Exception
{
    if (parser.getEventType() != XmlPullParser.START_TAG)
    {
        throw new IllegalStateException();
    }
    int depth = 1;
    while (depth != 0)
    {
        switch (parser.next())
        {
        case XmlPullParser.END_TAG:
            depth--;
            break;
        case XmlPullParser.START_TAG:
            depth++;
            break;
        }
    }
}
 
Example 7
Source File: PhonebookManager.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
  * xml-structure:
  * 
  * <book><entry><name>ABC</name><number>123</number></entry><entry>...</entry></book>
  * 
  * @param parser
  * @return
  * @throws Exception
  */
 private static PhoneNumber getBookEntry(XmlPullParser parser) throws Exception {
 	PhoneNumber entry = new PhoneNumber();

    // Check for Names
    boolean done = false;
    while (!done) {
        int eventType = parser.next();
        if (eventType == XmlPullParser.START_TAG && "name".equals(parser.getName())) {
       	 entry.setName(parser.nextText());
        }
        else if (eventType == XmlPullParser.START_TAG && "number".equals(parser.getName())) {
       	 entry.setNumber(parser.nextText());
        }
        else if (eventType == XmlPullParser.END_TAG && "entry".equals(parser.getName())) {
            done = true;
        }
    }

    return entry;
}
 
Example 8
Source File: DashManifestParser.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
protected SegmentList parseSegmentList(XmlPullParser xpp, SegmentList parent)
    throws XmlPullParserException, IOException {

  long timescale = parseLong(xpp, "timescale", parent != null ? parent.timescale : 1);
  long presentationTimeOffset = parseLong(xpp, "presentationTimeOffset",
      parent != null ? parent.presentationTimeOffset : 0);
  long duration = parseLong(xpp, "duration", parent != null ? parent.duration : C.TIME_UNSET);
  long startNumber = parseLong(xpp, "startNumber", parent != null ? parent.startNumber : 1);

  RangedUri initialization = null;
  List<SegmentTimelineElement> timeline = null;
  List<RangedUri> segments = null;

  do {
    xpp.next();
    if (XmlPullParserUtil.isStartTag(xpp, "Initialization")) {
      initialization = parseInitialization(xpp);
    } else if (XmlPullParserUtil.isStartTag(xpp, "SegmentTimeline")) {
      timeline = parseSegmentTimeline(xpp);
    } else if (XmlPullParserUtil.isStartTag(xpp, "SegmentURL")) {
      if (segments == null) {
        segments = new ArrayList<>();
      }
      segments.add(parseSegmentUrl(xpp));
    } else {
      maybeSkipTag(xpp);
    }
  } while (!XmlPullParserUtil.isEndTag(xpp, "SegmentList"));

  if (parent != null) {
    initialization = initialization != null ? initialization : parent.initialization;
    timeline = timeline != null ? timeline : parent.segmentTimeline;
    segments = segments != null ? segments : parent.mediaSegments;
  }

  return buildSegmentList(initialization, timescale, presentationTimeOffset,
      startNumber, duration, timeline, segments);
}
 
Example 9
Source File: KmlParser.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
private static Pair<String, String> readPair(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, NS, KmlFile.TAG_PAIR);
    String key = null, url = null;
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        switch (name) {
            case KmlFile.TAG_KEY:
                key = readTextElement(parser, KmlFile.TAG_KEY);
                break;
            case KmlFile.TAG_STYLE_URL:
                url = readTextElement(parser, KmlFile.TAG_STYLE_URL);
                break;
            default:
                skip(parser);
                break;
        }
    }
    parser.require(XmlPullParser.END_TAG, NS, KmlFile.TAG_PAIR);
    if (key == null || url == null) {
        throw new XmlPullParserException(KmlFile.TAG_PAIR + " should contain key and url", parser, null);
    }
    return new Pair<>(key, url);
}
 
Example 10
Source File: DynamicApkParser.java    From Android-plugin-support with MIT License 5 votes vote down vote up
private boolean parseAllMetaData(Resources res,
                                 XmlPullParser parser, AttributeSet attrs, String tag,
                                 Component outInfo, String[] outError)
        throws XmlPullParserException, IOException {
    int outerDepth = parser.getDepth();
    int type;
    while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG
            || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }

        if (parser.getName().equals("meta-data")) {
            if ((outInfo.metaData=parseMetaData(res, parser, attrs,
                    outInfo.metaData, outError)) == null) {
                return false;
            }
        } else {
            if (!RIGID_PARSER) {
                Log.w(TAG, "Unknown element under " + tag + ": "
                        + parser.getName() + " at " + mArchiveSourcePath + " "
                        + parser.getPositionDescription());
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else {
                outError[0] = "Bad element under " + tag + ": " + parser.getName();
                return false;
            }
        }
    }
    return true;
}
 
Example 11
Source File: DLNAEventParser.java    From Connect-SDK-Android-Core with Apache License 2.0 5 votes vote down vote up
private JSONObject readEvent(XmlPullParser parser) throws IOException, XmlPullParserException, JSONException {
    JSONObject event = new JSONObject();

    JSONArray instanceIDs = new JSONArray();
    JSONArray queueIDs = new JSONArray();

    parser.require(XmlPullParser.START_TAG, ns, "Event");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("InstanceID")) {
            instanceIDs.put(readInstanceID(parser));
        }
        else if (name.equals("QueueID")) {
            queueIDs.put(readQueueID(parser));
        }
        else {
            skip(parser);
        }
    }

    if (instanceIDs.length() > 0)
        event.put("InstanceID", instanceIDs);
    if (queueIDs.length() > 0)
        event.put("QueueID", queueIDs);

    return event;
}
 
Example 12
Source File: XmlUtils.java    From cwac-netsecurity with Apache License 2.0 5 votes vote down vote up
public static void skipCurrentTag(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    int outerDepth = parser.getDepth();
    int type;
    while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
           && (type != XmlPullParser.END_TAG
                   || parser.getDepth() > outerDepth)) {
    }
}
 
Example 13
Source File: MediaPresentationDescriptionParser.java    From Exoplayer_VLC with Apache License 2.0 5 votes vote down vote up
protected SingleSegmentBase parseSegmentBase(XmlPullParser xpp, Uri baseUrl,
    SingleSegmentBase parent) throws XmlPullParserException, IOException {

  long timescale = parseLong(xpp, "timescale", parent != null ? parent.timescale : 1);
  long presentationTimeOffset = parseLong(xpp, "presentationTimeOffset",
      parent != null ? parent.presentationTimeOffset : 0);

  long indexStart = parent != null ? parent.indexStart : 0;
  long indexLength = parent != null ? parent.indexLength : -1;
  String indexRangeText = xpp.getAttributeValue(null, "indexRange");
  if (indexRangeText != null) {
    String[] indexRange = indexRangeText.split("-");
    indexStart = Long.parseLong(indexRange[0]);
    indexLength = Long.parseLong(indexRange[1]) - indexStart + 1;
  }

  RangedUri initialization = parent != null ? parent.initialization : null;
  do {
    xpp.next();
    if (isStartTag(xpp, "Initialization")) {
      initialization = parseInitialization(xpp, baseUrl);
    }
  } while (!isEndTag(xpp, "SegmentBase"));

  return buildSingleSegmentBase(initialization, timescale, presentationTimeOffset, baseUrl,
      indexStart, indexLength);
}
 
Example 14
Source File: XmlUtils.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
public static boolean nextElementWithin(XmlPullParser parser, int outerDepth)
        throws IOException, XmlPullParserException {
    for (;;) {
        int type = parser.next();
        if (type == XmlPullParser.END_DOCUMENT
                || (type == XmlPullParser.END_TAG && parser.getDepth() == outerDepth)) {
            return false;
        }
        if (type == XmlPullParser.START_TAG
                && parser.getDepth() == outerDepth + 1) {
            return true;
        }
    }
}
 
Example 15
Source File: MenuInflater.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Called internally to fill the given menu. If a sub menu is seen, it will
 * call this recursively.
 */
private void parseMenu(XmlPullParser parser, AttributeSet attrs, Menu menu)
        throws XmlPullParserException, IOException {
    MenuState menuState = new MenuState(menu);

    int eventType = parser.getEventType();
    String tagName;
    boolean lookingForEndOfUnknownTag = false;
    String unknownTagName = null;

    // This loop will skip to the menu start tag
    do {
        if (eventType == XmlPullParser.START_TAG) {
            tagName = parser.getName();
            if (tagName.equals(XML_MENU)) {
                // Go to next tag
                eventType = parser.next();
                break;
            }

            throw new RuntimeException("Expecting menu, got " + tagName);
        }
        eventType = parser.next();
    } while (eventType != XmlPullParser.END_DOCUMENT);

    boolean reachedEndOfMenu = false;
    while (!reachedEndOfMenu) {
        switch (eventType) {
            case XmlPullParser.START_TAG:
                if (lookingForEndOfUnknownTag) {
                    break;
                }

                tagName = parser.getName();
                if (tagName.equals(XML_GROUP)) {
                    menuState.readGroup(attrs);
                } else if (tagName.equals(XML_ITEM)) {
                    menuState.readItem(attrs);
                } else if (tagName.equals(XML_MENU)) {
                    // A menu start tag denotes a submenu for an item
                    SubMenu subMenu = menuState.addSubMenuItem();

                    // Parse the submenu into returned SubMenu
                    parseMenu(parser, attrs, subMenu);
                } else {
                    lookingForEndOfUnknownTag = true;
                    unknownTagName = tagName;
                }
                break;

            case XmlPullParser.END_TAG:
                tagName = parser.getName();
                if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) {
                    lookingForEndOfUnknownTag = false;
                    unknownTagName = null;
                } else if (tagName.equals(XML_GROUP)) {
                    menuState.resetGroup();
                } else if (tagName.equals(XML_ITEM)) {
                    // Add the item if it hasn't been added (if the item was
                    // a submenu, it would have been added already)
                    if (!menuState.hasAddedItem()) {
                        if (menuState.itemActionProvider != null &&
                                menuState.itemActionProvider.hasSubMenu()) {
                            menuState.addSubMenuItem();
                        } else {
                            menuState.addItem();
                        }
                    }
                } else if (tagName.equals(XML_MENU)) {
                    reachedEndOfMenu = true;
                }
                break;

            case XmlPullParser.END_DOCUMENT:
                throw new RuntimeException("Unexpected end of document");
        }

        eventType = parser.next();
    }
}
 
Example 16
Source File: FloatingSearchView.java    From FloatingSearchView with Apache License 2.0 4 votes vote down vote up
private void parseMenu(XmlPullParser parser, AttributeSet attrs)
        throws XmlPullParserException, IOException {

    int eventType = parser.getEventType();
    String tagName;
    boolean lookingForEndOfUnknownTag = false;
    String unknownTagName = null;

    // This loop will skip to the menu start tag
    do {
        if (eventType == XmlPullParser.START_TAG) {
            tagName = parser.getName();
            if (tagName.equals("menu")) {
                // Go to next tag
                eventType = parser.next();
                break;
            }

            throw new RuntimeException("Expecting menu, got " + tagName);
        }
        eventType = parser.next();
    } while (eventType != XmlPullParser.END_DOCUMENT);

    boolean reachedEndOfMenu = false;

    while (!reachedEndOfMenu) {
        switch (eventType) {
            case XmlPullParser.START_TAG:
                if (lookingForEndOfUnknownTag) {
                    break;
                }

                tagName = parser.getName();
                if (tagName.equals("item")) {
                    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MenuItem);
                    int itemShowAsAction = a.getInt(R.styleable.MenuItem_showAsAction, -1);

                    if((itemShowAsAction & MenuItem.SHOW_AS_ACTION_ALWAYS) != 0) {
                        int itemId = a.getResourceId(R.styleable.MenuItem_android_id, NO_ID);
                        if(itemId != NO_ID) mAlwaysShowingMenu.add(itemId);
                    }
                    a.recycle();
                } else {
                    lookingForEndOfUnknownTag = true;
                    unknownTagName = tagName;
                }
                break;

            case XmlPullParser.END_TAG:
                tagName = parser.getName();
                if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) {
                    lookingForEndOfUnknownTag = false;
                    unknownTagName = null;
                } else if (tagName.equals("menu")) {
                    reachedEndOfMenu = true;
                }
                break;

            case XmlPullParser.END_DOCUMENT:
                throw new RuntimeException("Unexpected end of document");
        }

        eventType = parser.next();
    }
}
 
Example 17
Source File: PreferredComponent.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public PreferredComponent(Callbacks callbacks, XmlPullParser parser)
        throws XmlPullParserException, IOException {
    mCallbacks = callbacks;
    mShortComponent = parser.getAttributeValue(null, ATTR_NAME);
    mComponent = ComponentName.unflattenFromString(mShortComponent);
    if (mComponent == null) {
        mParseError = "Bad activity name " + mShortComponent;
    }
    String matchStr = parser.getAttributeValue(null, ATTR_MATCH);
    mMatch = matchStr != null ? Integer.parseInt(matchStr, 16) : 0;
    String setCountStr = parser.getAttributeValue(null, ATTR_SET);
    int setCount = setCountStr != null ? Integer.parseInt(setCountStr) : 0;
    String alwaysStr = parser.getAttributeValue(null, ATTR_ALWAYS);
    mAlways = alwaysStr != null ? Boolean.parseBoolean(alwaysStr) : true;

    String[] myPackages = setCount > 0 ? new String[setCount] : null;
    String[] myClasses = setCount > 0 ? new String[setCount] : null;
    String[] myComponents = setCount > 0 ? new String[setCount] : null;

    int setPos = 0;

    int outerDepth = parser.getDepth();
    int type;
    while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
           && (type != XmlPullParser.END_TAG
                   || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG
                || type == XmlPullParser.TEXT) {
            continue;
        }

        String tagName = parser.getName();
        //Log.i(TAG, "Parse outerDepth=" + outerDepth + " depth="
        //        + parser.getDepth() + " tag=" + tagName);
        if (tagName.equals(TAG_SET)) {
            String name = parser.getAttributeValue(null, ATTR_NAME);
            if (name == null) {
                if (mParseError == null) {
                    mParseError = "No name in set tag in preferred activity "
                        + mShortComponent;
                }
            } else if (setPos >= setCount) {
                if (mParseError == null) {
                    mParseError = "Too many set tags in preferred activity "
                        + mShortComponent;
                }
            } else {
                ComponentName cn = ComponentName.unflattenFromString(name);
                if (cn == null) {
                    if (mParseError == null) {
                        mParseError = "Bad set name " + name + " in preferred activity "
                            + mShortComponent;
                    }
                } else {
                    myPackages[setPos] = cn.getPackageName();
                    myClasses[setPos] = cn.getClassName();
                    myComponents[setPos] = name;
                    setPos++;
                }
            }
            XmlUtils.skipCurrentTag(parser);
        } else if (!mCallbacks.onReadTag(tagName, parser)) {
            Slog.w("PreferredComponent", "Unknown element: " + parser.getName());
            XmlUtils.skipCurrentTag(parser);
        }
    }

    if (setPos != setCount) {
        if (mParseError == null) {
            mParseError = "Not enough set tags (expected " + setCount
                + " but found " + setPos + ") in " + mShortComponent;
        }
    }

    mSetPackages = myPackages;
    mSetClasses = myClasses;
    mSetComponents = myComponents;
}
 
Example 18
Source File: ShortcutPackageInfo.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public void loadFromXml(XmlPullParser parser, boolean fromBackup)
        throws IOException, XmlPullParserException {
    // Don't use the version code from the backup file.
    final long versionCode = ShortcutService.parseLongAttribute(parser, ATTR_VERSION,
            ShortcutInfo.VERSION_CODE_UNKNOWN);

    final long lastUpdateTime = ShortcutService.parseLongAttribute(
            parser, ATTR_LAST_UPDATE_TIME);

    // When restoring from backup, it's always shadow.
    final boolean shadow =
            fromBackup || ShortcutService.parseBooleanAttribute(parser, ATTR_SHADOW);

    // We didn't used to save these attributes, and all backed up shortcuts were from
    // apps that support backups, so the default values take this fact into consideration.
    final long backupSourceVersion = ShortcutService.parseLongAttribute(parser,
            ATTR_BACKUP_SOURCE_VERSION, ShortcutInfo.VERSION_CODE_UNKNOWN);

    // Note the only time these "true" default value is used is when restoring from an old
    // build that didn't save ATTR_BACKUP_ALLOWED, and that means all the data included in
    // a backup file were from apps that support backup, so we can just use "true" as the
    // default.
    final boolean backupAllowed = ShortcutService.parseBooleanAttribute(
            parser, ATTR_BACKUP_ALLOWED, true);
    final boolean backupSourceBackupAllowed = ShortcutService.parseBooleanAttribute(
            parser, ATTR_BACKUP_SOURCE_BACKUP_ALLOWED, true);

    final ArrayList<byte[]> hashes = new ArrayList<>();

    final int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        final int depth = parser.getDepth();
        final String tag = parser.getName();

        if (depth == outerDepth + 1) {
            switch (tag) {
                case TAG_SIGNATURE: {
                    final String hash = ShortcutService.parseStringAttribute(
                            parser, ATTR_SIGNATURE_HASH);
                    // Throws IllegalArgumentException if hash is invalid base64 data
                    final byte[] decoded = Base64.getDecoder().decode(hash);
                    hashes.add(decoded);
                    continue;
                }
            }
        }
        ShortcutService.warnForInvalidTag(depth, tag);
    }

    // Successfully loaded; replace the fields.
    if (fromBackup) {
        mVersionCode = ShortcutInfo.VERSION_CODE_UNKNOWN;
        mBackupSourceVersionCode = versionCode;
        mBackupSourceBackupAllowed = backupAllowed;
    } else {
        mVersionCode = versionCode;
        mBackupSourceVersionCode = backupSourceVersion;
        mBackupSourceBackupAllowed = backupSourceBackupAllowed;
    }
    mLastUpdateTime = lastUpdateTime;
    mIsShadow = shadow;
    mSigHashes = hashes;

    // Note we don't restore it from the file because it didn't used to be saved.
    // We always start by assuming backup is disabled for the current package,
    // and this field will have been updated before we actually create a backup, at the same
    // time when we update the version code.
    // Until then, the value of mBackupAllowed shouldn't matter, but we don't want to print
    // a false flag on dumpsys, so set mBackupAllowedInitialized to false.
    mBackupAllowed = false;
    mBackupAllowedInitialized = false;
}
 
Example 19
Source File: CumulusXmlParser.java    From CumulusTV with MIT License 4 votes vote down vote up
private static Channel parseChannel(XmlPullParser parser)
        throws IOException, XmlPullParserException, ParseException {
    String id = null;
    boolean repeatPrograms = false;
    for (int i = 0; i < parser.getAttributeCount(); ++i) {
        String attr = parser.getAttributeName(i);
        String value = parser.getAttributeValue(i);
        if (ATTR_ID.equalsIgnoreCase(attr)) {
            id = value;
        } else if (ATTR_REPEAT_PROGRAMS.equalsIgnoreCase(attr)) {
            repeatPrograms = "TRUE".equalsIgnoreCase(value);
        }
    }
    String displayName = null;
    String displayNumber = null;
    XmlTvIcon icon = null;
    XmlTvAppLink appLink = null;
    Advertisement advertisement = null;
    while (parser.next() != XmlPullParser.END_DOCUMENT) {
        if (parser.getEventType() == XmlPullParser.START_TAG) {
            if (TAG_DISPLAY_NAME.equalsIgnoreCase(parser.getName())
                    && displayName == null) {
                displayName = parser.nextText();
            } else if (TAG_DISPLAY_NUMBER.equalsIgnoreCase(parser.getName())
                    && displayNumber == null) {
                displayNumber = parser.nextText();
            } else if (TAG_ICON.equalsIgnoreCase(parser.getName()) && icon == null) {
                icon = parseIcon(parser);
            } else if (TAG_APP_LINK.equalsIgnoreCase(parser.getName()) && appLink == null) {
                appLink = parseAppLink(parser);
            } else if (TAG_AD.equalsIgnoreCase(parser.getName()) && advertisement == null) {
                advertisement = parseAd(parser, TAG_CHANNEL);
            }
        } else if (TAG_CHANNEL.equalsIgnoreCase(parser.getName())
                && parser.getEventType() == XmlPullParser.END_TAG) {
            break;
        }
    }
    if (TextUtils.isEmpty(id) || TextUtils.isEmpty(displayName)) {
        throw new IllegalArgumentException("id and display-name can not be null.");
    }

    // Developers should assign original network ID in the right way not using the fake ID.
    InternalProviderData internalProviderData = new InternalProviderData();
    internalProviderData.setRepeatable(repeatPrograms);
    Channel.Builder builder = new Channel.Builder()
            .setDisplayName(displayName)
            .setDisplayNumber(displayNumber)
            .setOriginalNetworkId(id.hashCode())
            .setInternalProviderData(internalProviderData)
            .setTransportStreamId(0)
            .setServiceId(0);
    if (icon != null) {
        builder.setChannelLogo(icon.src);
    }
    if (appLink != null) {
        builder.setAppLinkColor(appLink.color)
                .setAppLinkIconUri(appLink.icon.src)
                .setAppLinkIntentUri(appLink.intentUri)
                .setAppLinkPosterArtUri(appLink.posterUri)
                .setAppLinkText(appLink.text);
    }
    if (advertisement != null) {
        List<Advertisement> advertisements = new ArrayList<>(1);
        advertisements.add(advertisement);
        internalProviderData.setAds(advertisements);
        builder.setInternalProviderData(internalProviderData);
    }
    return builder.build();
}
 
Example 20
Source File: XmlParseUtils.java    From openboard with GNU General Public License v3.0 4 votes vote down vote up
public static void checkEndTag(final String tag, final XmlPullParser parser)
        throws XmlPullParserException, IOException {
    if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName()))
        return;
    throw new NonEmptyTag(parser, tag);
}