001: // ResourceDescription.java
002: // $Id: ResourceDescription.java,v 1.3 2000/08/16 21:37:54 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.tools.resources.serialization;
006:
007: import java.util.Vector;
008:
009: import org.w3c.tools.resources.Attribute;
010: import org.w3c.tools.resources.Resource;
011: import org.w3c.tools.resources.ResourceFrame;
012:
013: /**
014: * @version $Revision: 1.3 $
015: * @author Benoît Mahé (bmahe@w3.org)
016: */
017: public class ResourceDescription {
018:
019: String classname = null;
020: String classes[] = null;
021: String interfaces[] = null;
022: String identifier = null;
023: AttributeDescription attributes[] = null;
024: String children[] = null;
025:
026: /**
027: * Get a clone of this resource description but with only the
028: * given list of attribute descriptions.
029: * @param attrs the new attribute descriptions
030: * @return a ResourceDescription;
031: */
032: public ResourceDescription getClone(AttributeDescription attrs[]) {
033: ResourceDescription descr = new ResourceDescription(classname);
034: descr.identifier = identifier;
035: descr.children = children;
036: descr.attributes = attrs;
037: descr.classes = classes;
038: descr.interfaces = interfaces;
039: return descr;
040: }
041:
042: /**
043: * Get this resource class hierarchy.
044: * @return a String array
045: */
046: public String[] getClassHierarchy() {
047: return classes;
048: }
049:
050: /**
051: * Get this resource interfaces
052: * @return a String array
053: */
054: public String[] getInterfaces() {
055: return interfaces;
056: }
057:
058: public String[] getClassesAndInterfaces() {
059: String all[] = new String[interfaces.length + classes.length];
060: System.arraycopy(classes, 0, all, 0, classes.length);
061: System.arraycopy(interfaces, 0, all, classes.length,
062: interfaces.length);
063: return all;
064: }
065:
066: /**
067: * Get the resource Class name.
068: * @return a String
069: */
070: public String getClassName() {
071: return classname;
072: }
073:
074: /**
075: * Get the resource identifier.
076: * @return a String instance
077: */
078: public String getIdentifier() {
079: if ((identifier == null) && (attributes != null)) {
080: for (int i = 0; i < attributes.length; i++) {
081: if (attributes[i].getName().equals("identifier"))
082: identifier = (String) attributes[i].getValue();
083: }
084: }
085: return identifier;
086: }
087:
088: /**
089: * get the children identifiers
090: * @return a String array
091: */
092: public String[] getChildren() {
093: return children;
094: }
095:
096: /**
097: * Set the children names.
098: * @param a String array
099: */
100: public void setChildren(String children[]) {
101: this .children = children;
102: }
103:
104: /**
105: * Get the attributes description.
106: * @return an AttributeDescription array
107: * @see AttributeDescription
108: */
109: public AttributeDescription[] getAttributeDescriptions() {
110: return attributes;
111: }
112:
113: /**
114: * Get the description of the frames associated to this resource.
115: * @return a ResourceDescription array.
116: */
117: public ResourceDescription[] getFrameDescriptions() {
118: for (int i = 0; i < attributes.length; i++) {
119: Object value = attributes[i].getValue();
120: if (value instanceof ResourceDescription[])
121: return (ResourceDescription[]) value;
122: }
123: return new ResourceDescription[0];
124: }
125:
126: /**
127: * Constructor.
128: * @param resource the resource to describe.
129: */
130: public ResourceDescription(Resource resource) {
131: this .classname = resource.getClass().getName();
132: //build class hierarchy
133: Vector vclasses = new Vector(8);
134: Vector vinterfaces = new Vector(8);
135: Class ints[] = resource.getClass().getInterfaces();
136: if (ints != null)
137: for (int i = 0; i < ints.length; i++)
138: vinterfaces.addElement(ints[i]);
139: for (Class c = resource.getClass().getSuperclass(); c != null; c = c
140: .getSuperclass()) {
141: vclasses.addElement(c.getName());
142: ints = c.getInterfaces();
143: if (ints != null)
144: for (int i = 0; i < ints.length; i++)
145: vinterfaces.addElement(ints[i]);
146: }
147: this .classes = new String[vclasses.size()];
148: vclasses.copyInto(this .classes);
149: this .interfaces = new String[vinterfaces.size()];
150: vinterfaces.copyInto(this .interfaces);
151: //build attributes description
152: Attribute attrs[] = resource.getAttributes();
153: Vector vattrs = new Vector(10);
154: for (int j = 0; j < attrs.length; j++) {
155: Object value = resource.getValue(j, null);
156: if (value instanceof ResourceFrame[]) {
157: ResourceFrame frames[] = (ResourceFrame[]) value;
158: int len = frames.length;
159: ResourceDescription descr[] = new ResourceDescription[len];
160: for (int i = 0; i < len; i++)
161: descr[i] = new ResourceDescription(frames[i]);
162: vattrs.addElement(new AttributeDescription(attrs[j],
163: descr));
164: } else {
165: vattrs.addElement(new AttributeDescription(attrs[j],
166: value));
167: }
168: }
169: this .attributes = new AttributeDescription[vattrs.size()];
170: vattrs.copyInto(attributes);
171: }
172:
173: /**
174: * Set the attributes description of the resource
175: * @param a Vector of AttributeDescription instances
176: */
177: public void setAttributeDescriptions(Vector attrs) {
178: attributes = new AttributeDescription[attrs.size()];
179: attrs.copyInto(attributes);
180: }
181:
182: /**
183: * Set the resource class hierarchy.
184: * @param a String array
185: */
186: public void setClassHierarchy(String classes[]) {
187: this .classes = classes;
188: }
189:
190: /**
191: * Set the resource class hierarchy.
192: * @param a String array
193: */
194: public void setInterfaces(String interfaces[]) {
195: this .interfaces = interfaces;
196: }
197:
198: /**
199: * Set the resource class hierarchy.
200: * @param a Vector of String instances
201: */
202: public void setClassHierarchy(Vector vclasses) {
203: this .classes = new String[vclasses.size()];
204: vclasses.copyInto(this .classes);
205: }
206:
207: /**
208: * Set the resource class hierarchy.
209: * @param a Vector of String instances
210: */
211: public void setInterfaces(Vector vinterfaces) {
212: this .interfaces = new String[vinterfaces.size()];
213: vinterfaces.copyInto(this .interfaces);
214: }
215:
216: /**
217: * Constructor.
218: * @param classname the resource class name
219: */
220: public ResourceDescription(String classname) {
221: this .classname = classname;
222: this .interfaces = new String[0];
223: this .classes = new String[0];
224: }
225:
226: }
|