Source code: com/dghda/module/ModulePathFileComponent.java
1 /* Copyright (C) 2001 Duane Griffin <duanegriffin@users.sourceforge.net>
2 This file is part of Kent.
3
4 Kent is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 Kent is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with Kent; see the file COPYING. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19
20 package com.dghda.module;
21
22 import java.io.*;
23 import java.util.*;
24
25 /**
26 A module path file component provides access to a module provider from a file.
27 */
28 public class ModulePathFileComponent implements ModulePathComponent {
29
30 /** An iterator that returns a provider for a file. */
31 protected class FileIterator implements Iterator {
32
33 /** Returns true for the first element only. */
34 public boolean hasNext() {
35 return m_First;
36 }
37
38 /** Returns the file's provider. */
39 public Object next() {
40 if (!m_First)
41 throw new NoSuchElementException();
42
43 m_First = false;
44 return getProvider();
45 }
46
47 /** Throws a UnsupportedOperationException exception. */
48 public void remove() {
49 throw new UnsupportedOperationException();
50 }
51
52 private boolean m_First = true;
53 }
54
55 /** Create a new ModulePathFileComponent representing the given file. */
56 public ModulePathFileComponent (String file) {
57 m_File = new File (file);
58 }
59
60 /**
61 The iterator will return at most one provider.
62 @param updated If true the file provider will only be returned if it has been modified since the last time it was scanned.
63 */
64 public Iterator getProviders (boolean updated) {
65
66 // Check whether we should return anything
67 if (updated && (m_Scanned >= m_File.lastModified())) {
68 return new ModulePathComponent.EmptyIterator();
69 } else {
70 m_Scanned = new Date().getTime();
71 return new FileIterator();
72 }
73 }
74
75 /** Return a provider representing the file. */
76 public ModulePathComponent.Provider getProvider() {
77 return new ModulePathComponent.Provider() {
78 public String getName() {
79 return m_File.getName();
80 }
81 public long getModified() {
82 return m_File.lastModified();
83 }
84 public InputStream getInputStream() throws IOException {
85 return new FileInputStream (m_File);
86 }
87 };
88 }
89
90 /** Returns the path to the file. */
91 public String getPath() {
92 return m_File.getName();
93 }
94
95 private File m_File;
96 private long m_Scanned = 0;
97 }