001: /*
002: * DirectoryEntry.java
003: *
004: * Copyright (C) 1998-2004 Peter Graves
005: * $Id: DirectoryEntry.java,v 1.7 2004/05/21 16:42:02 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import gnu.regexp.REMatch;
025: import java.text.SimpleDateFormat;
026: import java.util.Date;
027:
028: public final class DirectoryEntry {
029: private String name;
030: private long date;
031: private long size;
032: private boolean isDirectory;
033: private boolean isLink;
034: private String linkedTo;
035: private boolean isMarked;
036:
037: // For native format, this is all we store.
038: private final String string;
039:
040: private static final String DIR = " <DIR>";
041: private static final int DIRLENGTH = DIR.length();
042: private static final String DATEFORMAT = "MMM dd yyyy HH:mm";
043: private static SimpleDateFormat dateFormatter = new SimpleDateFormat(
044: DATEFORMAT);
045:
046: // Constructor for native "ls -l" format.
047: private DirectoryEntry(String string, char firstChar) {
048: this .string = string;
049: if (firstChar == 'd')
050: isDirectory = true;
051: else if (firstChar == 'l')
052: isLink = true;
053: size = -1;
054: }
055:
056: // Constructor for internal format.
057: public DirectoryEntry(String name, long date, long size) {
058: this .name = name;
059: this .date = date;
060: this .size = size;
061: string = null;
062: }
063:
064: // Constructor for internal format.
065: public DirectoryEntry(String name, long date, long size,
066: boolean isDirectory) {
067: this .name = name;
068: this .date = date;
069: this .size = size;
070: this .isDirectory = isDirectory;
071: string = null;
072: }
073:
074: // Wrapper for constructor for native "ls -l" format.
075: // Ignore strings that aren't really directory entries.
076: // Apply filter (if any).
077: public static DirectoryEntry getDirectoryEntry(String s,
078: DirectoryFilenameFilter filter) {
079: if (s.length() == 0)
080: return null;
081: // Ignore "total" line, command line echo.
082: // First char must be one of "-dlcbsp".
083: final char c = s.charAt(0);
084: if ("-dlcbsp".indexOf(c) < 0)
085: return null;
086: if (c == 'l' && s.startsWith("ls "))
087: return null;
088: if (filter != null) {
089: if (c != 'd' && !filter.accepts(getName(s)))
090: return null;
091: }
092: return new DirectoryEntry(s, c);
093: }
094:
095: // Extracts filename from "ls -l" directory listing.
096: public static String getName(String s) {
097: // Strip symbolic link if any.
098: int end = s.indexOf(" -> ");
099: if (end >= 0)
100: s = s.substring(0, end);
101: REMatch match = Directory.getNativeMoveToFilenameRegExp()
102: .getMatch(s);
103: if (match != null)
104: return s.substring(match.getEndIndex());
105: Log.error("DirectoryEntry.getName returning null s = |" + s
106: + "|");
107: return null;
108: }
109:
110: public static int getNameColumn(String s) {
111: // Strip symbolic link if any.
112: int end = s.indexOf(" -> ");
113: if (end >= 0)
114: s = s.substring(0, end);
115: REMatch match = Directory.getNativeMoveToFilenameRegExp()
116: .getMatch(s);
117: if (match != null)
118: return match.getEndIndex();
119: return 0;
120: }
121:
122: public final String extractName() {
123: if (name == null && string != null)
124: name = getName(string);
125: return name;
126: }
127:
128: // nameColumn must be > 0. It's just a hint.
129: public final String extractName(int nameColumn) {
130: if (name != null)
131: return name; // Cached.
132: if (string == null)
133: return null;
134: if (nameColumn > 0 && nameColumn < string.length()) {
135: String s = string;
136: // Strip symbolic link if any.
137: int end = s.indexOf(" -> ");
138: if (end >= 0)
139: s = s.substring(0, end);
140: if (s.charAt(nameColumn - 1) == ' ')
141: if (!Character.isWhitespace(s.charAt(nameColumn)))
142: return name = s.substring(nameColumn);
143: }
144: // Do it the hard way.
145: return getName(string);
146: }
147:
148: public final String getName() {
149: return name;
150: }
151:
152: public final long getDate() {
153: return date;
154: }
155:
156: public final long getSize() {
157: return size;
158: }
159:
160: public final void setSize(long size) {
161: this .size = size;
162: }
163:
164: public final boolean isDirectory() {
165: return isDirectory;
166: }
167:
168: public final boolean isLink() {
169: return isLink;
170: }
171:
172: public final void setLinkedTo(String linkedTo) {
173: this .linkedTo = linkedTo;
174: }
175:
176: public final boolean isMarked() {
177: return isMarked;
178: }
179:
180: public final void setMarked(boolean b) {
181: isMarked = b;
182: }
183:
184: public final String getString() {
185: return string;
186: }
187:
188: public String toString() {
189: String marked = isMarked ? "T " : " ";
190: // Use saved string for native format.
191: if (string != null)
192: return marked.concat(string);
193: // Construct string for internal format.
194: FastStringBuffer sb = new FastStringBuffer(256);
195: sb.append(marked);
196: if (isDirectory) {
197: sb.append(DIR);
198: } else {
199: String sizeString = String.valueOf(size);
200: for (int j = 0; j < DIRLENGTH - sizeString.length(); j++)
201: sb.append(' ');
202: sb.append(sizeString);
203: }
204: sb.append(' ');
205: String dateString = dateFormatter.format(new Date(date));
206: sb.append(dateString);
207: sb.append(' ');
208: sb.append(name);
209: if (linkedTo != null) {
210: sb.append(" -> ");
211: sb.append(linkedTo);
212: }
213: return sb.toString();
214: }
215: }
|