Code with Finding: |
class SampleResourceIndexer {
protected Resource createFileResource(File directory,
RequestInterface req,
String name,
Hashtable defs)
{
ResourceReference rr = null;
FramedResource template = null;
Resource newres = null;
Class proto = null;
try {
proto = Class.forName("org.w3c.tools.resources.ProtocolFrame");
} catch (Exception ex) {
// fatal error!
return null;
}
// Check that at least one class is defined for all the extensions:
String exts[] = getFileExtensions(name) ;
if ( exts == null )
return null ;
for (int i = exts.length-1 ; i >= 0 ; i--) {
rr = getTemplateFor(exts[i]) ;
if ( rr != null )
break ;
}
if ( rr == null ) {
// Look for a default template:
if ((rr = loadExtension(defname)) == null)
return null ;
}
// Create the runtime-time default values for attributes.
if ( defs == null )
defs = new Hashtable(5) ;
if ( defs.get("directory") == null )
defs.put("directory", directory) ;
if ( defs.get("identifier") == null )
defs.put("identifier", getIndexedFileName(name)) ;
else
defs.put("identifier",
getIndexedFileName((String)defs.get("identifier"))) ;
if ( defs.get("filename") == null)
defs.put("filename", name) ;
if ( defs.get("context") == null )
defs.put("context", getContext());
try {
template = (FramedResource) rr.lock();
if (exts != null) {
// Merge with values defined by the extension:
for (int i = exts.length ; --i >= 0 ; )
mergeDefaultAttributes(template, exts[i], defs) ;
}
// Create, initialize and return the new resouce
try {
newres = (FramedResource) template.getClone(defs);
} catch (Exception ex) {
ex.printStackTrace() ;
return null ;
}
} catch (InvalidResourceException ex) {
ex.printStackTrace();
return null;
} finally {
rr.unlock();
}
// clone has been done, merge frames now
if (exts != null) {
ResourceFrame rf[] = newres.collectFrames(proto);
if (rf != null) {
for (int j=0; j < rf.length; j++) {
for (int i = exts.length-1 ; i >= 0 ; i--) {
rr = getTemplateFor(exts[i]) ;
if ( rr != null ) {
FramedResource fr = null;
try {
fr = (FramedResource) rr.lock();
ResourceReference trr = null;
trr = fr.getFrameReference(proto);
if (trr != null) {
mergeFrameAttributes(rf[j], exts[i], trr);
}
} catch (InvalidResourceException iex) {
iex.printStackTrace();
return null;
} finally {
rr.unlock();
}
}
}
}
}
}
return newres;
}
}
|