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:org.jivesoftware.openfire.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;
    }/*  www.j a v a  2s.  c o m*/

    // Look for a DiscoItemsProvider associated with the requested entity.
    // We consider the host of the recipient JID of the packet as the entity. It's the 
    // DiscoItemsProvider responsibility to provide the items associated with the JID's name  
    // together with any possible requested node.
    DiscoItemsProvider 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 DiscoItemsProvider
                Element item;
                while (itemsItr.hasNext()) {
                    item = itemsItr.next().getElement();
                    item.setQName(new QName(item.getName(), queryElement.getNamespace()));
                    queryElement.add(item.createCopy());
                }
            }
        } else {
            // If the DiscoItemsProvider 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 DiscoItemsProvider then answer a not found error
        reply.setChildElement(packet.getChildElement().createCopy());
        reply.setError(PacketError.Condition.item_not_found);
    }

    return reply;
}

From source file:org.nuxeo.ecm.core.io.impl.transformers.SchemaRenamer.java

License:Open Source License

@Override
public boolean transform(ExportedDocument xdoc) throws IOException {
    Element root = xdoc.getDocument().getRootElement();

    List<Object> schemas = root.elements("schema");
    Element src = null;//from   www.ja va 2s .co m
    if (schemas != null) {
        for (Object s : schemas) {
            Element schema = (Element) s;
            String name = schema.attribute("name").getText();
            if (srcSchema.equalsIgnoreCase(name)) {
                Namespace ns = new Namespace(dstPrefix, "http://www.nuxeo.org/ecm/schemas/" + dstSchema);
                schema.add(ns);
                schema.setAttributeValue("name", dstSchema);
                List<Element> fields = schema.elements();
                for (Element field : fields) {
                    field.setQName(new QName(field.getName(), ns));
                    ;
                }
            }
        }
    }
    return true;
}

From source file:org.nuxeo.ecm.jsf2.migration.parser.GenericParser.java

License:Open Source License

@Override
public void migrate(Document input) throws Exception {
    // Migrate the elements matching the rule
    if (rule.isMigrationAuto()) {
        for (Node node : listElementsToMigrate) {
            if (!StringUtils.isEmpty(rule.getNewValue())) {
                Element element = (Element) node;
                String newValue = rule.getNewValue();
                String prefix = null;
                if (newValue.contains(":")) {
                    String[] split = newValue.split(":");
                    prefix = split[0];/* w  w w . java 2 s.  c  o  m*/
                    newValue = split[1];
                }
                element.setQName(new QName(newValue, input.getRootElement().getNamespaceForPrefix(prefix)));
            }
        }
    }
}

From source file:org.nuxeo.ecm.jsf2.migration.parser.NamespaceParser.java

License:Open Source License

@Override
public void migrate(Document input) throws Exception {
    Element root = input.getRootElement();
    for (String prefix : listPrefixToMigrate) {
        Namespace newNamespace = new Namespace(prefix, EnumPrefixes.getPrefix(prefix).getNamespace());
        Namespace oldNamespace = root.getNamespaceForPrefix(prefix);
        if (oldNamespace != null) {
            root.remove(oldNamespace);//  w  ww .jav  a  2s . c om
        }
        root.add(newNamespace);

        // Change the name of every elements with the prefix
        StringBuilder prefixXpath = new StringBuilder("//");
        prefixXpath.append(prefix);
        prefixXpath.append(":*");
        // Create a new XPath expression, with the old namespace in order
        // to
        // get the elements matching the expression
        XPath xpath = new Dom4jXPath(prefixXpath.toString());
        SimpleNamespaceContext nc = new SimpleNamespaceContext();
        nc.addNamespace(prefix, oldNamespace.getURI());
        xpath.setNamespaceContext(nc);

        @SuppressWarnings("unchecked")
        List<Element> elementsToMigrate = xpath.selectNodes(input);
        for (Element element : elementsToMigrate) {
            // The namespace to change is not hold by the element but the
            // QName
            QName qname = element.getQName();
            QName newQName = new QName(qname.getName(), newNamespace, qname.getQualifiedName());
            element.setQName(newQName);
        }
    }
}

From source file:org.openxml4j.opc.signature.RelationshipTransform.java

License:Apache License

@SuppressWarnings("unchecked")
public static void RemoveAllNameSpacesExceptRelationship(Element elem) {

    // if the default namespace is not correct or if it has a prefix
    // fix it by setting to correct value without prefix
    if (elem.getNamespace().getStringValue() != PackageNamespaces.RELATIONSHIPS
            || elem.getNamespace().getPrefix() != "") {
        elem.setQName(new QName(elem.getName(),
                DocumentFactory.getInstance().createNamespace("", PackageNamespaces.RELATIONSHIPS)));
    }// w  ww.  j  a v  a  2 s  . co  m

    // remove all additional namespace declarations
    List<Namespace> additionalNameSpaces = elem.additionalNamespaces();
    for (Namespace nms : additionalNameSpaces) {
        elem.remove(nms);
    }
}

From source file:org.talend.camel.designer.ui.wizards.export.RouteJavaScriptOSGIForESBManager.java

License:Open Source License

private void handleSpringXml(String targetFilePath, ProcessItem processItem, InputStream springInput,
        ExportFileResource osgiResource, boolean convertToBP, boolean convertImports) {

    File targetFile = new File(getTmpFolder() + PATH_SEPARATOR + targetFilePath);
    try {/*from   w  ww .j a  va2s .com*/
        SAXReader saxReader = new SAXReader();
        saxReader.setStripWhitespaceText(false);
        Document document = saxReader.read(springInput);
        Element root = document.getRootElement();

        if (convertToBP) {
            if ("blueprint".equals(root.getName())) {
                formatSchemaLocation(root, false, false);
                InputStream inputStream = new ByteArrayInputStream(root.asXML().getBytes());
                FilesUtils.copyFile(inputStream, targetFile);
                osgiResource.addResource(FileConstants.BLUEPRINT_FOLDER_NAME, targetFile.toURI().toURL());
                return;
            }

            String bpPrefix = "bp";
            int cnt = 0;
            while (root.getNamespaceForPrefix(bpPrefix) != null) {
                bpPrefix = "bp" + (++cnt);
            }
            root.setQName(QName.get("blueprint", bpPrefix, BLUEPRINT_NSURI));
        }

        Namespace springCamelNsp = Namespace.get("camel", CAMEL_SPRING_NSURI);
        boolean addCamel = springCamelNsp.equals(root.getNamespaceForPrefix("camel"));
        formatSchemaLocation(root, convertToBP, addCamel);

        for (Iterator<?> i = root.elementIterator("import"); i.hasNext();) {
            Element ip = (Element) i.next();
            Attribute resource = ip.attribute("resource");
            URL path = dummyURL(resource.getValue());
            for (ResourceDependencyModel resourceModel : RouteResourceUtil
                    .getResourceDependencies(processItem)) {
                if (matches(path, resourceModel)) {
                    IFile resourceFile = RouteResourceUtil.getSourceFile(resourceModel.getItem());
                    String cpUrl = adaptedClassPathUrl(resourceModel, convertImports);
                    handleSpringXml(cpUrl, processItem, resourceFile.getContents(), osgiResource,
                            convertImports, convertImports);
                    resource.setValue(IMPORT_RESOURCE_PREFIX + cpUrl);
                }
            }
            if (convertImports) {
                i.remove();
            }
        }

        if (CONVERT_CAMEL_CONTEXT) {
            if (CONVERT_CAMEL_CONTEXT_ALL) {
                moveNamespace(root, CAMEL_SPRING_NSURI, CAMEL_BLUEPRINT_NSURI);
            } else {
                Namespace blueprintCamelNsp = Namespace.get("camel", CAMEL_BLUEPRINT_NSURI);
                moveNamespace(root, springCamelNsp, blueprintCamelNsp);
                if (springCamelNsp.equals(root.getNamespaceForPrefix("camel"))) {
                    root.remove(springCamelNsp);
                    root.add(blueprintCamelNsp);
                }
                Namespace springCamelDefNsp = Namespace.get(CAMEL_SPRING_NSURI);
                Namespace blueprintCamelDefNsp = Namespace.get(CAMEL_BLUEPRINT_NSURI);
                for (Iterator<?> i = root.elementIterator("camelContext"); i.hasNext();) {
                    Element cc = (Element) i.next();
                    if (springCamelDefNsp.equals(cc.getNamespace())) {
                        moveNamespace(cc, springCamelDefNsp, blueprintCamelDefNsp);
                    }
                }
            }
        }

        InputStream inputStream = new ByteArrayInputStream(root.asXML().getBytes());
        FilesUtils.copyFile(inputStream, targetFile);
        osgiResource.addResource(adaptedResourceFolderName(targetFilePath, convertToBP),
                targetFile.toURI().toURL());
    } catch (Exception e) {
        Logger.getAnonymousLogger().log(Level.WARNING, "Custom Spring to OSGi conversion failed. ", e);
    } finally {
        try {
            springInput.close();
        } catch (IOException e) {
            Logger.getAnonymousLogger().log(Level.WARNING, "Unexpected File closing failure. ", e);
        }
    }
}

From source file:org.talend.camel.designer.ui.wizards.export.RouteJavaScriptOSGIForESBManager.java

License:Open Source License

private static void moveNamespace(Element treeRoot, Namespace oldNsp, Namespace newNsp) {
    if (treeRoot.getNamespace().equals(oldNsp)) {
        treeRoot.setQName(QName.get(treeRoot.getName(), newNsp));
        treeRoot.remove(oldNsp);/*from ww w .j ava2 s .  c o m*/
    }
    moveNamespaceInChildren(treeRoot, oldNsp, newNsp);
}

From source file:org.talend.camel.designer.ui.wizards.export.RouteJavaScriptOSGIForESBManager.java

License:Open Source License

private static void moveNamespaceInChildren(Element treeRoot, Namespace oldNsp, Namespace newNsp) {
    for (Iterator<?> i = treeRoot.elementIterator(); i.hasNext();) {
        Element e = (Element) i.next();
        if (e.getNamespace().equals(oldNsp)) {
            e.setQName(QName.get(e.getName(), newNsp));
            e.remove(oldNsp);/*from   w  w w . j  a  v  a2 s  .  c  om*/
        }
        moveNamespaceInChildren(e, oldNsp, newNsp);
    }
}

From source file:org.talend.camel.designer.ui.wizards.export.RouteJavaScriptOSGIForESBManager.java

License:Open Source License

private static void moveNamespace(Element treeRoot, String oldNspURI, String newNspURI) {
    Namespace oldNsp = treeRoot.getNamespace();
    if (oldNspURI.equals(oldNsp.getURI())) {
        Namespace newNsp = Namespace.get(oldNsp.getPrefix(), newNspURI);
        treeRoot.setQName(QName.get(treeRoot.getName(), newNsp));
        treeRoot.remove(oldNsp);/*from www.ja v  a2  s .c o  m*/
    }
    moveNamespaceInChildren(treeRoot, oldNspURI, newNspURI);
}

From source file:org.talend.camel.designer.ui.wizards.export.RouteJavaScriptOSGIForESBManager.java

License:Open Source License

private static void moveNamespaceInChildren(Element treeRoot, String oldNspURI, String newNspURI) {
    for (Iterator<?> i = treeRoot.elementIterator(); i.hasNext();) {
        Element e = (Element) i.next();
        Namespace oldNsp = e.getNamespace();
        if (oldNspURI.equals(oldNsp.getURI())) {
            Namespace newNsp = Namespace.get(oldNsp.getPrefix(), newNspURI);
            e.setQName(QName.get(e.getName(), newNsp));
            e.remove(oldNsp);//  www  .  j a v  a2  s  .  com
        }
        moveNamespaceInChildren(e, oldNspURI, newNspURI);
    }
}