1 package jsdsi;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.ObjectStreamException;
6 import java.io.Serializable;
7 import java.io.StringWriter;
8
9 import jsdsi.sexp.ObjInputStream;
10 import jsdsi.sexp.ObjOutputStream;
11 import jsdsi.sexp.Sexp;
12 import jsdsi.sexp.SexpList;
13 import jsdsi.sexp.SexpParseException;
14 import jsdsi.sexp.SexpUtil;
15
16 /***
17 * Common superclass of all SPKI/SDSI objects.
18 *
19 * <p><strong>Note:</strong> As serialization is performed by writing
20 * the object to the the stream as a canonical Sexpression, all
21 * subclasses of <code>jsdsi.Obj</code> should declare their fields as
22 * <code>transient</code>.</p>
23 *
24 * @author Sameer Ajmani
25 * @author Sean Radford
26 * @version $Revision: 1.10.2.1 $ $Date: 2005/11/08 03:12:52 $
27 */
28 public abstract class Obj implements Serializable {
29
30 private static final long serialVersionUID = -9005382351047668808L;
31
32 /*** Default value for Readable S-expression format */
33 private static final int OFFSET = 0;
34
35 /*** Default value for Readable S-expression format */
36 private static final int WIDTH = 72;
37
38 /*** Default value for Readable S-expression format */
39 private static final int LAST = 0;
40
41 /***
42 * @see java.lang.Object#equals(java.lang.Object)
43 */
44 public abstract boolean equals(Object o);
45
46 /***
47 * @see java.lang.Object#hashCode()
48 */
49 public abstract int hashCode();
50
51 /***
52 * Creates an <code>SexpList</code> -representation from this SDSI-object.
53 *
54 * @return an <code>SexpList</code> that represents this SDSI-object.
55 */
56 public abstract SexpList toSexp();
57
58 /***
59 * Returns the S-expression representation of <code>this</code>
60 * SPKI/SDSI object in readable form, using the standard
61 * S-expression charactrer encoding (8859-1).
62 *
63 * @see java.lang.Object#toString()
64 */
65 public String toString() {
66 return toString(OFFSET, WIDTH, LAST);
67 }
68
69 /***
70 * Returns the S-expression representation of <code>this</code>
71 * SPKI/SDSI object in readable form, using the standard
72 * S-expression charactrer encoding (8859-1).
73 *
74 * @param offset spaces indented from left.
75 * @param width total width of window, in characters.
76 * @param last spaces reserved on right (e.g., for closing parens)
77 * @return the S-expression
78 */
79 public String toString(int offset, int width, int last) {
80 try {
81 StringWriter sw = new StringWriter();
82 this.toSexp().writeReadable(sw, offset, width, last);
83 return sw.toString();
84 } catch (IOException e) {
85 throw new Error(e);
86 }
87 }
88
89 /***
90 * Returns the S-expression representation of <code>this</code>
91 * SPKI/SDSI object in transport form. If the transport form is
92 * required as a <code>java.lang.String</code>, then simply pass the
93 * resulting <code>byte[]</code> from this method into {@link
94 * jsdsi.sexp.Sexp#decodeString(byte[])} (This process is required
95 * as S-expression's must use the 8859-1 character encoding).
96 *
97 * @return the S-expression
98 */
99 public byte[] toTransport() {
100 try {
101 ByteArrayOutputStream baos = new ByteArrayOutputStream();
102 this.toSexp().writeTransport(baos);
103 return baos.toByteArray();
104 } catch (IOException e) {
105 throw new Error(e);
106 }
107 }
108
109 /***
110 * @return an array of bytes with the canonical representation of
111 * this SDSI/SPKI object.
112 */
113 public byte[] toByteArray() {
114 try {
115 ByteArrayOutputStream bos = new ByteArrayOutputStream();
116 this.toSexp().writeCanonical(bos);
117 return bos.toByteArray();
118 } catch (IOException e) {
119 throw new Error(e);
120 }
121 }
122
123 /***
124 * Writes <code>this</code> SPKI/SDSI object to the given
125 * <code>java.io.ObjectOutputStream</code>.
126 * @see java.io.Serializable
127 * @param out the output stream
128 * @throws IOException
129 */
130 private void writeObject(java.io.ObjectOutputStream out)
131 throws IOException {
132 ObjOutputStream objOut = new ObjOutputStream(out);
133 objOut.writeCanonical(this);
134 }
135
136 /***
137 * The replacement SPKI/SDSI object read from the
138 * <code>java.io.ObjectInputStream</code> during {@link
139 * #readObject(java.io.ObjectInputStream)} and to be returned via
140 * {@link #readResolve()}.
141 */
142 protected transient Obj _obj;
143
144 /***
145 * Reads <code>this</code> SPKI/SDSI object from the given
146 * <code>java.io.ObjectInputStream</code>.
147 * @see java.io.Serializable
148 * @param in the input stream
149 * @throws IOException
150 * @throws ClassNotFoundException
151 */
152 private void readObject(java.io.ObjectInputStream in) throws IOException,
153 ClassNotFoundException {
154 ObjInputStream objIn = new ObjInputStream(in);
155 try
156 {
157 _obj = objIn.readObj();
158 } catch (Exception e)
159 {
160 e.printStackTrace();
161 throw new IOException(e.toString());
162 }
163 }
164
165 /***
166 * @see java.io.Serializable
167 * @return the SPKI/SDSI object which is the replacement after
168 * serialization.
169 * @throws ObjectStreamException
170 */
171 protected Object readResolve() throws ObjectStreamException {
172 if (_obj!=null) {
173 return _obj;
174 } else {
175 return this;
176 }
177 }
178
179 /***
180 * Parses an S-expression an returns a SDSI object.
181 *
182 * @param s the S-expression to parse.
183 * @return a SDSI object stored in <code>s</code>.
184 * @throws SexpParseException
185 */
186 public static Obj parseObj(Sexp s) throws SexpParseException {
187 return parseObj(SexpUtil.getList(s));
188 }
189
190 /***
191 * Parses an <code>SexpList</code> an returns a SDSI object.
192 *
193 * @param l the <code>SexpList</code> that stores the SDSI object.
194 * @return a SDSI object stored in <code>l</code>.
195 * @throws SexpParseException
196 */
197 public static Obj parseObj(SexpList l) throws SexpParseException {
198 String type = l.getType();
199 if (type.equals("acl")) return Acl.parseAcl(l);
200 if (type.equals("entry")) return AclEntry.parseAclEntry(l);
201 if (type.equals("cert")) return Cert.parseCert(l);
202 if (type.equals("sequence")) return Sequence.parseSequence(l);
203 if (type.equals("public-key")) return PublicKey.parsePublicKey(l);
204 if (type.equals("do")) return Op.parseOp(l);
205 if (type.equals("signature")) return Signature.parseSignature(l);
206 if (type.equals("hash")) return Hash.parseHash(l);
207 if (type.equals("object-hash")) return ObjectHash.parseObjectHash(l);
208 if (type.equals("tag")) return Tag.parseTag(l);
209 if (type.equals("k-of-n")) return Threshold.parseThreshold(l);
210 if (type.equals("name")) return Name.parseName(l);
211 if (type.equals("principal")) return Principal.parsePrincipal(l);
212 if (type.equals("proof")) return Proof.parseProof(l);
213 if (type.equals("online")) return OnlineTest.parseOnlineTest(l);
214 if (type.equals("valid")) return Validity.parseValidity(l);
215
216 throw new SexpParseException("unrecognized object type: " + type);
217 }
218
219 }
This page was automatically generated by Maven