01: package org.mandarax.zkb;
02:
03: /*
04: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: import java.beans.XMLDecoder;
21: import java.beans.XMLEncoder;
22: import java.io.ByteArrayInputStream;
23: import java.io.ByteArrayOutputStream;
24: import java.io.IOException;
25: import java.io.InputStream;
26: import java.io.OutputStream;
27: import java.util.Map;
28:
29: import org.mandarax.util.xmlser.LogExceptionListener;
30:
31: /**
32: * Object persistency service based on XML serialization.
33: * <strong>Warning:</strong>Requires JDK 1.4 or better!
34: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
35: * @version 3.4 <7 March 05>
36: * @since 2.2
37: */
38: public class XMLSerializationOPS extends AbstractOPS {
39: /**
40: * Export objects.
41: * @param out an output stream
42: * @exception an io exception
43: */
44: public void exportObjects(OutputStream out) throws IOException {
45: XMLEncoder encoder = new XMLEncoder(out);
46: encoder.setExceptionListener(new LogExceptionListener());
47: encoder.writeObject(objectsByUri);
48: encoder.close();
49: }
50:
51: /**
52: * Import objects.
53: * @param in an input stream
54: * @exception an io exception
55: */
56: public void importObjects(InputStream in) throws IOException {
57: // buffer, otherwise closing the decoder closes the stream
58: ByteArrayOutputStream buf = new ByteArrayOutputStream();
59: byte b = -1;
60: while ((b = (byte) in.read()) > -1)
61: buf.write(b);
62: InputStream bin = new ByteArrayInputStream(buf.toByteArray());
63: XMLDecoder decoder = new XMLDecoder(bin);
64: decoder.setExceptionListener(new LogExceptionListener());
65: reset();
66: objectsByUri = (Map) decoder.readObject();
67: reverseMap(objectsByUri, urisByObject);
68: decoder.close();
69:
70: }
71:
72: /**
73: * Get name.
74: * @return the name of the service
75: */
76: public String getName() {
77: return "XML serialization";
78: }
79:
80: /**
81: * Get a brief description.
82: * @return a brief description of the service
83: */
84: public String getDescription() {
85: return "Object persistency service based on JDK XML serialization, requires JDK 1.4 or better";
86: }
87:
88: /**
89: * Get the extension of the associated files.
90: * @return a file extension
91: */
92: public String getExtension() {
93: return "xml";
94: }
95: }
|