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.util;
020:
021: import java.io.File;
022:
023: import de.schlund.pfixxml.resources.FileResource;
024:
025: /**
026: * Similar to java.io.File, but the base directory for relative paths can be
027: * specified.
028: *
029: * @deprecated Use {@link FileResource} instead
030: */
031:
032: @Deprecated
033: public class Path implements Comparable<Path> {
034: private static final String SEP = File.separator;
035:
036: public static final File ROOT = new File(SEP); // TODO: windows
037: public static final File HERE = new File(System
038: .getProperty("user.dir"));
039: public static final File USER = new File(System
040: .getProperty("user.home"));
041:
042: public static Path create(String relative) {
043: return create(HERE, relative);
044: }
045:
046: public static Path create(File base, String relative) {
047: return new Path(base, relative);
048: }
049:
050: public static String getRelativeString(File base, String absolute) {
051: String prefix = base.getAbsolutePath() + SEP;
052: if (absolute.startsWith(prefix)) {
053: return absolute.substring(prefix.length());
054: } else {
055: return null;
056: }
057: }
058:
059: //--
060:
061: /** starts and ends with SEP */
062: private final File base;
063:
064: /** never starts with SEP, may be "" */
065: private final String relative;
066:
067: /** use one of the create methods ... */
068: private Path(File base, String relative) {
069: if (relative.startsWith(SEP)) {
070: throw new IllegalArgumentException("relative is absolute: "
071: + relative);
072: }
073: if (!base.isAbsolute()) {
074: throw new IllegalArgumentException("relative base: " + base);
075: }
076: this .base = base;
077: this .relative = relative;
078: }
079:
080: public File getBase() {
081: return base;
082: }
083:
084: public String getRelative() {
085: return relative;
086: }
087:
088: public File resolve() {
089: if ("".equals(relative)) {
090: return base;
091: } else {
092: return new File(base.getPath(), relative);
093: }
094: }
095:
096: /**
097: * TODO: does not consider base ...
098: * @return null if no directory
099: * */
100: public String getDir() {
101: int idx;
102:
103: idx = relative.lastIndexOf(SEP);
104: return (idx == -1) ? null : relative.substring(0, idx);
105: }
106:
107: public String getName() {
108: return relative.substring(relative.lastIndexOf(SEP) + 1);
109: }
110:
111: public String getSuffix() {
112: int idx;
113:
114: idx = relative.lastIndexOf(".");
115: return (idx == -1) ? "" : relative.substring(idx);
116: }
117:
118: //--
119:
120: /** TODO: does not consider base */
121: public boolean equals(Object obj) {
122: if (obj instanceof Path) {
123: return ((Path) obj).relative.equals(relative);
124: } else {
125: return false;
126: }
127: }
128:
129: /** TODO: does not consider base */
130: public int compareTo(Path obj) {
131: // TODO: base might be different?!
132: return relative.compareTo(obj.relative);
133: }
134:
135: public int hashCode() {
136: return relative.hashCode();
137: }
138:
139: /**
140: * Returns a string representation in the form:
141: * <code>getClass().getName()+"[base=\""+getBase()+"\"; relative=\""+getRelative()+"\"]"</code>
142: *
143: * Use {@link #getBase()} and {@link #getRelative()} to get the path without additional text.
144: */
145: public String toString() {
146: return getClass().getName() + "[base=\"" + getBase()
147: + "\"; relative=\"" + getRelative() + "\"]";
148: }
149:
150: }
|