--- /dev/null
+// ConfigResourceIndexer.java\r
+// $Id: ConfigResourceIndexer.java,v 1.2 2010/06/15 17:53:12 smhuang Exp $\r
+// (c) COPYRIGHT MIT,INRIA and Keio, 2000.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.indexer;\r
+\r
+import java.io.File;\r
+import java.io.BufferedInputStream;\r
+import java.io.FileInputStream;\r
+\r
+import java.util.Enumeration;\r
+import java.util.Hashtable;\r
+\r
+import org.w3c.tools.resources.Resource;\r
+import org.w3c.tools.resources.ResourceFrame;\r
+import org.w3c.tools.resources.ContainerResource;\r
+import org.w3c.tools.resources.RequestInterface;\r
+import org.w3c.tools.resources.indexer.SampleResourceIndexer;\r
+import org.w3c.www.mime.MimeParser;\r
+import org.w3c.www.http.MimeParserMessageFactory;\r
+import org.w3c.www.http.HttpEntityMessage;\r
+import org.w3c.www.http.HeaderDescription;\r
+import org.w3c.jigsaw.frames.HTTPFrame;\r
+import org.w3c.www.mime.MimeType;\r
+\r
+/**\r
+ * This indexer allow to add a configuration file\r
+ * to overwrite the computed configuration\r
+ * for now, ./foo.html will have its configuration file in\r
+ * ./.meta/foo.html.meta\r
+ * but it can be extended to match other filename.\r
+ */\r
+public class ConfigResourceIndexer extends SampleResourceIndexer {\r
+\r
+ /**\r
+ * compute and return the file containing the configuration\r
+ */\r
+ private File getConfigFile(File directory, String name) {\r
+ File configdir = new File(directory, ".meta");\r
+ if (configdir.exists() && configdir.isDirectory()) {\r
+ File config = new File(configdir, name + ".meta");\r
+ if (config.exists() && !config.isDirectory())\r
+ return config;\r
+ }\r
+ return null;\r
+ }\r
+ \r
+\r
+ /**\r
+ * Try to create a resource for the given file.\r
+ * This method makes its best efforts to try to build a default\r
+ * resource out of a file. \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
+ public Resource createResource(ContainerResource container,\r
+ RequestInterface request,\r
+ File directory,\r
+ String name,\r
+ Hashtable defs) \r
+ {\r
+ Resource r = super.createResource(container, request, directory, \r
+ name, defs);\r
+ Class proto = null;\r
+ try {\r
+ proto = Class.forName("org.w3c.jigsaw.frames.HTTPFrame");\r
+ //Added by Jeff Huang\r
+ //TODO: FIXIT\r
+ } catch (Exception ex) {\r
+ // fatal error!\r
+ return r;\r
+ }\r
+ if (r == null)\r
+ return r;\r
+ HTTPFrame frame = null;\r
+ ResourceFrame rf[] = r.collectFrames(proto);\r
+ if (rf != null) {\r
+ frame = (HTTPFrame) rf[0];\r
+ }\r
+ if (frame == null) {\r
+ return r;\r
+ }\r
+\r
+ File config = getConfigFile(directory, name);\r
+ if (config != null) {\r
+ // we found a configuration file, do some more processing!\r
+ try {\r
+ // first, extract and parse the config file\r
+ BufferedInputStream buf;\r
+ buf = new BufferedInputStream(new FileInputStream(config));\r
+ MimeParser p = new MimeParser(buf,\r
+ new MimeParserMessageFactory());\r
+ HttpEntityMessage msg = (HttpEntityMessage) p.parse();\r
+ Enumeration e = msg.enumerateHeaderDescriptions();\r
+ // then override some configuration\r
+ while ( e.hasMoreElements() ) {\r
+ // use some well known descriptions\r
+ HeaderDescription d = (HeaderDescription) e.nextElement();\r
+ if ( d.isHeader(HttpEntityMessage.H_CONTENT_TYPE)) {\r
+ MimeType mtype = null;\r
+ try {\r
+ mtype = msg.getContentType();\r
+ } catch (Exception ex) {\r
+ // ok by default use something binary\r
+ mtype = MimeType.APPLICATION_OCTET_STREAM;\r
+ }\r
+ if (mtype.hasParameter("charset")) {\r
+ String charset =mtype.getParameterValue("charset");\r
+ MimeType m = new MimeType(mtype.getType(),\r
+ mtype.getSubtype());\r
+ frame.setValue("content-type", m);\r
+ frame.setValue("charset", charset);\r
+ } else {\r
+ frame.setValue("content-type", mtype);\r
+ }\r
+ continue;\r
+ }\r
+ if ( d.isHeader(HttpEntityMessage.H_CONTENT_LANGUAGE)) {\r
+ String lang[] = msg.getContentLanguage();\r
+ if (lang.length > 0 ) {\r
+ frame.setValue("content-language", lang[0]);\r
+ }\r
+ continue;\r
+ }\r
+ if ( d.isHeader(HttpEntityMessage.H_CONTENT_ENCODING)) {\r
+ String enc[] = msg.getContentEncoding();\r
+ if (enc.length > 0 ) {\r
+ frame.setValue("content-encoding", enc[0]);\r
+ }\r
+ continue;\r
+ }\r
+ }\r
+ } catch (Exception ex) {\r
+ // do nothing, keep configured as it was\r
+ // by the super indexer\r
+ ex.printStackTrace();\r
+ }\r
+ }\r
+ return r;\r
+ }\r
+}\r