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

The following examples show how to use org.xmlpull.v1.XmlPullParser#getNamespace() . 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: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * parse xml that is known to be a resource, and that is already being read by an XML Pull Parser
 * This is if a resource is in a bigger piece of XML.   
 * @ 
 */
public Resource parse(XmlPullParser xpp)  throws IOException, FHIRFormatError, XmlPullParserException {
	if (xpp.getNamespace() == null)
		throw new FHIRFormatError("This does not appear to be a FHIR resource (no namespace '"+xpp.getNamespace()+"') (@ /) "+Integer.toString(xpp.getEventType()));
	if (!xpp.getNamespace().equals(FHIR_NS))
		throw new FHIRFormatError("This does not appear to be a FHIR resource (wrong namespace '"+xpp.getNamespace()+"') (@ /)");
	return parseResource(xpp);
}
 
Example 2
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * parse xml that is known to be a resource, and that is already being read by an XML Pull Parser
 * This is if a resource is in a bigger piece of XML.   
 * @ 
 */
public Resource parse(XmlPullParser xpp)  throws IOException, FHIRFormatError, XmlPullParserException {
	if (xpp.getNamespace() == null)
		throw new FHIRFormatError("This does not appear to be a FHIR resource (no namespace '"+xpp.getNamespace()+"') (@ /) "+Integer.toString(xpp.getEventType()));
	if (!xpp.getNamespace().equals(FHIR_NS))
		throw new FHIRFormatError("This does not appear to be a FHIR resource (wrong namespace '"+xpp.getNamespace()+"') (@ /)");
	return parseResource(xpp);
}
 
Example 3
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * parse xml that is known to be a resource, and that is already being read by an XML Pull Parser
 * This is if a resource is in a bigger piece of XML.   
 * @ 
 */
public Resource parse(XmlPullParser xpp)  throws IOException, FHIRFormatError, XmlPullParserException {
	if (xpp.getNamespace() == null)
		throw new FHIRFormatError("This does not appear to be a FHIR resource (no namespace '"+xpp.getNamespace()+"') (@ /) "+Integer.toString(xpp.getEventType()));
	if (!xpp.getNamespace().equals(FHIR_NS))
		throw new FHIRFormatError("This does not appear to be a FHIR resource (wrong namespace '"+xpp.getNamespace()+"') (@ /)");
	return parseResource(xpp);
}
 
Example 4
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * parse xml that is known to be a resource, and that is already being read by an XML Pull Parser
 * This is if a resource is in a bigger piece of XML.   
 * @ 
 */
public Resource parse(XmlPullParser xpp)  throws IOException, FHIRFormatError, XmlPullParserException {
	if (xpp.getNamespace() == null)
		throw new FHIRFormatError("This does not appear to be a FHIR resource (no namespace '"+xpp.getNamespace()+"') (@ /) "+Integer.toString(xpp.getEventType()));
	if (!xpp.getNamespace().equals(FHIR_NS))
		throw new FHIRFormatError("This does not appear to be a FHIR resource (wrong namespace '"+xpp.getNamespace()+"') (@ /)");
	return parseResource(xpp);
}
 
Example 5
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * parse xml that is known to be a resource, and that is already being read by an XML Pull Parser
 * This is if a resource is in a bigger piece of XML.   
 * @ 
 */
public Resource parse(XmlPullParser xpp)  throws IOException, FHIRFormatError, XmlPullParserException {
	if (xpp.getNamespace() == null)
		throw new FHIRFormatError("This does not appear to be a FHIR resource (no namespace '"+xpp.getNamespace()+"') (@ /) "+Integer.toString(xpp.getEventType()));
	if (!xpp.getNamespace().equals(FHIR_NS))
		throw new FHIRFormatError("This does not appear to be a FHIR resource (wrong namespace '"+xpp.getNamespace()+"') (@ /)");
	return parseResource(xpp);
}
 
Example 6
Source File: ViewParser.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Helper to create Qualified name from current xml element
 * 
 * @param xpp XmlPullParser
 * @return QName
 */
private QName getName(XmlPullParser xpp)
{
    // Ensure namespace is valid
    String uri = xpp.getNamespace();
    if (namespaceService.getURIs().contains(uri) == false)
    {
        throw new ImporterException("Namespace URI " + uri + " has not been defined in the Repository dictionary");
    }
    
    // Construct name
    String name = xpp.getName();
    return QName.createQName(uri, name);
}
 
Example 7
Source File: PluginManifestUtil.java    From AndroidPlugin with MIT License 5 votes vote down vote up
private static void setAttrs(PlugInfo info, String manifestXML)
        throws XmlPullParserException, IOException {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser parser = factory.newPullParser();
    parser.setInput(new StringReader(manifestXML));
    int eventType = parser.getEventType();
    String namespaceAndroid = null;
    do {
        switch (eventType) {
        case XmlPullParser.START_DOCUMENT: {
            break;
        }
        case XmlPullParser.START_TAG: {
            String tag = parser.getName();
            if (tag.equals("manifest")) {
                namespaceAndroid = parser.getNamespace("android");
            } else if ("activity".equals(parser.getName())) {
                addActivity(info, namespaceAndroid, parser);
            } else if ("receiver".equals(parser.getName())) {
                addReceiver(info, namespaceAndroid, parser);
            } else if ("service".equals(parser.getName())) {
                addService(info, namespaceAndroid, parser);
            } else if ("application".equals(parser.getName())) {
                parseApplicationInfo(info, namespaceAndroid, parser);
            }
            break;
        }
        case XmlPullParser.END_TAG: {
            break;
        }
        }
        eventType = parser.next();
    } while (eventType != XmlPullParser.END_DOCUMENT);
}
 
Example 8
Source File: PullReader.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public Start(XmlPullParser parser) {
   this.reference = parser.getNamespace();
   this.line = parser.getLineNumber();
   this.prefix = parser.getPrefix();
   this.name = parser.getName();
}
 
Example 9
Source File: PacketParserUtils.java    From AndroidPNClient with Apache License 2.0 4 votes vote down vote up
/**
 * Parses an IQ packet.
 *
 * @param parser the XML parser, positioned at the start of an IQ packet.
 * @return an IQ object.
 * @throws Exception if an exception occurs while parsing the packet.
 */
public static IQ parseIQ(XmlPullParser parser, Connection connection) throws Exception {
    IQ iqPacket = null;

    String id = parser.getAttributeValue("", "id");
    String to = parser.getAttributeValue("", "to");
    String from = parser.getAttributeValue("", "from");
    IQ.Type type = IQ.Type.fromString(parser.getAttributeValue("", "type"));
    XMPPError error = null;

    boolean done = false;
    while (!done) {
        int eventType = parser.next();

        if (eventType == XmlPullParser.START_TAG) {
            String elementName = parser.getName();
            String namespace = parser.getNamespace();
            if (elementName.equals("error")) {
                error = PacketParserUtils.parseError(parser);
            }
            else if (elementName.equals("query") && namespace.equals("jabber:iq:auth")) {
                iqPacket = parseAuthentication(parser);
            }
            else if (elementName.equals("query") && namespace.equals("jabber:iq:roster")) {
                iqPacket = parseRoster(parser);
            }
            else if (elementName.equals("query") && namespace.equals("jabber:iq:register")) {
                iqPacket = parseRegistration(parser);
            }
            else if (elementName.equals("bind") &&
                    namespace.equals("urn:ietf:params:xml:ns:xmpp-bind")) {
                iqPacket = parseResourceBinding(parser);
            }
            // Otherwise, see if there is a registered provider for
            // this element name and namespace.
            else {
                Object provider = ProviderManager.getInstance().getIQProvider(elementName, namespace);
                if (provider != null) {
                    if (provider instanceof IQProvider) {
                        iqPacket = ((IQProvider)provider).parseIQ(parser);
                    }
                    else if (provider instanceof Class) {
                        iqPacket = (IQ)PacketParserUtils.parseWithIntrospection(elementName,
                                (Class)provider, parser);
                    }
                }
            }
        }
        else if (eventType == XmlPullParser.END_TAG) {
            if (parser.getName().equals("iq")) {
                done = true;
            }
        }
    }
    // Decide what to do when an IQ packet was not understood
    if (iqPacket == null) {
        if (IQ.Type.GET == type || IQ.Type.SET == type ) {
            // If the IQ stanza is of type "get" or "set" containing a child element
            // qualified by a namespace it does not understand, then answer an IQ of
            // type "error" with code 501 ("feature-not-implemented")
            iqPacket = new IQ() {
                public String getChildElementXML() {
                    return null;
                }
            };
            iqPacket.setPacketID(id);
            iqPacket.setTo(from);
            iqPacket.setFrom(to);
            iqPacket.setType(IQ.Type.ERROR);
            iqPacket.setError(new XMPPError(XMPPError.Condition.feature_not_implemented));
            connection.sendPacket(iqPacket);
            return null;
        }
        else {
            // If an IQ packet wasn't created above, create an empty IQ packet.
            iqPacket = new IQ() {
                public String getChildElementXML() {
                    return null;
                }
            };
        }
    }

    // Set basic values on the iq packet.
    iqPacket.setPacketID(id);
    iqPacket.setTo(to);
    iqPacket.setFrom(from);
    iqPacket.setType(type);
    iqPacket.setError(error);

    return iqPacket;
}
 
Example 10
Source File: PacketParserUtils.java    From AndroidPNClient with Apache License 2.0 4 votes vote down vote up
/**
 * Parses error sub-packets.
 *
 * @param parser the XML parser.
 * @return an error sub-packet.
 * @throws Exception if an exception occurs while parsing the packet.
 */
public static XMPPError parseError(XmlPullParser parser) throws Exception {
    final String errorNamespace = "urn:ietf:params:xml:ns:xmpp-stanzas";
	String errorCode = "-1";
    String type = null;
    String message = null;
    String condition = null;
    List<PacketExtension> extensions = new ArrayList<PacketExtension>();

    // Parse the error header
    for (int i=0; i<parser.getAttributeCount(); i++) {
        if (parser.getAttributeName(i).equals("code")) {
            errorCode = parser.getAttributeValue("", "code");
        }
        if (parser.getAttributeName(i).equals("type")) {
        	type = parser.getAttributeValue("", "type");
        }
    }
    boolean done = false;
    // Parse the text and condition tags
    while (!done) {
        int eventType = parser.next();
        if (eventType == XmlPullParser.START_TAG) {
            if (parser.getName().equals("text")) {
                message = parser.nextText();
            }
            else {
            	// Condition tag, it can be xmpp error or an application defined error.
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                if (errorNamespace.equals(namespace)) {
                	condition = elementName;
                }
                else {
                	extensions.add(parsePacketExtension(elementName, namespace, parser));
                }
            }
        }
            else if (eventType == XmlPullParser.END_TAG) {
                if (parser.getName().equals("error")) {
                    done = true;
                }
            }
    }
    // Parse the error type.
    XMPPError.Type errorType = XMPPError.Type.CANCEL;
    try {
        if (type != null) {
            errorType = XMPPError.Type.valueOf(type.toUpperCase());
        }
    }
    catch (IllegalArgumentException iae) {
        // Print stack trace. We shouldn't be getting an illegal error type.
        iae.printStackTrace();
    }
    return new XMPPError(Integer.parseInt(errorCode), errorType, condition, message, extensions);
}
 
Example 11
Source File: BodyParserXmlPull.java    From jbosh with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public BodyParserResults parse(final String xml) throws BOSHException {
    BodyParserResults result = new BodyParserResults();
    Exception thrown;
    try {
        XmlPullParser xpp = getXmlPullParser();

        xpp.setInput(new StringReader(xml));
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (LOG.isLoggable(Level.FINEST)) {
                    LOG.finest("Start tag: " + xpp.getName());
                }
            } else {
                eventType = xpp.next();
                continue;
            }

            String prefix = xpp.getPrefix();
            if (prefix == null) {
                prefix = XMLConstants.DEFAULT_NS_PREFIX;
            }
            String uri = xpp.getNamespace();
            String localName = xpp.getName();
            QName name = new QName(uri, localName, prefix);
            if (LOG.isLoggable(Level.FINEST)) {
                LOG.finest("Start element: ");
                LOG.finest("    prefix: " + prefix);
                LOG.finest("    URI: " + uri);
                LOG.finest("    local: " + localName);
            }

            BodyQName bodyName = AbstractBody.getBodyQName();
            if (!bodyName.equalsQName(name)) {
                throw(new IllegalStateException(
                        "Root element was not '" + bodyName.getLocalPart()
                        + "' in the '" + bodyName.getNamespaceURI()
                        + "' namespace.  (Was '" + localName
                        + "' in '" + uri + "')"));
            }

            for (int idx=0; idx < xpp.getAttributeCount(); idx++) {
                String attrURI = xpp.getAttributeNamespace(idx);
                if (attrURI.length() == 0) {
                    attrURI = xpp.getNamespace(null);
                }
                String attrPrefix = xpp.getAttributePrefix(idx);
                if (attrPrefix == null) {
                    attrPrefix = XMLConstants.DEFAULT_NS_PREFIX;
                }
                String attrLN = xpp.getAttributeName(idx);
                String attrVal = xpp.getAttributeValue(idx);
                BodyQName aqn = BodyQName.createWithPrefix(
                        attrURI, attrLN, attrPrefix);
                if (LOG.isLoggable(Level.FINEST)) {
                    LOG.finest("        Attribute: {" + attrURI + "}"
                            + attrLN + " = '" + attrVal + "'");
                }
                result.addBodyAttributeValue(aqn, attrVal);
            }
            break;
        }
        return result;
    } catch (RuntimeException rtx) {
        thrown = rtx;
    } catch (XmlPullParserException xmlppx) {
        thrown = xmlppx;
    } catch (IOException iox) {
        thrown = iox;
    }
    throw(new BOSHException("Could not parse body:\n" + xml, thrown));
}
 
Example 12
Source File: JingleProvider.java    From jitsi-hammer with Apache License 2.0 4 votes vote down vote up
@Override
public NewJingleIQ parse(XmlPullParser parser, int initialDepth)
        throws XmlPullParserException, IOException
{
    NewJingleIQ jingleIQ = new NewJingleIQ();
    NewJingleAction action = NewJingleAction.parseString(parser.getAttributeValue("", "action"));
    String initiator = parser.getAttributeValue("", "initiator");
    String responder = parser.getAttributeValue("", "responder");
    String sid = parser.getAttributeValue("", "sid");

    jingleIQ.setAction(action);
    jingleIQ.setInitiator(initiator);
    jingleIQ.setResponder(responder);
    jingleIQ.setSID(sid);
    boolean done = false;

    try
    {
        while (!done)
        {
            int eventType = parser.next();
            String elementName = parser.getName();
            String namespace = parser.getNamespace();
            if (eventType == XmlPullParser.START_TAG)
            {
                ExtensionElementProvider provider = ProviderManager.getExtensionProvider(elementName, namespace);
                if (provider != null)
                {
                    Element child = provider.parse(parser);
                    if (child instanceof NewContentPacketExtension)
                    {
                        jingleIQ.addContent((NewContentPacketExtension)child);
                    }
                    else
                    {
                        throw new IOException("JingleProvider doesn't handle child element " + elementName +
                            " in namespace " + namespace);
                    }
                }
                else
                {
                    throw new IOException("JingleProvider: no provider found for element " +
                            elementName + " in namespace " + namespace);
                }
            }

            if (eventType == XmlPullParser.END_TAG && parser.getName().equals("jingle"))
            {
                done = true;
            }
        }
    }
    catch (Exception e)
    {
    }

    return jingleIQ;
}
 
Example 13
Source File: PullReader.java    From simplexml with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor for the <code>Start</code> object. This will 
 * wrap the provided node and expose the required details such
 * as the name, namespace prefix and namespace reference. The
 * provided element node can be acquired for debugging purposes.
 * 
 * @param source this is the parser being wrapped by this
 */
public Start(XmlPullParser source) {
   this.reference = source.getNamespace();
   this.line = source.getLineNumber();
   this.prefix = source.getPrefix();
   this.name = source.getName();
   this.source = source;     
}