📄 framedresource.java
字号:
// FramedResource.java// $Id: FramedResource.java,v 1.28 2002/08/08 12:30:15 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.EventObject;import java.util.Hashtable;import java.io.PrintStream;import org.w3c.tools.resources.event.AttributeChangedEvent;import org.w3c.tools.resources.event.AttributeChangedListener;import org.w3c.tools.resources.event.Events;import org.w3c.tools.resources.event.FrameEvent;import org.w3c.tools.resources.event.FrameEventListener;import org.w3c.tools.resources.event.ResourceEvent;import org.w3c.tools.resources.event.ResourceEventMulticaster;import org.w3c.tools.resources.event.ResourceEventQueue;import org.w3c.tools.resources.event.StructureChangedEvent;import org.w3c.tools.resources.event.StructureChangedListener;/** * A FramedResource manage frames which are called during the * lookup and the perform. */public class FramedResource extends Resource implements FrameEventListener{ /** * The ResourceReference of frames. */ class FrameReference implements ResourceReference { Class frameClass = null; String identifier = null; ResourceReference framedr = null; int lockCount = 0; public void updateContext(ResourceContext ctxt) { //nothing to do } public int nbLock() { return lockCount; } /** * Lock the refered resource in memory. * @return A real pointer to the resource. */ public Resource lock() throws InvalidResourceException { FramedResource res = (FramedResource)framedr.lock(); lockCount++; return res.getFrame(frameClass, identifier); } /** * Lock the refered resource in memory. * @return A real pointer to the resource. */ public Resource unsafeLock() throws InvalidResourceException { FramedResource res = (FramedResource)framedr.lock(); lockCount++; return res.unsafeGetFrame(frameClass, identifier); } /** * Unlock that resource from memory. */ public void unlock() { framedr.unlock(); lockCount--; } /** * Is that resource reference locked ? */ public boolean isLocked() { return lockCount != 0; } FrameReference (ResourceFrame rframe, ResourceReference framedr) { this.frameClass = rframe.getClass(); this.framedr = framedr; this.identifier = rframe.getIdentifier(); } } /** * Debug flag */ protected final boolean debugEvent = false; /** * Do we handle events? */ protected boolean event_disabled = false; /** * Our frames references. */ protected Hashtable framesRef = null; //<ResourceFrame, Reference> /** * Our AttributeChangedListener. */ protected AttributeChangedListener attrListener = null; /** * Our StructureChangedListener. */ protected StructureChangedListener structListener = null; protected void disableEvent() { event_disabled = true; } protected void enableEvent() { event_disabled = false; } protected boolean eventDisabled() { return event_disabled; } /** * Attribute index - The object identifier. */ protected static int ATTR_OID = -1; static { Attribute a = null ; Class cls = null ; // Get a pointer to our class: try { cls = Class.forName("org.w3c.tools.resources.FramedResource") ; } catch (Exception ex) { ex.printStackTrace() ; System.exit(1) ; } // The object identifier, *should* be uniq (see below) a = new IntegerAttribute("oid", null, Attribute.COMPUTED); ATTR_OID = AttributeRegistry.registerAttribute(cls, a); } public Object getClone(Object values[]) { FramedResource clone = (FramedResource) super.getClone(values); clone.framesRef = new Hashtable(3); return clone; } /** * Get this resource's object identifier. * An object identifier is to be used specifically in etags. It's purpose * is to uniquify the etag of a resource. It's computed as a random number *, on demand only. * @return A uniq object identifier for that resource, as an inteeger. */ public int getOid() { int oid = getInt(ATTR_OID, -1); if ( oid == -1 ) { double d = Math.random() * ((double) Integer.MAX_VALUE); setInt(ATTR_OID, oid = (int) d); } return oid; } protected void displayEvent(FramedResource fr, EventObject evt) { System.out.println(">>> ["+fr.getIdentifier()+"] has receive "+evt); } /** * This handles the <code>FRAME_ADDED</code> kind of events. * @param evt The FrameEvent. */ public void frameAdded(FrameEvent evt) { if (debugEvent) displayEvent( this, evt ); if (! isUnloaded()) markModified(); } /** * This handles the <code>FRAME_MODIFIED</code> kind of events. * @param evt The event describing the change. */ public void frameModified(FrameEvent evt) { if (debugEvent) displayEvent( this, evt ); if (! isUnloaded()) markModified(); } /** * A frame is about to be removed * This handles the <code>FRAME_REMOVED</code> kind of events. * @param evt The event describing the change. */ public void frameRemoved(FrameEvent evt) { if (debugEvent) displayEvent( this, evt ); if (! isUnloaded()) markModified(); } /** * 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) { super.registerFrame(frame,defs); frame.addFrameEventListener(this); addAttributeChangedListener(frame); frame.registerResource(this); } /** * Register a new ResourceFrame if none (from the same class) has been * registered. * @param classname The ResourceFrame class * @param identifier The ResourceFrame identifier * @exception ClassNotFoundException if the class can't be found * @exception IllegalAccessException if the class or initializer is not * accessible * @exception InstantiationException if the class can't be instanciated * @exception ClassCastException if the class is not a ResourceFrame */ protected void registerFrameIfNone(String classname, String identifier) throws ClassNotFoundException, IllegalAccessException, InstantiationException, ClassCastException { Class frameclass = Class.forName(classname); ResourceFrame frame = getFrame(frameclass); if (frame == null) { Hashtable defs = new Hashtable(3); defs.put(id , identifier); registerFrame( (ResourceFrame)frameclass.newInstance() , defs ); } } /** * Unregister a resource frame from the given resource. * @param frame The frame to unregister from the resource. */ public synchronized void unregisterFrame(ResourceFrame frame) { super.unregisterFrame(frame); frame.unregisterResource(this); frame.removeFrameEventListener(this); removeAttributeChangedListener(frame); } private ResourceReference[] getReferenceArray(ResourceFrame[] frames) { if (frames == null) return null; ResourceReference[] refs = new ResourceReference[frames.length]; ResourceReference rr = null; for (int i=0 ; i < frames.length ; i++) { rr = (ResourceReference)framesRef.get(frames[i]); if (rr == null) { rr = (ResourceReference) new FrameReference(frames[i], getResourceReference()); framesRef.put(frames[i],rr); } refs[i] = rr; } return refs; } /** * Collect all frames references. * @return An array of ResourceReference, containing a set of * FrameReference instances or <strong>null</strong> if no resource * frame is available. */ public synchronized ResourceReference[] getFramesReference() { return getReferenceArray(getFrames()); } /** * Collect any frame reference pointing to an instance of the given class. * @param cls The class of frames we are looking for. * @return An array of ResourceReference, containing a set of * FrameReference pointing to instances of the given class, or * <strong>null</strong> if no resource frame is available. */ public synchronized ResourceReference[] collectFramesReference(Class c) { return getReferenceArray(collectFrames(c)); } /** * Get the first occurence of a frame of the given class. * @param cls The class of te frame to look for. * @return A ResourceReference instance, or <strong>null</strong>. */ public synchronized ResourceReference getFrameReference(Class c) { ResourceFrame frame = getFrame(c); if (frame == null) return null; ResourceReference rr = (ResourceReference)framesRef.get(frame); if (rr == null) { rr = (ResourceReference) new FrameReference(frame, getResourceReference()); framesRef.put(frame,rr); } return rr; } /** * Get The FrameReference of the given frame, or <strong>null</strong> * if the frame is not registered. * @param frame The ResourceFrame. * @return A ResourceReference instance. */ public synchronized ResourceReference getFrameReference(ResourceFrame frame) { ResourceReference rr = (ResourceReference)framesRef.get(frame); if (rr == null) { rr = (ResourceReference) new FrameReference(frame, getResourceReference()); framesRef.put(frame,rr); } return rr; } /** * Get the frame of the given class and identifier. * @param cls The class of frames we are looking for. * @param identifier the frame identifier * @return a ResourceFrame instance of <strong>null</strong> */ public synchronized ResourceFrame getFrame(Class c, String identifier) { ResourceFrame frames[] = collectFrames(c); if (frames != null) { for (int i = 0 ; i < frames.length ; i++) { ResourceFrame fr = frames[i]; if (fr.getIdentifier().equals(identifier)) return fr; } } return null; } /** * Get the frame of the given class and identifier.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -