Example usage for org.dom4j Element setQName

List of usage examples for org.dom4j Element setQName

Introduction

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

Prototype

void setQName(QName qname);

Source Link

Document

Sets the QName of this element which represents the local name, the qualified name and the Namespace.

Usage

From source file:nl.tue.gale.conversion.aha3.CLConvert.java

License:Open Source License

@SuppressWarnings("unchecked")
private void convertIf(Element element) {
    if ("gale".equals(element.getQName().getNamespacePrefix()))
        return;/*from w w  w .ja v  a 2 s  .co  m*/
    element.setQName(DocumentFactory.getInstance().createQName("if", "gale", "http://gale.tue.nl/adaptation"));
    String expr = convertExpr(element.attributeValue("expr"));
    element.addAttribute("expr", expr);
    List<Element> blocks = element.elements("block");
    if (blocks.size() > 0) {
        blocks.get(0).setQName(
                DocumentFactory.getInstance().createQName("then", "gale", "http://gale.tue.nl/adaptation"));
    }
    if (blocks.size() > 1) {
        blocks.get(1).setQName(
                DocumentFactory.getInstance().createQName("else", "gale", "http://gale.tue.nl/adaptation"));
    }
}

From source file:nl.tue.gale.conversion.aha3.CLConvert.java

License:Open Source License

private void convertObject(Element element) {
    if ("text/aha".equals(element.attributeValue("type"))) {
        if (element.attributeValue("name") == null) {
            element.addAttribute("type", null);
            element.setQName(DocumentFactory.getInstance().createQName("object", "gale",
                    "http://gale.tue.nl/adaptation"));
        } else {/*from   w  ww . j av a2 s .c o  m*/
            System.out.println("-- found name object: " + element.attributeValue("name"));
            element.addAttribute("type", null);
            element.setQName(DocumentFactory.getInstance().createQName("object", "gale",
                    "http://gale.tue.nl/adaptation"));
            String name = element.attributeValue("name");
            if (name.indexOf(".") >= 0)
                name = name.substring(name.lastIndexOf(".") + 1);
            element.addAttribute("name", name);
        }
    }
}

From source file:org.apache.archiva.xml.XMLReader.java

License:Apache License

/**
 * Remove namespaces from element recursively.
 *//*from   w  w w  . java 2 s.c  om*/
@SuppressWarnings("unchecked")
public void removeNamespaces(Element elem) {
    elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName()));

    Node n;

    Iterator<Node> it = elem.elementIterator();
    while (it.hasNext()) {
        n = it.next();

        switch (n.getNodeType()) {
        case Node.ATTRIBUTE_NODE:
            ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE);
            break;
        case Node.ELEMENT_NODE:
            removeNamespaces((Element) n);
            break;
        }
    }
}

From source file:org.b5chat.crossfire.xmpp.disco.IQDiscoInfoHandler.java

License:Open Source License

@Override
public IQ handleIQ(IQ packet) {
    // Create a copy of the sent pack that will be used as the reply
    // we only need to add the requested info to the reply if any otherwise add 
    // a not found error
    IQ reply = IQ.createResultIQ(packet);

    // Look for a IDiscoInfoProvider associated with the requested entity.
    // We consider the host of the recipient JID of the packet as the entity. It's the 
    // IDiscoInfoProvider responsibility to provide information about the JID's name together 
    // with any possible requested node.  
    IDiscoInfoProvider infoProvider = getProvider(
            packet.getTo() == null ? XmppServer.getInstance().getServerInfo().getXMPPDomain()
                    : packet.getTo().getDomain());
    if (infoProvider != null) {
        // Get the JID's name
        String name = packet.getTo() == null ? null : packet.getTo().getNode();
        if (name == null || name.trim().length() == 0) {
            name = null;// w  ww  . j  a  va2  s  .co  m
        }
        // Get the requested node
        Element iq = packet.getChildElement();
        String node = iq.attributeValue("node");
        //String node = metaData.getProperty("query:node");

        // Check if we have information about the requested name and node
        if (infoProvider.hasInfo(name, node, packet.getFrom())) {
            reply.setChildElement(iq.createCopy());
            Element queryElement = reply.getChildElement();

            // Add to the reply all the identities provided by the IDiscoInfoProvider
            Element identity;
            Iterator<Element> identities = infoProvider.getIdentities(name, node, packet.getFrom());
            while (identities.hasNext()) {
                identity = identities.next();
                identity.setQName(new QName(identity.getName(), queryElement.getNamespace()));
                queryElement.add((Element) identity.clone());
            }

            // Add to the reply all the features provided by the IDiscoInfoProvider
            Iterator<String> features = infoProvider.getFeatures(name, node, packet.getFrom());
            boolean hasDiscoInfoFeature = false;
            boolean hasDiscoItemsFeature = false;
            boolean hasResultSetManagementFeature = false;

            while (features.hasNext()) {
                final String feature = features.next();
                queryElement.addElement("feature").addAttribute("var", feature);
                if (feature.equals(NAMESPACE_DISCO_INFO)) {
                    hasDiscoInfoFeature = true;
                } else if (feature.equals("http://jabber.org/protocol/disco#items")) {
                    hasDiscoItemsFeature = true;
                } else if (feature.equals(ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT)) {
                    hasResultSetManagementFeature = true;
                }
            }

            if (hasDiscoItemsFeature && !hasResultSetManagementFeature) {
                // IQDiscoItemsHandler provides result set management
                // support.
                queryElement.addElement("feature").addAttribute("var",
                        ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT);
            }

            if (!hasDiscoInfoFeature) {
                // XEP-0030 requires that every entity that supports service
                // discovery broadcasts the disco#info feature.
                queryElement.addElement("feature").addAttribute("var", NAMESPACE_DISCO_INFO);
            }

            // Add to the reply the extended info (XDataForm) provided by the IDiscoInfoProvider
            DataForm dataForm = infoProvider.getExtendedInfo(name, node, packet.getFrom());
            if (dataForm != null) {
                queryElement.add(dataForm.getElement());
            }
        } else {
            // If the IDiscoInfoProvider has no information for the requested name and node 
            // then answer a not found error
            reply.setChildElement(packet.getChildElement().createCopy());
            reply.setError(PacketError.Condition.item_not_found);
        }
    } else {
        // If we didn't find a IDiscoInfoProvider then answer a not found error
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.item_not_found);
    }

    return reply;
}

From source file:org.b5chat.crossfire.xmpp.disco.IQDiscoItemsHandler.java

License:Open Source License

@Override
public IQ handleIQ(IQ packet) {
    // Create a copy of the sent pack that will be used as the reply
    // we only need to add the requested items to the reply if any otherwise add 
    // a not found error
    IQ reply = IQ.createResultIQ(packet);

    // TODO Implement publishing client items
    if (IQ.Type.set == packet.getType()) {
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.feature_not_implemented);
        return reply;
    }//from  ww w . j a v  a  2 s.c  om

    // Look for a IDiscoItemsProvider associated with the requested entity.
    // We consider the host of the recipient JID of the packet as the entity. It's the 
    // IDiscoItemsProvider responsibility to provide the items associated with the JID's name  
    // together with any possible requested node.
    IDiscoItemsProvider itemsProvider = getProvider(
            packet.getTo() == null ? XmppServer.getInstance().getServerInfo().getXMPPDomain()
                    : packet.getTo().getDomain());
    if (itemsProvider != null) {
        // Get the JID's name
        String name = packet.getTo() == null ? null : packet.getTo().getNode();
        if (name == null || name.trim().length() == 0) {
            name = null;
        }
        // Get the requested node
        Element iq = packet.getChildElement();
        String node = iq.attributeValue("node");

        // Check if we have items associated with the requested name and node
        Iterator<DiscoItem> itemsItr = itemsProvider.getItems(name, node, packet.getFrom());
        if (itemsItr != null) {
            reply.setChildElement(iq.createCopy());
            Element queryElement = reply.getChildElement();

            // See if the requesting entity would like to apply 'result set
            // management'
            final Element rsmElement = packet.getChildElement()
                    .element(QName.get("set", ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT));

            // apply RSM only if the element exists, and the (total) results
            // set is not empty.
            final boolean applyRSM = rsmElement != null && itemsItr.hasNext();

            if (applyRSM) {
                if (!ResultSet.isValidRSMRequest(rsmElement)) {
                    reply.setError(PacketError.Condition.bad_request);
                    return reply;
                }

                // Calculate which results to include.
                final List<DiscoItem> rsmResults;
                final List<DiscoItem> allItems = new ArrayList<DiscoItem>();
                while (itemsItr.hasNext()) {
                    allItems.add(itemsItr.next());
                }
                final ResultSet<DiscoItem> rs = new ResultSetImpl<DiscoItem>(allItems);
                try {
                    rsmResults = rs.applyRSMDirectives(rsmElement);
                } catch (NullPointerException e) {
                    final IQ itemNotFound = IQ.createResultIQ(packet);
                    itemNotFound.setError(PacketError.Condition.item_not_found);
                    return itemNotFound;
                }

                // add the applicable results to the IQ-result
                for (DiscoItem item : rsmResults) {
                    final Element resultElement = item.getElement();
                    resultElement.setQName(new QName(resultElement.getName(), queryElement.getNamespace()));
                    queryElement.add(resultElement.createCopy());
                }

                // overwrite the 'set' element.
                queryElement.remove(
                        queryElement.element(QName.get("set", ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT)));
                queryElement.add(rs.generateSetElementFromResults(rsmResults));
            } else {
                // don't apply RSM:
                // Add to the reply all the items provided by the IDiscoItemsProvider
                Element item;
                while (itemsItr.hasNext()) {
                    item = itemsItr.next().getElement();
                    item.setQName(new QName(item.getName(), queryElement.getNamespace()));
                    queryElement.add(item.createCopy());
                }
            }
        } else {
            // If the IDiscoItemsProvider has no items for the requested name and node 
            // then answer a not found error
            reply.setChildElement(packet.getChildElement().createCopy());
            reply.setError(PacketError.Condition.item_not_found);
        }
    } else {
        // If we didn't find a IDiscoItemsProvider then answer a not found error
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.item_not_found);
    }

    return reply;
}

From source file:org.codehaus.cargo.util.Dom4JXmlFileBuilder.java

License:Apache License

/**
 * @param element to traverse and change namespace of
 * @param parent - who to match namespaces with.
 *//*from www  .ja v a 2s. c om*/
private void setNamespaceOfElementToTheSameAsParent(Element element, Element parent) {
    final Namespace namespaceOfParent = parent.getNamespace();
    element.accept(new VisitorSupport() {
        @Override
        public void visit(Element node) {
            QName nameOfElementWithCorrectNamespace = new QName(node.getName(), namespaceOfParent);
            node.setQName(nameOfElementWithCorrectNamespace);
        }
    });
}

From source file:org.danann.cernunnos.xml.NodeProcessor.java

License:Apache License

/**
 * Recursively applies the specified <code>Namespace</code> to the specified
 * <code>Element</code>, unless the <code>Element</code> (or any child
 * <code>Element</code>) already specifies a <code>Namespace</code>.
 *
 * @param nsp Namespace to apply./*from   w  ww . j a va  2 s . c o m*/
 * @param e XML structure upon which to apply the Namespace.
 */
public static void applyNamespace(Namespace nsp, Element e) {

    // Assertions...
    if (nsp == null) {
        String msg = "Argument 'nsp' cannot be null.";
        throw new IllegalArgumentException(msg);
    }
    if (e == null) {
        String msg = "Argument 'e [Element]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    if (e.getNamespace().equals(Namespace.NO_NAMESPACE)) {
        e.setQName(new QName(e.getName(), nsp));
        for (Object n : e.elements()) {
            applyNamespace(nsp, (Element) n);
        }
    }

}

From source file:org.etudes.component.app.melete.MeleteAbstractExportServiceImpl.java

License:Apache License

/**
 * creates document root element "manifest" and adds the namespaces
 *
 * @return returns the manifest element/*from  ww  w  .  ja v a  2 s . c  o m*/
 * @throws  Exception
 */
public Element createManifest() throws Exception {
    Element root = DocumentHelper.createElement("manifest");
    //Set up the necessary namespaces
    root.setQName(new QName("manifest", new Namespace(null, DEFAULT_NAMESPACE_URI)));
    root.add(new Namespace("imsmd", getMetaDataNameSpace()));
    root.add(new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));

    /*root.addAttribute("xsi:schemaLocation",
    "http://www.imsglobal.org/xsd/imscp_v1p1 "
          + "http://www.imsglobal.org/xsd/imscp_v1p1.xsd "
          + "http://www.imsglobal.org/xsd/imsmd_v1p2 "
          + "http://www.imsglobal.org/xsd/imsmd_v1p2.xsd ");
    */

    root.addAttribute("identifier", "Manifest-" + getUUID());
    root.addAttribute("version", "IMS CP 1.1.4");
    return root;
}

From source file:org.etudes.component.app.melete.MeleteAbstractExportServiceImpl.java

License:Apache License

/**
 * creates the default namespace element
 * @param elename - element name/*ww w. ja v  a 2 s . c o  m*/
 * @param qname - qualified name
 * @return - returns the default namespace element
 */
public Element createDefaultNSElement(String elename, String qname) {
    Element metadata = DocumentHelper.createElement(elename);
    metadata.setQName(new QName(qname, new Namespace(null, DEFAULT_NAMESPACE_URI)));
    return metadata;
}

From source file:org.etudes.component.app.melete.MeleteAbstractExportServiceImpl.java

License:Apache License

/**
 * creates the LOM metadata element/*w  w w.ja  v a 2  s .  c o  m*/
 * @param elename - element name
 * @param qname - qualified name
 * @return - returns the metadata element
 */
public Element createLOMElement(String elename, String qname) {

    Element imsmdlom = DocumentHelper.createElement(elename);
    imsmdlom.setQName(new QName(qname, new Namespace("imsmd", getMetaDataNameSpace())));

    return imsmdlom;
}