--- /dev/null
+// ResourceDescription.java\r
+// $Id: ResourceDescription.java,v 1.1 2010/06/15 12:28:13 smhuang Exp $\r
+// (c) COPYRIGHT MIT, INRIA and Keio, 1999.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+package org.w3c.tools.resources.serialization;\r
+\r
+import java.util.Vector;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+import org.w3c.tools.resources.Resource;\r
+import org.w3c.tools.resources.ResourceFrame;\r
+\r
+/**\r
+ * @version $Revision: 1.1 $\r
+ * @author Benoît Mahé (bmahe@w3.org)\r
+ */\r
+public class ResourceDescription {\r
+\r
+ String classname = null;\r
+ String classes[] = null;\r
+ String interfaces[] = null;\r
+ String identifier = null;\r
+ AttributeDescription attributes[] = null;\r
+ String children[] = null;\r
+\r
+ /**\r
+ * Get a clone of this resource description but with only the\r
+ * given list of attribute descriptions.\r
+ * @param attrs the new attribute descriptions\r
+ * @return a ResourceDescription;\r
+ */\r
+ public ResourceDescription getClone(AttributeDescription attrs[]) {\r
+ ResourceDescription descr = new ResourceDescription(classname);\r
+ descr.identifier = identifier;\r
+ descr.children = children;\r
+ descr.attributes = attrs;\r
+ descr.classes = classes;\r
+ descr.interfaces = interfaces;\r
+ return descr;\r
+ }\r
+\r
+ /**\r
+ * Get this resource class hierarchy.\r
+ * @return a String array\r
+ */\r
+ public String[] getClassHierarchy() {\r
+ return classes;\r
+ }\r
+\r
+ /**\r
+ * Get this resource interfaces\r
+ * @return a String array\r
+ */\r
+ public String[] getInterfaces() {\r
+ return interfaces;\r
+ }\r
+\r
+ public String[] getClassesAndInterfaces() {\r
+ String all[] = new String[interfaces.length+classes.length];\r
+ System.arraycopy(classes, 0, all, 0, classes.length);\r
+ System.arraycopy(interfaces, 0, all, classes.length, \r
+ interfaces.length);\r
+ return all;\r
+ }\r
+\r
+ /**\r
+ * Get the resource Class name.\r
+ * @return a String\r
+ */\r
+ public String getClassName() {\r
+ return classname;\r
+ }\r
+\r
+ /**\r
+ * Get the resource identifier.\r
+ * @return a String instance\r
+ */\r
+ public String getIdentifier() {\r
+ if ((identifier == null) && (attributes != null)) {\r
+ for (int i = 0 ; i < attributes.length ; i ++) {\r
+ if (attributes[i].getName().equals("identifier"))\r
+ identifier = (String)attributes[i].getValue();\r
+ }\r
+ }\r
+ return identifier;\r
+ }\r
+\r
+ /**\r
+ * get the children identifiers\r
+ * @return a String array\r
+ */\r
+ public String[] getChildren() {\r
+ return children;\r
+ }\r
+\r
+ /**\r
+ * Set the children names.\r
+ * @param a String array\r
+ */\r
+ public void setChildren(String children[]) {\r
+ this.children = children;\r
+ }\r
+\r
+ /**\r
+ * Get the attributes description.\r
+ * @return an AttributeDescription array\r
+ * @see AttributeDescription\r
+ */\r
+ public AttributeDescription[] getAttributeDescriptions() {\r
+ return attributes;\r
+ }\r
+\r
+ /**\r
+ * Get the description of the frames associated to this resource.\r
+ * @return a ResourceDescription array.\r
+ */\r
+ public ResourceDescription[] getFrameDescriptions() {\r
+ for (int i = 0 ; i < attributes.length ; i ++) {\r
+ Object value = attributes[i].getValue();\r
+ if (value instanceof ResourceDescription[])\r
+ return (ResourceDescription[])value;\r
+ }\r
+ return new ResourceDescription[0];\r
+ }\r
+\r
+ /**\r
+ * Constructor.\r
+ * @param resource the resource to describe.\r
+ */\r
+ public ResourceDescription(Resource resource) {\r
+ this.classname = resource.getClass().getName();\r
+ //build class hierarchy\r
+ Vector vclasses = new Vector(8);\r
+ Vector vinterfaces = new Vector(8);\r
+ Class ints[] = resource.getClass().getInterfaces();\r
+ if (ints != null)\r
+ for (int i = 0 ; i < ints.length ; i++)\r
+ vinterfaces.addElement(ints[i]);\r
+ for (Class c = resource.getClass().getSuperclass(); \r
+ c != null; \r
+ c = c.getSuperclass()) {\r
+ vclasses.addElement(c.getName());\r
+ ints = c.getInterfaces();\r
+ if (ints != null)\r
+ for (int i = 0 ; i < ints.length ; i++)\r
+ vinterfaces.addElement(ints[i]);\r
+ }\r
+ this.classes = new String[vclasses.size()];\r
+ vclasses.copyInto(this.classes);\r
+ this.interfaces = new String[vinterfaces.size()];\r
+ vinterfaces.copyInto(this.interfaces);\r
+ //build attributes description\r
+ Attribute attrs [] = resource.getAttributes();\r
+ Vector vattrs = new Vector(10);\r
+ for (int j = 0 ; j < attrs.length ; j++) {\r
+ Object value = resource.getValue(j, null);\r
+ if (value instanceof ResourceFrame[]) {\r
+ ResourceFrame frames[] = (ResourceFrame[])value;\r
+ int len = frames.length;\r
+ ResourceDescription descr[] = new ResourceDescription[len];\r
+ for (int i = 0 ; i < len ; i++)\r
+ descr[i] = new ResourceDescription(frames[i]);\r
+ vattrs.addElement(new AttributeDescription(attrs[j],\r
+ descr));\r
+ } else {\r
+ vattrs.addElement(new AttributeDescription(attrs[j], \r
+ value));\r
+ }\r
+ }\r
+ this.attributes = new AttributeDescription[vattrs.size()];\r
+ vattrs.copyInto(attributes);\r
+ }\r
+\r
+ /**\r
+ * Set the attributes description of the resource\r
+ * @param a Vector of AttributeDescription instances\r
+ */\r
+ public void setAttributeDescriptions(Vector attrs) {\r
+ attributes = new AttributeDescription[attrs.size()];\r
+ attrs.copyInto(attributes);\r
+ }\r
+\r
+ /**\r
+ * Set the resource class hierarchy.\r
+ * @param a String array\r
+ */\r
+ public void setClassHierarchy(String classes[]) {\r
+ this.classes = classes;\r
+ }\r
+\r
+ /**\r
+ * Set the resource class hierarchy.\r
+ * @param a String array\r
+ */\r
+ public void setInterfaces(String interfaces[]) {\r
+ this.interfaces = interfaces;\r
+ }\r
+\r
+ /**\r
+ * Set the resource class hierarchy.\r
+ * @param a Vector of String instances\r
+ */\r
+ public void setClassHierarchy(Vector vclasses) {\r
+ this.classes = new String[vclasses.size()];\r
+ vclasses.copyInto(this.classes);\r
+ }\r
+\r
+ /**\r
+ * Set the resource class hierarchy.\r
+ * @param a Vector of String instances\r
+ */\r
+ public void setInterfaces(Vector vinterfaces) {\r
+ this.interfaces = new String[vinterfaces.size()];\r
+ vinterfaces.copyInto(this.interfaces);\r
+ }\r
+ /**\r
+ * Constructor.\r
+ * @param classname the resource class name\r
+ */\r
+ public ResourceDescription(String classname) {\r
+ this.classname = classname;\r
+ this.interfaces = new String[0];\r
+ this.classes = new String[0];\r
+ }\r
+\r
+}\r