mas01mj@732: /* mas01mj@732: Copyright (c) 2008, Adobe Systems Incorporated mas01mj@732: All rights reserved. mas01mj@732: mas01mj@732: Redistribution and use in source and binary forms, with or without mas01mj@732: modification, are permitted provided that the following conditions are mas01mj@732: met: mas01mj@732: mas01mj@732: * Redistributions of source code must retain the above copyright notice, mas01mj@732: this list of conditions and the following disclaimer. mas01mj@732: mas01mj@732: * Redistributions in binary form must reproduce the above copyright mas01mj@732: notice, this list of conditions and the following disclaimer in the mas01mj@732: documentation and/or other materials provided with the distribution. mas01mj@732: mas01mj@732: * Neither the name of Adobe Systems Incorporated nor the names of its mas01mj@732: contributors may be used to endorse or promote products derived from mas01mj@732: this software without specific prior written permission. mas01mj@732: mas01mj@732: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS mas01mj@732: IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, mas01mj@732: THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR mas01mj@732: PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR mas01mj@732: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, mas01mj@732: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, mas01mj@732: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR mas01mj@732: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF mas01mj@732: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING mas01mj@732: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS mas01mj@732: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. mas01mj@732: */ mas01mj@732: mas01mj@732: package com.adobe.utils mas01mj@732: { mas01mj@732: mas01mj@732: public class XMLUtil mas01mj@732: { mas01mj@732: /** mas01mj@732: * Constant representing a text node type returned from XML.nodeKind. mas01mj@732: * mas01mj@732: * @see XML.nodeKind() mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: */ mas01mj@732: public static const TEXT:String = "text"; mas01mj@732: mas01mj@732: /** mas01mj@732: * Constant representing a comment node type returned from XML.nodeKind. mas01mj@732: * mas01mj@732: * @see XML.nodeKind() mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: */ mas01mj@732: public static const COMMENT:String = "comment"; mas01mj@732: mas01mj@732: /** mas01mj@732: * Constant representing a processing instruction type returned from XML.nodeKind. mas01mj@732: * mas01mj@732: * @see XML.nodeKind() mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: */ mas01mj@732: public static const PROCESSING_INSTRUCTION:String = "processing-instruction"; mas01mj@732: mas01mj@732: /** mas01mj@732: * Constant representing an attribute type returned from XML.nodeKind. mas01mj@732: * mas01mj@732: * @see XML.nodeKind() mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: */ mas01mj@732: public static const ATTRIBUTE:String = "attribute"; mas01mj@732: mas01mj@732: /** mas01mj@732: * Constant representing a element type returned from XML.nodeKind. mas01mj@732: * mas01mj@732: * @see XML.nodeKind() mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: */ mas01mj@732: public static const ELEMENT:String = "element"; mas01mj@732: mas01mj@732: /** mas01mj@732: * Checks whether the specified string is valid and well formed XML. mas01mj@732: * mas01mj@732: * @param data The string that is being checked to see if it is valid XML. mas01mj@732: * mas01mj@732: * @return A Boolean value indicating whether the specified string is mas01mj@732: * valid XML. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: */ mas01mj@732: public static function isValidXML(data:String):Boolean mas01mj@732: { mas01mj@732: var xml:XML; mas01mj@732: mas01mj@732: try mas01mj@732: { mas01mj@732: xml = new XML(data); mas01mj@732: } mas01mj@732: catch(e:Error) mas01mj@732: { mas01mj@732: return false; mas01mj@732: } mas01mj@732: mas01mj@732: if(xml.nodeKind() != XMLUtil.ELEMENT) mas01mj@732: { mas01mj@732: return false; mas01mj@732: } mas01mj@732: mas01mj@732: return true; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the next sibling of the specified node relative to the node's parent. mas01mj@732: * mas01mj@732: * @param x The node whose next sibling will be returned. mas01mj@732: * mas01mj@732: * @return The next sibling of the node. null if the node does not have mas01mj@732: * a sibling after it, or if the node has no parent. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: */ mas01mj@732: public static function getNextSibling(x:XML):XML mas01mj@732: { mas01mj@732: return XMLUtil.getSiblingByIndex(x, 1); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Returns the sibling before the specified node relative to the node's parent. mas01mj@732: * mas01mj@732: * @param x The node whose sibling before it will be returned. mas01mj@732: * mas01mj@732: * @return The sibling before the node. null if the node does not have mas01mj@732: * a sibling before it, or if the node has no parent. mas01mj@732: * mas01mj@732: * @langversion ActionScript 3.0 mas01mj@732: * @playerversion Flash 9.0 mas01mj@732: */ mas01mj@732: public static function getPreviousSibling(x:XML):XML mas01mj@732: { mas01mj@732: return XMLUtil.getSiblingByIndex(x, -1); mas01mj@732: } mas01mj@732: mas01mj@732: protected static function getSiblingByIndex(x:XML, count:int):XML mas01mj@732: { mas01mj@732: var out:XML; mas01mj@732: mas01mj@732: try mas01mj@732: { mas01mj@732: out = x.parent().children()[x.childIndex() + count]; mas01mj@732: } mas01mj@732: catch(e:Error) mas01mj@732: { mas01mj@732: return null; mas01mj@732: } mas01mj@732: mas01mj@732: return out; mas01mj@732: } mas01mj@732: } mas01mj@732: }