Search in sources :

Example 1 with OutputFormat

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

the class ManifestFileUtils method updatePreProcessManifestFile.

/**
     * 更新libManifest文件
     *
     * @param libManifestFile
     * @param mainManifestFileObject param updateSdkVersion
     */
public static void updatePreProcessManifestFile(File libManifestFile, ManifestFileObject mainManifestFileObject, boolean updateSdkVersion) throws IOException, DocumentException {
    if (!libManifestFile.exists()) {
        return;
    }
    File orgManifestFile = new File(libManifestFile.getParentFile(), "AndroidManifest-org.xml");
    if (orgManifestFile.exists()) {
        return;
    }
    libManifestFile.renameTo(orgManifestFile);
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    // 设置XML文件的编码格式
    format.setEncoding("UTF-8");
    // 读取XML文件
    Document document = reader.read(orgManifestFile);
    // 得到根节点
    Element root = document.getRootElement();
    if (updateSdkVersion) {
        Element useSdkElement = root.element("uses-sdk");
        if (null == useSdkElement) {
            useSdkElement = root.addElement("uses-sdk");
        }
        if (null != useSdkElement) {
            updateElement(useSdkElement, mainManifestFileObject.getUseSdkProperties());
        }
    }
    // 先人工处理一下tools:remove和tools:replace规则,发现有些通过ManifestMerge不一定可以
    Element applicationElement = root.element("application");
    Map<String, String> replaceAttrs = mainManifestFileObject.getReplaceApplicationAttribute();
    List<String> removeAttrs = mainManifestFileObject.getRemoveApplicationAttribute();
    if (null != applicationElement) {
        // 去除lib项目里的tools属性
        List<Attribute> toRomoved = new ArrayList<Attribute>();
        for (Attribute attribute : applicationElement.attributes()) {
            if (attribute.getName().equals("replace") || attribute.getName().equals("remove")) {
                // applicationElement.remove(attribute);
                // applicationElement.attributes().remove(attribute);
                toRomoved.add(attribute);
            }
        }
        if (toRomoved.size() > 0) {
            for (Attribute attribute : toRomoved) {
                applicationElement.remove(attribute);
            }
        }
        updateApplicationElement(applicationElement, replaceAttrs, removeAttrs);
    }
    //处理packageName TODO
    String packageName = root.attributeValue("package");
    if (StringUtils.isEmpty(packageName)) {
        packageName = ManifestFileUtils.getPackage(orgManifestFile);
    }
    List<? extends Node> applicatNodes = root.selectNodes("//application");
    for (Node node : applicatNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute != null) {
            if (!attribute.getValue().startsWith(packageName)) {
                attribute.setValue(packageName + attribute.getValue());
            }
        }
    }
    fillFullClazzName(root, packageName, "activity");
    fillFullClazzName(root, packageName, "provider");
    fillFullClazzName(root, packageName, "receiver");
    fillFullClazzName(root, packageName, "service");
    saveFile(document, format, libManifestFile);
}
Also used : Attribute(org.dom4j.Attribute) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Node(org.dom4j.Node) OutputFormat(org.dom4j.io.OutputFormat) ArrayList(java.util.ArrayList) Document(org.dom4j.Document) File(java.io.File)

Example 2 with OutputFormat

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

the class XMLUtil method formatXMLFile.

/**
     * format the exist xml file.
     * 
     * @param filename
     * @return
     */
public static int formatXMLFile(String filename, String enCode) {
    int returnValue = 0;
    try {
        SAXReader saxReader = new SAXReader();
        org.dom4j.Document document = saxReader.read(new File(filename));
        XMLWriter writer = null;
        /** format the output like the webBrowser */
        OutputFormat format = OutputFormat.createPrettyPrint();
        /** give the xml encoding */
        format.setEncoding(enCode);
        writer = new XMLWriter(new FileWriter(new File(filename)), format);
        writer.write(document);
        writer.close();
        /** succes will retun 1 */
        returnValue = 1;
    } catch (Exception ex) {
        // ex.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(ex);
    }
    return returnValue;
}
Also used : SAXReader(org.dom4j.io.SAXReader) FileWriter(java.io.FileWriter) OutputFormat(org.dom4j.io.OutputFormat) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException)

Example 3 with OutputFormat

use of org.dom4j.io.OutputFormat in project eweb4j-framework by laiweiwei.

the class BeanXMLWriter method write.

public File write() throws Exception {
    Document doc = createDoc();
    // 读取文件
    FileOutputStream fos = new FileOutputStream(this.file);
    // 设置文件编码
    OutputFormat format = OutputFormat.createPrettyPrint();
    // 创建写文件方法
    org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(fos, format);
    // 写入文件
    xmlWriter.write(doc);
    // 关闭
    fos.close();
    xmlWriter.close();
    return this.file;
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document)

Example 4 with OutputFormat

use of org.dom4j.io.OutputFormat in project dq-easy-cloud by dq-open-cloud.

the class Dom4jTest method testAdd.

@Test
public void testAdd() throws Exception {
    SAXReader reader = new SAXReader();
    Document document = reader.read(new File(path));
    Element resultMap = document.getRootElement().element("resultMap");
    List list = resultMap.elements();
    // 设置新增的person的信息
    Element newEle = DocumentHelper.createElement("result");
    newEle.addAttribute("column", "update_by");
    newEle.addAttribute("jdbcType", "VARCHAR");
    newEle.addAttribute("property", "updateBy");
    list.add(2, newEle);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format = new OutputFormat();
    format.setIndentSize(4);
    format.setNewlines(true);
    format.setTrimText(true);
    format.setPadText(true);
    format.setEncoding("UTF-8");
    XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(path)), format);
    writer.write(document);
    writer.close();
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) Document(org.dom4j.Document) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter) Test(org.junit.Test)

Example 5 with OutputFormat

use of org.dom4j.io.OutputFormat in project cubrid-manager by CUBRID.

the class FormatXMLAction method writeTo.

/**
	 * Format XML strings to a output stream.
	 *
	 * @param out OutputStream
	 * @param content the content to be formated.
	 * @param encoding String
	 * @throws DocumentException when document error raised.
	 * @throws IOException when IO errors.
	 */
public static void writeTo(OutputStream out, String content, String encoding) throws DocumentException, IOException {
    StringReader reader = new StringReader(content);
    SAXReader xmlReader = new SAXReader();
    Document doc = xmlReader.read(reader);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(encoding);
    format.setIndent(true);
    format.setIndent(" ");
    format.setIndentSize(4);
    XMLWriter writer = new XMLWriter(out, format);
    writer.write(doc);
    writer.flush();
    writer.close();
    reader.close();
}
Also used : SAXReader(org.dom4j.io.SAXReader) StringReader(java.io.StringReader) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) 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