001: // Attribute.java
002: // $Id: Attribute.java,v 1.4 2002/06/09 09:44:12 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;
007:
008: import java.io.Serializable;
009:
010: /**
011: * Instances of this class describe an attribute of a resource.
012: */
013:
014: abstract public class Attribute implements Serializable {
015: /**
016: * Flags value - This attribute is computed from the resource state.
017: */
018: public static final int COMPUTED = (1 << 0);
019: /**
020: * Flag value - This attribute is editable.
021: */
022: public static final int EDITABLE = (1 << 1);
023: /**
024: * Flag value - This attribute is mandatory.
025: */
026: public static final int MANDATORY = (1 << 2);
027: /**
028: * Flag value - This attribute shouldn't be saved.
029: */
030: public static final int DONTSAVE = (1 << 3);
031: /**
032: * The attribute name.
033: */
034: protected String name = null;
035: /**
036: * The attribute's value type, as the name of its class.
037: */
038: protected String type = null;
039: /**
040: * The attribute's default value.
041: */
042: private transient Object defvalue = null;
043: /**
044: * The associated flags (see the predefined flags).
045: */
046: protected int flags = 0;
047:
048: public String getFlag() {
049: return String.valueOf(flags);
050: }
051:
052: public void setFlag(String flag) {
053: try {
054: flags = Integer.parseInt(flag);
055: } catch (Exception ex) {
056: flags = 0;
057: }
058: }
059:
060: /**
061: * Get this attribute name.
062: * @return A String giving the attribute name.
063: */
064:
065: public String getName() {
066: return name;
067: }
068:
069: /**
070: * set the attribute name.
071: * @param name the attribute name.
072: */
073: public void setName(String name) {
074: this .name = name.intern();
075: }
076:
077: /**
078: * Get this attribute type.
079: */
080:
081: public String getType() {
082: return type;
083: }
084:
085: /**
086: * Check some flag on this attribute description.
087: */
088:
089: public boolean checkFlag(int tst) {
090: return (flags & tst) == tst;
091: }
092:
093: /**
094: * Get this attribute default value.
095: * @return A default value for this attribute (may be
096: * <strong>null</strong>).
097: */
098:
099: public Object getDefault() {
100: return defvalue;
101: }
102:
103: /**
104: * Is the provided object a suitable value for this attribute ?
105: * If so, store it into the given store.
106: * @param value The value to check.
107: * @param store The array to store the value to if succeed.
108: * @param idx The location in the above array.
109: * @return A boolean <strong>true</strong> if this object can be used
110: * as a value for this attribute.
111: * @exception IllegalAttributeAccess If the provided value doesn't match
112: * the expected type.
113: */
114:
115: public abstract boolean checkValue(Object value);
116:
117: public abstract String stringify(Object value);
118:
119: /**
120: * Private constructore to create a new resource attribute description.
121: * @param name The name of the attribute.
122: * @param def Its default value.
123: * @param flags Its associated flags.
124: */
125:
126: public Attribute(String name, Object def, int flags) {
127: this .name = name.intern();
128: this .defvalue = def;
129: this .flags = flags;
130: }
131:
132: /**
133: * Empty contructor, (cls.newInstance())
134: */
135: public Attribute() {
136: this.defvalue = null;
137: this.flags = COMPUTED;
138: }
139: }
|