KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > components > wikidatasource > WikiDataSourceFactory


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend.components.wikidatasource;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.context.Context;
25 import org.apache.avalon.framework.context.ContextException;
26 import org.apache.avalon.framework.context.Contextualizable;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.avalon.framework.thread.ThreadSafe;
29 import org.apache.avalon.framework.activity.Initializable;
30 import org.apache.cocoon.components.ContextHelper;
31 import org.apache.cocoon.environment.Request;
32 import org.apache.excalibur.source.Source;
33 import org.apache.excalibur.source.SourceFactory;
34 import org.outerj.daisy.frontend.util.WikiDataDirHelper;
35 import org.outerj.daisy.frontend.WikiHelper;
36
37 /**
38  * This is a sort of 'fallback' source: it first checks if the request source
39  * exists in the wikidata directory, and otherwise falls back to the daisy
40  * directory in the webapp.
41  *
42  * <p>Note: this should be read as wikidata-source, not as wiki-datasource.
43  * It is a source called wikidata, not a datasource called wiki :-)
44  */

45 public class WikiDataSourceFactory extends AbstractLogEnabled implements SourceFactory, Contextualizable, ThreadSafe, Initializable {
46
47     private Context context;
48     private File JavaDoc wikiDataDir;
49     private volatile File JavaDoc wikiDataFallbackDir;
50     private final static String JavaDoc FALLBACK_INDICATION = "/(webapp)";
51
52     public void contextualize(Context context) throws ContextException {
53         this.context = context;
54     }
55
56     public void initialize() throws Exception JavaDoc {
57         this.wikiDataDir = new File JavaDoc(WikiDataDirHelper.getWikiDataDir(context)).getAbsoluteFile();
58     }
59
60     public Source getSource(String JavaDoc location, Map JavaDoc parameters) throws IOException JavaDoc, MalformedURLException JavaDoc {
61
62         if (wikiDataFallbackDir == null) {
63             synchronized(this) {
64                 if (wikiDataFallbackDir == null) {
65                     Request request = ContextHelper.getRequest(context);
66                     wikiDataFallbackDir = new File JavaDoc(new URL JavaDoc(WikiHelper.getDaisyContextPath(request)).getPath()).getAbsoluteFile();
67                 }
68             }
69         }
70
71         String JavaDoc filePath = location.substring(location.indexOf(":") + 1);
72
73         // The check on just '..' is a bit rude, but avoids needing to check both '../' and '..\'
74
if (filePath.indexOf("..") != -1) {
75             throw new MalformedURLException JavaDoc("The 'wikidata:' source does not allow relative URLs.");
76         }
77
78         File JavaDoc resource;
79         File JavaDoc fallbackResource;
80
81         // If FALLBACK_INDICATION is present in the filePath, always take the file
82
// from the fallback location (the webapp), not the wikidata dir
83
boolean forceFallback = filePath.startsWith(FALLBACK_INDICATION);
84
85         if (forceFallback) {
86             filePath = location.substring(FALLBACK_INDICATION.length());
87             // twice wikiFallbackDir on purpose!
88
resource = new File JavaDoc (wikiDataFallbackDir, filePath);
89             fallbackResource = new File JavaDoc(wikiDataFallbackDir, filePath);
90         } else {
91             // the normal case
92
resource = new File JavaDoc (wikiDataDir, filePath);
93             fallbackResource = new File JavaDoc(wikiDataFallbackDir, filePath);
94         }
95
96         StringBuffer JavaDoc absolutePath = new StringBuffer JavaDoc(150);
97         absolutePath.append("wikidata:");
98         if (forceFallback)
99             absolutePath.append("/(webapp)");
100         absolutePath.append(filePath);
101
102         return new WikiDataSource(resource, fallbackResource, absolutePath.toString());
103     }
104
105     public void release(Source source) {}
106
107 }
108
Popular Tags