001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.xml;
011:
012: import java.io.*;
013: import org.w3c.dom.*;
014: import org.xml.sax.InputSource;
015: import org.xml.sax.SAXException;
016: import javax.xml.parsers.DocumentBuilder;
017: import org.mmbase.util.logging.*;
018:
019: /**
020: * Wraps an {@link org.w3c.dom.Document} to be certainly serializable (and cloneable). If it is not by itself (IIRC
021: * the Xerces implementation is serializable), then this class serializes to a stringification.
022: *
023: * This can be used if a Serializable class needs an Document member. Choose for a
024: * DocumentSerializable member in stead, and use {@link #getDocument}.
025: *
026: * @author Michiel Meeuwissen
027: * @version $Id: DocumentSerializable.java,v 1.6 2007/08/02 10:00:54 michiel Exp $
028: * @since MMBase-1.8
029: */
030: public class DocumentSerializable implements Serializable,
031: org.mmbase.util.PublicCloneable {
032: private static final Logger log = Logging
033: .getLoggerInstance(DocumentSerializable.class);
034: private static final long serialVersionUID = 1L;
035:
036: private Document document;
037:
038: // implementation of serializable
039: private void writeObject(ObjectOutputStream out) throws IOException {
040: if (document instanceof Serializable) {
041: out.writeObject(document);
042: } else {
043: String string = XMLWriter.write(document, false);
044: out.writeObject(string);
045: }
046: }
047:
048: // implementation of serializable
049: private void readObject(ObjectInputStream in) throws IOException,
050: ClassNotFoundException {
051: Object o = in.readObject();
052: if (o instanceof Document) {
053: document = (Document) o;
054: } else {
055: try {
056: DocumentBuilder documentBuilder = DocumentReader
057: .getDocumentBuilder(false, null, null);
058: document = documentBuilder.parse(new InputSource(
059: new StringReader("" + o)));
060: } catch (SAXException e) {
061: log.warn(e);
062: }
063: }
064: }
065:
066: public DocumentSerializable(Document d) {
067: document = d;
068: }
069:
070: public <T> T unwrap(Class<T> iface) {
071: return (T) document;
072: }
073:
074: public final Document getDocument() {
075: return document;
076: }
077:
078: public String toString() {
079: return XMLWriter.write(document, false, true);
080: }
081:
082: public int hashCode() {
083: return document.hashCode();
084: }
085:
086: public boolean equals(Object o) {
087: return o != null
088: && o instanceof DocumentSerializable
089: && document
090: .isEqualNode(((DocumentSerializable) o).document);
091: }
092:
093: public Object clone() {
094: Document newDocument = DocumentReader.getDocumentBuilder(false,
095: null, null).newDocument();
096: Node root = newDocument.importNode(document
097: .getDocumentElement(), true);
098: newDocument.appendChild(root);
099: return new DocumentSerializable(newDocument);
100: }
101:
102: }
|