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.FileNotFoundException;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.net.MalformedURLException;
026: import java.net.URI;
027: import java.net.URISyntaxException;
028: import java.net.URL;
029: import java.util.ArrayList;
030:
031: /**
032: * Base class for classes implementing the {@link DocrootResource} interface.
033: *
034: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
035: */
036: public abstract class AbstractDocrootResourceImpl implements
037: DocrootResource {
038: /**
039: * Stores the path relative to the docroot
040: */
041: protected String path;
042:
043: /**
044: * Stores the complete URI of the resource
045: */
046: protected URI uri;
047:
048: /**
049: * Is set to true, if the URI used to create the resource had a trailing
050: * slash ("/").
051: */
052: protected boolean trailingSlash;
053:
054: protected AbstractDocrootResourceImpl(URI uri) {
055: // Sanity checks
056: if (uri.getScheme() == null
057: || !uri.getScheme().equals("pfixroot")) {
058: throw new IllegalArgumentException("Cannot handle scheme "
059: + uri.getScheme());
060: }
061: if (uri.getAuthority() != null
062: && uri.getAuthority().length() != 0) {
063: throw new IllegalArgumentException(
064: "pfixroot:// URI may not specify authority");
065: }
066: if (uri.getHost() != null && uri.getHost().length() != 0) {
067: throw new IllegalArgumentException(
068: "pfixroot:// URI may not specify host");
069: }
070: if (uri.getPort() != -1) {
071: throw new IllegalArgumentException(
072: "pfixroot:// URI may not specify port");
073: }
074: if (uri.getQuery() != null && uri.getQuery().length() != 0) {
075: throw new IllegalArgumentException(
076: "pfixroot:// URI may not specify query");
077: }
078: if (uri.getFragment() != null
079: && uri.getFragment().length() != 0) {
080: throw new IllegalArgumentException(
081: "pfixroot:// URI may not specify fragment");
082: }
083:
084: String path = uri.getPath();
085: if (!path.startsWith("/")) {
086: throw new IllegalArgumentException("Path \"" + path
087: + "\" does not start with a /");
088: }
089: if (path.contains("//")) {
090: throw new IllegalArgumentException("Path \"" + path
091: + "\" is not well-formed");
092: }
093:
094: if (path.endsWith("/")) {
095: this .trailingSlash = true;
096: path = path.substring(0, path.length() - 1);
097: }
098:
099: try {
100: this .uri = new URI("pfixroot", null, path, null, null)
101: .normalize();
102: } catch (URISyntaxException e) {
103: throw new RuntimeException(e);
104: }
105: this .path = this .uri.getPath();
106: }
107:
108: public abstract boolean canRead();
109:
110: public abstract boolean canWrite();
111:
112: public abstract boolean createNewFile() throws IOException;
113:
114: public abstract boolean delete();
115:
116: public abstract boolean exists();
117:
118: public String getName() {
119: if (path == "/") {
120: return "/";
121: } else {
122: return path.substring(path.lastIndexOf('/') + 1);
123: }
124: }
125:
126: public FileResource getParentAsFileResource() {
127: if (path.equals("/")) {
128: return null;
129: } else {
130: String parentPath = path
131: .substring(0, path.lastIndexOf('/'));
132: try {
133: return ResourceUtil.getFileResource(new URI("pfixroot",
134: null, parentPath, null, null));
135: } catch (URISyntaxException e) {
136: throw new RuntimeException(e);
137: }
138: }
139: }
140:
141: public abstract boolean isDirectory();
142:
143: public abstract boolean isFile();
144:
145: public abstract boolean isHidden();
146:
147: public abstract long lastModified();
148:
149: public abstract String[] list();
150:
151: public FileResource[] listFileResources() {
152: String[] names = list();
153: ArrayList<FileResource> list = new ArrayList<FileResource>();
154: for (String name : names) {
155: list.add(ResourceUtil.getFileResource(this , name));
156: }
157: return list.toArray(new FileResource[list.size()]);
158: }
159:
160: public abstract boolean mkdir();
161:
162: public abstract boolean mkdirs();
163:
164: public URI toURI() {
165: return uri;
166: }
167:
168: public abstract URL toURL() throws MalformedURLException;
169:
170: public abstract InputStream getInputStream()
171: throws FileNotFoundException;
172:
173: public abstract OutputStream getOutputStream()
174: throws FileNotFoundException;
175:
176: public abstract OutputStream getOutputStream(boolean append)
177: throws FileNotFoundException;
178:
179: public int compareTo(FileResource o) {
180: return uri.compareTo(o.toURI());
181: }
182:
183: public String toString() {
184: return uri.toString();
185: }
186:
187: public boolean equals(Object o) {
188: AbstractDocrootResourceImpl res;
189: try {
190: res = (AbstractDocrootResourceImpl) o;
191: } catch (ClassCastException e) {
192: return false;
193: }
194: return uri.equals(res.uri);
195: }
196:
197: public int hashCode() {
198: return uri.hashCode();
199: }
200:
201: public String getRelativePath() {
202: String path = toURI().getPath();
203: if (path.length() > 0) {
204: path = path.substring(1);
205: }
206: return path;
207: }
208: }
|