Annotation of java/classes/org/w3c/jigsaw/servlet/ServletIndexer.java, revision 1.7
1.1 bmahe 1: // ServletIndexer.java
1.7 ! bmahe 2: // $Id: ServletIndexer.java,v 1.6 1999/03/29 20:56:50 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:
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: /**
1.7 ! bmahe 17: * @version $Revision: 1.6 $
1.1 bmahe 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);
1.4 bmahe 84: String id = getIndexedFileName(name);
1.1 bmahe 85: tempdefs.put("identifier", id) ;
86: String url = (String) tempdefs.get("url");
87: if ((url != null) && (url.endsWith(".class"))) {
1.4 bmahe 88: int idx = url.lastIndexOf(".class");
1.1 bmahe 89: tempdefs.put("url", url.substring(0, idx));
90: }
91: } else {
92: if ( tempdefs.get("identifier") == null )
93: tempdefs.put("identifier", name);
94: }
95: if (exts != null) {
96: // Merge with values defined by the extension:
97: for (int i = exts.length ; --i >= 0 ; )
98: mergeDefaultAttributes(template, exts[i], tempdefs) ;
99: }
1.2 bmahe 100: // Create, initialize and return the new resource
1.1 bmahe 101: try {
102: FramedResource cloned =
103: (FramedResource) template.getClone(tempdefs);
104: if (cloned instanceof ServletWrapper) {
105: ServletWrapper wrapper = (ServletWrapper) cloned;
106: // check the servlet class
107: if (! wrapper.isWrappingAServlet())
108: return null;
109: }
1.2 bmahe 110: //ok, the defs are good.
1.1 bmahe 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: }
1.3 bmahe 124: }
125:
126: /**
127: * Try to create a virtual resource if the real (physical) resource
128: * is not there.
129: * @param directory The directory the file is in.
130: * @param name The name of the file.
131: * @param defs Any default attribute values that should be provided
132: * to the created resource at initialization time.
133: * @return A Resource instance, or <strong>null</strong> if the given
134: * file can't be truned into a resource given our configuration
135: * database.
136: */
137:
138: protected Resource createVirtualResource( File directory,
1.6 ylafon 139: RequestInterface req,
1.3 bmahe 140: String name,
141: Hashtable defs)
142: {
1.6 ylafon 143: Resource res = super.createVirtualResource(directory, req, name, defs);
1.3 bmahe 144: if (res != null)
145: return res;
146: //could be a servlet
1.5 bmahe 147: char fileSeparatorChar = File.separatorChar;
148: String sname = name.replace('.', fileSeparatorChar)+".class";
1.3 bmahe 149: File servletfile = new File(directory, sname);
150: if (servletfile.exists())
151: return createFileResource(directory, null, sname, defs);
152: else
153: return null;
1.4 bmahe 154: }
155:
156: protected String getIndexedFileName(String name) {
157: String indexed = name;
158: int idx = name.lastIndexOf(".class");
159: if (idx != -1)
160: indexed = name.substring(0,idx);
161: return indexed;
1.1 bmahe 162: }
163: }
Webmaster