List of usage examples for org.dom4j.io OutputFormat createPrettyPrint
public static OutputFormat createPrettyPrint()
From source file:com.smartwork.im.StreamError.java
License:Open Source License
public String toString() { StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); try {//from w w w . j av a2s . c o m writer.write(element); } catch (Exception e) { } return out.toString(); }
From source file:com.smartwork.im.utils.XMLProperties.java
License:Open Source License
/** * Saves the properties to disk as an XML document. A temporary file is * used during the writing process for maximum safety. *///from ww w . j ava 2 s .c o m private synchronized void saveProperties() { boolean error = false; // Write data out to a temporary file first. File tempFile = null; Writer writer = null; try { tempFile = new File(file.getParentFile(), file.getName() + ".tmp"); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile))); OutputFormat prettyPrinter = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter); xmlWriter.write(document); } catch (Exception e) { logger.error(e.getMessage(), e); // There were errors so abort replacing the old property file. error = true; } finally { if (writer != null) { try { writer.close(); } catch (IOException e1) { logger.error(e1.getMessage(), e1); error = true; } } } // No errors occured, so delete the main file. if (!error) { // Delete the old file so we can replace it. if (!file.delete()) { logger.error("Error deleting property file: " + file.getAbsolutePath()); return; } // Copy new contents to the file. try { copy(tempFile, file); } catch (Exception e) { logger.error(e.getMessage(), e); // There were errors so abort replacing the old property file. error = true; } // If no errors, delete the temp file. if (!error) { tempFile.delete(); } } }
From source file:com.sofrecom.MybatisGenXmlHandler.java
public void saveDocument(Document document) throws FileNotFoundException, UnsupportedEncodingException, IOException { try (FileOutputStream fos = new FileOutputStream(Configuration.XMLTemplate)) { OutputFormat format = OutputFormat.createPrettyPrint(); // Create the xml writer by passing outputstream and format XMLWriter writer = new XMLWriter(fos, format); // Write to the xml document writer.write(document);//from w w w .j a v a2 s . co m // Flush after done writer.flush(); writer.close(); } }
From source file:com.stratumsoft.xmlgen.SchemaTypeXmlGenerator.java
License:Open Source License
/** * Generate an xml representation of the dom structure of the schema element * * @param elName the QName of the element for which the XML should be generated * @param isPrettyPrint if true formats and indents the generated xml * @return the xml instance for the schema element, or empty string if no element with the given qname was found *///from ww w . j av a 2 s . co m public String generateXml(QName elName, boolean isPrettyPrint) { String xml = ""; if (elName != null && schemaColl != null) { Document doc = factory.createDocument("utf-8"); //NON-NLS Element el = generateElement(elName); if (el != null) { doc.add(el); } else { logger.warn("got null for element generated for qname: {}", elName); } if (isPrettyPrint) outputFormat = OutputFormat.createPrettyPrint(); StringWriter sw = new StringWriter(); writer = new XMLWriter(sw, outputFormat); try { writer.write(doc); xml = sw.toString(); logger.trace("Serialized dom4j doc to xml string: {}", xml); } catch (IOException e) { logger.error("dom4j Document to xml creation error", e); } } return xml; }
From source file:com.suneee.core.util.XMLProperties.java
License:Open Source License
/** * Saves the properties to disk as an XML document. A temporary file is * used during the writing process for maximum safety. *///from w w w . j ava 2s .c om private synchronized void saveProperties() { boolean error = false; // Write data out to a temporary file first. File tempFile = null; Writer writer = null; try { tempFile = new File(file.getParentFile(), file.getName() + ".tmp"); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8")); OutputFormat prettyPrinter = OutputFormat.createPrettyPrint(); XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter); xmlWriter.write(document); } catch (Exception e) { Log.error(e.getMessage(), e); // There were errors so abort replacing the old property file. error = true; } finally { if (writer != null) { try { writer.close(); } catch (IOException e1) { Log.error(e1.getMessage(), e1); error = true; } } } // No errors occured, so delete the main file. if (!error) { // Delete the old file so we can replace it. if (!file.delete()) { Log.error("Error deleting property file: " + file.getAbsolutePath()); return; } // Copy new contents to the file. try { copy(tempFile, file); } catch (Exception e) { Log.error(e.getMessage(), e); // There were errors so abort replacing the old property file. error = true; } // If no errors, delete the temp file. if (!error) { tempFile.delete(); } } }
From source file:com.taobao.android.builder.tasks.library.publish.UpdatePomTask.java
License:Apache License
private void updatePomXml(File xml) throws IOException, DocumentException { Map<String, DependencyExtraInfo> extraInfoMap = getExtraMap(); File backupFile = new File(xml.getParentFile(), "pom-backup.xml"); FileUtils.deleteQuietly(backupFile); FileUtils.moveFile(xml, backupFile); XMLWriter writer = null;// XML SAXReader reader = new SAXReader(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");// XML?? FileOutputStream fos = new FileOutputStream(xml); try {//from ww w . ja v a 2s . co m Document document = reader.read(backupFile);// ?XML Element dependencies = document.getRootElement().element("dependencies"); if ((null != dependencies) && null != dependencies.elements()) { List<Element> list = dependencies.elements(); for (Element element : list) { List<Element> itemList = element.elements(); String group = ""; String name = ""; String type; String scope; Element scopeEl = null; Element typeEl = null; for (Element element1 : itemList) { if ("groupId".equals(element1.getQName().getName())) { group = element1.getStringValue(); } else if ("artifactId".equals(element1.getQName().getName())) { name = element1.getStringValue(); } else if ("scope".equals(element1.getQName().getName())) { scope = element1.getStringValue(); scopeEl = element1; } else if ("type".equals(element1.getQName().getName())) { type = element1.getStringValue(); typeEl = element1; } } DependencyExtraInfo dependencyExtraInfo = extraInfoMap.get(group + ":" + name); if (null == dependencyExtraInfo) { continue; } //update scope if (StringUtils.isNotEmpty(dependencyExtraInfo.scope)) { if (null != scopeEl) { scopeEl.setText(dependencyExtraInfo.scope); } else { Element newEl = element.addElement("scope"); newEl.setText(dependencyExtraInfo.scope); } } if (StringUtils.isNotEmpty(dependencyExtraInfo.type)) { if (null != typeEl) { typeEl.setText(dependencyExtraInfo.type); } else { Element newEl = element.addElement("type"); newEl.setText(dependencyExtraInfo.type); } } } } writer = new XMLWriter(fos, format); writer.write(document); } finally { if (null != writer) { writer.close(); } IOUtils.closeQuietly(fos); } }
From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java
License:Apache License
/** * manifest???/*from w w w.j a v a 2s . co m*/ * * @param mainManifest * @param libManifestMap * @param baseBunfleInfoFile * @param manifestOptions */ public static void postProcessManifests(File mainManifest, Map<String, File> libManifestMap, Multimap<String, File> libDependenciesMaps, File baseBunfleInfoFile, ManifestOptions manifestOptions, boolean addMultiDex, Set<String> remoteBundles) throws IOException, DocumentException { File backupFile = new File(mainManifest.getParentFile(), "AndroidManifest-backup.xml"); FileUtils.deleteQuietly(backupFile); FileUtils.moveFile(mainManifest, backupFile); XMLWriter writer = null;// XML SAXReader reader = new SAXReader(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");// XML?? FileOutputStream fos = new FileOutputStream(mainManifest); if (mainManifest.exists()) { try { Document document = reader.read(backupFile);// ?XML if (null != baseBunfleInfoFile && baseBunfleInfoFile.exists()) { addApplicationMetaData(document, libManifestMap, baseBunfleInfoFile, manifestOptions, remoteBundles); } if (null != manifestOptions && manifestOptions.isAddBundleLocation()) { addBundleLocationToDestManifest(document, libManifestMap, libDependenciesMaps, manifestOptions); } if (null != manifestOptions && manifestOptions.isReplaceApplication()) { replaceManifestApplicationName(document); } if ((null != manifestOptions && manifestOptions.isAddMultiDexMetaData()) || addMultiDex) { addMultiDexMetaData(document); } if (null != manifestOptions && manifestOptions.isRemoveProvider()) { removeProvider(document); } removeCustomLaunches(document, manifestOptions); updatePermission(document, manifestOptions); removeComments(document); writer = new XMLWriter(fos, format); writer.write(document); } finally { if (null != writer) { writer.close(); } IOUtils.closeQuietly(fos); } } }
From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java
License:Apache License
/** * libManifest// w w w . ja v a2s. co m * * @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(); format.setEncoding("UTF-8");// XML?? Document document = reader.read(orgManifestFile);// ?XML 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:removetools:replace?ManifestMerge?? Element applicationElement = root.element("application"); Map<String, String> replaceAttrs = mainManifestFileObject.getReplaceApplicationAttribute(); List<String> removeAttrs = mainManifestFileObject.getRemoveApplicationAttribute(); if (null != applicationElement) { // libtools 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); }
From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java
License:Apache License
public static void minifyManifest(File mainManifest, File destManifest) throws IOException, DocumentException { XMLWriter writer = null;// XML SAXReader reader = new SAXReader(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");// XML?? FileOutputStream fos = new FileOutputStream(destManifest); if (mainManifest.exists()) { try {//from w w w . j a va2 s . c o m Document document = reader.read(mainManifest);// ?XML // 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); } } }
From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java
License:Apache License
public static void removeProvider(File androidManifestFile) throws IOException, DocumentException { File backupFile = new File(androidManifestFile.getParentFile(), "AndroidManifest-backup.xml"); FileUtils.deleteQuietly(backupFile); FileUtils.moveFile(androidManifestFile, backupFile); XMLWriter writer = null;// XML SAXReader reader = new SAXReader(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");// XML?? FileOutputStream fos = new FileOutputStream(androidManifestFile); if (androidManifestFile.exists()) { try {/*ww w . j ava 2 s . co m*/ Document document = reader.read(backupFile);// ?XML Element root = document.getRootElement();// List<? extends Node> nodes = root.selectNodes("//provider"); for (Node node : nodes) { Element element = (Element) node; String name = element.attributeValue("name"); logger.info("[Remove Provider]" + name); element.getParent().remove(element); } writer = new XMLWriter(fos, format); writer.write(document); } finally { if (null != writer) { writer.close(); } IOUtils.closeQuietly(fos); } } }