The resource we will write here will be the PassDirectory. The tutorial will go through the following steps:
import java.util.*; import java.io.*; import org.w3c.tools.resources.*; public class PassDirectory extends org.w3c.jigsaw.resources.DirectoryResource { }
For our sample resource, we don't need to create a new package, let's use org.w3c.jigsaw.resources. We can write in it the following PassDirectory.java file:
package org.w3c.jigsaw.resources ; import java.util.*; import java.io.*; import org.w3c.tools.resources.*; public class PassDirectory extends org.w3c.jigsaw.resources.DirectoryResource { }
The directory wrapped by the resource can be described as an editable FileAttribute, which has no defaults value.
Now that we now the attribute our resource is to have, we should declare it to the AttributeRegistry. This Registry keeps track of all the attributes of all resource classes. For each class it knows of, it maintains an ordered list of the attribute it defines. The fact that this list is ordered is important, since it allows for fast attribute value access (through a simple indirection in the attribute value array of each resource instance). Attribute declaration should be done at class initialization time, so we introduce a static statement in the class, whose purpose is to declare our attribute:
package org.w3c.jigsaw.resources ; import java.util.*; import java.io.*; import org.w3c.tools.resources.*; public class PassDirectory extends org.w3c.jigsaw.resources.DirectoryResource { /** * Attribute index - The target physical directory of this resource. */ protected static int ATTR_PASSTARGET = -1 ; static { Attribute a = null ; Class cls = null ; // Get a pointer to our class. try { cls = Class.forName("org.w3c.jigsaw.resources.PassDirectory") ; } catch (Exception ex) { ex.printStackTrace() ; System.exit(1) ; } // The directory attribute. a = new FileAttribute("pass-target" , null , Attribute.EDITABLE); ATTR_PASSTARGET = AttributeRegistry.registerAttribute(cls, a) ; } }
package org.w3c.jigsaw.resources ; import java.util.*; import java.io.*; import org.w3c.tools.resources.*; public class PassDirectory extends org.w3c.jigsaw.resources.DirectoryResource { /** * Attribute index - The target physical directory of this resource. */ protected static int ATTR_PASSTARGET = -1 ; static { Attribute a = null ; Class cls = null ; // Get a pointer to our class. try { cls = Class.forName("org.w3c.jigsaw.resources.PassDirectory") ; } catch (Exception ex) { ex.printStackTrace() ; System.exit(1) ; } // The directory attribute. a = new FileAttribute("pass-target" , null , Attribute.EDITABLE); ATTR_PASSTARGET = AttributeRegistry.registerAttribute(cls, a) ; } /** * Catch side-effects on pass-target, to absolutize it. * @param idx The attribute to set. * @param value The new value. */ public void setValue(int idx, Object value) { super.setValue(idx, value); if ( idx == ATTR_PASSTARGET ) { File file = (File) value; if ( ! file.isAbsolute() ) { // Make it absolute, relative to the server space. File abs = new File(getServer().getRootDirectory() , file.toString()); values[ATTR_PASSTARGET] = abs; values[ATTR_DIRECTORY] = abs; } } } /** * The getDirectory method now returns the pass-directory. * @return The pass target location. */ public File getDirectory() { return (File) getValue(ATTR_PASSTARGET, null) ; } /** * Make the directory attribute default to the target location. * This is required for classes that rely on the directory attribute to * compute their own attributes. * @param values The values we should initialized from. */ public void initialize(Object values[]) { super.initialize(values); File target = getDirectory(); if ( target != null ) setValue(ATTR_DIRECTORY, target); } }
The example we have been walking through is probably one of the simplest one, however, by now, you shouldn't have to write a new resource class but a new frame class, see the internal design of Jigsaw.
Enjoy !
$Id: writing-resources.html,v 1.1 1998/04/06 11:44:22 benoit Exp $