Search in sources :

Example 81 with XmlPullParser

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

the class SearchableInfo method getActivityMetaData.

/**
     * Get the metadata for a given activity
     * 
     * @param context runtime context
     * @param xml XML parser for reading attributes
     * @param cName The component name of the searchable activity
     * 
     * @result A completely constructed SearchableInfo, or null if insufficient XML data for it
     */
private static SearchableInfo getActivityMetaData(Context context, XmlPullParser xml, final ComponentName cName) {
    SearchableInfo result = null;
    Context activityContext = createActivityContext(context, cName);
    if (activityContext == null)
        return null;
    // forward through the file until it's reading the tag of interest.
    try {
        int tagType = xml.next();
        while (tagType != XmlPullParser.END_DOCUMENT) {
            if (tagType == XmlPullParser.START_TAG) {
                if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE)) {
                    AttributeSet attr = Xml.asAttributeSet(xml);
                    if (attr != null) {
                        try {
                            result = new SearchableInfo(activityContext, attr, cName);
                        } catch (IllegalArgumentException ex) {
                            Log.w(LOG_TAG, "Invalid searchable metadata for " + cName.flattenToShortString() + ": " + ex.getMessage());
                            return null;
                        }
                    }
                } else if (xml.getName().equals(MD_XML_ELEMENT_SEARCHABLE_ACTION_KEY)) {
                    if (result == null) {
                        // Can't process an embedded element if we haven't seen the enclosing
                        return null;
                    }
                    AttributeSet attr = Xml.asAttributeSet(xml);
                    if (attr != null) {
                        try {
                            result.addActionKey(new ActionKeyInfo(activityContext, attr));
                        } catch (IllegalArgumentException ex) {
                            Log.w(LOG_TAG, "Invalid action key for " + cName.flattenToShortString() + ": " + ex.getMessage());
                            return null;
                        }
                    }
                }
            }
            tagType = xml.next();
        }
    } catch (XmlPullParserException e) {
        Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
        return null;
    } catch (IOException e) {
        Log.w(LOG_TAG, "Reading searchable metadata for " + cName.flattenToShortString(), e);
        return null;
    }
    return result;
}
Also used : Context(android.content.Context) AttributeSet(android.util.AttributeSet) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 82 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project android-job by evernote.

the class XmlUtils method readThisLongArrayXml.

/**
     * Read a long[] object from an XmlPullParser.  The XML data could
     * previously have been generated by writeLongArrayXml().  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 Returns a newly generated long[].
     *
     * @see #readListXml
     */
public static final long[] readThisLongArrayXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, java.io.IOException {
    int num;
    try {
        num = Integer.parseInt(parser.getAttributeValue(null, "num"));
    } catch (NullPointerException e) {
        throw new XmlPullParserException("Need num attribute in long-array");
    } catch (NumberFormatException e) {
        throw new XmlPullParserException("Not a number in num attribute in long-array");
    }
    parser.next();
    long[] array = new long[num];
    int i = 0;
    int eventType = parser.getEventType();
    do {
        if (eventType == parser.START_TAG) {
            if (parser.getName().equals("item")) {
                try {
                    array[i] = Long.parseLong(parser.getAttributeValue(null, "value"));
                } catch (NullPointerException e) {
                    throw new XmlPullParserException("Need value attribute in item");
                } catch (NumberFormatException e) {
                    throw new XmlPullParserException("Not a number in value attribute in item");
                }
            } else {
                throw new XmlPullParserException("Expected item tag at: " + parser.getName());
            }
        } else if (eventType == parser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return array;
            } else if (parser.getName().equals("item")) {
                i++;
            } else {
                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");
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 83 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project android-job by evernote.

the class XmlUtils method readThisValueXml.

private static final Object readThisValueXml(XmlPullParser parser, String[] name, ReadMapCallback callback) throws XmlPullParserException, java.io.IOException {
    final String valueName = parser.getAttributeValue(null, "name");
    final String tagName = parser.getName();
    //System.out.println("Reading this value tag: " + tagName + ", name=" + valueName);
    Object res;
    if (tagName.equals("null")) {
        res = null;
    } else if (tagName.equals("string")) {
        String value = "";
        int eventType;
        while ((eventType = parser.next()) != parser.END_DOCUMENT) {
            if (eventType == parser.END_TAG) {
                if (parser.getName().equals("string")) {
                    name[0] = valueName;
                    //System.out.println("Returning value for " + valueName + ": " + value);
                    return value;
                }
                throw new XmlPullParserException("Unexpected end tag in <string>: " + parser.getName());
            } else if (eventType == parser.TEXT) {
                value += parser.getText();
            } else if (eventType == parser.START_TAG) {
                throw new XmlPullParserException("Unexpected start tag in <string>: " + parser.getName());
            }
        }
        throw new XmlPullParserException("Unexpected end of document in <string>");
    } else if ((res = readThisPrimitiveValueXml(parser, tagName)) != null) {
    // all work already done by readThisPrimitiveValueXml
    } else if (tagName.equals("int-array")) {
        res = readThisIntArrayXml(parser, "int-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("long-array")) {
        res = readThisLongArrayXml(parser, "long-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("double-array")) {
        res = readThisDoubleArrayXml(parser, "double-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("string-array")) {
        res = readThisStringArrayXml(parser, "string-array", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("map")) {
        parser.next();
        res = readThisMapXml(parser, "map", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("list")) {
        parser.next();
        res = readThisListXml(parser, "list", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (tagName.equals("set")) {
        parser.next();
        res = readThisSetXml(parser, "set", name);
        name[0] = valueName;
        //System.out.println("Returning value for " + valueName + ": " + res);
        return res;
    } else if (callback != null) {
        res = callback.readThisUnknownObjectXml(parser, tagName);
        name[0] = valueName;
        return res;
    } else {
        throw new XmlPullParserException("Unknown tag: " + tagName);
    }
    // Skip through to end tag.
    int eventType;
    while ((eventType = parser.next()) != parser.END_DOCUMENT) {
        if (eventType == parser.END_TAG) {
            if (parser.getName().equals(tagName)) {
                name[0] = valueName;
                //System.out.println("Returning value for " + valueName + ": " + res);
                return res;
            }
            throw new XmlPullParserException("Unexpected end tag in <" + tagName + ">: " + parser.getName());
        } else if (eventType == parser.TEXT) {
            throw new XmlPullParserException("Unexpected text in <" + tagName + ">: " + parser.getName());
        } else if (eventType == parser.START_TAG) {
            throw new XmlPullParserException("Unexpected start tag in <" + tagName + ">: " + parser.getName());
        }
    }
    throw new XmlPullParserException("Unexpected end of document in <" + tagName + ">");
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 84 with XmlPullParser

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

the class DisplayedExtensionTest method checkDisplayedProvider.

@Test
public void checkDisplayedProvider() throws Exception {
    XmlPullParser parser = PacketParserUtils.getParserFor(displayedExtension);
    DisplayedExtension displayedExtension1 = new DisplayedProvider().parse(parser);
    Assert.assertEquals("message-1", displayedExtension1.getId());
    Message message = (Message) PacketParserUtils.parseStanza(displayedMessageStanza);
    DisplayedExtension displayedExtension2 = DisplayedExtension.from(message);
    Assert.assertEquals("message-1", displayedExtension2.getId());
}
Also used : Message(org.jivesoftware.smack.packet.Message) DisplayedExtension(org.jivesoftware.smackx.chat_markers.element.ChatMarkersElements.DisplayedExtension) XmlPullParser(org.xmlpull.v1.XmlPullParser) DisplayedProvider(org.jivesoftware.smackx.chat_markers.provider.DisplayedProvider) Test(org.junit.Test)

Example 85 with XmlPullParser

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

the class AbstractHttpOverXmppProviderTest method areRespHeadersParsedCorrectly.

@Test
public void areRespHeadersParsedCorrectly() throws Exception {
    String string = "<resp xmlns='urn:xmpp:http' version='1.1' statusCode='200' statusMessage='OK'>" + "<headers xmlns='http://jabber.org/protocol/shim'>" + "<header name='Date'>Fri, 03 May 2013 13:52:10 GMT-4</header>" + "<header name='Allow'>OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE</header>" + "<header name='Content-Length'>0</header>" + "</headers>" + "</resp>";
    Map<String, String> expectedHeaders = new HashMap<String, String>();
    expectedHeaders.put("Date", "Fri, 03 May 2013 13:52:10 GMT-4");
    expectedHeaders.put("Allow", "OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE");
    expectedHeaders.put("Content-Length", "0");
    HttpOverXmppRespProvider provider = new HttpOverXmppRespProvider();
    XmlPullParser parser = PacketParserUtils.getParserFor(string);
    IQ iq = provider.parse(parser);
    assertTrue(iq instanceof HttpOverXmppResp);
    HttpOverXmppResp body = ((HttpOverXmppResp) iq);
    checkHeaders(body.getHeaders(), expectedHeaders);
}
Also used : HttpOverXmppResp(org.jivesoftware.smackx.hoxt.packet.HttpOverXmppResp) HashMap(java.util.HashMap) XmlPullParser(org.xmlpull.v1.XmlPullParser) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.Test)

Aggregations

XmlPullParser (org.xmlpull.v1.XmlPullParser)673 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)623 IOException (java.io.IOException)377 FileInputStream (java.io.FileInputStream)185 FileNotFoundException (java.io.FileNotFoundException)185 File (java.io.File)107 ArrayList (java.util.ArrayList)77 StringReader (java.io.StringReader)65 AttributeSet (android.util.AttributeSet)61 Test (org.junit.Test)57 TypedArray (android.content.res.TypedArray)56 InputStream (java.io.InputStream)48 AtomicFile (android.util.AtomicFile)47 HashMap (java.util.HashMap)45 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)39 FileReader (java.io.FileReader)36 BufferedInputStream (java.io.BufferedInputStream)30 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)30 RemoteException (android.os.RemoteException)28 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)28