source: code/Website/open-flash-chart/com/adobe/utils/XMLUtil.as@ 7849

Last change on this file since 7849 was 7849, checked in by dennisw, 15 years ago
File size: 5.0 KB
Line 
1/*
2 Adobe Systems Incorporated(r) Source Code License Agreement
3 Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
4
5 Please read this Source Code License Agreement carefully before using
6 the source code.
7
8 Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
9 no-charge, royalty-free, irrevocable copyright license, to reproduce,
10 prepare derivative works of, publicly display, publicly perform, and
11 distribute this source code and such derivative works in source or
12 object code form without any attribution requirements.
13
14 The name "Adobe Systems Incorporated" must not be used to endorse or promote products
15 derived from the source code without prior written permission.
16
17 You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
18 against any loss, damage, claims or lawsuits, including attorney's
19 fees that arise or result from your use or distribution of the source
20 code.
21
22 THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
23 ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
24 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
26 NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
27 OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
33 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36package com.adobe.utils
37{
38
39 public class XMLUtil
40 {
41 /**
42 * Constant representing a text node type returned from XML.nodeKind.
43 *
44 * @see XML.nodeKind()
45 *
46 * @langversion ActionScript 3.0
47 * @playerversion Flash 9.0
48 */
49 public static const TEXT:String = "text";
50
51 /**
52 * Constant representing a comment node type returned from XML.nodeKind.
53 *
54 * @see XML.nodeKind()
55 *
56 * @langversion ActionScript 3.0
57 * @playerversion Flash 9.0
58 */
59 public static const COMMENT:String = "comment";
60
61 /**
62 * Constant representing a processing instruction type returned from XML.nodeKind.
63 *
64 * @see XML.nodeKind()
65 *
66 * @langversion ActionScript 3.0
67 * @playerversion Flash 9.0
68 */
69 public static const PROCESSING_INSTRUCTION:String = "processing-instruction";
70
71 /**
72 * Constant representing an attribute type returned from XML.nodeKind.
73 *
74 * @see XML.nodeKind()
75 *
76 * @langversion ActionScript 3.0
77 * @playerversion Flash 9.0
78 */
79 public static const ATTRIBUTE:String = "attribute";
80
81 /**
82 * Constant representing a element type returned from XML.nodeKind.
83 *
84 * @see XML.nodeKind()
85 *
86 * @langversion ActionScript 3.0
87 * @playerversion Flash 9.0
88 */
89 public static const ELEMENT:String = "element";
90
91 /**
92 * Checks whether the specified string is valid and well formed XML.
93 *
94 * @param data The string that is being checked to see if it is valid XML.
95 *
96 * @return A Boolean value indicating whether the specified string is
97 * valid XML.
98 *
99 * @langversion ActionScript 3.0
100 * @playerversion Flash 9.0
101 */
102 public static function isValidXML(data:String):Boolean
103 {
104 var xml:XML;
105
106 try
107 {
108 xml = new XML(data);
109 }
110 catch(e:Error)
111 {
112 return false;
113 }
114
115 if(xml.nodeKind() != XMLUtil.ELEMENT)
116 {
117 return false;
118 }
119
120 return true;
121 }
122
123 /**
124 * Returns the next sibling of the specified node relative to the node's parent.
125 *
126 * @param x The node whose next sibling will be returned.
127 *
128 * @return The next sibling of the node. null if the node does not have
129 * a sibling after it, or if the node has no parent.
130 *
131 * @langversion ActionScript 3.0
132 * @playerversion Flash 9.0
133 */
134 public static function getNextSibling(x:XML):XML
135 {
136 return XMLUtil.getSiblingByIndex(x, 1);
137 }
138
139 /**
140 * Returns the sibling before the specified node relative to the node's parent.
141 *
142 * @param x The node whose sibling before it will be returned.
143 *
144 * @return The sibling before the node. null if the node does not have
145 * a sibling before it, or if the node has no parent.
146 *
147 * @langversion ActionScript 3.0
148 * @playerversion Flash 9.0
149 */
150 public static function getPreviousSibling(x:XML):XML
151 {
152 return XMLUtil.getSiblingByIndex(x, -1);
153 }
154
155 protected static function getSiblingByIndex(x:XML, count:int):XML
156 {
157 var out:XML;
158
159 try
160 {
161 out = x.parent().children()[x.childIndex() + count];
162 }
163 catch(e:Error)
164 {
165 return null;
166 }
167
168 return out;
169 }
170 }
171}
Note: See TracBrowser for help on using the repository browser.