001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xml;
030:
031: import com.caucho.util.L10N;
032: import com.caucho.vfs.IOExceptionWrapper;
033: import com.caucho.vfs.ReadStream;
034: import com.caucho.vfs.Vfs;
035:
036: import org.w3c.dom.Node;
037:
038: import java.io.IOException;
039: import java.io.InputStream;
040: import java.io.ObjectInput;
041: import java.io.ObjectOutput;
042: import java.io.OutputStream;
043:
044: /**
045: * QAbstractNode is an abstract implementation for any DOM node.
046: */
047: public class SerializedXml implements java.io.Externalizable {
048: protected static L10N L = new L10N(SerializedXml.class);
049:
050: private Node _node;
051:
052: public SerializedXml() {
053: }
054:
055: public SerializedXml(Node node) {
056: _node = node;
057: }
058:
059: public void writeExternal(ObjectOutput out) throws IOException {
060: XmlPrinter printer = new XmlPrinter(
061: new OutputStreamWrapper(out));
062:
063: printer.printXml(_node);
064:
065: // feff
066: out.writeByte(0xef);
067: out.writeByte(0xbf);
068: out.writeByte(0xbf);
069: }
070:
071: public void readExternal(ObjectInput in) throws IOException {
072: Xml parser = Xml.create();
073:
074: try {
075: ReadStream is = Vfs.openRead(new InputStreamWrapper(in));
076: _node = parser.parseDocument(is);
077: is.close();
078: } catch (IOException e) {
079: throw e;
080: } catch (Exception e) {
081: throw new IOExceptionWrapper(e);
082: }
083:
084: parser.free();
085: }
086:
087: private Object readResolve() {
088: return _node;
089: }
090:
091: static class InputStreamWrapper extends InputStream {
092: private ObjectInput _in;
093:
094: InputStreamWrapper(ObjectInput in) {
095: _in = in;
096: }
097:
098: public int read() throws IOException {
099: int ch = _in.readByte() & 0xff;
100:
101: return ch;
102: }
103:
104: public int read(byte[] buffer, int off, int length)
105: throws IOException {
106: buffer[off] = _in.readByte();
107:
108: return 1;
109: }
110: }
111:
112: static class OutputStreamWrapper extends OutputStream {
113: private ObjectOutput _out;
114:
115: OutputStreamWrapper(ObjectOutput out) {
116: _out = out;
117: }
118:
119: public void write(int ch) throws IOException {
120: _out.write(ch);
121: }
122:
123: public void write(byte[] buf, int off, int len)
124: throws IOException {
125: _out.write(buf, off, len);
126: }
127: }
128: }
|