Example usage for org.dom4j Attribute setValue

List of usage examples for org.dom4j Attribute setValue

Introduction

In this page you can find the example usage for org.dom4j Attribute setValue.

Prototype

void setValue(String value);

Source Link

Document

Sets the value of this attribute or this method will throw an UnsupportedOperationException if it is read-only.

Usage

From source file:org.pentaho.agilebi.spoon.publish.PublisherHelper.java

License:Open Source License

private static String replaceAttributeValue(String aElement, String anAttribute, String aValue, String aFile)
        throws Exception {

    String originalValue = null;/* w  w w.  j a  va2 s  . co m*/
    if (aFile != null) {
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File(aFile));
        Element root = doc.getRootElement();

        for (Iterator<Element> i = root.elementIterator(); i.hasNext();) {
            Element element = i.next();
            if (element.getName().equals(aElement)) {
                Attribute attr = element.attribute(anAttribute);
                originalValue = attr.getValue();
                attr.setValue(aValue);
            }
        }

        XMLWriter writer = new XMLWriter(new FileWriter(aFile));
        writer.write(doc);
        writer.close();
    }
    return originalValue;
}

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  ava  2s  . 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);
        }
    }
}

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

License:Open Source License

private static void formatSchemaLocation(Element root, boolean addBlueprint, boolean addCamel) {
    Attribute schemaLocation = root.attribute("schemaLocation");
    if (schemaLocation == null) {
        return;//from w w  w. j av a2  s .  c  om
    }
    String value = schemaLocation.getValue().replaceAll("(\\A|\\b)\\s\\s+\\b", "\n            ");
    if (addBlueprint) {
        value += "\n            http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd";
    }
    if (addCamel) {
        value += "\n            http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd";
    }
    schemaLocation.setValue(value);
}

From source file:org.talend.designer.documentation.generation.HTMLDocGenerator.java

License:Open Source License

private List<URL> parseXml2HtmlPdf(String tempFolderPath, String jobName, String xslFilePath) throws Exception {
    // clear the cache, maybe need improve it latter.
    HTMLHandler.clearExternalNodeFileCache();
    String htmlFilePath = tempFolderPath + File.separatorChar + jobName + IHTMLDocConstants.HTML_FILE_SUFFIX;
    String xmlFilePath = tempFolderPath + File.separatorChar + jobName + IHTMLDocConstants.XML_FILE_SUFFIX;
    HTMLHandler.generateHTMLFile(tempFolderPath, xslFilePath, xmlFilePath, htmlFilePath,
            this.externalNodeHTMLMap);

    // for pdf/*from  w w w. jav a  2  s  .  co  m*/
    File originalXmlFile = new File(xmlFilePath);
    if (originalXmlFile.exists()) {
        String pdfXmlPath = tempFolderPath + File.separatorChar + "pdf_" + jobName //$NON-NLS-1$
                + IHTMLDocConstants.XML_FILE_SUFFIX;
        File pdfXmlFile = new File(pdfXmlPath);
        if (pdfXmlFile.exists()) {
            pdfXmlFile.delete();
        }
        FilesUtils.copyFile(originalXmlFile, pdfXmlFile);

        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(pdfXmlPath);
        Attribute attri = (Attribute) document.selectNodes("/project/job/preview/@picture").get(0); //$NON-NLS-1$
        attri.setValue(IHTMLDocConstants.PICTUREFOLDERPATH + "pdf_" + jobName //$NON-NLS-1$
                + IHTMLDocConstants.JOB_PREVIEW_PIC_SUFFIX);

        List attributeList = document.selectNodes("/project/job/externalNodeComponents/component/@preview"); //$NON-NLS-1$
        for (int i = 0; i < attributeList.size(); i++) {
            Attribute at = (Attribute) attributeList.get(i);
            String externalValue = at.getValue().substring(at.getValue().indexOf("/") + 1); //$NON-NLS-1$
            String value = IHTMLDocConstants.PICTUREFOLDERPATH + "pdf_" + externalValue; //$NON-NLS-1$
            at.setValue(value);
        }

        XMLHandler.generateXMLFile(tempFolderPath, pdfXmlPath, document);

        HTMLHandler.clearExternalNodeFileCache();
        String htmlPdfPath = tempFolderPath + File.separatorChar + "pdf_" + jobName //$NON-NLS-1$
                + IHTMLDocConstants.HTML_FILE_SUFFIX;
        HTMLHandler.generateHTMLFile(tempFolderPath, xslFilePath, pdfXmlPath, htmlPdfPath,
                this.externalNodeHTMLMap);
    }

    return getParsedUrl(tempFolderPath);
}

From source file:org.talend.repository.ui.wizards.exportjob.scriptsmanager.JobJavaScriptsWSManager.java

License:Open Source License

/**
 * DOC x Comment method "genWebInfoForder".
 * //www  .jav a 2s. c  o  m
 * @param list
 */
private void editWSDDFile(ProcessItem processItem) {
    String projectName = getCorrespondingProjectName(processItem);
    String selectedProcessVersion = processItem.getProperty().getVersion();
    if (!isMultiNodes() && this.getSelectedJobVersion() != null) {
        selectedProcessVersion = this.getSelectedJobVersion();
    }

    String jobFolderName = JavaResourcesHelper.getJobFolderName(escapeFileNameSpace(processItem),
            selectedProcessVersion);

    String deployFileName = getTmpFolder() + PATH_SEPARATOR + projectName + PATH_SEPARATOR + jobFolderName
            + PATH_SEPARATOR + "deploy.wsdd"; //$NON-NLS-1$
    String serverConfigFile = getTmpFolder() + PATH_SEPARATOR + "server-config.wsdd"; //$NON-NLS-1$

    File deployFile = new File(deployFileName);
    if (!deployFile.exists()) {
        log.error(org.talend.repository.i18n.Messages.getString("JobJavaScriptsWSManager.errorMessage")); //$NON-NLS-1$
        return;
    }
    // edit the server-config.wsdd file
    try {

        File wsddFile = new File(serverConfigFile);
        BufferedReader reader = new BufferedReader(new FileReader(wsddFile));

        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(reader);

        BufferedReader wsdlreader = new BufferedReader(new FileReader(deployFile));
        SAXReader wsdlsaxReader = new SAXReader();
        Document wsdldoc = wsdlsaxReader.read(wsdlreader);
        Element wsdlroot = wsdldoc.getRootElement();
        Element element = wsdlroot.element("service"); //$NON-NLS-1$

        List<Element> elements = element.elements("arrayMapping"); //$NON-NLS-1$
        for (Element item : elements) {
            Attribute attribute = item.attribute("qname"); //$NON-NLS-1$
            item.remove(attribute);
            attribute.setValue(attribute.getValue().replaceFirst(">", "")); //$NON-NLS-1$ //$NON-NLS-2$
            item.add(attribute);
        }

        Element root = doc.getRootElement();
        List<Node> content = root.content();
        for (int i = 0; i < content.size(); i++) {
            Node n = content.get(i);
            if (n instanceof Element) {
                if (n.getName().equals("transport")) { //$NON-NLS-1$
                    content.add(i - 1, element);
                    break;
                }
            }
        }

        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(serverConfigFile), "UTF-8")); //$NON-NLS-1$

        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter output = new XMLWriter(writer, format);
        output.write(doc);
        output.flush();
        output.close();

    } catch (Exception e) {
        ExceptionHandler.process(e);
    }
}

From source file:org.tsp.bws.BWSDocument.java

License:Open Source License

/** 
 * Get and rewrite all calls to a specific BWS occuring in the document.
 *
 * @param attributeValue the id of the script that is searched for.
 *///from  w w w  .  j a v a 2s .c  om
public void getAttributeElement(String attributeValue) {
    XPath xpathSelector = DocumentHelper.createXPath("//@*");

    List results = xpathSelector.selectNodes(xmlDocument);
    List elementAttributes;

    Attribute curAttribute;

    for (Iterator attributeIterator = results.iterator(); attributeIterator.hasNext();) {
        curAttribute = (Attribute) attributeIterator.next();
        if (debug > 0) {
            System.out.println(attributeValue + "-" + curAttribute.getValue() + "-"
                    + curAttribute.getValue().equals("#:" + attributeValue));
        }
        if ((curAttribute.getValue().startsWith("bws:") || curAttribute.getValue().startsWith("#:"))
                && curAttribute.getValue().indexOf(attributeValue) > 0) {
            //curAttribute.setValue("document.getElementById('BWSApplet').executeScript('" + attributeValue + "',this)");
            //System.out.println("#:"  + curAttribute.getValue().indexOf(":"));
            curAttribute.setValue("document.getElementById('BWSApplet').executeScript('"
                    + curAttribute.getValue().substring(curAttribute.getValue().indexOf(":") + 1) + "',this)");
            //curAttribute.setValue("javascript:bwsexec(" + attributeValue + ")");
        }
    }

}

From source file:org.unitime.commons.ant.LowercaseTableNames.java

License:Open Source License

private void convert(Element element) {
    for (Iterator<Attribute> i = element.attributeIterator(); i.hasNext();) {
        Attribute attribute = i.next();
        for (String name : sAttributes) {
            if (name.equals(attribute.getName())) {
                if (!attribute.getValue().equals(attribute.getValue().toLowerCase())) {
                    info("  -- converting " + name + " " + attribute.getValue() + " to "
                            + attribute.getValue().toLowerCase());
                }/* w w  w. j ava  2 s  . c  o m*/
                attribute.setValue(attribute.getValue().toLowerCase());
            }
        }
        for (String name : sFormulas) {
            if (name.equals(attribute.getName())) {
                if (!lowerFormula(attribute.getValue()).equals(attribute.getValue())) {
                    info("  -- converting " + name + ": " + attribute.getValue());
                    info("  -- into : " + lowerFormula(attribute.getValue()));
                }
                attribute.setValue(lowerFormula(attribute.getValue()));
            }
        }
    }
    if (element.getName().equals("param")) {
        for (String name : sAttributes) {
            if (name.equals(element.attributeValue("name", ""))) {
                if (!element.getText().equals(element.getText().toLowerCase())) {
                    info("  -- converting " + name + " " + element.getText() + " to "
                            + element.getText().toLowerCase());
                }
                element.setText(element.getText().toLowerCase());
            }
        }
    }
    for (Iterator<Element> i = element.elementIterator(); i.hasNext();) {
        convert(i.next());
    }
}

From source file:org.withinsea.izayoi.cortile.template.html.grammar.attr.core.Output.java

License:Mozilla Public License

@Override
@Priority(Priority.LOWER)/*from   w w w  .  j  av a  2s. c o m*/
public void processAttr(Attribute attr) throws CortileException {

    Element elem = attr.getParent();
    String attrname = attr.getName();
    String attrvalue = attr.getValue();

    if (attrname.startsWith("attr.")) {
        String forAttrname = attrname.substring("attr.".length());
        Attribute forAttr = elem.attribute(forAttrname);
        String escapedValue = attrvalue
                .replace("<%=", "<%=" + Output.class.getCanonicalName() + ".escapeAttrValue(")
                .replace("%>", ")%>");
        if (forAttr == null) {
            elem.addAttribute(forAttrname, escapedValue);
        } else {
            forAttr.setValue(escapedValue);
        }
    } else {
        elem.clearContent();
        elem.addText(attr.getValue());
    }

    attr.detach();
}

From source file:org.withinsea.izayoi.cortile.template.html.grammar.el.EmbeddedEL.java

License:Mozilla Public License

@Override
@Priority(Priority.LOW)/*from w  w w .  j a  va 2  s .  c  o m*/
public void processAttr(Attribute attr) throws CortileException {
    attr.setValue(compileEmbeddedELs(attr.getValue()));
}