Annotation of java/classes/org/w3c/jigsaw/servlet/ServletIndexer.java, revision 1.1
1.1 ! bmahe 1: // ServletIndexer.java
! 2: // $Id$
! 3: // (c) COPYRIGHT MIT and INRIA, 1998.
! 4: // Please first read the full copyright statement in file COPYRIGHT.html
! 5:
! 6: package org.w3c.jigsaw.servlet;
! 7:
! 8: import java.io.*;
! 9: import java.util.*;
! 10:
! 11: import javax.servlet.*;
! 12:
! 13: import org.w3c.tools.resources.*;
! 14: import org.w3c.tools.resources.indexer.*;
! 15:
! 16: /**
! 17: * @version $Revision$
! 18: * @author Benoît Mahé (bmahe@w3.org)
! 19: */
! 20: public class ServletIndexer extends SampleResourceIndexer {
! 21:
! 22: /**
! 23: * Copy one hastable in another one.
! 24: * @param fromdefs The source
! 25: * @param todefs The destination
! 26: */
! 27: protected void copyDefs(Hashtable fromdefs, Hashtable toDefs) {
! 28: Enumeration keys = fromdefs.keys();
! 29: while(keys.hasMoreElements()) {
! 30: Object key = keys.nextElement();
! 31: toDefs.put(keys, fromdefs.get(key));
! 32: }
! 33: }
! 34:
! 35: /**
! 36: * Create a default file resource for this file (that exists).
! 37: * @param directory The directory of the file.
! 38: * @param name The name of the file.
! 39: * @param defs A set of default attribute values.
! 40: * @return An instance of Resource, or <strong>null</strong> if
! 41: * we were unable to create it.
! 42: */
! 43:
! 44: protected Resource createFileResource(File directory,
! 45: RequestInterface req,
! 46: String name,
! 47: Hashtable defs)
! 48: {
! 49: if (! name.endsWith(".class"))
! 50: return super.createFileResource(directory, req, name, defs);
! 51: ResourceReference rr = null;
! 52: FramedResource template = null;
! 53:
! 54: // Check that at least one class is defined for all the extensions:
! 55: String exts[] = getFileExtensions(name) ;
! 56: if ( exts == null )
! 57: return null ;
! 58: for (int i = exts.length-1 ; i >= 0 ; i--) {
! 59: rr = getTemplateFor(exts[i]) ;
! 60: if ( rr != null )
! 61: break ;
! 62: }
! 63: if ( rr == null ) {
! 64: // Look for a default template:
! 65: if ((rr = loadExtension(defname)) == null)
! 66: return null ;
! 67: return super.createFileResource(directory, req, name, defs);
! 68: } else {
! 69: //this could become a servlet
! 70: Hashtable tempdefs = null;
! 71: if (defs != null)
! 72: tempdefs = (Hashtable) defs.clone();
! 73: else
! 74: tempdefs = new Hashtable(5) ;
! 75: if ( tempdefs.get("directory") == null )
! 76: tempdefs.put("directory", directory) ;
! 77: if ( tempdefs.get("context") == null )
! 78: tempdefs.put("context", getContext());
! 79: try {
! 80: template = (FramedResource) rr.lock();
! 81: if (template instanceof ServletWrapper) {
! 82: if (tempdefs.get("servlet-class") == null)
! 83: tempdefs.put("servlet-class", name);
! 84: int idx = name.lastIndexOf(".class");
! 85: String id = name.substring(0,idx);
! 86: tempdefs.put("identifier", id) ;
! 87: String url = (String) tempdefs.get("url");
! 88: if ((url != null) && (url.endsWith(".class"))) {
! 89: idx = url.lastIndexOf(".class");
! 90: tempdefs.put("url", url.substring(0, idx));
! 91: }
! 92: } else {
! 93: if ( tempdefs.get("identifier") == null )
! 94: tempdefs.put("identifier", name);
! 95: }
! 96: if (exts != null) {
! 97: // Merge with values defined by the extension:
! 98: for (int i = exts.length ; --i >= 0 ; )
! 99: mergeDefaultAttributes(template, exts[i], tempdefs) ;
! 100: }
! 101: // Create, initialize and return the new resouce
! 102: try {
! 103: FramedResource cloned =
! 104: (FramedResource) template.getClone(tempdefs);
! 105: if (cloned instanceof ServletWrapper) {
! 106: ServletWrapper wrapper = (ServletWrapper) cloned;
! 107: // check the servlet class
! 108: if (! wrapper.isWrappingAServlet())
! 109: return null;
! 110: }
! 111: copyDefs(tempdefs, defs);
! 112: return cloned;
! 113: } catch (Exception ex) {
! 114: ex.printStackTrace() ;
! 115: return null ;
! 116: }
! 117: } catch (InvalidResourceException ex) {
! 118: ex.printStackTrace();
! 119: return null;
! 120: } finally {
! 121: rr.unlock();
! 122: }
! 123: }
! 124: }
! 125: }
Webmaster