--- /dev/null
+// ServletIndexer.java\r
+// $Id: ServletIndexer.java,v 1.1 2010/06/15 12:24:11 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1998.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.servlet;\r
+\r
+import java.io.File;\r
+\r
+import java.util.Enumeration;\r
+import java.util.Hashtable;\r
+\r
+\r
+\r
+import org.w3c.tools.resources.AttributeHolder;\r
+import org.w3c.tools.resources.FramedResource;\r
+import org.w3c.tools.resources.InvalidResourceException;\r
+import org.w3c.tools.resources.RequestInterface;\r
+import org.w3c.tools.resources.Resource;\r
+import org.w3c.tools.resources.ResourceReference;\r
+\r
+import org.w3c.tools.resources.indexer.SampleResourceIndexer;\r
+\r
+/**\r
+ * @version $Revision: 1.1 $\r
+ * @author Benoît Mahé (bmahe@w3.org)\r
+ */\r
+public class ServletIndexer extends SampleResourceIndexer {\r
+\r
+ /**\r
+ * Copy one hastable in another one.\r
+ * @param fromdefs The source\r
+ * @param todefs The destination\r
+ */\r
+ protected void copyDefs(Hashtable fromdefs, Hashtable toDefs) {\r
+ Enumeration keys = fromdefs.keys();\r
+ while(keys.hasMoreElements()) {\r
+ Object key = keys.nextElement();\r
+ toDefs.put(keys, fromdefs.get(key));\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Create a default file resource for this file (that exists).\r
+ * @param directory The directory of the file.\r
+ * @param name The name of the file.\r
+ * @param defs A set of default attribute values.\r
+ * @return An instance of Resource, or <strong>null</strong> if\r
+ * we were unable to create it.\r
+ */\r
+\r
+ protected Resource createFileResource(File directory,\r
+ RequestInterface req,\r
+ String name,\r
+ Hashtable defs) \r
+ {\r
+ if (! name.endsWith(".class"))\r
+ return super.createFileResource(directory, req, name, defs);\r
+ ResourceReference rr = null;\r
+ FramedResource template = null;\r
+ \r
+ // Check that at least one class is defined for all the extensions:\r
+ String exts[] = getFileExtensions(name) ;\r
+ if ( exts == null )\r
+ return null ;\r
+ for (int i = exts.length-1 ; i >= 0 ; i--) {\r
+ rr = getTemplateFor(exts[i]) ;\r
+ if ( rr != null )\r
+ break ;\r
+ }\r
+ if ( rr == null ) {\r
+ // Look for a default template:\r
+ if ((rr = loadExtension(defname)) == null)\r
+ return null ;\r
+ return super.createFileResource(directory, req, name, defs);\r
+ } else {\r
+ //this could become a servlet\r
+ Hashtable tempdefs = null;\r
+ String s_dir = "directory".intern();\r
+ String s_ide = "identifier".intern();\r
+ String s_ser = "servlet-class".intern();\r
+ String s_con = "context".intern(); \r
+ String s_url = "url".intern(); \r
+ if (defs != null) {\r
+ tempdefs = (Hashtable) defs.clone();\r
+ } else {\r
+ tempdefs = new Hashtable(5) ;\r
+ }\r
+ if ( tempdefs.get(s_dir) == null )\r
+ tempdefs.put(s_dir, directory) ;\r
+ if ( tempdefs.get(s_con) == null )\r
+ tempdefs.put(s_con, getContext());\r
+ try {\r
+ template = (FramedResource) rr.lock();\r
+ if (template instanceof ServletWrapper) {\r
+ if (tempdefs.get(s_ser) == null)\r
+ tempdefs.put(s_ser, name);\r
+ String id = getIndexedFileName(name);\r
+ tempdefs.put(s_ide, id) ;\r
+ String url = (String) tempdefs.get(s_url);\r
+ if ((url != null) && (url.endsWith(".class"))) {\r
+ int idx = url.lastIndexOf(".class");\r
+ tempdefs.put(s_url, url.substring(0, idx));\r
+ }\r
+ } else {\r
+ if ( tempdefs.get(s_ide) == null )\r
+ tempdefs.put(s_ide, name);\r
+ }\r
+ if (exts != null) {\r
+ // Merge with values defined by the extension:\r
+ for (int i = exts.length ; --i >= 0 ; ) \r
+ mergeDefaultAttributes(template, exts[i], tempdefs) ;\r
+ }\r
+ // Create, initialize and return the new resource\r
+ try {\r
+ FramedResource cloned = \r
+ (FramedResource) template.getClone(tempdefs);\r
+ if (cloned instanceof ServletWrapper) {\r
+ ServletWrapper wrapper = (ServletWrapper) cloned;\r
+ // check the servlet class\r
+ if (! wrapper.isWrappingAServlet())\r
+ return null;\r
+ }\r
+ //ok, the defs are good.\r
+ copyDefs(tempdefs, defs);\r
+ return cloned;\r
+ } catch (Exception ex) {\r
+ ex.printStackTrace() ;\r
+ return null ;\r
+ }\r
+ } catch (InvalidResourceException ex) {\r
+ ex.printStackTrace();\r
+ return null;\r
+ } finally {\r
+ rr.unlock();\r
+ }\r
+ } \r
+ }\r
+\r
+ /**\r
+ * Try to create a virtual resource if the real (physical) resource\r
+ * is not there.\r
+ * @param directory The directory the file is in.\r
+ * @param name The name of the file.\r
+ * @param defs Any default attribute values that should be provided\r
+ * to the created resource at initialization time.\r
+ * @return A Resource instance, or <strong>null</strong> if the given\r
+ * file can't be truned into a resource given our configuration\r
+ * database.\r
+ */\r
+\r
+ protected Resource createVirtualResource( File directory,\r
+ RequestInterface req,\r
+ String name,\r
+ Hashtable defs) \r
+ {\r
+ Resource res = super.createVirtualResource(directory, req, name, defs);\r
+ if (res != null)\r
+ return res;\r
+ //could be a servlet\r
+ char fileSeparatorChar = File.separatorChar;\r
+ String sname = name.replace('.', fileSeparatorChar)+".class";\r
+ File servletfile = new File(directory, sname);\r
+ if (servletfile.exists())\r
+ return createFileResource(directory, null, name+".class", defs);\r
+ else\r
+ return null;\r
+ }\r
+\r
+ protected String getIndexedFileName(String name) {\r
+ String indexed = name;\r
+ int idx = name.lastIndexOf(".class");\r
+ if (idx != -1)\r
+ indexed = name.substring(0,idx);\r
+ return indexed;\r
+ }\r
+}\r