1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
42
43
44
45
46
47
48 public class SAILXMLEncoder extends XMLEncoder {
49
50
51
52 private static final Logger logger = Logger.getLogger(SAILXMLEncoder.class
53 .getName());
54
55
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);
63 throw new RuntimeException("stop the show", e);
64 }
65 };
66
67 public SAILXMLEncoder(OutputStream out) {
68 super(out);
69 myOut = out;
70
71
72
73
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
100
101
102
103
104
105 public void closeXML() throws IOException {
106 flush();
107
108 myOut.write("</java> \n".getBytes());
109 }
110
111 }