XMLDecoder.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* java.beans.XMLDecoder --
  2. Copyright (C) 2004, 2005, 2006 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 java.beans;
  32. import gnu.java.beans.DefaultExceptionListener;
  33. import gnu.java.beans.decoder.PersistenceParser;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.util.Iterator;
  37. import java.util.NoSuchElementException;
  38. /**
  39. * The XMLDecoder reads XML data that is structured according to
  40. * <a href="http://java.sun.com/products/jfc/tsc/articles/persistence3/javabeans.dtd">this</a> DTD
  41. * and creates objects according to the content. Usually such data is generated using the
  42. * {@link XMLEncoder} class.
  43. * <p>
  44. * An example XML document might look like this:
  45. * <code>
  46. * &lt;java&gt;
  47. * &lt;string&gt;Hello World&lt;/string&gt;
  48. * &lt;int&gt;200&lt;/int&gt;
  49. * &lt;/java&gt;
  50. * </code>
  51. * <p>To read the <code>String</code> and the <code>Integer</code> instance the following can be used (assume
  52. * the XML data can be obtained from the InputStream):</p>
  53. * <code>
  54. * XMLDecoder decoder = new XMLDecoder(inputStreamContainingXMLData);
  55. * String message = (String) decoder.readObject();
  56. * Integer number = (Integer) decoder.readObject();
  57. * </code>
  58. * <p>Besides this basic functionality the <code>XMLDecoder</code> has some more features that might come
  59. * handy in certain situations:</p>
  60. * <p>An owner object can be set using the <code>setOwner</code> method which can then be accessed when
  61. * decoding. This feature is only useful if the XML data is aware of the owner object. Such data may
  62. * look like this (assume that the owner object is a JFrame instance):</p>
  63. * <code>
  64. * &lt;java&gt;
  65. * &lt;void method="getOwner"&gt;
  66. * &lt;void method="setVisible"&gt;
  67. * &lt;boolean&gt;true&lt;boolean&gt;
  68. * &lt;/void&gt;
  69. * &lt;/void&gt;
  70. * &lt;/java&gt;
  71. * </code>
  72. * This accesses the <code>JFrame</code> and makes it visible using the <code>setVisible</code> method.
  73. * <p>Please note that changing the owner <b>after</b> the having read the first object has no effect,
  74. * because all object have been decoded then.</p>
  75. * <p>If the <code>XMLDecoder</code> is created with no {@link ExceptionListener} instance a default one
  76. * is used that prints an error message to <code>System.err</code> whenever a recoverable exception
  77. * is thrown. Recovarable exceptions occur when the XML data cannot be interpreted correctly (e.g
  78. * unknown classes or methods, invocation on null, ...). In general be very careful when the
  79. * <code>XMLDecoder</code> provoked such exceptions because the resulting object(s) may be in an
  80. * undesirable state.</p>
  81. * <p>Note that changing the ExceptionListener instance after <code>readObject</code> has been called
  82. * once has no effect because the decoding is completed then.</p>
  83. * <p>At last one can provide a specific <code>ClassLoader</code> which is then used when <code>Class</code>
  84. * objects are accessed. See {@link java.lang.Class#forName(String, boolean, ClassLoader)} for details
  85. * on this.</p>
  86. * <p>Note: If the <code>InputStream</code> instance given to any of the constructors is <code>null</code>
  87. * the resulting <code>XMLDecoder</code> will be silently (without any exception) useless. Each call
  88. * to <code>readObject</code> will return <code>null</code> and never throws an
  89. * <code>ArrayIndexOutOfBoundsException</code>.</p>
  90. *
  91. * @author Robert Schuster
  92. * @since 1.4
  93. * @status updated to 1.5
  94. */
  95. public class XMLDecoder
  96. implements AutoCloseable
  97. {
  98. private Object owner;
  99. private ExceptionListener exceptionListener;
  100. private InputStream inputStream;
  101. private boolean isStreamClosed;
  102. private ClassLoader classLoader;
  103. private Iterator iterator;
  104. /** Creates a XMLDecoder instance that parses the XML data of the given input stream.
  105. * Using this constructor no special ClassLoader, a default ExceptionListener
  106. * and no owner object is used.
  107. *
  108. * @param in InputStream to read XML data from.
  109. */
  110. public XMLDecoder(InputStream in)
  111. {
  112. this(in, null);
  113. }
  114. /** Creates a XMLDecoder instance that parses the XML data of the given input stream.
  115. * Using this constructor no special ClassLoader and a default ExceptionListener
  116. * is used.
  117. *
  118. * @param in InputStream to read XML data from.
  119. * @param owner Owner object which can be accessed and modified while parsing.
  120. */
  121. public XMLDecoder(InputStream in, Object owner)
  122. {
  123. this(in, owner, null);
  124. }
  125. /** Creates a XMLDecoder instance that parses the XML data of the given input stream.
  126. * If the ExceptionListener argument is null a default implementation is used.
  127. *
  128. * @param in InputStream to read XML data from.
  129. * @param owner Owner object which can be accessed and modified while parsing.
  130. * @param exceptionListener ExceptionListener instance to which exception notifications are send.
  131. */
  132. public XMLDecoder(
  133. InputStream in,
  134. Object owner,
  135. ExceptionListener exceptionListener)
  136. {
  137. this(
  138. in,
  139. owner,
  140. exceptionListener,
  141. Thread.currentThread().getContextClassLoader());
  142. }
  143. /** Creates a XMLDecoder instance that parses the XML data of the given input stream.
  144. * If the ExceptionListener argument is null a default implementation is used.
  145. *
  146. * @param in InputStream to read XML data from.
  147. * @param owner Owner object which can be accessed and modified while parsing.
  148. * @param listener ExceptionListener instance to which exception notifications are send.
  149. * @param cl ClassLoader instance that is used for calls to <code>Class.forName(String, boolean, ClassLoader)</code>
  150. * @since 1.5
  151. */
  152. public XMLDecoder(
  153. InputStream in,
  154. Object owner,
  155. ExceptionListener listener,
  156. ClassLoader cl)
  157. {
  158. // initially here was a check for the validity of the InputStream argument but some
  159. // great engineers decided that this API should silently discard this and behave rather
  160. // odd: readObject will always return null ...
  161. inputStream = in;
  162. setExceptionListener(listener);
  163. // validity of this object is checked in Class.forName() and therefore may be null
  164. classLoader = cl;
  165. this.owner = owner;
  166. }
  167. /** Closes the stream associated with this decoder. This should be done after having read all
  168. * decoded objects.
  169. * <p>See the description of the {@link #readObject()} for the effect caused by <code>close</code>.</p>
  170. */
  171. public void close()
  172. {
  173. if (isStreamClosed)
  174. {
  175. return;
  176. }
  177. try
  178. {
  179. inputStream.close();
  180. isStreamClosed = true;
  181. }
  182. catch (IOException e)
  183. {
  184. // bad style forced by original API design ...
  185. }
  186. }
  187. /** Returns the ExceptionListener instance associated with this decoder.
  188. * <p>See the description of {@link XMLDecoder} class for more information on the ExceptionListener.</p>
  189. *
  190. * @return Current ExceptionListener of the decoder.
  191. */
  192. public ExceptionListener getExceptionListener()
  193. {
  194. return exceptionListener;
  195. }
  196. /** Returns the owner object of the decoder. This method is usually called
  197. * from within the parsed XML data.
  198. * <p>See the description of {@link XMLDecoder} class for more information on the owner object.</p>
  199. *
  200. * @return The owner object of this decoder.
  201. */
  202. public Object getOwner()
  203. {
  204. return owner;
  205. }
  206. /** Returns the next available decoded object.
  207. * <p>Note that the actual decoding takes place when the method is called for the first time.</p>
  208. * <p>If the <code>close</code> method was already called a <code>NoSuchElementException</code>
  209. * is thrown.</p>
  210. * <p>If the InputStream instance used in the constructors was <code>null</code> this method
  211. * will always return <code>null</code> itself.</p>
  212. *
  213. * @return The next object in a sequence decoded from XML data.
  214. * @throws ArrayIndexOutOfBoundsException When no more objects are available.
  215. */
  216. public Object readObject() throws ArrayIndexOutOfBoundsException
  217. {
  218. // note: the RI does it this way ...
  219. if(inputStream == null) {
  220. return null;
  221. }
  222. // note: the original API documentation says nothing on what to do
  223. // when the stream was closed before readObject is called but it actually
  224. // throws a NoSuchElementException - this behaviour is imitated here
  225. if (isStreamClosed)
  226. {
  227. throw new NoSuchElementException("Cannot read any objects - XMLDecoder was already closed.");
  228. }
  229. // creates the PersistenceParser (doing the parsing and decoding) and returns its
  230. // Iterator on first invocation
  231. if (iterator == null)
  232. {
  233. iterator =
  234. new PersistenceParser(
  235. inputStream,
  236. exceptionListener,
  237. classLoader,
  238. this)
  239. .iterator();
  240. }
  241. // note: done according to the official documentation
  242. if (!iterator.hasNext())
  243. {
  244. throw new ArrayIndexOutOfBoundsException("No more objects available from this XMLDecoder.");
  245. }
  246. // returns just the next object if there was no problem
  247. return iterator.next();
  248. }
  249. /** Sets the ExceptionListener instance to which notifications of exceptions are send
  250. * while parsing the XML data.
  251. * <p>See the description of {@link XMLDecoder} class for more information on the ExceptionListener.</p>
  252. *
  253. * @param listener
  254. */
  255. public void setExceptionListener(ExceptionListener listener)
  256. {
  257. // uses a default implementation when null
  258. if (listener == null)
  259. {
  260. listener = DefaultExceptionListener.INSTANCE;
  261. }
  262. exceptionListener = listener;
  263. }
  264. /** Sets the owner object which can be accessed from the parsed XML data.
  265. * <p>See the description of {@link XMLDecoder} class for more information on the owner object.</p>
  266. *
  267. * @param newOwner
  268. */
  269. public void setOwner(Object newOwner)
  270. {
  271. owner = newOwner;
  272. }
  273. }