2
0

XMLUtil.as 4.7 KB

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