XMLEncoder.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* XMLEncoder.java
  2. Copyright (C) 2004, 2005 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.encoder.ScanEngine;
  33. import java.io.OutputStream;
  34. /**
  35. * This class uses the {@link PersistenceDelegate} and {@link Encoder}
  36. * infrastructure to generate an XML representation of the objects it
  37. * serializes.
  38. *
  39. * @author Robert Schuster (robertschuster@fsfe.org)
  40. * @since 1.4
  41. */
  42. public class XMLEncoder
  43. extends Encoder
  44. implements AutoCloseable
  45. {
  46. Object owner;
  47. Exception exception;
  48. ScanEngine scanEngine;
  49. private int accessCounter = 0;
  50. public XMLEncoder(OutputStream os)
  51. {
  52. scanEngine = new ScanEngine(os);
  53. }
  54. public void close()
  55. {
  56. if (scanEngine != null)
  57. {
  58. scanEngine.close();
  59. scanEngine = null;
  60. }
  61. }
  62. public void flush()
  63. {
  64. scanEngine.flush();
  65. }
  66. public void writeExpression(Expression expr)
  67. {
  68. // Implementation note: Why is this method overwritten and nearly exactly
  69. // reimplemented as in Encoder?
  70. // The Encoder class can (and should be) subclassed by users outside of the
  71. // java.beans package. While I have doubts that this is possible from an
  72. // API design point of view I tried to replicate the Encoder's behavior
  73. // in the JDK as exactly as possible. This strictness however made it
  74. // extremely complicated to implement the XMLEncoder's backend. Therefore
  75. // I decided to copy the Encoder's implementation and make all changes
  76. // I needed for a succesfull operation of XMLEncoder.
  77. //
  78. // The same is true for the writeStatement method.
  79. // Silently ignore out of bounds calls.
  80. if (accessCounter <= 0)
  81. return;
  82. scanEngine.writeExpression(expr);
  83. Object target = expr.getTarget();
  84. Object value = null;
  85. Object newValue = null;
  86. try
  87. {
  88. value = expr.getValue();
  89. }
  90. catch (Exception e)
  91. {
  92. getExceptionListener().exceptionThrown(e);
  93. return;
  94. }
  95. newValue = get(value);
  96. if (newValue == null)
  97. {
  98. Object newTarget = get(target);
  99. if (newTarget == null)
  100. {
  101. writeObject(target);
  102. newTarget = get(target);
  103. // May happen if exception was thrown.
  104. if (newTarget == null)
  105. {
  106. return;
  107. }
  108. }
  109. Object[] args = expr.getArguments();
  110. Object[] newArgs = new Object[args.length];
  111. for (int i = 0; i < args.length; i++)
  112. {
  113. newArgs[i] = get(args[i]);
  114. if (newArgs[i] == null || isImmutableType(args[i].getClass()))
  115. {
  116. writeObject(args[i]);
  117. newArgs[i] = get(args[i]);
  118. }
  119. }
  120. Expression newExpr = new Expression(newTarget, expr.getMethodName(),
  121. newArgs);
  122. // Fakes the result of Class.forName(<primitiveType>) to make it possible
  123. // to hand such a type to the encoding process.
  124. if (value instanceof Class && ((Class) value).isPrimitive())
  125. newExpr.setValue(value);
  126. // Instantiates the new object.
  127. try
  128. {
  129. newValue = newExpr.getValue();
  130. putCandidate(value, newValue);
  131. }
  132. catch (Exception e)
  133. {
  134. getExceptionListener().exceptionThrown(e);
  135. // In Statement.writeExpression we had no possibility to flags
  136. // an erroneous state to the ScanEngine without behaving different
  137. // to the JDK.
  138. scanEngine.revoke();
  139. return;
  140. }
  141. writeObject(value);
  142. }
  143. else if(value.getClass() == String.class || value.getClass() == Class.class)
  144. {
  145. writeObject(value);
  146. }
  147. scanEngine.end();
  148. }
  149. public void writeStatement(Statement stmt)
  150. {
  151. // In case of questions have a at the implementation note in
  152. // writeExpression.
  153. scanEngine.writeStatement(stmt);
  154. // Silently ignore out of bounds calls.
  155. if (accessCounter <= 0)
  156. return;
  157. Object target = stmt.getTarget();
  158. Object newTarget = get(target);
  159. if (newTarget == null)
  160. {
  161. writeObject(target);
  162. newTarget = get(target);
  163. }
  164. Object[] args = stmt.getArguments();
  165. Object[] newArgs = new Object[args.length];
  166. for (int i = 0; i < args.length; i++)
  167. {
  168. // Here is the difference to the original writeStatement
  169. // method in Encoder. In case that the object is known or
  170. // not an immutable we put it directly into the ScanEngine
  171. // which will then generate an object reference for it.
  172. newArgs[i] = get(args[i]);
  173. if (newArgs[i] == null || isImmutableType(args[i].getClass()))
  174. {
  175. writeObject(args[i]);
  176. newArgs[i] = get(args[i]);
  177. }
  178. else
  179. scanEngine.writeObject(args[i]);
  180. }
  181. Statement newStmt = new Statement(newTarget, stmt.getMethodName(), newArgs);
  182. try
  183. {
  184. newStmt.execute();
  185. }
  186. catch (Exception e)
  187. {
  188. getExceptionListener().exceptionThrown(e);
  189. // In Statement.writeStatement we had no possibility to flags
  190. // an erroneous state to the ScanEngine without behaving different
  191. // to the JDK.
  192. scanEngine.revoke();
  193. return;
  194. }
  195. scanEngine.end();
  196. }
  197. public void writeObject(Object o)
  198. {
  199. accessCounter++;
  200. scanEngine.writeObject(o);
  201. if (get(o) == null)
  202. super.writeObject(o);
  203. accessCounter--;
  204. }
  205. public void setOwner(Object o)
  206. {
  207. owner = o;
  208. }
  209. public Object getOwner()
  210. {
  211. return owner;
  212. }
  213. }