DomXPathResult.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* DomXPathResult.java --
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.dom;
  32. import java.util.Collection;
  33. import java.util.Iterator;
  34. import org.w3c.dom.Node;
  35. import org.w3c.dom.xpath.XPathException;
  36. import org.w3c.dom.xpath.XPathResult;
  37. /**
  38. * An XPath result object.
  39. *
  40. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  41. */
  42. class DomXPathResult
  43. implements XPathResult
  44. {
  45. final Object value;
  46. final short type;
  47. Iterator iterator;
  48. DomXPathResult (Object value, short requestedType)
  49. {
  50. this.value = value;
  51. if (value instanceof Boolean)
  52. {
  53. type = XPathResult.BOOLEAN_TYPE;
  54. }
  55. else if (value instanceof Double)
  56. {
  57. type = XPathResult.NUMBER_TYPE;
  58. }
  59. else if (value instanceof String)
  60. {
  61. type = XPathResult.STRING_TYPE;
  62. }
  63. else if (value instanceof Collection)
  64. {
  65. Collection ns = (Collection) value;
  66. switch (requestedType)
  67. {
  68. case XPathResult.ANY_TYPE:
  69. case XPathResult.ANY_UNORDERED_NODE_TYPE:
  70. type = (ns.size () == 1) ? XPathResult.FIRST_ORDERED_NODE_TYPE :
  71. XPathResult.ORDERED_NODE_ITERATOR_TYPE;
  72. break;
  73. default:
  74. type = requestedType;
  75. }
  76. iterator = ns.iterator ();
  77. }
  78. else
  79. {
  80. throw new IllegalArgumentException ();
  81. }
  82. }
  83. public boolean getBooleanValue()
  84. {
  85. if (type == XPathResult.BOOLEAN_TYPE)
  86. {
  87. return ((Boolean) value).booleanValue ();
  88. }
  89. throw new XPathException (XPathException.TYPE_ERR, value.toString ());
  90. }
  91. public boolean getInvalidIteratorState()
  92. {
  93. return iterator == null;
  94. }
  95. public double getNumberValue()
  96. {
  97. if (type == XPathResult.NUMBER_TYPE)
  98. {
  99. return ((Double) value).doubleValue ();
  100. }
  101. throw new XPathException (XPathException.TYPE_ERR, value.toString ());
  102. }
  103. public short getResultType()
  104. {
  105. return type;
  106. }
  107. public Node getSingleNodeValue()
  108. {
  109. switch (type)
  110. {
  111. case XPathResult.FIRST_ORDERED_NODE_TYPE:
  112. case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
  113. case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
  114. case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
  115. case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
  116. Collection ns = (Collection) value;
  117. if (ns.isEmpty ())
  118. {
  119. return null;
  120. }
  121. else
  122. {
  123. return (Node) ns.iterator ().next ();
  124. }
  125. }
  126. throw new XPathException (XPathException.TYPE_ERR, value.toString ());
  127. }
  128. public int getSnapshotLength()
  129. {
  130. switch (type)
  131. {
  132. case XPathResult.FIRST_ORDERED_NODE_TYPE:
  133. case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
  134. case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
  135. case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
  136. case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
  137. return ((Collection) value).size ();
  138. }
  139. throw new XPathException (XPathException.TYPE_ERR, value.toString ());
  140. }
  141. public String getStringValue()
  142. {
  143. if (type == XPathResult.STRING_TYPE)
  144. {
  145. return (String) value;
  146. }
  147. throw new XPathException (XPathException.TYPE_ERR, value.toString ());
  148. }
  149. public Node iterateNext()
  150. {
  151. if (iterator != null)
  152. {
  153. if (iterator.hasNext ())
  154. {
  155. return (Node) iterator.next ();
  156. }
  157. else
  158. {
  159. iterator = null;
  160. return null;
  161. }
  162. }
  163. throw new XPathException (XPathException.TYPE_ERR, value.toString ());
  164. }
  165. public Node snapshotItem(int index)
  166. {
  167. switch (type)
  168. {
  169. case XPathResult.FIRST_ORDERED_NODE_TYPE:
  170. case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
  171. case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
  172. case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
  173. case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
  174. Collection ns = (Collection) value;
  175. Node[] nodes = new Node[ns.size ()];
  176. ns.toArray (nodes);
  177. return nodes[index];
  178. }
  179. throw new XPathException (XPathException.TYPE_ERR, value.toString ());
  180. }
  181. public String toString ()
  182. {
  183. return getClass ().getName () + "[type=" + typeName (type) + ",value=" +
  184. value + ']';
  185. }
  186. private String typeName (short type)
  187. {
  188. switch (type)
  189. {
  190. case XPathResult.BOOLEAN_TYPE:
  191. return "BOOLEAN_TYPE";
  192. case XPathResult.NUMBER_TYPE:
  193. return "NUMBER_TYPE";
  194. case XPathResult.STRING_TYPE:
  195. return "STRING_TYPE";
  196. case XPathResult.FIRST_ORDERED_NODE_TYPE:
  197. return "FIRST_ORDERED_NODE_TYPE";
  198. case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
  199. return "ORDERED_NODE_ITERATOR_TYPE";
  200. case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
  201. return "ORDERED_NODE_SNAPSHOT_TYPE";
  202. case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
  203. return "UNORDERED_NODE_ITERATOR_TYPE";
  204. case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
  205. return "UNORDERED_NODE_SNAPSHOT_TYPE";
  206. default:
  207. return "(unknown)";
  208. }
  209. }
  210. }