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

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

Webmaster