01: // ClassAttribute.java
02: // $Id: ClassAttribute.java,v 1.4 2000/08/16 21:37:55 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:
12: /**
13: * The generic description of an ClassAttribute.
14: */
15:
16: public class ClassAttribute extends Attribute {
17:
18: /**
19: * Make a String out of a ClassAttribute value.
20: * The default <code>toString</code> method on classes doesn't work
21: * for that purpose, since it will preceed the class name with
22: * a <strong>class</strong> keyword.
23: * @return The String name of the class.
24: */
25:
26: public String stringify(Object value) {
27: return ((value instanceof Class) ? ((Class) value).getName()
28: : "<unknown-class>");
29: }
30:
31: /**
32: * Is the given object a valid ClassAttribute value ?
33: * @param obj The object to test.
34: * @return A boolean <strong>true</strong> if okay.
35: */
36:
37: public boolean checkValue(Object obj) {
38: return (obj instanceof Class);
39: }
40:
41: /**
42: * Get the number of bytes required to save that attribute value.
43: * @param The value about to be pickled.
44: * @return The number of bytes needed to pickle that value.
45: */
46:
47: public final int getPickleLength(Object value) {
48: String str = ((Class) value).getName();
49: int strlen = str.length();
50: int utflen = 0;
51:
52: for (int i = 0; i < strlen; i++) {
53: int c = str.charAt(i);
54: if ((c >= 0x0001) && (c <= 0x007F)) {
55: utflen++;
56: } else if (c > 0x07FF) {
57: utflen += 3;
58: } else {
59: utflen += 2;
60: }
61: }
62: return utflen + 2;
63: }
64:
65: /**
66: * Pickle an integer to the given output stream.
67: * @param out The output stream to pickle to.
68: * @param obj The object to pickle.
69: * @exception IOException If some IO error occured.
70: */
71:
72: public void pickle(DataOutputStream out, Object c)
73: throws IOException {
74: out.writeUTF(((Class) c).getName());
75: }
76:
77: /**
78: * Unpickle an integer from the given input stream.
79: * @param in The input stream to unpickle from.
80: * @return An instance of Integer.
81: * @exception IOException If some IO error occured.
82: */
83:
84: public Object unpickle(DataInputStream in) throws IOException {
85: String clsname = in.readUTF();
86: try {
87: return Class.forName(clsname);
88: } catch (Exception ex) {
89: throw new IOException("Unable to restore class " + clsname
90: + ": " + ex.getMessage());
91: }
92: }
93:
94: public ClassAttribute(String name, Class def, Integer flags) {
95: super (name, def, flags);
96: this .type = "java.lang.Class";
97: }
98:
99: }
|