From 3d2166cd8d9434f56ff53b38c98d74a318289ff7 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Tue, 5 Feb 2008 23:47:20 +0000 Subject: [PATCH] Added utility methods for safe XML operations. So broken! The XML class is not specified in the ECMA-262 standard. Now I know why they needed the guidelines of ECMAscript: when left to their own devices they design broken-ass shit. Adobe needs a cork on their fork. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4932 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/Util.as | 77 ++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/src/as/com/threerings/util/Util.as b/src/as/com/threerings/util/Util.as index 5eba49562..4c160d4b5 100644 --- a/src/as/com/threerings/util/Util.as +++ b/src/as/com/threerings/util/Util.as @@ -78,6 +78,83 @@ public class Util } } + /** + * Parse the 'value' object into XML safely. This is equivalent to new XML(value) + * but offers protection from other code that may have changing the default settings + * used for parsing XML. Also, if you would like to use non-standard parsing settings + * this method will protect other code from being broken by you. + * + * @param value the value to parse into XML. + * @param settings an Object containing your desired non-standard XML parsing settings. + * @see XML#setSettings() + */ + public static function newXML (value :Object, settings :Object = null) :XML + { + return safeXMLOp(function () :* { + return new XML(value); + }, settings) as XML; + } + + /** + * Call toString() on the specified XML object safely. This is equivalent to + * xml.toString() but offers protection from other code that may have changed + * the default settings used for stringing XML. Also, if you would like to use the + * non-standard printing settings this method will protect other code from being + * broken by you. + * + * @param xml the xml value to Stringify. + * @param settings an Object containing + * @see XML#toString() + * @see XML#setSettings() + */ + public static function XMLtoString (xml :XML, settings :Object = null) :String + { + return safeXMLOp(function () :* { + return xml.toString(); + }, settings) as String; + } + + /** + * Call toXMLString() on the specified XML object safely. This is equivalent to + * xml.toXMLString() but offers protection from other code that may have changed + * the default settings used for stringing XML. Also, if you would like to use the + * non-standard printing settings this method will protect other code from being + * broken by you. + * + * @param xml the xml value to Stringify. + * @param settings an Object containing + * @see XML#toXMLString() + * @see XML#setSettings() + */ + public static function XMLtoXMLString (xml :XML, settings :Object = null) :String + { + return safeXMLOp(function () :* { + return xml.toXMLString(); + }, settings) as String; + } + + /** + * Perform an operation on XML that takes place using the specified settings, and + * restores the XML settings to their previous values. + * + * @param fn a function to be called with no arguments. + * @param settings any custom XML settings, or null to use the defaults. + * + * @return the return value of your function, if any. + * @see XML#setSettings() + * @see XML#settings() + */ + public static function safeXMLOp (fn :Function, settings :Object = null) :* + { + var oldSettings :Object = XML.settings(); + try { + XML.setSettings(settings); + return fn(); + } finally { + XML.setSettings(oldSettings); + } + } + /** * A nice utility method for testing equality in a better way. * If the objects are Equalable, then that will be tested. Arrays