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