1 16 package org.outerj.daisy.frontend.components.wikidatasource; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.URLConnection ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collection ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Set ; 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 41 public class WikiDataSource implements Source, TraversableSource { 42 private File actualFile; 43 private File file; 44 private File fallbackFile; 45 private String uri; 46 47 public WikiDataSource(File file, File fallbackFile, String 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 getInputStream() throws IOException , SourceNotFoundException { 63 try { 64 return new FileInputStream (getFile()); 65 } catch (FileNotFoundException fnfe) { 66 throw new SourceNotFoundException(uri + " doesn't exist.", fnfe); 67 } 68 } 69 70 public String getURI() { 71 return uri; 72 } 73 74 public String 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 getMimeType() { 87 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 child) throws SourceException { 100 return new WikiDataSource(new File (file, child), new File (fallbackFile, child), uri + "/" + child); 101 } 102 103 public Collection getChildren() throws SourceException { 104 Set children = new HashSet (); 105 children.addAll(Arrays.asList(file.list())); 106 children.addAll(Arrays.asList(fallbackFile.list())); 107 List sources = new ArrayList (children.size()); 108 Iterator childIterator = children.iterator(); 109 while (childIterator.hasNext()) { 110 String child = (String )childIterator.next(); 111 sources.add(new WikiDataSource(new File (file, child), new File (fallbackFile, child), uri + "/" + child)); 112 } 113 return sources; 114 } 115 116 public String getName() { 117 return getFile().getName(); 118 } 119 120 public Source getParent() throws SourceException { 121 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 getFile () { 131 return actualFile; 132 } 133 } 134 | Popular Tags |