Java Code Examples for org.xmlpull.v1.XmlPullParser

The following examples show how to use org.xmlpull.v1.XmlPullParser. These examples are extracted from open source projects. 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 Project: android_9.0.0_r45   Source File: PackageStatusStorage.java    License: Apache License 2.0 6 votes vote down vote up
@GuardedBy("this")
private PackageStatus getPackageStatusLocked() throws ParseException {
    try (FileInputStream fis = mPackageStatusFile.openRead()) {
        XmlPullParser parser = parseToPackageStatusTag(fis);
        Integer checkStatus = getNullableIntAttribute(parser, ATTRIBUTE_CHECK_STATUS);
        if (checkStatus == null) {
            return null;
        }
        int updateAppVersion = getIntAttribute(parser, ATTRIBUTE_UPDATE_APP_VERSION);
        int dataAppVersion = getIntAttribute(parser, ATTRIBUTE_DATA_APP_VERSION);
        return new PackageStatus(checkStatus,
                new PackageVersions(updateAppVersion, dataAppVersion));
    } catch (IOException e) {
        ParseException e2 = new ParseException("Error reading package status", 0);
        e2.initCause(e);
        throw e2;
    }
}
 
Example 2
Source Project: HeadsUp   Source File: XmlUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void beginDocument(XmlPullParser parser, String firstElementName)
        throws XmlPullParserException, IOException {
    int type;
    //noinspection StatementWithEmptyBody
    while ((type = parser.next()) != XmlPullParser.START_TAG
            && type != XmlPullParser.END_DOCUMENT) ;

    if (type != XmlPullParser.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 3
Source Project: ssj   Source File: SimpleXmlParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param in               InputStream
 * @param searchPath       String[]
 * @param searchAttributes String[]
 * @return XmlValues
 * @throws XmlPullParserException
 * @throws IOException
 */
public XmlValues parse(InputStream in, String[] searchPath, String[] searchAttributes) throws XmlPullParserException, IOException
{
    this.searchPath = searchPath;
    this.searchAttributes = searchAttributes;
    xmlValues = new XmlValues();
    try
    {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        search(parser, 0);
        return xmlValues;
    } finally
    {
        in.close();
    }
}
 
Example 4
Source Project: ProjectX   Source File: FontsReaderApi21.java    License: Apache License 2.0 6 votes vote down vote up
protected void startAlias(XmlPullParser parser) {
    // alias
    final String name = parser.getAttributeValue(null, ATTR_NAME);
    if (TextUtils.isEmpty(name))
        return;
    final String to = parser.getAttributeValue(null, ATTR_TO);
    if (TextUtils.isEmpty(to))
        return;
    int weight;
    try {
        weight = Integer.parseInt(parser.getAttributeValue(null, ATTR_WEIGHT));
    } catch (Exception e) {
        weight = -1;
    }
    mAlias = new Alias(name, to, weight);
}
 
Example 5
/**
 * The purpose of this test is to map an XML with a {@link Collection} of {@link Integer} objects.
 */
@Test
public void testParseCollectionTypeInteger() throws Exception {
  CollectionTypeIntegerGeneric xml = new CollectionTypeIntegerGeneric();
  XmlPullParser parser = Xml.createParser();
  parser.setInput(new StringReader(MULTIPLE_INTEGER_ELEMENT));
  XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
  Xml.parseElement(parser, xml, namespaceDictionary, null);
  // check type
  assertEquals(2, xml.rep.size());
  assertEquals(1, xml.rep.toArray(new Integer[] {})[0].intValue());
  assertEquals(2, xml.rep.toArray(new Integer[] {})[1].intValue());
  // serialize
  XmlSerializer serializer = Xml.createSerializer();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  serializer.setOutput(out, "UTF-8");
  namespaceDictionary.serialize(serializer, "any", xml);
  assertEquals(MULTIPLE_INTEGER_ELEMENT, out.toString());
}
 
Example 6
/**
 * Process aspect definition
 * 
 * @param xpp XmlPullParser
 * @param aspectDef AspectDefinition
 * @param parserContext ParserContext
 * @throws XmlPullParserException
 * @throws IOException
 */
private void processAspect(XmlPullParser xpp, AspectDefinition aspectDef, ParserContext parserContext)
    throws XmlPullParserException, IOException
{
    NodeContext node = peekNodeContext(parserContext.elementStack);
    node.addAspect(aspectDef);
    
    int eventType = xpp.next();
    if (eventType != XmlPullParser.END_TAG)
    {
        throw new ImporterException("Aspect " + aspectDef.getName() + " definition is not valid - it cannot contain any elements");
    }
    
    if (logger.isDebugEnabled())
        logger.debug(indentLog("Processed aspect " + aspectDef.getName(), parserContext.elementStack.size()));
}
 
Example 7
private void parseKeyboardLayoutSetElement(final XmlPullParser parser)
        throws XmlPullParserException, IOException {
    final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
            R.styleable.KeyboardLayoutSet_Element);
    try {
        XmlParseUtils.checkAttributeExists(a,
                R.styleable.KeyboardLayoutSet_Element_elementName, "elementName",
                TAG_ELEMENT, parser);
        XmlParseUtils.checkAttributeExists(a,
                R.styleable.KeyboardLayoutSet_Element_elementKeyboard, "elementKeyboard",
                TAG_ELEMENT, parser);
        XmlParseUtils.checkEndTag(TAG_ELEMENT, parser);

        final ElementParams elementParams = new ElementParams();
        final int elementName = a.getInt(
                R.styleable.KeyboardLayoutSet_Element_elementName, 0);
        elementParams.mKeyboardXmlId = a.getResourceId(
                R.styleable.KeyboardLayoutSet_Element_elementKeyboard, 0);
        elementParams.mAllowRedundantMoreKeys = a.getBoolean(
                R.styleable.KeyboardLayoutSet_Element_allowRedundantMoreKeys, true);
        mParams.mKeyboardLayoutSetElementIdToParamsMap.put(elementName, elementParams);
    } finally {
        a.recycle();
    }
}
 
Example 8
Source Project: ChannelSurfer   Source File: XMLTVParser.java    License: MIT License 6 votes vote down vote up
public static List<Program> parse(String in) throws XmlPullParserException, IOException {
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            Log.d(TAG, "Start parsing");
            Log.d(TAG, in.substring(0, 36));
            xpp.setInput(new StringReader(in));
            int eventType = xpp.getEventType();
            Log.d(TAG, eventType+"");
//            if(eventType == XmlPullParser.START_DOCUMENT)
//                xpp.next();
            /*
            xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            xpp.setInput(new InputStreamReader(in));*/
            /*xpp.nextTag();
            xpp.nextTag();
            */return readFeed(xpp);
        } finally {
//            in.close();
        }
    }
 
Example 9
/**
 *
 */
private static void skip(XmlPullParser p)
  throws IOException, XmlPullParserException
{
  int level = 0;
  int event = p.getEventType();

  while (event != XmlPullParser.END_DOCUMENT) {
    if (event == XmlPullParser.START_TAG) {
      level++;
    } else if (event == XmlPullParser.END_TAG) {
      level--;
      if (level <= 0) {
        p.next(); // jump beyond stopping tag.
        break;
      }
    }
    event = p.next();
  }
}
 
Example 10
Source Project: ProjectX   Source File: FontsReaderApi26.java    License: Apache License 2.0 6 votes vote down vote up
protected void startAxis(XmlPullParser parser) {
    // axis
    final String tag = parser.getAttributeValue(null, ATTR_TAG);
    if (TextUtils.isEmpty(tag))
        return;
    float value;
    try {
        value = Float.parseFloat(
                parser.getAttributeValue(null, ATTR_STYLEVALUE));
    } catch (Exception e) {
        return;
    }
    if (Axis.TAG_ITAL.equals(tag) || Axis.TAG_OPSZ.equals(tag) ||
            Axis.TAG_SLNT.equals(tag) || Axis.TAG_WDTH.equals(tag) ||
            Axis.TAG_WGHT.equals(tag))
        mAxis = new Axis(tag, value);
}
 
Example 11
Source Project: react-native-3d-model-view   Source File: XmlParser.java    License: MIT License 6 votes vote down vote up
private static void loadNode(XmlPullParser xpp, XmlNode parentNode) throws XmlPullParserException, IOException {
	int eventType = xpp.next();
	while(eventType != XmlPullParser.END_DOCUMENT) {
		if (eventType == XmlPullParser.START_TAG) {
			XmlNode childNode = new XmlNode(xpp.getName());
			for (int i=0; i<xpp.getAttributeCount(); i++){
				childNode.addAttribute(xpp.getAttributeName(i), xpp.getAttributeValue(i));
			}
			parentNode.addChild(childNode);
			loadNode(xpp, childNode);
		} else if (eventType == XmlPullParser.END_TAG) {
			return;
		} else if (eventType == XmlPullParser.TEXT) {
			parentNode.setData(xpp.getText());
		}
		eventType = xpp.next();
	}
}
 
Example 12
Source Project: cwac-netsecurity   Source File: XmlUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Read a HashSet object from an XmlPullParser. The XML data could previously
 * have been generated by writeSetXml(). The XmlPullParser must be positioned
 * <em>after</em> the tag that begins the set.
 * 
 * @param parser The XmlPullParser from which to read the set data.
 * @param endTag Name of the tag that will end the set, usually "set".
 * @param name An array of one string, used to return the name attribute
 *             of the set's tag.
 *
 * @return HashSet The newly generated set.
 * 
 * @throws XmlPullParserException
 * @throws IOException
 * 
 * @see #readSetXml
 * @hide
 */
private static final HashSet readThisSetXml(XmlPullParser parser, String endTag, String[] name,
        ReadMapCallback callback, boolean arrayMap)
        throws XmlPullParserException, IOException {
    HashSet set = new HashSet();
    
    int eventType = parser.getEventType();
    do {
        if (eventType == parser.START_TAG) {
            Object val = readThisValueXml(parser, name, callback, arrayMap);
            set.add(val);
            //System.out.println("Adding to set: " + val);
        } else if (eventType == parser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return set;
            }
            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 13
Source Project: TowerCollector   Source File: XmlUtils.java    License: Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Read a flattened object from an XmlPullParser.  The XML data could
 * previously have been written with writeMapXml(), writeListXml(), or
 * writeValueXml().  The XmlPullParser must be positioned <em>at</em> the
 * tag that defines the value.
 *
 * @param parser The XmlPullParser from which to read the object.
 * @param name An array of one string, used to return the name attribute
 *             of the value's tag.
 *
 * @return Object The newly generated value object.
 *
 * @see #readMapXml
 * @see #readListXml
 * @see #writeValueXml
 */
public static final Object readValueXml(XmlPullParser parser, String[] name)
        throws XmlPullParserException, java.io.IOException
{
    int eventType = parser.getEventType();
    do {
        if (eventType == parser.START_TAG) {
            return readThisValueXml(parser, name);
        } else if (eventType == parser.END_TAG) {
            throw new XmlPullParserException(
                    "Unexpected end tag at: " + parser.getName());
        } else if (eventType == parser.TEXT) {
            throw new XmlPullParserException(
                    "Unexpected text: " + parser.getText());
        }
        eventType = parser.next();
    } while (eventType != parser.END_DOCUMENT);

    throw new XmlPullParserException(
            "Unexpected end of document");
}
 
Example 14
Source Project: android-gpx-parser   Source File: GPXParser.java    License: Apache License 2.0 6 votes vote down vote up
private Copyright readCopyright(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, namespace, TAG_COPYRIGHT);

    Copyright.Builder copyrightBuilder = new Copyright.Builder();
    copyrightBuilder.setAuthor(parser.getAttributeValue(namespace, TAG_AUTHOR));

    while (loopMustContinue(parser.next())) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        switch (name) {
            case TAG_YEAR:
                copyrightBuilder.setYear(readYear(parser));
                break;
            case TAG_LICENSE:
                copyrightBuilder.setLicense(readString(parser, TAG_LICENSE));
                break;
            default:
                skip(parser);
                break;
        }
    }
    parser.require(XmlPullParser.END_TAG, namespace, TAG_COPYRIGHT);
    return copyrightBuilder.build();
}
 
Example 15
Source Project: directory-ldap-api   Source File: Dsmlv2ResponseParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Processes the task required in the grammar to the given tag type
 *
 * @param container the DSML container
 * @param tagType the tag type
 * @throws XmlPullParserException when an error occurs during the parsing
 */
private static void processTag( Dsmlv2Container container, int tagType ) throws XmlPullParserException
{
    XmlPullParser xpp = container.getParser();

    String tagName = Strings.lowerCase( xpp.getName() );

    GrammarTransition transition = container.getTransition( container.getState(), new Tag( tagName, tagType ) );

    if ( transition != null )
    {
        container.setState( transition.getNextState() );

        if ( transition.hasAction() )
        {
            transition.getAction().action( container );
        }
    }
    else
    {
        throw new XmlPullParserException( I18n.err( I18n.ERR_03036_MISSING_TAG, new Tag( tagName, tagType ) ), xpp, null );
    }
}
 
Example 16
public static List<WifiNetwork> parse(InputStream in)
        throws XmlPullParserException, IOException {
    List<WifiNetwork> wifiNetworks = new ArrayList<>();

    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        while (parser.next() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            if (parser.getName().equalsIgnoreCase("NetworkList")) {
                wifiNetworks.addAll(readNetworkList(parser));
            }
        }
        return wifiNetworks;
    } finally {
        in.close();
    }
}
 
Example 17
public void readFromXml(XmlPullParser parser)
{
    String attr = parser.getAttributeValue(null, XML_PROP_DISABLE_CALCULATION);
    if (attr != null)
    {
        disableCalculation = Boolean.parseBoolean(attr);
    }
    attr = parser.getAttributeValue(null, XML_PROP_HIDE_RESULT_FIELD);
    if (attr != null)
    {
        hideResultField = Boolean.parseBoolean(attr);
    }
    attr = parser.getAttributeValue(null, XML_PROP_ARRAY_LENGTH);
    if (attr != null)
    {
        arrayLength = Integer.parseInt(attr);
    }
    attr = parser.getAttributeValue(null, XML_PROP_UNITS);
    if (attr != null)
    {
        units = attr;
    }
}
 
Example 18
Source Project: HeadsUp   Source File: XmlUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static float readFloatAttribute(XmlPullParser in, String name) throws IOException {
    final String value = in.getAttributeValue(null, name);
    try {
        return Float.parseFloat(value);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing " + name + "=" + value + " as long");
    }
}
 
Example 19
Source Project: cwac-netsecurity   Source File: XmlUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Bitmap readBitmapAttribute(XmlPullParser in, String name) {
    final byte[] value = readByteArrayAttribute(in, name);
    if (value != null) {
        return BitmapFactory.decodeByteArray(value, 0, value.length);
    } else {
        return null;
    }
}
 
Example 20
private int parseType(XmlPullParser parser) throws ParserException {
  String value = parser.getAttributeValue(null, KEY_TYPE);
  if (value != null) {
    if (KEY_TYPE_AUDIO.equalsIgnoreCase(value)) {
      return StreamElement.TYPE_AUDIO;
    } else if (KEY_TYPE_VIDEO.equalsIgnoreCase(value)) {
      return StreamElement.TYPE_VIDEO;
    } else if (KEY_TYPE_TEXT.equalsIgnoreCase(value)) {
      return StreamElement.TYPE_TEXT;
    } else {
      throw new ParserException("Invalid key value[" + value + "]");
    }
  }
  throw new MissingFieldException(KEY_TYPE);
}
 
Example 21
Source Project: ticdesign   Source File: XmlUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Bitmap readBitmapAttribute(XmlPullParser in, String name) {
    final byte[] value = readByteArrayAttribute(in, name);
    if (value != null) {
        return BitmapFactory.decodeByteArray(value, 0, value.length);
    } else {
        return null;
    }
}
 
Example 22
Source Project: android_9.0.0_r45   Source File: WallpaperManagerService.java    License: Apache License 2.0 5 votes vote down vote up
private int getAttributeInt(XmlPullParser parser, String name, int defValue) {
    String value = parser.getAttributeValue(null, name);
    if (value == null) {
        return defValue;
    }
    return Integer.parseInt(value);
}
 
Example 23
Source Project: org.hl7.fhir.core   Source File: XmlParserBase.java    License: Apache License 2.0 5 votes vote down vote up
private String parseString(XmlPullParser xpp) throws XmlPullParserException, FHIRFormatError, IOException {
	StringBuilder res = new StringBuilder();
	next(xpp);
	while (xpp.getEventType() == XmlPullParser.TEXT || xpp.getEventType() == XmlPullParser.IGNORABLE_WHITESPACE || xpp.getEventType() == XmlPullParser.ENTITY_REF) {
		res.append(xpp.getText());
		next(xpp);
	}
	if (xpp.getEventType() != XmlPullParser.END_TAG)
		throw new FHIRFormatError("Bad String Structure - parsed "+res.toString()+" now found "+Integer.toString(xpp.getEventType()));
	next(xpp);
	return res.length() == 0 ? null : res.toString();
}
 
Example 24
Source Project: AndroidPNClient   Source File: PacketParserUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static Authentication parseAuthentication(XmlPullParser parser) throws Exception {
    Authentication authentication = new Authentication();
    boolean done = false;
    while (!done) {
        int eventType = parser.next();
        if (eventType == XmlPullParser.START_TAG) {
            if (parser.getName().equals("username")) {
                authentication.setUsername(parser.nextText());
            }
            else if (parser.getName().equals("password")) {
                authentication.setPassword(parser.nextText());
            }
            else if (parser.getName().equals("digest")) {
                authentication.setDigest(parser.nextText());
            }
            else if (parser.getName().equals("resource")) {
                authentication.setResource(parser.nextText());
            }
        }
        else if (eventType == XmlPullParser.END_TAG) {
            if (parser.getName().equals("query")) {
                done = true;
            }
        }
    }
    return authentication;
}
 
Example 25
Source Project: android-maps-utils   Source File: KmlFeatureParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiGeometries() throws Exception {
    XmlPullParser xmlPullParser = createParser("amu_nested_multigeometry.kml");
    KmlPlacemark feature = KmlFeatureParser.createPlacemark(xmlPullParser);
    assertEquals(feature.getProperty("name"), "multiPointLine");
    assertEquals(feature.getProperty("description"), "Nested MultiGeometry structure");
    assertEquals(feature.getGeometry().getGeometryType(), "MultiGeometry");
    List<Geometry> objects = (ArrayList<Geometry>) feature.getGeometry().getGeometryObject();
    assertEquals(objects.get(0).getGeometryType(), "Point");
    assertEquals(objects.get(1).getGeometryType(), "LineString");
    assertEquals(objects.get(2).getGeometryType(), "MultiGeometry");
    List<Geometry> subObjects = (ArrayList<Geometry>) objects.get(2).getGeometryObject();
    assertEquals(subObjects.get(0).getGeometryType(), "Point");
    assertEquals(subObjects.get(1).getGeometryType(), "LineString");
}
 
Example 26
Source Project: SensorTag-CC2650   Source File: firmwareEntriesParser.java    License: Apache License 2.0 5 votes vote down vote up
private String readTag(XmlPullParser parser,String tag) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, tag);
    String wS = "";
    if (parser.next() == XmlPullParser.TEXT) {
        wS = parser.getText();
        parser.nextTag();
    }
    parser.require(XmlPullParser.END_TAG, ns, tag);
    return wS;
}
 
Example 27
Source Project: LB-Launcher   Source File: AutoInstallsLayout.java    License: Apache License 2.0 5 votes vote down vote up
protected static final void beginDocument(XmlPullParser parser, String firstElementName)
        throws XmlPullParserException, IOException {
    int type;
    while ((type = parser.next()) != XmlPullParser.START_TAG
            && type != XmlPullParser.END_DOCUMENT);

    if (type != XmlPullParser.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 28
Source Project: Stringlate   Source File: ResourcesParser.java    License: MIT License 5 votes vote down vote up
private static boolean readFirstBooleanAttr(XmlPullParser parser, String[] attrs, boolean defaultV) {
    for (String attr : attrs) {
        String value = parser.getAttributeValue(null, attr);
        if (value != null) {
            return Boolean.parseBoolean(value);
        }
    }

    return defaultV;
}
 
Example 29
Source Project: cwac-netsecurity   Source File: XmlUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static final void nextElement(XmlPullParser parser) throws XmlPullParserException, IOException
{
    int type;
    while ((type=parser.next()) != parser.START_TAG
               && type != parser.END_DOCUMENT) {
        ;
    }
}
 
Example 30
Source Project: lams   Source File: Xpp3DomBuilder.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @deprecated As of 1.4, use {@link XppDom#build(XmlPullParser)} instead
 */
public static Xpp3Dom build(Reader reader) throws Exception {
    XmlPullParser parser = new MXParser();
    parser.setInput(reader);
    try {
        return (Xpp3Dom)XppDom.build(parser);
    } finally {
        reader.close();
    }
}