01: // SegmentArrayAttribute.java
02: // $Id: SegmentArrayAttribute.java,v 1.4 2000/08/16 21:37:56 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1996.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.tools.resources.upgrade;
07:
08: import java.io.DataInputStream;
09: import java.io.DataOutputStream;
10: import java.io.IOException;
11: import java.io.OutputStream;
12:
13: import org.w3c.util.CountOutputStream;
14: import org.w3c.jigsaw.ssi.Segment;
15:
16: /**
17: * Attribute used to make the document segment information persistent.
18: * @author Antonio Ramirez <anto@mit.edu>
19: */
20: class SegmentArrayAttribute extends Attribute {
21:
22: public boolean checkValue(Object value) {
23: return (value instanceof Segment[] || value == null);
24: }
25:
26: /**
27: * Get the number of bytes required to save that attribute value.
28: * @param The value about to be pickled.
29: * @return The number of bytes needed to pickle that value.
30: */
31:
32: public final int getPickleLength(Object value) {
33: CountOutputStream out = new CountOutputStream();
34: DataOutputStream dout = new DataOutputStream(out);
35: Segment ss[] = (Segment[]) value;
36: try {
37: pickle(dout, ss);
38: dout.close();
39: return out.getCount();
40: } catch (IOException ex) {
41: throw new RuntimeException("IO erred in CountOutputStream.");
42: }
43: }
44:
45: public void pickle(DataOutputStream out, Object obj)
46: throws IOException {
47: Segment[] segs = (Segment[]) obj;
48: out.writeInt(segs.length);
49: for (int i = 0; i < segs.length; i++)
50: segs[i].pickle(out);
51: }
52:
53: public Object unpickle(DataInputStream in) throws IOException {
54: int n = in.readInt();
55:
56: Segment segs[] = new Segment[n];
57:
58: for (int i = 0; i < n; i++)
59: segs[i] = Segment.unpickle(in);
60:
61: return segs;
62: }
63:
64: public SegmentArrayAttribute(String name, Segment[] def,
65: Integer flags) {
66: super (name, def, flags);
67: this .type = "[Lorg.w3c.jigsaw.ssi.Segment;";
68: }
69:
70: public String stringify(Object value) {
71: Segment[] segs = (Segment[]) value;
72: StringBuffer buf = new StringBuffer(160);
73: buf.append('[');
74: for (int i = 0; i < segs.length; i++) {
75: buf.append(segs[i].toString());
76: buf.append(' ');
77: }
78: buf.append(']');
79: return buf.toString();
80: }
81: }
|