Example usage for org.dom4j Element getNamespaceURI

List of usage examples for org.dom4j Element getNamespaceURI

Introduction

In this page you can find the example usage for org.dom4j Element getNamespaceURI.

Prototype

String getNamespaceURI();

Source Link

Document

Returns the URI mapped to the namespace of this element if one exists otherwise an empty String is returned.

Usage

From source file:android.apn.androidpn.server.xmpp.net.StanzaHandler.java

License:Open Source License

private IQ getIQ(Element doc) {
    Element query = doc.element("query");
    if (query != null && "jabber:iq:roster".equals(query.getNamespaceURI())) {
        return new Roster(doc);
    } else {//  w  ww . j a va2  s .  co m
        return new IQ(doc, false);
    }
}

From source file:android.apn.androidpn.server.xmpp.router.IQRouter.java

License:Open Source License

private void handle(IQ packet) {
    try {/*from  ww  w.j  av a2  s .com*/
        Element childElement = packet.getChildElement();
        String namespace = null;
        if (childElement != null) {
            namespace = childElement.getNamespaceURI();
        }
        if (namespace == null) {
            if (packet.getType() != IQ.Type.result && packet.getType() != IQ.Type.error) {
                log.warn("Unknown packet " + packet);
            }
        } else {
            IQHandler handler = getHandler(namespace);
            if (handler == null) {
                sendErrorPacket(packet, PacketError.Condition.service_unavailable);
            } else {
                handler.process(packet);
            }
        }

    } catch (Exception e) {
        log.error("Could not route packet", e);
        Session session = sessionManager.getSession(packet.getFrom());
        if (session != null) {
            IQ reply = IQ.createResultIQ(packet);
            reply.setError(PacketError.Condition.internal_server_error);
            session.process(reply);
        }
    }
}

From source file:at.jabberwocky.impl.core.io.PacketReader.java

private Packet getPacket() throws Exception {
    Element doc = parseDocument().getRootElement();
    if (null == doc) {
        return (null);
    }//from   w  ww.  j  a  va  2s.  c o m

    String tag = doc.getName();
    if ("message".equals(tag)) {
        return (new Message(doc));
    }

    if ("presence".equals(tag)) {
        return (new Presence(doc));
    }

    Element query = doc.element("query");
    if (query != null && "jabber:iq:roster".equals(query.getNamespaceURI())) {
        return new Roster(doc);
    }

    return new IQ(doc);
}

From source file:com.adspore.splat.xep0060.PubSubEngine.java

License:Open Source License

/**
 * Handles IQ packets sent to the pubsub service. Requests of disco#info and disco#items
 * are not being handled by the engine. Instead the service itself should handle disco packets.
 *
 * @param service the PubSub service this action is to be performed for.
 * @param iq the IQ packet sent to the pubsub service.
 * @return true if the IQ packet was handled by the engine.
 *//* w  w  w .  j a v a 2s.  c  o m*/
public static IQ process(PubSubService service, IQ iq) {
    // Ignore IQs of type ERROR or RESULT
    if (IQ.Type.error == iq.getType() || IQ.Type.result == iq.getType()) {
        return createErrorPacket(iq, Condition.bad_request, null);
    }

    Element childElement = iq.getChildElement();
    String namespace = null;

    if (childElement != null) {
        namespace = childElement.getNamespaceURI();
    }

    //   PUBSUB
    if ("http://jabber.org/protocol/pubsub".equals(namespace)) {

        //   PUBLISH
        Element action = childElement.element("publish");
        if (action != null) {
            // Entity publishes an item
            return publishItemsToNode(service, iq, action);
        }

        //   SUBSCRIBE
        action = childElement.element("subscribe");
        if (action != null) {
            // Entity subscribes to a node
            return subscribeNode(service, iq, childElement, action);
        }

        //   OPTIONS
        action = childElement.element("options");
        if (action != null) {
            if (IQ.Type.get == iq.getType()) {
                // Subscriber requests subscription options form
                return getSubscriptionConfiguration(service, iq, childElement, action);
            } else {
                // Subscriber submits completed options form
                return configureSubscription(service, iq, action);
            }
        }

        //   CREATE
        action = childElement.element("create");
        if (action != null) {
            // Entity is requesting to create a new node
            return createNode(service, iq, childElement, action);
        }

        //   UNSUBSCRIBE
        action = childElement.element("unsubscribe");
        if (action != null) {
            // Entity unsubscribes from a node
            return unsubscribeNode(service, iq, action);
        }

        //   GET SUBSCRIPTIONS
        action = childElement.element("subscriptions");
        if (action != null) {
            // Entity requests all current subscriptions
            return getSubscriptions(service, iq, childElement);
        }

        //   GET AFFILIATIONS
        action = childElement.element("affiliations");
        if (action != null) {
            // Entity requests all current affiliations
            return getAffiliations(service, iq, childElement);
        }

        //   GET ITEMS
        action = childElement.element("items");
        if (action != null) {
            // Subscriber requests all active items
            return getPublishedItems(service, iq, action);
        }

        //   RETRACT
        action = childElement.element("retract");
        if (action != null) {
            // Entity deletes an item
            return deleteItems(service, iq, action);
        }

        // Unknown action requested
        //   TODO:  Add error packet
        return createErrorPacket(iq, PacketError.Condition.bad_request, null);
    }

    else if ("http://jabber.org/protocol/pubsub#owner".equals(namespace)) {
        //   CONFIGURE
        Element action = childElement.element("configure");
        if (action != null) {
            String nodeID = action.attributeValue("node");
            if (nodeID == null) {
                // if user is not sysadmin then return nodeid-required error
                if (!service.isServiceAdmin(iq.getFrom()) || !service.isCollectionNodesSupported()) {
                    // Configure elements must have a node attribute so answer an error
                    Element pubsubError = DocumentHelper.createElement(
                            QName.get("nodeid-required", "http://jabber.org/protocol/pubsub#errors"));
                    return createErrorPacket(iq, PacketError.Condition.bad_request, pubsubError);
                } else {
                    // Sysadmin is trying to configure root collection node
                    nodeID = service.getRootCollectionNode().getNodeID();
                }
            }
            if (IQ.Type.get == iq.getType()) {
                // Owner requests configuration form of a node
                return getNodeConfiguration(service, iq, childElement, nodeID);
            } else {
                // Owner submits or cancels node configuration form
                configureNode(service, iq, action, nodeID);
            }
        }

        //   DEFAULT
        action = childElement.element("default");
        if (action != null) {
            // Owner requests default configuration options for
            // leaf or collection nodes
            return getDefaultNodeConfiguration(service, iq, childElement, action);
        }

        //   DELETE
        action = childElement.element("delete");
        if (action != null) {
            // Owner deletes a node
            return deleteNode(service, iq, action);
        }

        //   SUBSCRIPTIONS
        action = childElement.element("subscriptions");
        if (action != null) {
            if (IQ.Type.get == iq.getType()) {
                // Owner requests all affiliated entities
                return getNodeSubscriptions(service, iq, action);
            } else {
                return modifyNodeSubscriptions(service, iq, action);
            }
        }

        //   AFFILIATIONS
        action = childElement.element("affiliations");
        if (action != null) {
            if (IQ.Type.get == iq.getType()) {
                // Owner requests all affiliated entities
                return getNodeAffiliations(service, iq, action);
            } else {
                return modifyNodeAffiliations(service, iq, action);
            }
        }

        //   PURGE
        action = childElement.element("purge");
        if (action != null) {
            // Owner purges items from a node
            return purgeNode(service, iq, action);

        }
    }

    //   TODO:  Temporarily blocking commands; this should be handled by the command service, not pubsub
    //        //   GET COMMANDS
    //        else if (SplatNamespaces.NAMESPACE_COMMANDS.equals(namespace)) {
    //            // Process ad-hoc command
    //            IQ reply = service.getManager().process(iq);
    //            router.route(reply);
    //            return true;
    //        }
    return createErrorPacket(iq, PacketError.Condition.bad_request, null);
}

From source file:com.alibaba.citrus.dev.handler.util.DomUtil.java

License:Open Source License

private static Element copy(org.dom4j.Element dom4jElement, ElementFilter filter) throws Exception {
    dom4jElement = filter.filter(dom4jElement);

    if (dom4jElement == null) {
        return null;
    }//w w w  .  j a v  a2 s . c  o m

    Element element = new Element(dom4jElement.getQualifiedName(), dom4jElement.getNamespaceURI());

    for (Object attr : dom4jElement.attributes()) {
        String name = ((Attribute) attr).getQualifiedName();
        String value = ((Attribute) attr).getValue();

        element.addAttribute(name, value);
    }

    for (Object ns : dom4jElement.declaredNamespaces()) {
        String name = ((Namespace) ns).getPrefix();
        String value = ((Namespace) ns).getURI();

        if (isEmpty(name)) {
            name = "xmlns";
        } else {
            name = "xmlns:" + name;
        }

        element.addAttribute(name, value);
    }

    for (Object e : dom4jElement.elements()) {
        Element subElement = copy((org.dom4j.Element) e, filter);

        if (subElement != null) {
            element.addSubElement(subElement);
        }
    }

    if (dom4jElement.elements().isEmpty()) {
        String text = trimToNull(dom4jElement.getText());

        if (text != null) {
            element.setText(text);
        }
    }

    return element;
}

From source file:com.alibaba.citrus.springext.impl.SchemaImpl.java

License:Open Source License

/**
 * ?schema??// w w  w.  ja v a 2  s  .  co m
 * <ol>
 * <li>targetNamespace</li>
 * <li>include name</li>
 * </ol>
 */
@Override
protected void doAnalyze() {
    Document doc = getDocument(); // ??null
    org.dom4j.Element root = doc.getRootElement();

    // return if not a schema file
    if (!W3C_XML_SCHEMA_NS_URI.equals(root.getNamespaceURI()) || !"schema".equals(root.getName())) {
        return;
    }

    // parse targetNamespace
    if (parsingTargetNamespace) {
        Attribute attr = root.attribute("targetNamespace");

        if (attr != null) {
            targetNamespace = trimToNull(attr.getStringValue());
        }
    }

    // parse include
    Namespace xsd = DocumentHelper.createNamespace("xsd", W3C_XML_SCHEMA_NS_URI);
    QName includeName = DocumentHelper.createQName("include", xsd);
    List<String> includeNames = createLinkedList();

    // for each <xsd:include>
    for (Iterator<?> i = root.elementIterator(includeName); i.hasNext();) {
        org.dom4j.Element includeElement = (org.dom4j.Element) i.next();
        String schemaLocation = trimToNull(includeElement.attributeValue("schemaLocation"));

        if (schemaLocation != null) {
            includeNames.add(schemaLocation);
        }
    }

    includes = includeNames.toArray(new String[includeNames.size()]);

    // parse xsd:element
    QName elementName = DocumentHelper.createQName("element", xsd);

    // for each <xsd:element>
    for (Iterator<?> i = root.elementIterator(elementName); i.hasNext();) {
        Element element = new ElementImpl((org.dom4j.Element) i.next());

        if (element.getName() != null) {
            this.elements.put(element.getName(), element);
        }
    }
}

From source file:com.alibaba.citrus.springext.support.SchemaUtil.java

License:Open Source License

/** schemaincludes */
public static Transformer getTransformerWhoRemovesIncludes() {
    return new Transformer() {
        public void transform(Document document, String systemId) {
            Element root = document.getRootElement();

            // <xsd:schema>
            if (W3C_XML_SCHEMA_NS_URI.equals(root.getNamespaceURI()) && "schema".equals(root.getName())) {
                // for each <xsd:include>
                for (Iterator<?> i = root.elementIterator(XSD_INCLUDE); i.hasNext();) {
                    i.next();//ww w. j a  v a2  s.  c om
                    i.remove();
                }
            }
        }
    };
}

From source file:com.alibaba.citrus.springext.support.SchemaUtil.java

License:Open Source License

/** schema?includes */
public static Transformer getTransformerWhoAddsIndirectIncludes(final Map<String, Schema> includes) {
    return new Transformer() {
        public void transform(Document document, String systemId) {
            Element root = document.getRootElement();

            root.addNamespace("xsd", W3C_XML_SCHEMA_NS_URI);

            // <xsd:schema>
            if (W3C_XML_SCHEMA_NS_URI.equals(root.getNamespaceURI()) && "schema".equals(root.getName())) {
                Namespace xsd = DocumentHelper.createNamespace("xsd", W3C_XML_SCHEMA_NS_URI);
                QName includeName = DocumentHelper.createQName("include", xsd);

                // for each <xsd:include>
                for (Iterator<?> i = root.elementIterator(includeName); i.hasNext();) {
                    i.next();/*from   w  w w  .  j a  v  a 2  s.  co  m*/
                    i.remove();
                }

                // includes
                @SuppressWarnings("unchecked")
                List<Node> nodes = root.elements();
                int i = 0;

                for (Schema includedSchema : includes.values()) {
                    Element includeElement = DocumentHelper.createElement(includeName);
                    nodes.add(i++, includeElement);

                    includeElement.addAttribute("schemaLocation", includedSchema.getName());
                }
            }
        }
    };
}

From source file:com.alibaba.citrus.springext.support.SchemaUtil.java

License:Open Source License

/** ?URI? */
public static Transformer getAddPrefixTransformer(final SchemaSet schemas, String prefix) {
    if (prefix != null) {
        if (!prefix.endsWith("/")) {
            prefix += "/";
        }// ww  w . j  a v  a2  s  .  c  o m
    }

    final String normalizedPrefix = prefix;

    return new Transformer() {
        public void transform(Document document, String systemId) {
            if (normalizedPrefix != null) {
                Element root = document.getRootElement();

                // <xsd:schema>
                if (W3C_XML_SCHEMA_NS_URI.equals(root.getNamespaceURI()) && "schema".equals(root.getName())) {
                    Namespace xsd = DocumentHelper.createNamespace("xsd", W3C_XML_SCHEMA_NS_URI);
                    QName includeName = DocumentHelper.createQName("include", xsd);
                    QName importName = DocumentHelper.createQName("import", xsd);

                    // for each <xsd:include>
                    for (Iterator<?> i = root.elementIterator(includeName); i.hasNext();) {
                        Element includeElement = (Element) i.next();
                        String schemaLocation = trimToNull(includeElement.attributeValue("schemaLocation"));

                        if (schemaLocation != null) {
                            schemaLocation = getNewSchemaLocation(schemaLocation, null, systemId);

                            if (schemaLocation != null) {
                                includeElement.addAttribute("schemaLocation", schemaLocation);
                            }
                        }
                    }

                    // for each <xsd:import>
                    for (Iterator<?> i = root.elementIterator(importName); i.hasNext();) {
                        Element importElement = (Element) i.next();
                        String schemaLocation = importElement.attributeValue("schemaLocation");
                        String namespace = trimToNull(importElement.attributeValue("namespace"));

                        if (schemaLocation != null || namespace != null) {
                            schemaLocation = getNewSchemaLocation(schemaLocation, namespace, systemId);

                            if (schemaLocation != null) {
                                importElement.addAttribute("schemaLocation", schemaLocation);
                            }
                        }
                    }
                }
            }
        }

        private String getNewSchemaLocation(String schemaLocation, String namespace, String systemId) {
            // ?schemaLocation
            if (schemaLocation != null) {
                Schema schema = schemas.findSchema(schemaLocation);

                if (schema != null) {
                    return normalizedPrefix + schema.getName();
                } else {
                    return schemaLocation; // location??
                }
            }

            // ??namespace
            if (namespace != null) {
                Set<Schema> nsSchemas = schemas.getNamespaceMappings().get(namespace);

                if (nsSchemas != null && !nsSchemas.isEmpty()) {
                    // ?nsschema?schema
                    String versionedExtension = getVersionedExtension(systemId);

                    if (versionedExtension != null) {
                        for (Schema schema : nsSchemas) {
                            if (schema.getName().endsWith(versionedExtension)) {
                                return normalizedPrefix + schema.getName();
                            }
                        }
                    }

                    // schema?beans.xsd?beans-2.5.xsd?beans-2.0.xsd
                    return normalizedPrefix + nsSchemas.iterator().next().getName();
                }
            }

            return null;
        }

        /** spring-aop-2.5.xsd?-2.5.xsd */
        private String getVersionedExtension(String systemId) {
            if (systemId != null) {
                int dashIndex = systemId.lastIndexOf("-");
                int slashIndex = systemId.lastIndexOf("/");

                if (dashIndex > slashIndex) {
                    return systemId.substring(dashIndex);
                }
            }

            return null;
        }
    };
}

From source file:com.alibaba.citrus.springext.util.ConvertToUnqualifiedStyle.java

License:Open Source License

/** Root element?<code>&lt;beans:bean&gt;</code> */
private boolean isSpringConfigurationFile(Document doc) {
    Element root = doc.getRootElement();
    return "http://www.springframework.org/schema/beans".equals(root.getNamespaceURI())
            && "beans".equals(root.getName());
}