001: // ConfigResourceIndexer.java
002: // $Id: ConfigResourceIndexer.java,v 1.3 2000/07/13 09:49:00 ylafon Exp $
003: // (c) COPYRIGHT MIT,INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.indexer;
007:
008: import java.io.File;
009: import java.io.BufferedInputStream;
010: import java.io.FileInputStream;
011:
012: import java.util.Enumeration;
013: import java.util.Hashtable;
014:
015: import org.w3c.tools.resources.Resource;
016: import org.w3c.tools.resources.ResourceFrame;
017: import org.w3c.tools.resources.ContainerResource;
018: import org.w3c.tools.resources.RequestInterface;
019: import org.w3c.tools.resources.indexer.SampleResourceIndexer;
020: import org.w3c.www.mime.MimeParser;
021: import org.w3c.www.http.MimeParserMessageFactory;
022: import org.w3c.www.http.HttpEntityMessage;
023: import org.w3c.www.http.HeaderDescription;
024: import org.w3c.jigsaw.frames.HTTPFrame;
025: import org.w3c.www.mime.MimeType;
026:
027: /**
028: * This indexer allow to add a configuration file
029: * to overwrite the computed configuration
030: * for now, ./foo.html will have its configuration file in
031: * ./.meta/foo.html.meta
032: * but it can be extended to match other filename.
033: */
034: public class ConfigResourceIndexer extends SampleResourceIndexer {
035:
036: /**
037: * compute and return the file containing the configuration
038: */
039: private File getConfigFile(File directory, String name) {
040: File configdir = new File(directory, ".meta");
041: if (configdir.exists() && configdir.isDirectory()) {
042: File config = new File(configdir, name + ".meta");
043: if (config.exists() && !config.isDirectory())
044: return config;
045: }
046: return null;
047: }
048:
049: /**
050: * Try to create a resource for the given file.
051: * This method makes its best efforts to try to build a default
052: * resource out of a file.
053: * @param directory The directory the file is in.
054: * @param name The name of the file.
055: * @param defs Any default attribute values that should be provided
056: * to the created resource at initialization time.
057: * @return A Resource instance, or <strong>null</strong> if the given
058: * file can't be truned into a resource given our configuration
059: * database.
060: */
061: public Resource createResource(ContainerResource container,
062: RequestInterface request, File directory, String name,
063: Hashtable defs) {
064: Resource r = super .createResource(container, request,
065: directory, name, defs);
066: Class proto = null;
067: try {
068: proto = Class.forName("org.w3c.jigsaw.frames.HTTPFrame");
069: } catch (Exception ex) {
070: // fatal error!
071: return r;
072: }
073: if (r == null)
074: return r;
075: HTTPFrame frame = null;
076: ResourceFrame rf[] = r.collectFrames(proto);
077: if (rf != null) {
078: frame = (HTTPFrame) rf[0];
079: }
080: if (frame == null) {
081: return r;
082: }
083:
084: File config = getConfigFile(directory, name);
085: if (config != null) {
086: // we found a configuration file, do some more processing!
087: try {
088: // first, extract and parse the config file
089: BufferedInputStream buf;
090: buf = new BufferedInputStream(new FileInputStream(
091: config));
092: MimeParser p = new MimeParser(buf,
093: new MimeParserMessageFactory());
094: HttpEntityMessage msg = (HttpEntityMessage) p.parse();
095: Enumeration e = msg.enumerateHeaderDescriptions();
096: // then override some configuration
097: while (e.hasMoreElements()) {
098: // use some well known descriptions
099: HeaderDescription d = (HeaderDescription) e
100: .nextElement();
101: if (d.isHeader(HttpEntityMessage.H_CONTENT_TYPE)) {
102: MimeType mtype = null;
103: try {
104: mtype = msg.getContentType();
105: } catch (Exception ex) {
106: // ok by default use something binary
107: mtype = MimeType.APPLICATION_OCTET_STREAM;
108: }
109: if (mtype.hasParameter("charset")) {
110: String charset = mtype
111: .getParameterValue("charset");
112: MimeType m = new MimeType(mtype.getType(),
113: mtype.getSubtype());
114: frame.setValue("content-type", m);
115: frame.setValue("charset", charset);
116: } else {
117: frame.setValue("content-type", mtype);
118: }
119: continue;
120: }
121: if (d
122: .isHeader(HttpEntityMessage.H_CONTENT_LANGUAGE)) {
123: String lang[] = msg.getContentLanguage();
124: if (lang.length > 0) {
125: frame.setValue("content-language", lang[0]);
126: }
127: continue;
128: }
129: if (d
130: .isHeader(HttpEntityMessage.H_CONTENT_ENCODING)) {
131: String enc[] = msg.getContentEncoding();
132: if (enc.length > 0) {
133: frame.setValue("content-encoding", enc[0]);
134: }
135: continue;
136: }
137: }
138: } catch (Exception ex) {
139: // do nothing, keep configured as it was
140: // by the super indexer
141: ex.printStackTrace();
142: }
143: }
144: return r;
145: }
146: }
|