1 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 ; 37 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.io.ObjectInput ; 41 import java.io.ObjectOutput ; 42 import java.io.OutputStream ; 43 44 47 public class SerializedXml implements java.io.Externalizable { 48 protected static L10N L = new L10N(SerializedXml.class); 49 50 private Node _node; 51 52 public SerializedXml() 53 { 54 } 55 56 public SerializedXml(Node node) 57 { 58 _node = node; 59 } 60 61 public void writeExternal(ObjectOutput out) 62 throws IOException 63 { 64 XmlPrinter printer = new XmlPrinter(new OutputStreamWrapper(out)); 65 66 printer.printXml(_node); 67 68 out.writeByte(0xef); 70 out.writeByte(0xbf); 71 out.writeByte(0xbf); 72 } 73 74 public void readExternal(ObjectInput in) 75 throws IOException 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 e) { 84 throw e; 85 } catch (Exception e) { 86 throw new IOExceptionWrapper(e); 87 } 88 89 parser.free(); 90 } 91 92 private Object readResolve() 93 { 94 return _node; 95 } 96 97 static class InputStreamWrapper extends InputStream { 98 private ObjectInput _in; 99 100 InputStreamWrapper(ObjectInput in) 101 { 102 _in = in; 103 } 104 105 public int read() 106 throws IOException 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 115 { 116 buffer[off] = _in.readByte(); 117 118 return 1; 119 } 120 } 121 122 static class OutputStreamWrapper extends OutputStream { 123 private ObjectOutput _out; 124 125 OutputStreamWrapper(ObjectOutput out) 126 { 127 _out = out; 128 } 129 130 public void write(int ch) 131 throws IOException 132 { 133 _out.write(ch); 134 } 135 136 public void write(byte []buf, int off, int len) 137 throws IOException 138 { 139 _out.write(buf, off, len); 140 } 141 } 142 } 143 | Popular Tags |