Search in sources :

Example 41 with OutputFormat

use of org.dom4j.io.OutputFormat in project atlas by alibaba.

the class ManifestFileUtils method minifyManifest.

public static void minifyManifest(File mainManifest, File destManifest) throws IOException, DocumentException {
    // 声明写XML的对象
    XMLWriter writer = null;
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    // 设置XML文件的编码格式
    format.setEncoding("UTF-8");
    FileOutputStream fos = new FileOutputStream(destManifest);
    if (mainManifest.exists()) {
        try {
            // 读取XML文件
            Document document = reader.read(mainManifest);
            //                removeComments(document);
            Element element = document.getRootElement();
            element.clearContent();
            writer = new XMLWriter(fos, format);
            writer.write(document);
        } finally {
            if (null != writer) {
                writer.close();
            }
            IOUtils.closeQuietly(fos);
        }
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) FileOutputStream(java.io.FileOutputStream) Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 42 with OutputFormat

use of org.dom4j.io.OutputFormat in project tdi-studio-se by Talend.

the class JobJavaScriptsWSManager method editWSDDFile.

/**
     * DOC x Comment method "genWebInfoForder".
     * 
     * @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 + //$NON-NLS-1$
    "deploy.wsdd";
    //$NON-NLS-1$
    String serverConfigFile = getTmpFolder() + PATH_SEPARATOR + "server-config.wsdd";
    File deployFile = new File(deployFileName);
    if (!deployFile.exists()) {
        //$NON-NLS-1$
        log.error(org.talend.repository.i18n.Messages.getString("JobJavaScriptsWSManager.errorMessage"));
        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();
        //$NON-NLS-1$
        Element element = wsdlroot.element("service");
        //$NON-NLS-1$
        List<Element> elements = element.elements("arrayMapping");
        for (Element item : elements) {
            //$NON-NLS-1$
            Attribute attribute = item.attribute("qname");
            item.remove(attribute);
            //$NON-NLS-1$ //$NON-NLS-2$
            attribute.setValue(attribute.getValue().replaceFirst(">", ""));
            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;
                }
            }
        }
        //$NON-NLS-1$
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(serverConfigFile), "UTF-8"));
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter output = new XMLWriter(writer, format);
        output.write(doc);
        output.flush();
        output.close();
    } catch (Exception e) {
        ExceptionHandler.process(e);
    }
}
Also used : Attribute(org.dom4j.Attribute) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Node(org.dom4j.Node) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) ProcessorException(org.talend.designer.runprocess.ProcessorException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 43 with OutputFormat

use of org.dom4j.io.OutputFormat in project tdi-studio-se by Talend.

the class ComponentFolderManager method writeXMLContent.

/**
     * DOC slanglois Comment method "writeXMLContent".
     * 
     * @param iFile
     * @param document
     * @param enCode
     * @throws CoreException
     */
private void writeXMLContent(IFile iFile, Document document, String enCode) throws CoreException {
    PrintWriter pw = null;
    XMLWriter writer = null;
    byte[] byteArray = null;
    // get xml content as inputstream
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty(OutputKeys.ENCODING, enCode);
    //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    ByteArrayOutputStream sw = new ByteArrayOutputStream();
    pw = new PrintWriter(sw);
    StreamResult result = new StreamResult(pw);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    try {
        sw.flush();
    } catch (IOException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
    byteArray = sw.toByteArray();
    // format the xml content
    SAXReader saxReader = new SAXReader();
    org.dom4j.Document dom4jDocument = null;
    try {
        dom4jDocument = saxReader.read(new ByteArrayInputStream(byteArray));
    } catch (DocumentException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    /** format the output like the webBrowser */
    OutputFormat format = OutputFormat.createPrettyPrint();
    /** give the xml encoding */
    format.setEncoding(enCode);
    sw = new ByteArrayOutputStream();
    try {
        writer = new XMLWriter(sw, format);
    } catch (UnsupportedEncodingException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    try {
        writer.write(dom4jDocument);
        writer.flush();
    } catch (IOException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                // e.printStackTrace();
                org.talend.componentdesigner.exception.ExceptionHandler.process(e);
            }
        }
    }
    byteArray = sw.toByteArray();
    // write content
    iFile.setContents(new ByteArrayInputStream(byteArray), true, false, null);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) SAXReader(org.dom4j.io.SAXReader) OutputFormat(org.dom4j.io.OutputFormat) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentException(org.dom4j.DocumentException) TransformerException(javax.xml.transform.TransformerException) PrintWriter(java.io.PrintWriter)

Example 44 with OutputFormat

use of org.dom4j.io.OutputFormat in project cuba by cuba-platform.

the class Dom4j method writeDocument.

public static void writeDocument(Document doc, boolean prettyPrint, Writer writer) {
    XMLWriter xmlWriter;
    try {
        if (prettyPrint) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            xmlWriter = new XMLWriter(writer, format);
        } else {
            xmlWriter = new XMLWriter(writer);
        }
        xmlWriter.write(doc);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write XML", e);
    }
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter)

Example 45 with OutputFormat

use of org.dom4j.io.OutputFormat in project cuba by cuba-platform.

the class Dom4j method writeDocument.

public static void writeDocument(Document doc, boolean prettyPrint, OutputStream stream) {
    XMLWriter xmlWriter;
    try {
        if (prettyPrint) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            xmlWriter = new XMLWriter(stream, format);
        } else {
            xmlWriter = new XMLWriter(stream);
        }
        xmlWriter.write(doc);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write XML", e);
    }
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter)

Aggregations

OutputFormat (org.dom4j.io.OutputFormat)89 XMLWriter (org.dom4j.io.XMLWriter)75 IOException (java.io.IOException)39 Document (org.dom4j.Document)31 Element (org.dom4j.Element)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 StringWriter (java.io.StringWriter)18 SAXReader (org.dom4j.io.SAXReader)16 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)14 FileWriter (java.io.FileWriter)10 OutputStreamWriter (java.io.OutputStreamWriter)8 StringReader (java.io.StringReader)6 DocumentException (org.dom4j.DocumentException)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4