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