KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > SerializedXml


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml;
30
31 import com.caucho.util.L10N;
32 import com.caucho.vfs.IOExceptionWrapper;
33 import com.caucho.vfs.ReadStream;
34 import com.caucho.vfs.Vfs;
35
36 import org.w3c.dom.Node JavaDoc;
37
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.ObjectInput JavaDoc;
41 import java.io.ObjectOutput JavaDoc;
42 import java.io.OutputStream JavaDoc;
43
44 /**
45  * QAbstractNode is an abstract implementation for any DOM node.
46  */

47 public class SerializedXml implements java.io.Externalizable JavaDoc {
48   protected static L10N L = new L10N(SerializedXml.class);
49   
50   private Node JavaDoc _node;
51
52   public SerializedXml()
53   {
54   }
55
56   public SerializedXml(Node JavaDoc node)
57   {
58     _node = node;
59   }
60
61   public void writeExternal(ObjectOutput JavaDoc out)
62     throws IOException JavaDoc
63   {
64     XmlPrinter printer = new XmlPrinter(new OutputStreamWrapper(out));
65
66     printer.printXml(_node);
67
68     // feff
69
out.writeByte(0xef);
70     out.writeByte(0xbf);
71     out.writeByte(0xbf);
72   }
73
74   public void readExternal(ObjectInput JavaDoc in)
75     throws IOException JavaDoc
76   {
77     Xml parser = Xml.create();
78
79     try {
80       ReadStream is = Vfs.openRead(new InputStreamWrapper(in));
81       _node = parser.parseDocument(is);
82       is.close();
83     } catch (IOException JavaDoc e) {
84       throw e;
85     } catch (Exception JavaDoc e) {
86       throw new IOExceptionWrapper(e);
87     }
88
89     parser.free();
90   }
91
92   private Object JavaDoc readResolve()
93   {
94     return _node;
95   }
96
97   static class InputStreamWrapper extends InputStream JavaDoc {
98     private ObjectInput JavaDoc _in;
99
100     InputStreamWrapper(ObjectInput JavaDoc in)
101     {
102       _in = in;
103     }
104
105     public int read()
106       throws IOException JavaDoc
107     {
108       int ch = _in.readByte() & 0xff;
109       
110       return ch;
111     }
112
113     public int read(byte []buffer, int off, int length)
114       throws IOException JavaDoc
115     {
116       buffer[off] = _in.readByte();
117
118       return 1;
119     }
120   }
121   
122   static class OutputStreamWrapper extends OutputStream JavaDoc {
123     private ObjectOutput JavaDoc _out;
124
125     OutputStreamWrapper(ObjectOutput JavaDoc out)
126     {
127       _out = out;
128     }
129
130     public void write(int ch)
131       throws IOException JavaDoc
132     {
133       _out.write(ch);
134     }
135
136     public void write(byte []buf, int off, int len)
137       throws IOException JavaDoc
138     {
139       _out.write(buf, off, len);
140     }
141   }
142 }
143
Popular Tags