mas01mj@732
|
1 /*
|
mas01mj@732
|
2 Copyright (c) 2008, Adobe Systems Incorporated
|
mas01mj@732
|
3 All rights reserved.
|
mas01mj@732
|
4
|
mas01mj@732
|
5 Redistribution and use in source and binary forms, with or without
|
mas01mj@732
|
6 modification, are permitted provided that the following conditions are
|
mas01mj@732
|
7 met:
|
mas01mj@732
|
8
|
mas01mj@732
|
9 * Redistributions of source code must retain the above copyright notice,
|
mas01mj@732
|
10 this list of conditions and the following disclaimer.
|
mas01mj@732
|
11
|
mas01mj@732
|
12 * Redistributions in binary form must reproduce the above copyright
|
mas01mj@732
|
13 notice, this list of conditions and the following disclaimer in the
|
mas01mj@732
|
14 documentation and/or other materials provided with the distribution.
|
mas01mj@732
|
15
|
mas01mj@732
|
16 * Neither the name of Adobe Systems Incorporated nor the names of its
|
mas01mj@732
|
17 contributors may be used to endorse or promote products derived from
|
mas01mj@732
|
18 this software without specific prior written permission.
|
mas01mj@732
|
19
|
mas01mj@732
|
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
mas01mj@732
|
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
mas01mj@732
|
23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
mas01mj@732
|
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
mas01mj@732
|
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
mas01mj@732
|
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
mas01mj@732
|
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
mas01mj@732
|
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
mas01mj@732
|
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
mas01mj@732
|
31 */
|
mas01mj@732
|
32
|
mas01mj@732
|
33 package com.adobe.utils
|
mas01mj@732
|
34 {
|
mas01mj@732
|
35
|
mas01mj@732
|
36 public class XMLUtil
|
mas01mj@732
|
37 {
|
mas01mj@732
|
38 /**
|
mas01mj@732
|
39 * Constant representing a text node type returned from XML.nodeKind.
|
mas01mj@732
|
40 *
|
mas01mj@732
|
41 * @see XML.nodeKind()
|
mas01mj@732
|
42 *
|
mas01mj@732
|
43 * @langversion ActionScript 3.0
|
mas01mj@732
|
44 * @playerversion Flash 9.0
|
mas01mj@732
|
45 */
|
mas01mj@732
|
46 public static const TEXT:String = "text";
|
mas01mj@732
|
47
|
mas01mj@732
|
48 /**
|
mas01mj@732
|
49 * Constant representing a comment node type returned from XML.nodeKind.
|
mas01mj@732
|
50 *
|
mas01mj@732
|
51 * @see XML.nodeKind()
|
mas01mj@732
|
52 *
|
mas01mj@732
|
53 * @langversion ActionScript 3.0
|
mas01mj@732
|
54 * @playerversion Flash 9.0
|
mas01mj@732
|
55 */
|
mas01mj@732
|
56 public static const COMMENT:String = "comment";
|
mas01mj@732
|
57
|
mas01mj@732
|
58 /**
|
mas01mj@732
|
59 * Constant representing a processing instruction type returned from XML.nodeKind.
|
mas01mj@732
|
60 *
|
mas01mj@732
|
61 * @see XML.nodeKind()
|
mas01mj@732
|
62 *
|
mas01mj@732
|
63 * @langversion ActionScript 3.0
|
mas01mj@732
|
64 * @playerversion Flash 9.0
|
mas01mj@732
|
65 */
|
mas01mj@732
|
66 public static const PROCESSING_INSTRUCTION:String = "processing-instruction";
|
mas01mj@732
|
67
|
mas01mj@732
|
68 /**
|
mas01mj@732
|
69 * Constant representing an attribute type returned from XML.nodeKind.
|
mas01mj@732
|
70 *
|
mas01mj@732
|
71 * @see XML.nodeKind()
|
mas01mj@732
|
72 *
|
mas01mj@732
|
73 * @langversion ActionScript 3.0
|
mas01mj@732
|
74 * @playerversion Flash 9.0
|
mas01mj@732
|
75 */
|
mas01mj@732
|
76 public static const ATTRIBUTE:String = "attribute";
|
mas01mj@732
|
77
|
mas01mj@732
|
78 /**
|
mas01mj@732
|
79 * Constant representing a element type returned from XML.nodeKind.
|
mas01mj@732
|
80 *
|
mas01mj@732
|
81 * @see XML.nodeKind()
|
mas01mj@732
|
82 *
|
mas01mj@732
|
83 * @langversion ActionScript 3.0
|
mas01mj@732
|
84 * @playerversion Flash 9.0
|
mas01mj@732
|
85 */
|
mas01mj@732
|
86 public static const ELEMENT:String = "element";
|
mas01mj@732
|
87
|
mas01mj@732
|
88 /**
|
mas01mj@732
|
89 * Checks whether the specified string is valid and well formed XML.
|
mas01mj@732
|
90 *
|
mas01mj@732
|
91 * @param data The string that is being checked to see if it is valid XML.
|
mas01mj@732
|
92 *
|
mas01mj@732
|
93 * @return A Boolean value indicating whether the specified string is
|
mas01mj@732
|
94 * valid XML.
|
mas01mj@732
|
95 *
|
mas01mj@732
|
96 * @langversion ActionScript 3.0
|
mas01mj@732
|
97 * @playerversion Flash 9.0
|
mas01mj@732
|
98 */
|
mas01mj@732
|
99 public static function isValidXML(data:String):Boolean
|
mas01mj@732
|
100 {
|
mas01mj@732
|
101 var xml:XML;
|
mas01mj@732
|
102
|
mas01mj@732
|
103 try
|
mas01mj@732
|
104 {
|
mas01mj@732
|
105 xml = new XML(data);
|
mas01mj@732
|
106 }
|
mas01mj@732
|
107 catch(e:Error)
|
mas01mj@732
|
108 {
|
mas01mj@732
|
109 return false;
|
mas01mj@732
|
110 }
|
mas01mj@732
|
111
|
mas01mj@732
|
112 if(xml.nodeKind() != XMLUtil.ELEMENT)
|
mas01mj@732
|
113 {
|
mas01mj@732
|
114 return false;
|
mas01mj@732
|
115 }
|
mas01mj@732
|
116
|
mas01mj@732
|
117 return true;
|
mas01mj@732
|
118 }
|
mas01mj@732
|
119
|
mas01mj@732
|
120 /**
|
mas01mj@732
|
121 * Returns the next sibling of the specified node relative to the node's parent.
|
mas01mj@732
|
122 *
|
mas01mj@732
|
123 * @param x The node whose next sibling will be returned.
|
mas01mj@732
|
124 *
|
mas01mj@732
|
125 * @return The next sibling of the node. null if the node does not have
|
mas01mj@732
|
126 * a sibling after it, or if the node has no parent.
|
mas01mj@732
|
127 *
|
mas01mj@732
|
128 * @langversion ActionScript 3.0
|
mas01mj@732
|
129 * @playerversion Flash 9.0
|
mas01mj@732
|
130 */
|
mas01mj@732
|
131 public static function getNextSibling(x:XML):XML
|
mas01mj@732
|
132 {
|
mas01mj@732
|
133 return XMLUtil.getSiblingByIndex(x, 1);
|
mas01mj@732
|
134 }
|
mas01mj@732
|
135
|
mas01mj@732
|
136 /**
|
mas01mj@732
|
137 * Returns the sibling before the specified node relative to the node's parent.
|
mas01mj@732
|
138 *
|
mas01mj@732
|
139 * @param x The node whose sibling before it will be returned.
|
mas01mj@732
|
140 *
|
mas01mj@732
|
141 * @return The sibling before the node. null if the node does not have
|
mas01mj@732
|
142 * a sibling before it, or if the node has no parent.
|
mas01mj@732
|
143 *
|
mas01mj@732
|
144 * @langversion ActionScript 3.0
|
mas01mj@732
|
145 * @playerversion Flash 9.0
|
mas01mj@732
|
146 */
|
mas01mj@732
|
147 public static function getPreviousSibling(x:XML):XML
|
mas01mj@732
|
148 {
|
mas01mj@732
|
149 return XMLUtil.getSiblingByIndex(x, -1);
|
mas01mj@732
|
150 }
|
mas01mj@732
|
151
|
mas01mj@732
|
152 protected static function getSiblingByIndex(x:XML, count:int):XML
|
mas01mj@732
|
153 {
|
mas01mj@732
|
154 var out:XML;
|
mas01mj@732
|
155
|
mas01mj@732
|
156 try
|
mas01mj@732
|
157 {
|
mas01mj@732
|
158 out = x.parent().children()[x.childIndex() + count];
|
mas01mj@732
|
159 }
|
mas01mj@732
|
160 catch(e:Error)
|
mas01mj@732
|
161 {
|
mas01mj@732
|
162 return null;
|
mas01mj@732
|
163 }
|
mas01mj@732
|
164
|
mas01mj@732
|
165 return out;
|
mas01mj@732
|
166 }
|
mas01mj@732
|
167 }
|
mas01mj@732
|
168 } |