📄 resource.java
字号:
// Resource.java// $Id: Resource.java,v 1.23 2003/06/06 14:23:14 ylafon Exp $// (c) COPYRIGHT MIT and INRIA, 1996.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.tools.resources ;import java.util.Hashtable;import java.util.Vector;import java.lang.ArrayIndexOutOfBoundsException;/** * The resource class describes an object, accessible through the server. * Resource objects are required to have the following properties: * <ul> * <li>They must be persistent (their life-time can span multiple server * life-time). * <li>They must be editable, so that one can change some of their aspects * (such as any associated attribute). * <li>They must be self-described: each resource must now what kind * of attribute it <em>understands</em>. * <li>They must be able to update themselves: some of the meta-information * associated with a resource may require lot of CPU to compute. * <li>They must implement some name-service policy. * </ul> * <p>These resource objects do not define how they are accessed. See the * sub-classes for specific accesses. */public class Resource extends AttributeHolder { private static final boolean debugunload = false; /** * Attribute index - The index of the resource store entry attribute. */ protected static int ATTR_STORE_ENTRY = -1; /** * Attribute index - The index for the identifier attribute. */ protected static int ATTR_IDENTIFIER = -1 ; /** * Attribute index - Associated resource frames */ protected static int ATTR_RESOURCE_FRAMES = -1; /** * Attribute index - The index for our parent attribute. */ protected static int ATTR_PARENT = -1 ; /** * Attribute index - The hierarchical context of the resource. */ protected static int ATTR_CONTEXT = -1; /** * Attribute index - The index for our URL attribute. */ protected static int ATTR_URL = -1; /** * Attribute index - The index for the last-modified attribute. */ protected static int ATTR_LAST_MODIFIED = -1 ; /** * Attribute index - The help URL for this resource (Ref doc) */ protected static int ATTR_HELP_URL = -1 ; public static String id = "identifier".intern(); public static String co = "context".intern(); static { Attribute a = null ; Class cls = null ; // Get a pointer to our own class: try { cls = Class.forName("org.w3c.tools.resources.Resource") ; } catch (Exception ex) { ex.printStackTrace() ; System.exit(1) ; } // Our parent resource (the one that created us) a = new ObjectAttribute("parent", "org.w3c.tools.resources.Resource", null, Attribute.COMPUTED|Attribute.DONTSAVE); ATTR_PARENT = AttributeRegistry.registerAttribute(cls, a) ; // Our runtime context a = new ObjectAttribute("context", "org.w3c.tools.resources.ResourceContext", null, Attribute.COMPUTED|Attribute.DONTSAVE); ATTR_CONTEXT = AttributeRegistry.registerAttribute(cls, a) ; // The resource store entry for that resource: a = new ObjectAttribute("store-entry" , "java.lang.Object" , null , Attribute.DONTSAVE); ATTR_STORE_ENTRY = AttributeRegistry.registerAttribute(cls, a); // The identifier attribute: a = new StringAttribute("identifier" , null , Attribute.MANDATORY|Attribute.EDITABLE); ATTR_IDENTIFIER = AttributeRegistry.registerAttribute(cls, a); // The frames associated to that resource: a = new FrameArrayAttribute("frames" , null , Attribute.COMPUTED); ATTR_RESOURCE_FRAMES = AttributeRegistry.registerAttribute(cls, a); // Our URL a = new StringAttribute("url", null, Attribute.COMPUTED|Attribute.DONTSAVE); ATTR_URL = AttributeRegistry.registerAttribute(cls, a) ; // The last modified attribute: a = new DateAttribute("last-modified", null, Attribute.COMPUTED|Attribute.EDITABLE) ; ATTR_LAST_MODIFIED = AttributeRegistry.registerAttribute(cls,a); // The help url attribute a = new StringAttribute("help-url", null, Attribute.COMPUTED); ATTR_HELP_URL = AttributeRegistry.registerAttribute(cls,a); } public Object getClone(Object values[]) { // The frame attribute needs one more level of cloning: ResourceFrame f[] = (ResourceFrame[]) values[ATTR_RESOURCE_FRAMES]; if ( f != null ) { ResourceFrame c[] = new ResourceFrame[f.length] ; for (int i = 0 ; i < f.length ; i++) { if ( f[i] == null ) c[i] = null; else c[i] = (ResourceFrame) f[i].getClone(); } values[ATTR_RESOURCE_FRAMES] = c; } return super.getClone(values); } /** * Get this resource parent resource. * The parent of a resource can be either <strong>null</strong> if it is * the server root resource, or any Resource. * @return An instance of ResourceReference, or <strong>null</strong> */ public ResourceReference getParent() { ResourceContext context = unsafeGetContext(); if (context == null) //are we external? return null; return context.getContainer(); } /** * Get the file part of the URL this resource is attached to. * @return An URL object specifying the location in the information * space of this resource. */ public String getURLPath() { return getString(ATTR_URL, null) ; } /** * Get the server this resource is served by. * @return The first instance of Jigsaw this resource was attached to. */ public ServerInterface getServer() { return ((ResourceContext) getValue(ATTR_CONTEXT, null)).getServer(); } /** * Get this resource's help url. * @return An URL, encoded as a String, or <strong>null</strong> if not * available. */ public String getHelpURL() { return getString(ATTR_HELP_URL, null); } private String computeHelpUrl() { try { StringBuffer sb = new StringBuffer(128); sb.append(getServer().getDocumentationURL()); sb.append('/'); sb.append(getClass().getName()); sb.append(".html"); return sb.toString().intern(); } catch (Exception ex) { return null; } } synchronized public Object getValue (int idx, Object def) { if ((idx == ATTR_HELP_URL) && (values[ATTR_HELP_URL] == null)) values[ATTR_HELP_URL] = computeHelpUrl(); return super.getValue(idx, def); } public Object unsafeGetValue (int idx, Object def) { if ((idx == ATTR_HELP_URL) && (values[ATTR_HELP_URL] == null)) values[ATTR_HELP_URL] = computeHelpUrl(); return super.unsafeGetValue(idx, def); } /** * Get the help URL for that resource's topic. * @param topic The topic you want help for. * @return A String encoded URL, or <strong>null</strong> if none * was found. */ public String getHelpURL(String topics) { return null; } /** * Get the hierarchical context for that resource. * @return A ResourceContext instance, guaranteed not to be <strong>null * </strong> */ protected ResourceContext getContext() { return (ResourceContext) getValue(ATTR_CONTEXT, null); } protected ResourceContext unsafeGetContext() { return (ResourceContext) unsafeGetValue(ATTR_CONTEXT, null); } /** * Set the given context as the current context of this resource. * @param context The new context. */ protected void setContext(ResourceContext context) { context.setResourceReference(getResourceReference()); setValue(ATTR_CONTEXT, context); } /** * Set the given context as the current context of this resource. * @param context The new context. * @param keepmodules If true the new context will have the same * modules than the old one. */ protected void setContext(ResourceContext context, boolean keepmodules) { context.setResourceReference(getResourceReference()); if (keepmodules) { ResourceContext ctxt = getContext(); if (ctxt != null) context.modules = ctxt.modules; } setValue(ATTR_CONTEXT, context); } /** * Get the store entry for that resource. * Only the resource store in charge of this resource knows about the * type of the resulting object. Buy declaring the class of that object * private, the resource store can assume some private access to it. * @return A java Object instance, or <strong>null</strong> if no * store entry is attached to that resource. */ public Object getStoreEntry() { return getValue(ATTR_STORE_ENTRY, null); } /** * Get this resource identifier. * @return The String value for the identifier. */ public String getIdentifier() { return getString(ATTR_IDENTIFIER, null) ; } /** * Get this resource identifier. * @return The String value for the identifier. */ public String unsafeGetIdentifier() { return unsafeGetString(ATTR_IDENTIFIER, null) ; } /** * Get the space entry for that resource. This Object is use to * retrieve the resource in the resource space. * @return A spaceEntry instance. */ protected SpaceEntry getSpaceEntry() { ResourceReference rr = getParent(); if (rr != null) { try { ContainerResource cont = (ContainerResource) rr.lock(); return new SpaceEntryImpl(cont); } catch (InvalidResourceException ex) { return null; } finally { rr.unlock(); } } return null; } /** * Get the ResourceSpace where this resource is stored. * @return A ResourceSpace instance. */ protected ResourceSpace getSpace() { ResourceContext context = getContext(); if (context != null) return context.getSpace(); return null; } /** * Get the ResourceReference of that resource. ResourceReference is the * only public way to access a resource. * @return a ResourceReference instance. */ public ResourceReference getResourceReference() { ResourceContext context = getContext(); if (context != null) return context.getResourceReference(); return null; } /** * Initialize and attach a new ResourceFrame to that resource. * @param frame An uninitialized ResourceFrame instance. * @param defs A default set of attribute values. */ public void registerFrame(ResourceFrame frame, Hashtable defs) { synchronized (this) { ResourceFrame frames[] = null; frames = (ResourceFrame[]) getValue(ATTR_RESOURCE_FRAMES, null); // Initialize the frame with given default attributes: if ( defs.get(id) == null ) { String fname = "frame-"+((frames == null) ? 0 : frames.length); defs.put(id, fname.intern()); } frame.initialize(defs); // Look for a free slot frame: if ( frames == null ) { frames = new ResourceFrame[1]; frames[0] = frame; } else { int slot = -1; // Look for a free slot: for (int i = 0 ; i < frames.length ; i++) { if ( frames[i] == null ) { slot = i; break; } } if ( slot >= 0 ) { // Free slot available: frames[slot] = frame; } else { // Resize frames: ResourceFrame nf[] = new ResourceFrame[frames.length+1]; System.arraycopy(frames, 0, nf, 0, frames.length); nf[frames.length] = frame; frames = nf; } } // Set the frames: setValue(ATTR_RESOURCE_FRAMES, frames);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -