001: // AttributeRegistry.java
002: // $Id: AttributeRegistry.java,v 1.3 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.util.Hashtable;
009: import java.util.Vector;
010:
011: class ClassAttributes {
012: Attribute fixed[] = null;
013: Vector attrs = null;
014: int nextid = 0;
015:
016: /**
017: * Fix the description of the attributes for this record.
018: */
019:
020: void fix() {
021: if (fixed == null) {
022: fixed = new Attribute[attrs.size()];
023: attrs.copyInto(fixed);
024: attrs = null;
025: }
026: }
027:
028: /**
029: * Add a new attribute description into this record.
030: * @param attr The new attribute description.
031: * @return The attribute index.
032: */
033:
034: int add(Attribute attr) {
035: if (attrs == null)
036: throw new RuntimeException("add in a fixed record.");
037: attrs.addElement(attr);
038: return nextid++;
039: }
040:
041: ClassAttributes(int idx) {
042: this .fixed = null;
043: this .attrs = new Vector();
044: this .nextid = idx;
045: }
046:
047: ClassAttributes(ClassAttributes sup) {
048: this .fixed = null;
049: this .attrs = new Vector(sup.fixed.length);
050: this .nextid = sup.nextid;
051: for (int i = 0; i < sup.fixed.length; i++)
052: attrs.addElement(sup.fixed[i]);
053: }
054: }
055:
056: public class AttributeRegistry {
057: private static Hashtable registery = new Hashtable();
058: private static Class top = null;
059:
060: static {
061: try {
062: top = Class
063: .forName("org.w3c.tools.resources.AttributeHolder");
064: } catch (Exception ex) {
065: ex.printStackTrace();
066: System.exit(1);
067: }
068: }
069:
070: /**
071: * Register a new attribute for a given class.
072: * This method create the approrpriate attribute description record if
073: * required, and return the index of this attribute in the corresponding
074: * holder instances.
075: * @param cls The class that defines this attribute.
076: * @param attr The attribute to declare.
077: * @return The attribute index.
078: */
079:
080: public static synchronized int registerAttribute(Class cls,
081: Attribute attr) {
082: // Do we have a description record for this class ?
083: ClassAttributes record = (ClassAttributes) registery.get(cls);
084: if (record == null) {
085: // Create and register the record:
086: if (cls == top) {
087: record = new ClassAttributes(0);
088: } else {
089: // Get our super class record:
090: for (Class clsptr = cls.getSuperclass(); (record == null); clsptr = clsptr
091: .getSuperclass()) {
092: record = (ClassAttributes) registery.get(clsptr);
093: if (clsptr == top) {
094: if (record == null)
095: record = new ClassAttributes(0);
096: break;
097: }
098: }
099: if (record == null)
100: throw new RuntimeException("inconsistent state.");
101: record.fix();
102: record = new ClassAttributes(record);
103: }
104: registery.put(cls, record);
105: }
106: return record.add(attr);
107: }
108:
109: /**
110: * Get this class declared attributes.
111: * @param cls The class we are querying.
112: * @return An array of Attribute instances, describing each of the
113: * attributes of all instances of the class, or <strong>null</strong>
114: * if the class hasn't defined any attributes.
115: */
116:
117: public static synchronized Attribute[] getClassAttributes(Class cls) {
118: Object result = registery.get(cls);
119: while ((cls != top) && (result == null)) {
120: cls = cls.getSuperclass();
121: result = registery.get(cls);
122: }
123: // Fix the resulting record before returning it:
124: if (result == null)
125: return null;
126: ClassAttributes record = (ClassAttributes) result;
127: record.fix();
128: return record.fixed;
129: }
130:
131: /**
132: * Get the name of the class that has declared this attribute.
133: * @param cls The class that makes the starting point of lookup.
134: * @param attr The attribute we are looking for.
135: * @return The name of the class that defined that attribute, or <strong>
136: * null</strong>.
137: */
138:
139: public static Class getAttributeClass(Class cls, String attrname) {
140: Class lookup = cls;
141: // We lookup until we find the class that doesn't have this attribute:
142: while (true) {
143: boolean found = false;
144: Attribute a[] = getClassAttributes(cls);
145: // Lookup the attribute in that set:
146: if (a != null) {
147: for (int i = 0; i < a.length; i++) {
148: if ((found = a[i].getName().equals(attrname)))
149: break;
150: }
151: }
152: if (found) {
153: lookup = cls;
154: cls = cls.getSuperclass();
155: } else {
156: return (lookup == cls) ? null : lookup;
157: }
158: }
159:
160: }
161:
162: }
|