KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > xml > DocumentSerializable


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.xml;
11
12 import java.util.*;
13 import java.io.*;
14 import org.w3c.dom.*;
15 import org.xml.sax.InputSource JavaDoc;
16 import org.xml.sax.SAXException JavaDoc;
17 import javax.xml.parsers.DocumentBuilder JavaDoc;
18 import org.mmbase.util.logging.*;
19
20 /**
21  * Wraps an {@link org.w3c.dom.Document} to be certainly serializable (and cloneable). If it is not by itself (IIRC
22  * the Xerces implementation is serializable), then this class serializes to a stringification.
23  *
24  * This can be used if a Serializable class needs an Document member. Choose for a
25  * DocumentSerializable member in stead, and use {@link #getDocument}.
26  *
27  * @author Michiel Meeuwissen
28  * @version $Id: DocumentSerializable.java,v 1.4 2006/01/06 17:36:43 michiel Exp $
29  * @since MMBase-1.8
30  */

31 public class DocumentSerializable implements Serializable, org.mmbase.util.PublicCloneable {
32     private static final Logger log = Logging.getLoggerInstance(DocumentSerializable.class);
33     private static final long serialVersionUID = 1L;
34
35     private Document document;
36     // implementation of serializable
37
private void writeObject(ObjectOutputStream out) throws IOException {
38         if (document instanceof Serializable) {
39             out.writeObject(document);
40         } else {
41             String JavaDoc string = XMLWriter.write(document, false);
42             out.writeObject(string);
43         }
44     }
45     // implementation of serializable
46
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException JavaDoc {
47         Object JavaDoc o = in.readObject();
48         if (o instanceof Document) {
49             document = (Document) o;
50         } else {
51             try {
52                 DocumentBuilder JavaDoc documentBuilder = DocumentReader.getDocumentBuilder(false, null, null);
53                 document = documentBuilder.parse(new InputSource JavaDoc(new StringReader("" + o)));
54             } catch (SAXException JavaDoc e) {
55                 log.warn(e);
56             }
57         }
58     }
59
60
61
62     public DocumentSerializable(Document d) {
63         document = d;
64     }
65
66     public final Document getDocument() {
67         return document;
68     }
69
70     public String JavaDoc toString() {
71         return XMLWriter.write(document, false, true);
72     }
73
74     public int hashCode() {
75         return document.hashCode();
76     }
77     public boolean equals(Object JavaDoc o) {
78         return
79             o != null &&
80             o instanceof DocumentSerializable &&
81             document.equals(((DocumentSerializable)o).document);
82     }
83
84     public Object JavaDoc clone() {
85         Document newDocument = DocumentReader.getDocumentBuilder(false, null, null).newDocument();
86         Node root = newDocument.importNode(document.getDocumentElement(), true);
87         newDocument.appendChild(root);
88         return new DocumentSerializable(newDocument);
89     }
90
91 }
92
Popular Tags