Author: adrianc
Date: Thu May 7 06:43:53 2009 New Revision: 772517 URL: http://svn.apache.org/viewvc?rev=772517&view=rev Log: Added UEL functions to read and write XML files. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelFunctions.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelFunctions.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelFunctions.java?rev=772517&r1=772516&r2=772517&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelFunctions.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/UelFunctions.java Thu May 7 06:43:53 2009 @@ -18,7 +18,9 @@ *******************************************************************************/ package org.ofbiz.base.util.string; +import java.io.File; import java.io.InputStream; +import java.io.FileOutputStream; import java.lang.reflect.Method; import java.net.URL; import java.text.DateFormat; @@ -33,7 +35,13 @@ import org.ofbiz.base.location.FlexibleLocation; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.FileUtil; import org.ofbiz.base.util.UtilDateTime; +import org.ofbiz.base.util.UtilXml; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; /** Implements Unified Expression Language functions. * <p>Built-in functions are divided into a number of @@ -130,6 +138,9 @@ * <tr><td><code>util:size(Object)</code></td><td>Returns the size of <code>Maps</code>, * <code>Collections</code>, and <code>Strings</code>. Invalid <code>Object</code> types return -1.</td></tr> * <tr><td><code>util:urlExists(String)</code></td><td>Returns <code>true</code> if the specified URL exists.</td></tr> + * <tr><td colspan="2"><b><code>dom:</code> contains <code>org.w3c.dom.*</code> functions</b></td></tr> + * <tr><td><code>dom:readXmlDocument(String)</code></td><td>Reads an XML file and returns a <code>org.w3c.dom.Document</code> instance.</td></tr> + * <tr><td><code>dom:writeXmlDocument(String, Node, String encoding, boolean omitXmlDeclaration, boolean indent, int indentAmount)</code></td><td>Writes a <code>org.w3c.dom.Node</code> to an XML file and returns <code>true</code> if successful.</td></tr> * </table> */ public class UelFunctions { @@ -233,8 +244,10 @@ this.functionMap.put("util:defaultLocale", Locale.class.getMethod("getDefault")); this.functionMap.put("util:defaultTimeZone", TimeZone.class.getMethod("getDefault")); this.functionMap.put("util:urlExists", UelFunctions.class.getMethod("urlExists", String.class)); + this.functionMap.put("dom:readXmlDocument", UelFunctions.class.getMethod("readXmlDocument", String.class)); + this.functionMap.put("dom:writeXmlDocument", UelFunctions.class.getMethod("writeXmlDocument", String.class, Node.class, String.class, boolean.class, boolean.class, int.class)); } catch (Exception e) { - Debug.logError("Error while initializing UelFunctions.Functions instance: " + e, module); + Debug.logError(e, "Error while initializing UelFunctions.Functions instance", module); } Debug.logVerbose("UelFunctions.Functions loaded " + this.functionMap.size() + " functions", module); } @@ -404,4 +417,52 @@ } catch (Exception e) {} return result; } + + public static Document readXmlDocument(String str) { + Document document = null; + try { + URL url = FlexibleLocation.resolveLocation(str); + if (url != null) { + InputStream is = url.openStream(); + document = UtilXml.readXmlDocument(is, str); + is.close(); + } else { + Debug.logError("Unable to locate XML document " + str, module); + } + } catch (Exception e) { + Debug.logError(e, "Error while reading XML document " + str, module); + } + return document; + } + + public static boolean writeXmlDocument(String str, Node node, String encoding, boolean omitXmlDeclaration, boolean indent, int indentAmount) { + Element element = null; + if (node instanceof Document) { + element = ((Document) node).getDocumentElement(); + } else { + try { + element = (Element) node; + } catch (Exception e) {} + } + if (element != null) { + try { + File file = FileUtil.getFile(str); + if (file != null) { + FileOutputStream os = new FileOutputStream(file); + UtilXml.writeXmlDocument(element, os, encoding, omitXmlDeclaration, indent, indentAmount); + os.close(); + return true; + } else { + Debug.logError("Unable to create XML document " + str, module); + } + } catch (Exception e) { + Debug.logError(e, "Error while writing XML document " + str, module); + } + } else { + Debug.logError("Error while writing XML document " + str + + ": Node is not an instance of Document or Element - " + + node.getClass().getName(), module); + } + return false; + } } |
Free forum by Nabble | Edit this page |