001: // ObjectAttribute.java
002: // $Id: ObjectAttribute.java,v 1.4 2000/08/16 21:37:56 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:
011: /**
012: * A generic Object attribute.
013: * This attribute is usefull for attributes that are:
014: * <ul>
015: * <li>Have Object values.
016: * <li>Need not be saved (have the DONTSAVE bit set).
017: * </ul>
018: */
019:
020: public class ObjectAttribute extends Attribute {
021: /**
022: * The class for values of this attribute.
023: */
024: protected Class cls = null;
025:
026: /**
027: * Check that a value is allowed for this attribute.
028: * @param value The value to check.
029: * @return A boolean <strong>true</strong> if value is allowed.
030: */
031:
032: public boolean checkValue(Object value) {
033: return true;
034: }
035:
036: /**
037: * Get the number of bytes required to save that attribute value.
038: * @param The value about to be pickled.
039: * @return The number of bytes needed to pickle that value.
040: * @exception RuntimeException Always throw since ObjectAttribute
041: * can't be pickled.
042: */
043:
044: public final int getPickleLength(Object value) {
045: throw new RuntimeException("Can't pickle ObjectAttribute");
046: }
047:
048: /**
049: * The ObjectAttribute values can't be pickled.
050: */
051:
052: public void pickle(DataOutputStream out, Object obj) {
053: throw new RuntimeException("Can't pickle ObjectAttribute.");
054: }
055:
056: /**
057: * The ObjectAttribute values can't be unpickled.
058: */
059:
060: public Object unpickle(DataInputStream in) {
061: throw new RuntimeException("Can't unpickle ObjectAttribute.");
062: }
063:
064: /**
065: * Create a new ObjectAttribute instance.
066: * @param name The name of the attribute.
067: * @param cls The class for this attribute values.
068: * @param def The default value for this attribute.
069: * @param flags The attribute flags.
070: */
071:
072: public ObjectAttribute(String name, Class cls, Object def,
073: Integer flags) {
074: super (name, def, flags);
075: // Check consistency
076: if (!checkFlag(DONTSAVE)) {
077: String error = "ObjectAttribute can't pickle themselves.";
078: throw new RuntimeException(error);
079: }
080: this .cls = cls;
081: }
082:
083: /**
084: * Create a new ObjectAttribute instance.
085: * @param name The name of the attribute.
086: * @param cname The name class for this attribute values.
087: * @param def The default value for this attribute.
088: * @param flags The attribute flags.
089: * @exception RuntimeException If we couldn't resolve the class name.
090: */
091:
092: public ObjectAttribute(String name, String cname, Object def,
093: Integer flags) {
094: super (name, def, flags);
095: // Check consistency:
096: if (!checkFlag(DONTSAVE)) {
097: String error = "ObjectAttribute can't pickle themselves.";
098: throw new RuntimeException(error);
099: }
100: // Resolve the class:
101: try {
102: this .cls = Class.forName(cname);
103: } catch (Exception ex) {
104: throw new RuntimeException("unable to resolve class "
105: + cname);
106: }
107: this .type = "java.lang.Object";
108: }
109:
110: }
|