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);
}
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;
}
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;
}
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();
}
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();
}
Aggregations