package org.w3c.tools.resources ;
import java.util.*;
import java.lang.ArrayIndexOutOfBoundsException;
public class Resource extends AttributeHolder {
private static final boolean debugunload = false;
protected static int ATTR_STORE_ENTRY = -1;
protected static int ATTR_IDENTIFIER = -1 ;
protected static int ATTR_RESOURCE_FRAMES = -1;
protected static int ATTR_PARENT = -1 ;
protected static int ATTR_CONTEXT = -1;
protected static int ATTR_URL = -1;
protected static int ATTR_LAST_MODIFIED = -1 ;
static {
Attribute a = null ;
Class cls = null ;
try {
cls = Class.forName("org.w3c.tools.resources.Resource") ;
} catch (Exception ex) {
ex.printStackTrace() ;
System.exit(1) ;
}
a = new ObjectAttribute("parent",
"org.w3c.tools.resources.Resource",
null,
Attribute.COMPUTED|Attribute.DONTSAVE);
ATTR_PARENT = AttributeRegistry.registerAttribute(cls, a) ;
a = new ObjectAttribute("context",
"org.w3c.tools.resources.ResourceContext",
null,
Attribute.COMPUTED|Attribute.DONTSAVE);
ATTR_CONTEXT = AttributeRegistry.registerAttribute(cls, a) ;
a = new ObjectAttribute("store-entry"
, "java.lang.Object"
, null
, Attribute.DONTSAVE);
ATTR_STORE_ENTRY = AttributeRegistry.registerAttribute(cls, a);
a = new StringAttribute("identifier"
, null
, Attribute.MANDATORY|Attribute.EDITABLE);
ATTR_IDENTIFIER = AttributeRegistry.registerAttribute(cls, a);
a = new FrameArrayAttribute("frames"
, null
, Attribute.COMPUTED);
ATTR_RESOURCE_FRAMES = AttributeRegistry.registerAttribute(cls, a);
a = new StringAttribute("url",
null,
Attribute.COMPUTED|Attribute.DONTSAVE);
ATTR_URL = AttributeRegistry.registerAttribute(cls, a) ;
a = new DateAttribute("last-modified",
null,
Attribute.COMPUTED|Attribute.EDITABLE) ;
ATTR_LAST_MODIFIED = AttributeRegistry.registerAttribute(cls,a);
}
public Object getClone(Object values[]) {
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);
}
@return
public ResourceReference getParent() {
ResourceContext context = getContext();
if (context == null) return null;
return context.getContainer();
}
@return
public String getURLPath() {
return getString(ATTR_URL, null) ;
}
@return
public String getHelpURL() {
return null;
}
@param topic@return
public String getHelpURL(String topics) {
return null;
}
@return
protected ResourceContext getContext() {
return (ResourceContext) getValue(ATTR_CONTEXT, null);
}
@param context
protected void setContext(ResourceContext context) {
context.setResourceReference(getResourceReference());
setValue(ATTR_CONTEXT, context);
}
@param context@param keepmodules
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);
}
@return
public Object getStoreEntry() {
return getValue(ATTR_STORE_ENTRY, null);
}
@return
public String getIdentifier() {
return getString(ATTR_IDENTIFIER, null) ;
}
@return
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;
}
@return
protected ResourceSpace getSpace() {
ResourceContext context = getContext();
if (context != null)
return context.getSpace();
return null;
}
@return
public ResourceReference getResourceReference() {
ResourceContext context = getContext();
if (context != null)
return context.getResourceReference();
return null;
}
@param frame@param defs
public void registerFrame(ResourceFrame frame, Hashtable defs) {
synchronized (this) {
ResourceFrame frames[] = null;
frames = (ResourceFrame[]) getValue(ATTR_RESOURCE_FRAMES, null);
if ( defs.get("identifier") == null )
defs.put("identifier"
, "frame-"+((frames == null) ? 0 : frames.length));
frame.initialize(defs);
if ( frames == null ) {
frames = new ResourceFrame[1];
frames[0] = frame;
} else {
int slot = -1;
for (int i = 0 ; i < frames.length ; i++) {
if ( frames[i] == null ) {
slot = i;
break;
}
}
if ( slot >= 0 ) {
frames[slot] = frame;
} else {
ResourceFrame nf[] = new ResourceFrame[frames.length+1];
System.arraycopy(frames, 0, nf, 0, frames.length);
nf[frames.length] = frame;
frames = nf;
}
}
setValue(ATTR_RESOURCE_FRAMES, frames);
}
}
@param frame
public synchronized void unregisterFrame(ResourceFrame frame) {
ResourceFrame frames[] = null;
frames = (ResourceFrame[]) getValue(ATTR_RESOURCE_FRAMES, null);
if ( frames != null ) {
ResourceFrame f[] = new ResourceFrame[frames.length-1];
int j=0;
for (int i = 0; i < frames.length ; i++) {
if ( frames[i] == frame ) {
System.arraycopy(frames, i+1, f, j, frames.length-i-1);
setValue(ATTR_RESOURCE_FRAMES, f);
return;
} else {
try {
f[j++] = frames[i];
} catch (ArrayIndexOutOfBoundsException ex) {
return; }
}
}
}
}
@return
public synchronized ResourceFrame[] getFrames() {
return (ResourceFrame[]) getValue(ATTR_RESOURCE_FRAMES, null);
}
@param cls@return
public synchronized ResourceFrame[] collectFrames(Class c) {
ResourceFrame frames[] = null;
frames = (ResourceFrame[]) getValue(ATTR_RESOURCE_FRAMES, null);
if ( frames != null ) {
Vector v = new Vector();
for (int i = 0 ; i < frames.length ; i++) {
if ( c.isInstance(frames[i]) )
v.addElement(frames[i]);
}
int sz = v.size();
if ( sz > 0 ) {
ResourceFrame ret[] = new ResourceFrame[sz];
v.copyInto(ret);
return ret;
}
}
return null;
}
@param cls@return
public synchronized ResourceFrame getFrame(Class c) {
ResourceFrame frames[] = null;
frames = (ResourceFrame[]) getValue(ATTR_RESOURCE_FRAMES, null);
if ( frames != null ) {
for (int i = 0 ; i < frames.length ; i++) {
if ( c.isInstance(frames[i]) )
return frames[i];
}
}
return null;
}
@param cls@param idx@param def@return
public synchronized Object getValue(Class c, int idx, Object def) {
ResourceFrame frame = getFrame(c);
if ( frame != null )
return frame.getValue(idx, def);
throw new IllegalAttributeAccess(this, idx);
}
@param cls@param idx@param val
public synchronized void setValue(Class c, int idx, Object val) {
ResourceFrame frame = getFrame(c);
if ( frame != null ) {
frame.setValue(idx, val);
markModified();
return;
}
throw new IllegalAttributeAccess(this, idx);
}
@return
public long getLastModified() {
return getLong(ATTR_LAST_MODIFIED, (long) -1) ;
}
public void markModified() {
ResourceSpace space = getSpace();
if ((space != null) && (getSpaceEntry() != null))
space.markModified(getSpaceEntry(), this);
super.setValue(ATTR_LAST_MODIFIED,
new Long(System.currentTimeMillis()));
}
@param idx@param value
public void setValue(int idx, Object value) {
if ( idx == ATTR_IDENTIFIER ) {
String oldid = getIdentifier();
try {
super.setValue(idx, value);
} catch (IllegalAttributeAccess ex) {
throw ex;
}
if (getSpaceEntry() != null) {
ResourceSpace space = getSpace();
space.renameResource(getSpaceEntry(), oldid, (String) value);
}
markModified();
return;
}
super.setValue(idx, value) ;
if (( ! attributes[idx].checkFlag(Attribute.DONTSAVE) ) &&
( idx != ATTR_LAST_MODIFIED))
markModified() ;
}
@return
public boolean acceptUnload() {
if ( debugunload ) {
try {
System.out.println(getValue("url","<??>")+": acceptUnload");
} catch (IllegalAttributeAccess ex) {
}
}
return true;
}
public void notifyUnload() {
if ( debugunload ) {
try {
System.out.println(getValue("url","<??>")+": unloaded.");
} catch (IllegalAttributeAccess ex) {
}
}
values = null ;
}
public void updateAttributes() {
return ;
}
@exception MultipleLockException
protected void checkMultipleLock(ResourceReference rr)
throws MultipleLockException
{
if (rr.nbLock() > 1)
throw new MultipleLockException(rr.nbLock(), this, "can't delete");
}
public synchronized void delete()
throws MultipleLockException
{
ResourceSpace space = getSpace();
if ((space != null) && (getSpaceEntry() != null)) {
ResourceReference self = getResourceReference();
if (self != null) {
synchronized (self) {
checkMultipleLock(self);
space.deleteResource(getSpaceEntry(), this);
}
} else {
space.deleteResource(getSpaceEntry(), this);
}
}
}
public boolean isInitialized() {
return (values != null);
}
public Resource() {
super() ;
}
}