Author: adrianc
Date: Tue Jun 30 17:07:07 2009 New Revision: 789830 URL: http://svn.apache.org/viewvc?rev=789830&view=rev Log: Code cleanup in UtilXml.java - eliminated some redundant methods. Document, DocumentFragment, and Element all extend the Node interface, so we only need one method that accepts a Node argument. This change is completely backwards compatible. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilXml.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilXml.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilXml.java?rev=789830&r1=789829&r2=789830&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilXml.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilXml.java Tue Jun 30 17:07:07 2009 @@ -21,6 +21,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -228,87 +229,53 @@ // ------------------------------------ // - public static String writeXmlDocument(Document document) throws java.io.IOException { - if (document == null) { - Debug.logWarning("[UtilXml.writeXmlDocument] Document was null, doing nothing", module); + public static String writeXmlDocument(Node node) throws java.io.IOException { + if (node == null) { + Debug.logWarning("[UtilXml.writeXmlDocument] Node was null, doing nothing", module); return null; } - return writeXmlDocument(document.getDocumentElement()); - } - - public static String writeXmlDocument(Element element) throws java.io.IOException { - if (element == null) { - Debug.logWarning("[UtilXml.writeXmlDocument] Element was null, doing nothing", module); - return null; - } - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - writeXmlDocument(bos, element); - String outString = bos.toString("UTF-8"); - - if (bos != null) bos.close(); - return outString; - } - - public static String writeXmlDocument(DocumentFragment fragment) throws java.io.IOException { - if (fragment == null) { - Debug.logWarning("[UtilXml.writeXmlDocument] DocumentFragment was null, doing nothing", module); - return null; - } - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - List<? extends Element> elementList = UtilXml.childElementList(fragment); - for (Element element: elementList) { - writeXmlDocument(bos, element); - } - String outString = bos.toString("UTF-8"); - - if (bos != null) bos.close(); - return outString; + writeXmlDocument(bos, node); + return bos.toString("UTF-8"); } - public static void writeXmlDocument(String filename, Document document) - throws java.io.FileNotFoundException, java.io.IOException { - if (document == null) { - Debug.logWarning("[UtilXml.writeXmlDocument] Document was null, doing nothing", module); - return; - } - writeXmlDocument(filename, document.getDocumentElement()); - } - - public static void writeXmlDocument(String filename, Element element) - throws java.io.FileNotFoundException, java.io.IOException { - if (element == null) { - Debug.logWarning("[UtilXml.writeXmlDocument] Element was null, doing nothing", module); + public static void writeXmlDocument(String filename, Node node) throws FileNotFoundException, IOException { + if (node == null) { + Debug.logWarning("[UtilXml.writeXmlDocument] Node was null, doing nothing", module); return; } if (filename == null) { Debug.logWarning("[UtilXml.writeXmlDocument] Filename was null, doing nothing", module); return; } - File outFile = new File(filename); FileOutputStream fos = null; - fos = new FileOutputStream(outFile); - try { - writeXmlDocument(fos, element); - } finally { - if (fos != null) fos.close(); + fos = new FileOutputStream(outFile); + writeXmlDocument(fos, node); + } catch (FileNotFoundException e) { + throw e; + } catch (IOException e) { + throw e; + } + if (fos != null) { + fos.close(); } } - public static void writeXmlDocument(OutputStream os, Document document) throws java.io.IOException { - if (document == null) { - Debug.logWarning("[UtilXml.writeXmlDocument] Document was null, doing nothing", module); + public static void writeXmlDocument(OutputStream os, Node node) throws java.io.IOException { + if (node == null) { + Debug.logWarning("[UtilXml.writeXmlDocument] Node was null, doing nothing", module); return; } - writeXmlDocument(os, document.getDocumentElement()); - } - - @SuppressWarnings("deprecation") - public static void writeXmlDocument(OutputStream os, Element element) throws java.io.IOException { - writeXmlDocument(os, element, new OutputFormat()); + // OutputFormat defaults are: indent on, indent = 4, include XML declaration, + // charset = UTF-8, line width = 72 + try { + writeXmlDocument(node, os, "UTF-8", false, true, 4); + } catch (TransformerException e) { + // Wrapping this exception for backwards compatibility + throw new IOException(e.getMessage()); + } } /** |
Free forum by Nabble | Edit this page |