001: // Attribute.java
002: // $Id: Attribute.java,v 1.4 2000/08/16 21:37:55 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.resources.upgrade;
007:
008: import java.io.DataInputStream;
009: import java.io.DataOutputStream;
010: import java.io.IOException;
011: import java.io.Serializable;
012:
013: /**
014: * Instances of this class describe an attribute of a resource.
015: */
016:
017: abstract public class Attribute implements Serializable {
018: /**
019: * Flags value - This attribute is computed from the resource state.
020: */
021: public static final int COMPUTED = (1 << 0);
022: /**
023: * Flag value - This attribute is editable.
024: */
025: public static final int EDITABLE = (1 << 1);
026: /**
027: * Flag value - This attribute is mandatory.
028: */
029: public static final int MANDATORY = (1 << 2);
030: /**
031: * Flag value - This attribute shouldn't be saved.
032: */
033: public static final int DONTSAVE = (1 << 3);
034: /**
035: * The attribute name.
036: */
037: protected String name = null;
038: /**
039: * The attribute's value type, as the name of its class.
040: */
041: protected String type = null;
042: /**
043: * The attribute's default value.
044: */
045: private transient Object defvalue = null;
046: /**
047: * The associated flags (see the predefined flags).
048: */
049: protected int flags = 0;
050:
051: /**
052: * Get this attribute name.
053: * @return A String giving the attribute name.
054: */
055:
056: public String getName() {
057: return name;
058: }
059:
060: /**
061: * Get this attribute type.
062: */
063:
064: public String getType() {
065: return type;
066: }
067:
068: /**
069: * Check some flag on this attribute description.
070: */
071:
072: public boolean checkFlag(int tst) {
073: return (flags & tst) == tst;
074: }
075:
076: /**
077: * Get this attribute default value.
078: * @return A default value for this attribute (may be
079: * <strong>null</strong>).
080: */
081:
082: public Object getDefault() {
083: return defvalue;
084: }
085:
086: /**
087: * Stringify a value of this kind.
088: * @param obj The value to stringify.
089: */
090:
091: public String stringify(Object value) {
092: return value.toString();
093: }
094:
095: /**
096: * Is the provided object a suitable value for this attribute ?
097: * If so, store it into the given store.
098: * @param value The value to check.
099: * @param store The array to store the value to if succeed.
100: * @param idx The location in the above array.
101: * @return A boolean <strong>true</strong> if this object can be used
102: * as a value for this attribute.
103: * @exception IllegalAttributeAccess If the provided value doesn't match
104: * the expected type.
105: */
106:
107: abstract public boolean checkValue(Object value);
108:
109: /**
110: * Get number of bytes needed to pickle that attribute.
111: * This method is always called before pickling an attribute, to
112: * get the length of that attribute value, and record it before saving
113: * the actual bytes. This allows, for example, to skip attribute whose
114: * definition was removed from a class.
115: * <p>In an ASCII format, this plays a role similar to emitting
116: * a newline.
117: * @param value The value that is about to be pickled.
118: * @return The number of bytes needed to pickle that value.
119: */
120:
121: abstract public int getPickleLength(Object value);
122:
123: /**
124: * Pickle an attribute of this type to the given stream.
125: * This method is used to make attribute values persistent, the pickle
126: * method should dump the provided value in whatever format, provided
127: * its unpickle method is able to restore it.
128: * @param out The DataOutputStream to dump the object to.
129: * @param obj The object to pickle.
130: * @exception IOException If some IO error occured while dump the
131: * attribute.
132: */
133:
134: abstract public void pickle(DataOutputStream out, Object obj)
135: throws IOException;
136:
137: /**
138: * Unpickle an attribute of this type from the given stream.
139: * This method is used to restore a pickled attribute value from the given
140: * stream. It should read in the format it used at pickle time, and
141: * consume the same number of bytes from the stream.
142: * @param in The DataInputStream to read from.
143: * @return The object value.
144: * @exception IOException If some IOError occured while reading the stream.
145: */
146:
147: abstract public Object unpickle(DataInputStream in)
148: throws IOException;
149:
150: /**
151: * Private constructore to create a new resource attribute description.
152: * @param name The name of the attribute.
153: * @param type Its type (as a Java class).
154: * @param def Its default value.
155: * @param flags Its associated flags.
156: */
157:
158: public Attribute(String name, Object def, Integer flags) {
159: this.name = name;
160: this.defvalue = def;
161: this.flags = flags.intValue();
162: }
163: }
|