KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URLConnection JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import org.apache.excalibur.source.Source;
33 import org.apache.excalibur.source.SourceException;
34 import org.apache.excalibur.source.SourceNotFoundException;
35 import org.apache.excalibur.source.SourceValidity;
36 import org.apache.excalibur.source.TraversableSource;
37
38 /**
39  * See {@link WikiDataSourceFactory}.
40  */

41 public class WikiDataSource implements Source, TraversableSource {
42     private File JavaDoc actualFile;
43     private File JavaDoc file;
44     private File JavaDoc fallbackFile;
45     private String JavaDoc uri;
46
47     public WikiDataSource(File JavaDoc file, File JavaDoc fallbackFile, String JavaDoc uri) {
48         this.file = file;
49         this.fallbackFile = fallbackFile;
50         this.uri = uri;
51         determineActualFile();
52     }
53
54     private void determineActualFile() {
55         this.actualFile = file.exists() ? file: fallbackFile;
56     }
57
58     public boolean exists() {
59         return getFile().exists();
60     }
61
62     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, SourceNotFoundException {
63         try {
64             return new FileInputStream JavaDoc(getFile());
65         } catch (FileNotFoundException JavaDoc fnfe) {
66             throw new SourceNotFoundException(uri + " doesn't exist.", fnfe);
67         }
68     }
69
70     public String JavaDoc getURI() {
71         return uri;
72     }
73
74     public String JavaDoc getScheme() {
75         return "wikidata";
76     }
77
78     public SourceValidity getValidity() {
79         return new WikiDataSourceValidity(file);
80     }
81
82     public void refresh() {
83         determineActualFile();
84     }
85
86     public String JavaDoc getMimeType() {
87         // file and fallback file have always the same name
88
return URLConnection.getFileNameMap().getContentTypeFor(getFile().getName());
89     }
90
91     public long getContentLength() {
92         return getFile().length();
93     }
94
95     public long getLastModified() {
96         return getFile().lastModified();
97     }
98
99     public Source getChild(String JavaDoc child) throws SourceException {
100         return new WikiDataSource(new File JavaDoc(file, child), new File JavaDoc(fallbackFile, child), uri + "/" + child);
101     }
102
103     public Collection JavaDoc getChildren() throws SourceException {
104         Set JavaDoc children = new HashSet JavaDoc();
105         children.addAll(Arrays.asList(file.list()));
106         children.addAll(Arrays.asList(fallbackFile.list()));
107         List JavaDoc sources = new ArrayList JavaDoc(children.size());
108         Iterator JavaDoc childIterator = children.iterator();
109         while (childIterator.hasNext()) {
110            String JavaDoc child = (String JavaDoc)childIterator.next();
111            sources.add(new WikiDataSource(new File JavaDoc(file, child), new File JavaDoc(fallbackFile, child), uri + "/" + child));
112         }
113         return sources;
114     }
115
116     public String JavaDoc getName() {
117         return getFile().getName();
118     }
119
120     public Source getParent() throws SourceException {
121         // TODO we could check that we don't go above the "root", i.e. outside the wikidata directory
122
// though this doesn't matter much as its currently not used
123
return new WikiDataSource(file.getParentFile(), fallbackFile.getParentFile(), uri.substring(0, uri.lastIndexOf("/")));
124     }
125
126     public boolean isCollection() {
127         return getFile().isDirectory();
128     }
129     
130     public File JavaDoc getFile () {
131         return actualFile;
132     }
133 }
134
Popular Tags