Example usage for org.dom4j QName get

List of usage examples for org.dom4j QName get

Introduction

In this page you can find the example usage for org.dom4j QName get.

Prototype

public static QName get(String localName, Namespace namespace, String qualifiedName) 

Source Link

Usage

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 ww w. j  a  va2 s  .  c  o  m*/
        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);
        }
    }
}