001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixxml.resources;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileNotFoundException;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.OutputStream;
028: import java.net.MalformedURLException;
029: import java.net.URI;
030: import java.net.URISyntaxException;
031: import java.net.URL;
032: import java.util.ArrayList;
033:
034: /**
035: * Implementation of FileResource used to handle <code>file://</code> URIs. This
036: * class should never be exposed to another package, instead the interface should
037: * be used.
038: *
039: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
040: */
041: class FileSystemResourceImpl implements FileSystemResource {
042: URI uri;
043: File file;
044:
045: FileSystemResourceImpl(URI uri) {
046: // Sanity checks
047: if (uri.getScheme() == null || !uri.getScheme().equals("file")) {
048: throw new IllegalArgumentException("Cannot handle scheme "
049: + uri.getScheme());
050: }
051: if (uri.getAuthority() != null
052: && uri.getAuthority().length() != 0) {
053: throw new IllegalArgumentException(
054: "pfixroot:// URI may not specify authority");
055: }
056: if (uri.getHost() != null && uri.getHost().length() != 0) {
057: throw new IllegalArgumentException(
058: "file:// URI may not specify host");
059: }
060: if (uri.getPort() != -1) {
061: throw new IllegalArgumentException(
062: "file:// URI may not specify port");
063: }
064: if (uri.getQuery() != null && uri.getQuery().length() != 0) {
065: throw new IllegalArgumentException(
066: "file:// URI may not specify query");
067: }
068: if (uri.getFragment() != null
069: && uri.getFragment().length() != 0) {
070: throw new IllegalArgumentException(
071: "file:// URI may not specify fragment");
072: }
073:
074: String path = uri.getPath();
075: if (path.contains("//")) {
076: throw new IllegalArgumentException("\"" + uri.toString()
077: + "\" is not a valid URI");
078: }
079: if (!path.startsWith("/")) {
080: throw new IllegalArgumentException("Path in URI + \""
081: + uri.toString() + "\" is not absolute");
082: }
083: boolean dirPath = false;
084: if (path.endsWith("/")) {
085: path = path.substring(0, path.length() - 1);
086: dirPath = true;
087: }
088: file = new File(path);
089: if (dirPath && file.exists() && !file.isDirectory()) {
090: throw new IllegalArgumentException("URI \""
091: + uri.toString()
092: + "\" does not point to a directory");
093: }
094: try {
095: this .uri = new URI("file", null, path, null, null)
096: .normalize();
097: } catch (URISyntaxException e) {
098: // Should never happen
099: throw new RuntimeException(e);
100: }
101: }
102:
103: public boolean canRead() {
104: return file.canRead();
105: }
106:
107: public boolean canWrite() {
108: return file.canWrite();
109: }
110:
111: public boolean createNewFile() throws IOException {
112: return file.createNewFile();
113: }
114:
115: public boolean delete() {
116: return file.delete();
117: }
118:
119: public boolean exists() {
120: return file.exists();
121: }
122:
123: public String getName() {
124: return file.getName();
125: }
126:
127: public FileResource getParentAsFileResource() {
128: File parentFile = file.getParentFile();
129: if (parentFile != null) {
130: return ResourceUtil.getFileResource(file.getParentFile()
131: .toURI());
132: } else {
133: return null;
134: }
135: }
136:
137: public boolean isDirectory() {
138: return file.isDirectory();
139: }
140:
141: public boolean isFile() {
142: return file.isFile();
143: }
144:
145: public boolean isHidden() {
146: return file.isHidden();
147: }
148:
149: public long lastModified() {
150: return file.lastModified();
151: }
152:
153: public String[] list() {
154: return file.list();
155: }
156:
157: public FileResource[] listFileResources() {
158: File[] files = file.listFiles();
159: ArrayList<FileResource> list = new ArrayList<FileResource>();
160: for (File item : files) {
161: list.add(ResourceUtil.getFileResource(item.toURI()));
162: }
163: return list.toArray(new FileResource[list.size()]);
164: }
165:
166: public boolean mkdir() {
167: return file.mkdir();
168: }
169:
170: public boolean mkdirs() {
171: return file.mkdirs();
172: }
173:
174: public URI toURI() {
175: return uri;
176: }
177:
178: public URL toURL() throws MalformedURLException {
179: return file.toURI().toURL();
180: }
181:
182: public InputStream getInputStream() throws FileNotFoundException {
183: return new FileInputStream(file);
184: }
185:
186: public OutputStream getOutputStream() throws FileNotFoundException {
187: return new FileOutputStream(file);
188: }
189:
190: public OutputStream getOutputStream(boolean append)
191: throws FileNotFoundException {
192: return new FileOutputStream(file, append);
193: }
194:
195: public int compareTo(FileResource o) {
196: return uri.compareTo(o.toURI());
197: }
198:
199: public String toString() {
200: return uri.toString();
201: }
202:
203: public boolean equals(Object o) {
204: FileSystemResourceImpl res;
205: try {
206: res = (FileSystemResourceImpl) o;
207: } catch (ClassCastException e) {
208: return false;
209: }
210: return uri.equals(res.uri);
211: }
212:
213: public int hashCode() {
214: return uri.hashCode();
215: }
216:
217: public String getPathOnFileSystem() {
218: return toURI().getPath();
219: }
220: }
|