001: // ServletIndexer.java
002: // $Id: ServletIndexer.java,v 1.10 2002/06/26 17:55:05 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.servlet;
007:
008: import java.io.File;
009:
010: import java.util.Enumeration;
011: import java.util.Hashtable;
012:
013: import org.w3c.tools.resources.AttributeHolder;
014: import org.w3c.tools.resources.FramedResource;
015: import org.w3c.tools.resources.InvalidResourceException;
016: import org.w3c.tools.resources.RequestInterface;
017: import org.w3c.tools.resources.Resource;
018: import org.w3c.tools.resources.ResourceReference;
019:
020: import org.w3c.tools.resources.indexer.SampleResourceIndexer;
021:
022: /**
023: * @version $Revision: 1.10 $
024: * @author Benoît Mahé (bmahe@w3.org)
025: */
026: public class ServletIndexer extends SampleResourceIndexer {
027:
028: /**
029: * Copy one hastable in another one.
030: * @param fromdefs The source
031: * @param todefs The destination
032: */
033: protected void copyDefs(Hashtable fromdefs, Hashtable toDefs) {
034: Enumeration keys = fromdefs.keys();
035: while (keys.hasMoreElements()) {
036: Object key = keys.nextElement();
037: toDefs.put(keys, fromdefs.get(key));
038: }
039: }
040:
041: /**
042: * Create a default file resource for this file (that exists).
043: * @param directory The directory of the file.
044: * @param name The name of the file.
045: * @param defs A set of default attribute values.
046: * @return An instance of Resource, or <strong>null</strong> if
047: * we were unable to create it.
048: */
049:
050: protected Resource createFileResource(File directory,
051: RequestInterface req, String name, Hashtable defs) {
052: if (!name.endsWith(".class"))
053: return super .createFileResource(directory, req, name, defs);
054: ResourceReference rr = null;
055: FramedResource template = null;
056:
057: // Check that at least one class is defined for all the extensions:
058: String exts[] = getFileExtensions(name);
059: if (exts == null)
060: return null;
061: for (int i = exts.length - 1; i >= 0; i--) {
062: rr = getTemplateFor(exts[i]);
063: if (rr != null)
064: break;
065: }
066: if (rr == null) {
067: // Look for a default template:
068: if ((rr = loadExtension(defname)) == null)
069: return null;
070: return super .createFileResource(directory, req, name, defs);
071: } else {
072: //this could become a servlet
073: Hashtable tempdefs = null;
074: String s_dir = "directory".intern();
075: String s_ide = "identifier".intern();
076: String s_ser = "servlet-class".intern();
077: String s_con = "context".intern();
078: String s_url = "url".intern();
079: if (defs != null) {
080: tempdefs = (Hashtable) defs.clone();
081: } else {
082: tempdefs = new Hashtable(5);
083: }
084: if (tempdefs.get(s_dir) == null)
085: tempdefs.put(s_dir, directory);
086: if (tempdefs.get(s_con) == null)
087: tempdefs.put(s_con, getContext());
088: try {
089: template = (FramedResource) rr.lock();
090: if (template instanceof ServletWrapper) {
091: if (tempdefs.get(s_ser) == null)
092: tempdefs.put(s_ser, name);
093: String id = getIndexedFileName(name);
094: tempdefs.put(s_ide, id);
095: String url = (String) tempdefs.get(s_url);
096: if ((url != null) && (url.endsWith(".class"))) {
097: int idx = url.lastIndexOf(".class");
098: tempdefs.put(s_url, url.substring(0, idx));
099: }
100: } else {
101: if (tempdefs.get(s_ide) == null)
102: tempdefs.put(s_ide, name);
103: }
104: if (exts != null) {
105: // Merge with values defined by the extension:
106: for (int i = exts.length; --i >= 0;)
107: mergeDefaultAttributes(template, exts[i],
108: tempdefs);
109: }
110: // Create, initialize and return the new resource
111: try {
112: FramedResource cloned = (FramedResource) template
113: .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: }
134: }
135:
136: /**
137: * Try to create a virtual resource if the real (physical) resource
138: * is not there.
139: * @param directory The directory the file is in.
140: * @param name The name of the file.
141: * @param defs Any default attribute values that should be provided
142: * to the created resource at initialization time.
143: * @return A Resource instance, or <strong>null</strong> if the given
144: * file can't be truned into a resource given our configuration
145: * database.
146: */
147:
148: protected Resource createVirtualResource(File directory,
149: RequestInterface req, String name, Hashtable defs) {
150: Resource res = super .createVirtualResource(directory, req,
151: name, defs);
152: if (res != null)
153: return res;
154: //could be a servlet
155: char fileSeparatorChar = File.separatorChar;
156: String sname = name.replace('.', fileSeparatorChar) + ".class";
157: File servletfile = new File(directory, sname);
158: if (servletfile.exists())
159: return createFileResource(directory, null, name + ".class",
160: defs);
161: else
162: return null;
163: }
164:
165: protected String getIndexedFileName(String name) {
166: String indexed = name;
167: int idx = name.lastIndexOf(".class");
168: if (idx != -1)
169: indexed = name.substring(0, idx);
170: return indexed;
171: }
172: }
|