1 /*
2 * Copyright (c) 2005 Regents of the University of California (Regents). Created
3 * by TELS, Graduate School of Education, University of California at Berkeley.
4 *
5 * This software is distributed under the GNU Lesser General Public License, v2.
6 *
7 * Permission is hereby granted, without written agreement and without license
8 * or royalty fees, to use, copy, modify, and distribute this software and its
9 * documentation for any purpose, provided that the above copyright notice and
10 * the following two paragraphs appear in all copies of this software.
11 *
12 * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15 * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 *
18 * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21 * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 package net.sf.sail.core.curnit;
24
25 import java.util.logging.Logger;
26
27 import java.beans.Encoder;
28 import java.beans.ExceptionListener;
29 import java.beans.Expression;
30 import java.beans.PersistenceDelegate;
31 import java.beans.XMLEncoder;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.net.URL;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 import net.sf.sail.core.beans.Pod;
39
40 /**
41 * <p>
42 * Our XMLEncoder which adds PeristanceDelegates that we need and the standard
43 * XMLEncoder doesn't provide.
44 * </p>
45 *
46 * @author turadg
47 */
48 public class SAILXMLEncoder extends XMLEncoder {
49 /**
50 * Logger for this class
51 */
52 private static final Logger logger = Logger.getLogger(SAILXMLEncoder.class
53 .getName());
54
55 // super's OutputStream is private
56 OutputStream myOut;
57
58 Pod currentPod;
59
60 public static final ExceptionListener haltingExceptionListener = new ExceptionListener() {
61 public void exceptionThrown(Exception e) {
62 logger.severe("Exception - : exception: " + e); //$NON-NLS-1$
63 throw new RuntimeException("stop the show", e);
64 }
65 };
66
67 public SAILXMLEncoder(OutputStream out) {
68 super(out);
69 myOut = out;
70 /*
71 * Consider using the parsed version and reconstructing with set()
72 * instead of new()
73 * http://java.sun.com/products/jfc/tsc/articles/persistence4/
74 */
75 setPersistenceDelegate(URL.class, new PersistenceDelegate() {
76 @Override
77 protected Expression instantiate(Object oldInstance, Encoder out) {
78 return new Expression(oldInstance, oldInstance.getClass(),
79 "new", new Object[] { oldInstance.toString() });
80 }
81 });
82
83 for (Map.Entry<Class<?>,PersistenceDelegate> entry : sailDelegates.entrySet()) {
84 Class<?> clazz = entry.getKey();
85 PersistenceDelegate delegate = entry
86 .getValue();
87 setPersistenceDelegate(clazz, delegate);
88 }
89 }
90
91 final static Map<Class<?>, PersistenceDelegate> sailDelegates = new HashMap<Class<?>, PersistenceDelegate>();
92
93 public static void registerPersistenceDelegate(Class<?> clazz,
94 PersistenceDelegate delegate) {
95 sailDelegates.put(clazz, delegate);
96 }
97
98 /**
99 * Alternative to close() which closes the XML node and also the
100 * OutputStream. Sometimes we need to close the XML without closing the
101 * OutputStream. For example, entries in a ZipOutputStream.
102 *
103 * @throws IOException
104 */
105 public void closeXML() throws IOException {
106 flush();
107 // borrowed from XMLEncoder's close()
108 myOut.write("</java> \n".getBytes());
109 }
110
111 }