Annotation of java/classes/org/w3c/jigsaw/servlet/ServletIndexer.java, revision 1.8.4.1

1.1       bmahe       1: // ServletIndexer.java
1.8.4.1 ! bmahe       2: // $Id: ServletIndexer.java,v 1.9 2000/08/16 21:37:45 ylafon Exp $
1.1       bmahe       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: 
1.8.4.1 ! bmahe       8: import java.io.File;
1.1       bmahe       9: 
1.8.4.1 ! bmahe      10: import java.util.Enumeration;
        !            11: import java.util.Hashtable;
        !            12: 
        !            13: 
        !            14: 
        !            15: import org.w3c.tools.resources.AttributeHolder;
        !            16: import org.w3c.tools.resources.FramedResource;
        !            17: import org.w3c.tools.resources.InvalidResourceException;
        !            18: import org.w3c.tools.resources.RequestInterface;
        !            19: import org.w3c.tools.resources.Resource;
        !            20: import org.w3c.tools.resources.ResourceReference;
        !            21: 
        !            22: import org.w3c.tools.resources.indexer.SampleResourceIndexer;
1.1       bmahe      23: 
                     24: /**
1.8.4.1 ! bmahe      25:  * @version $Revision: 1.9 $
1.1       bmahe      26:  * @author  Benoît Mahé (bmahe@w3.org)
                     27:  */
                     28: public class ServletIndexer extends SampleResourceIndexer {
                     29: 
                     30:     /**
                     31:      * Copy one hastable in another one.
                     32:      * @param fromdefs The source
                     33:      * @param todefs The destination
                     34:      */
                     35:     protected void copyDefs(Hashtable fromdefs, Hashtable toDefs) {
                     36:        Enumeration keys = fromdefs.keys();
                     37:        while(keys.hasMoreElements()) {
                     38:            Object key = keys.nextElement();
                     39:            toDefs.put(keys, fromdefs.get(key));
                     40:        }
                     41:     }
                     42: 
                     43:     /**
                     44:      * Create a default file resource for this file (that exists).
                     45:      * @param directory The directory of the file.
                     46:      * @param name The name of the file.
                     47:      * @param defs A set of default attribute values.
                     48:      * @return An instance of Resource, or <strong>null</strong> if
                     49:      *    we were unable to create it.
                     50:      */
                     51: 
                     52:     protected Resource createFileResource(File directory,
                     53:                                          RequestInterface req,
                     54:                                          String name,
                     55:                                          Hashtable defs) 
                     56:     {
                     57:        if (! name.endsWith(".class"))
                     58:            return super.createFileResource(directory, req, name, defs);
                     59:        ResourceReference rr = null;
                     60:        FramedResource template = null;
                     61:        
                     62:        // Check that at least one class is defined for all the extensions:
                     63:        String exts[] = getFileExtensions(name) ;
                     64:        if ( exts == null )
                     65:            return null ;
                     66:        for (int i = exts.length-1 ; i >= 0 ; i--) {
                     67:            rr = getTemplateFor(exts[i]) ;
                     68:            if ( rr != null )
                     69:                break ;
                     70:        }
                     71:        if ( rr == null ) {
                     72:            // Look for a default template:
                     73:            if ((rr = loadExtension(defname)) == null)
                     74:                return null ;
                     75:            return super.createFileResource(directory, req, name, defs);
                     76:        } else {
                     77:            //this could become a servlet
                     78:            Hashtable tempdefs = null;
                     79:            if (defs != null)
                     80:                tempdefs = (Hashtable) defs.clone();
                     81:            else
                     82:                tempdefs = new Hashtable(5) ;
                     83:            if ( tempdefs.get("directory") == null )
                     84:                tempdefs.put("directory", directory) ;
                     85:            if ( tempdefs.get("context") == null )
                     86:                tempdefs.put("context", getContext());
                     87:            try {
                     88:                template = (FramedResource) rr.lock();
                     89:                if (template instanceof ServletWrapper) {
                     90:                    if (tempdefs.get("servlet-class") == null)
                     91:                        tempdefs.put("servlet-class", name);
1.4       bmahe      92:                    String id = getIndexedFileName(name);
1.1       bmahe      93:                    tempdefs.put("identifier", id) ;
                     94:                    String url = (String) tempdefs.get("url");
                     95:                    if ((url != null) && (url.endsWith(".class"))) {
1.4       bmahe      96:                        int idx = url.lastIndexOf(".class");
1.1       bmahe      97:                        tempdefs.put("url", url.substring(0, idx));
                     98:                    }
                     99:                } else {
                    100:                    if ( tempdefs.get("identifier") == null )
                    101:                        tempdefs.put("identifier", name);
                    102:                }
                    103:                if (exts != null) {
                    104:                    // Merge with values defined by the extension:
                    105:                    for (int i = exts.length ; --i >= 0 ; ) 
                    106:                        mergeDefaultAttributes(template, exts[i], tempdefs) ;
                    107:                }
1.2       bmahe     108:                // Create, initialize and return the new resource
1.1       bmahe     109:                try {
                    110:                    FramedResource cloned = 
                    111:                        (FramedResource) template.getClone(tempdefs);
                    112:                    if (cloned instanceof ServletWrapper) {
                    113:                        ServletWrapper wrapper = (ServletWrapper) cloned;
                    114:                        // check the servlet class
                    115:                        if (! wrapper.isWrappingAServlet())
                    116:                            return null;
                    117:                    }
1.2       bmahe     118:                    //ok, the defs are good.
1.1       bmahe     119:                    copyDefs(tempdefs, defs);
                    120:                    return cloned;
                    121:                } catch (Exception ex) {
                    122:                    ex.printStackTrace() ;
                    123:                    return null ;
                    124:                }
                    125:            } catch (InvalidResourceException ex) {
                    126:                ex.printStackTrace();
                    127:                return null;
                    128:            } finally {
                    129:                rr.unlock();
                    130:            }
                    131:        } 
1.3       bmahe     132:     }
                    133: 
                    134:     /**
                    135:      * Try to create a virtual resource if the real (physical) resource
                    136:      * is not there.
                    137:      * @param directory The directory the file is in.
                    138:      * @param name The name of the file.
                    139:      * @param defs Any default attribute values that should be provided
                    140:      *    to the created resource at initialization time.
                    141:      * @return A Resource instance, or <strong>null</strong> if the given
                    142:      *    file can't be truned into a resource given our configuration
                    143:      *    database.
                    144:      */
                    145: 
                    146:     protected Resource createVirtualResource( File directory,
1.6       ylafon    147:                                              RequestInterface req,
1.3       bmahe     148:                                              String name,
                    149:                                              Hashtable defs) 
                    150:     {
1.6       ylafon    151:        Resource res = super.createVirtualResource(directory, req, name, defs);
1.3       bmahe     152:        if (res != null)
                    153:            return res;
                    154:        //could be a servlet
1.5       bmahe     155:        char fileSeparatorChar = File.separatorChar;
                    156:        String sname = name.replace('.', fileSeparatorChar)+".class";
1.3       bmahe     157:        File servletfile = new File(directory, sname);
                    158:        if (servletfile.exists())
1.8       bmahe     159:            return createFileResource(directory, null, name+".class", defs);
1.3       bmahe     160:        else
                    161:            return null;
1.4       bmahe     162:     }
                    163: 
                    164:     protected String getIndexedFileName(String name) {
                    165:        String indexed = name;
                    166:        int idx = name.lastIndexOf(".class");
                    167:        if (idx != -1)
                    168:            indexed = name.substring(0,idx);
                    169:        return indexed;
1.1       bmahe     170:     }
                    171: }

Webmaster