01: // StringAttribute.java
02: // $Id: StringAttribute.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:
12: /**
13: * The generic description of an StringAttribute.
14: */
15:
16: public class StringAttribute extends Attribute {
17:
18: /**
19: * Is the given object a valid StringAttribute value ?
20: * @param obj The object to test.
21: * @return A boolean <strong>true</strong> if value is valid.
22: */
23:
24: public boolean checkValue(Object obj) {
25: return (obj instanceof String) || (obj == null);
26: }
27:
28: /**
29: * Get the number of bytes required to save that attribute value.
30: * @param The value about to be pickled.
31: * @return The number of bytes needed to pickle that value.
32: */
33:
34: public final int getPickleLength(Object value) {
35: String str = (String) value;
36: int strlen = str.length();
37: int utflen = 0;
38:
39: for (int i = 0; i < strlen; i++) {
40: int c = str.charAt(i);
41: if ((c >= 0x0001) && (c <= 0x007F)) {
42: utflen++;
43: } else if (c > 0x07FF) {
44: utflen += 3;
45: } else {
46: utflen += 2;
47: }
48: }
49: return utflen + 2;
50: }
51:
52: /**
53: * Pickle an string to the given output stream.
54: * @param out The output stream to pickle to.
55: * @param obj The object to pickle.
56: * @exception IOException If some IO error occured.
57: */
58:
59: public void pickle(DataOutputStream out, Object i)
60: throws IOException {
61: out.writeUTF((String) i);
62: }
63:
64: /**
65: * Unpickle an string from the given input stream.
66: * the empty string defaults to a null object
67: * @param in The input stream to unpickle from.
68: * @return An instance of String.
69: * @exception IOException If some IO error occured.
70: */
71:
72: public Object unpickle(DataInputStream in) throws IOException {
73: Object o = in.readUTF();
74: if (o instanceof String) {
75: String s = (String) o;
76: if (s.equals("")) {
77: return null;
78: }
79: }
80: return o;
81: }
82:
83: /**
84: * Create a description for a generic String attribute.
85: * @param name The attribute name.
86: * @param def The default value for these attributes.
87: * @param flags The associated flags.
88: */
89:
90: public StringAttribute(String name, String def, Integer flags) {
91: super (name, def, flags);
92: this .type = "java.lang.String";
93: }
94:
95: }
|