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

1.1       bmahe       1: // ServletIndexer.java
1.10    ! ylafon      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.9       ylafon      8: import java.io.File;
1.1       bmahe       9: 
1.9       ylafon     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.10    ! ylafon     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;
1.10    ! ylafon     79:            String s_dir = "directory".intern();
        !            80:            String s_ide = "identifier".intern();
        !            81:            String s_ser = "servlet-class".intern();
        !            82:            String s_con = "context".intern(); 
        !            83:            String s_url = "url".intern(); 
        !            84:            if (defs != null) {
1.1       bmahe      85:                tempdefs = (Hashtable) defs.clone();
1.10    ! ylafon     86:            } else {
1.1       bmahe      87:                tempdefs = new Hashtable(5) ;
1.10    ! ylafon     88:            }
        !            89:            if ( tempdefs.get(s_dir) == null )
        !            90:                tempdefs.put(s_dir, directory) ;
        !            91:            if ( tempdefs.get(s_con) == null )
        !            92:                tempdefs.put(s_con, getContext());
1.1       bmahe      93:            try {
                     94:                template = (FramedResource) rr.lock();
                     95:                if (template instanceof ServletWrapper) {
1.10    ! ylafon     96:                    if (tempdefs.get(s_ser) == null)
        !            97:                        tempdefs.put(s_ser, name);
1.4       bmahe      98:                    String id = getIndexedFileName(name);
1.10    ! ylafon     99:                    tempdefs.put(s_ide, id) ;
        !           100:                    String url = (String) tempdefs.get(s_url);
1.1       bmahe     101:                    if ((url != null) && (url.endsWith(".class"))) {
1.4       bmahe     102:                        int idx = url.lastIndexOf(".class");
1.10    ! ylafon    103:                        tempdefs.put(s_url, url.substring(0, idx));
1.1       bmahe     104:                    }
                    105:                } else {
1.10    ! ylafon    106:                    if ( tempdefs.get(s_ide) == null )
        !           107:                        tempdefs.put(s_ide, name);
1.1       bmahe     108:                }
                    109:                if (exts != null) {
                    110:                    // Merge with values defined by the extension:
                    111:                    for (int i = exts.length ; --i >= 0 ; ) 
                    112:                        mergeDefaultAttributes(template, exts[i], tempdefs) ;
                    113:                }
1.2       bmahe     114:                // Create, initialize and return the new resource
1.1       bmahe     115:                try {
                    116:                    FramedResource cloned = 
                    117:                        (FramedResource) template.getClone(tempdefs);
                    118:                    if (cloned instanceof ServletWrapper) {
                    119:                        ServletWrapper wrapper = (ServletWrapper) cloned;
                    120:                        // check the servlet class
                    121:                        if (! wrapper.isWrappingAServlet())
                    122:                            return null;
                    123:                    }
1.2       bmahe     124:                    //ok, the defs are good.
1.1       bmahe     125:                    copyDefs(tempdefs, defs);
                    126:                    return cloned;
                    127:                } catch (Exception ex) {
                    128:                    ex.printStackTrace() ;
                    129:                    return null ;
                    130:                }
                    131:            } catch (InvalidResourceException ex) {
                    132:                ex.printStackTrace();
                    133:                return null;
                    134:            } finally {
                    135:                rr.unlock();
                    136:            }
                    137:        } 
1.3       bmahe     138:     }
                    139: 
                    140:     /**
                    141:      * Try to create a virtual resource if the real (physical) resource
                    142:      * is not there.
                    143:      * @param directory The directory the file is in.
                    144:      * @param name The name of the file.
                    145:      * @param defs Any default attribute values that should be provided
                    146:      *    to the created resource at initialization time.
                    147:      * @return A Resource instance, or <strong>null</strong> if the given
                    148:      *    file can't be truned into a resource given our configuration
                    149:      *    database.
                    150:      */
                    151: 
                    152:     protected Resource createVirtualResource( File directory,
1.6       ylafon    153:                                              RequestInterface req,
1.3       bmahe     154:                                              String name,
                    155:                                              Hashtable defs) 
                    156:     {
1.6       ylafon    157:        Resource res = super.createVirtualResource(directory, req, name, defs);
1.3       bmahe     158:        if (res != null)
                    159:            return res;
                    160:        //could be a servlet
1.5       bmahe     161:        char fileSeparatorChar = File.separatorChar;
                    162:        String sname = name.replace('.', fileSeparatorChar)+".class";
1.3       bmahe     163:        File servletfile = new File(directory, sname);
                    164:        if (servletfile.exists())
1.8       bmahe     165:            return createFileResource(directory, null, name+".class", defs);
1.3       bmahe     166:        else
                    167:            return null;
1.4       bmahe     168:     }
                    169: 
                    170:     protected String getIndexedFileName(String name) {
                    171:        String indexed = name;
                    172:        int idx = name.lastIndexOf(".class");
                    173:        if (idx != -1)
                    174:            indexed = name.substring(0,idx);
                    175:        return indexed;
1.1       bmahe     176:     }
                    177: }

Webmaster