001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.ftp.parser;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.text.ParseException;
022: import java.util.StringTokenizer;
023:
024: import org.apache.commons.net.ftp.FTPClientConfig;
025: import org.apache.commons.net.ftp.FTPFile;
026: import org.apache.commons.net.ftp.FTPListParseEngine;
027:
028: /**
029: * Implementation FTPFileEntryParser and FTPFileListParser for VMS Systems.
030: * This is a sample of VMS LIST output
031: *
032: * "1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
033: * "1-JUN.LIS;2 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
034: * "DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
035: * <P><B>
036: * Note: VMSFTPEntryParser can only be instantiated through the
037: * DefaultFTPParserFactory by classname. It will not be chosen
038: * by the autodetection scheme.
039: * </B>
040: * <P>
041: *
042: * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
043: * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
044: * @author <a href="sestegra@free.fr">Stephane ESTE-GRACIAS</a>
045: * @version $Id: VMSFTPEntryParser.java 155429 2005-02-26 13:13:04Z dirkv $
046: *
047: * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
048: * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
049: */
050: public class VMSFTPEntryParser extends
051: ConfigurableFTPFileEntryParserImpl {
052:
053: private static final String DEFAULT_DATE_FORMAT = "d-MMM-yyyy HH:mm:ss"; //9-NOV-2001 12:30:24
054:
055: /**
056: * this is the regular expression used by this parser.
057: */
058: private static final String REGEX = "(.*;[0-9]+)\\s*"
059: + "(\\d+)/\\d+\\s*"
060: + "(\\S+)\\s+(\\S+)\\s+"
061: + "\\[(([0-9$A-Za-z_]+)|([0-9$A-Za-z_]+),([0-9$a-zA-Z_]+))\\]?\\s*"
062: + "\\([a-zA-Z]*,[a-zA-Z]*,[a-zA-Z]*,[a-zA-Z]*\\)";
063:
064: /**
065: * Constructor for a VMSFTPEntryParser object.
066: *
067: * @exception IllegalArgumentException
068: * Thrown if the regular expression is unparseable. Should not be seen
069: * under normal conditions. It it is seen, this is a sign that
070: * <code>REGEX</code> is not a valid regular expression.
071: */
072: public VMSFTPEntryParser() {
073: this (null);
074: }
075:
076: /**
077: * This constructor allows the creation of a VMSFTPEntryParser object with
078: * something other than the default configuration.
079: *
080: * @param config The {@link FTPClientConfig configuration} object used to
081: * configure this parser.
082: * @exception IllegalArgumentException
083: * Thrown if the regular expression is unparseable. Should not be seen
084: * under normal conditions. It it is seen, this is a sign that
085: * <code>REGEX</code> is not a valid regular expression.
086: * @since 1.4
087: */
088: public VMSFTPEntryParser(FTPClientConfig config) {
089: super (REGEX);
090: configure(config);
091: }
092:
093: /***
094: * Parses an FTP server file listing and converts it into a usable format
095: * in the form of an array of <code> FTPFile </code> instances. If the
096: * file list contains no files, <code> null </code> should be
097: * returned, otherwise an array of <code> FTPFile </code> instances
098: * representing the files in the directory is returned.
099: * <p>
100: * @param listStream The InputStream from which the file list should be
101: * read.
102: * @return The list of file information contained in the given path. null
103: * if the list could not be obtained or if there are no files in
104: * the directory.
105: * @exception IOException If an I/O error occurs reading the listStream.
106: ***/
107: public FTPFile[] parseFileList(InputStream listStream)
108: throws IOException {
109: FTPListParseEngine engine = new FTPListParseEngine(this );
110: engine.readServerList(listStream);
111: return engine.getFiles();
112: }
113:
114: /**
115: * Parses a line of a VMS FTP server file listing and converts it into a
116: * usable format in the form of an <code> FTPFile </code> instance. If the
117: * file listing line doesn't describe a file, <code> null </code> is
118: * returned, otherwise a <code> FTPFile </code> instance representing the
119: * files in the directory is returned.
120: * <p>
121: * @param entry A line of text from the file listing
122: * @return An FTPFile instance corresponding to the supplied entry
123: */
124: public FTPFile parseFTPEntry(String entry) {
125: //one block in VMS equals 512 bytes
126: long longBlock = 512;
127:
128: if (matches(entry)) {
129: FTPFile f = new FTPFile();
130: f.setRawListing(entry);
131: String name = group(1);
132: String size = group(2);
133: String datestr = group(3) + " " + group(4);
134: String owner = group(5);
135: try {
136: f.setTimestamp(super .parseTimestamp(datestr));
137: } catch (ParseException e) {
138: return null; // this is a parsing failure too.
139: }
140:
141: String grp;
142: String user;
143: StringTokenizer t = new StringTokenizer(owner, ",");
144: switch (t.countTokens()) {
145: case 1:
146: grp = null;
147: user = t.nextToken();
148: break;
149: case 2:
150: grp = t.nextToken();
151: user = t.nextToken();
152: break;
153: default:
154: grp = null;
155: user = null;
156: }
157:
158: if (name.lastIndexOf(".DIR") != -1) {
159: f.setType(FTPFile.DIRECTORY_TYPE);
160: } else {
161: f.setType(FTPFile.FILE_TYPE);
162: }
163: //set FTPFile name
164: //Check also for versions to be returned or not
165: if (isVersioning()) {
166: f.setName(name);
167: } else {
168: name = name.substring(0, name.lastIndexOf(";"));
169: f.setName(name);
170: }
171: //size is retreived in blocks and needs to be put in bytes
172: //for us humans and added to the FTPFile array
173: long sizeInBytes = Long.parseLong(size) * longBlock;
174: f.setSize(sizeInBytes);
175:
176: f.setGroup(grp);
177: f.setUser(user);
178: //set group and owner
179: //Since I don't need the persmissions on this file (RWED), I'll
180: //leave that for further development. 'Cause it will be a bit
181: //elaborate to do it right with VMSes World, Global and so forth.
182: return f;
183: }
184: return null;
185: }
186:
187: /**
188: * Reads the next entry using the supplied BufferedReader object up to
189: * whatever delemits one entry from the next. This parser cannot use
190: * the default implementation of simply calling BufferedReader.readLine(),
191: * because one entry may span multiple lines.
192: *
193: * @param reader The BufferedReader object from which entries are to be
194: * read.
195: *
196: * @return A string representing the next ftp entry or null if none found.
197: * @exception IOException thrown on any IO Error reading from the reader.
198: */
199: public String readNextEntry(BufferedReader reader)
200: throws IOException {
201: String line = reader.readLine();
202: StringBuffer entry = new StringBuffer();
203: while (line != null) {
204: if (line.startsWith("Directory")
205: || line.startsWith("Total")) {
206: line = reader.readLine();
207: continue;
208: }
209:
210: entry.append(line);
211: if (line.trim().endsWith(")")) {
212: break;
213: }
214: line = reader.readLine();
215: }
216: return (entry.length() == 0 ? null : entry.toString());
217: }
218:
219: protected boolean isVersioning() {
220: return false;
221: }
222:
223: /**
224: * Defines a default configuration to be used when this class is
225: * instantiated without a {@link FTPClientConfig FTPClientConfig}
226: * parameter being specified.
227: * @return the default configuration for this parser.
228: */
229: protected FTPClientConfig getDefaultConfiguration() {
230: return new FTPClientConfig(FTPClientConfig.SYST_VMS,
231: DEFAULT_DATE_FORMAT, null, null, null, null);
232: }
233:
234: }
235:
236: /* Emacs configuration
237: * Local variables: **
238: * mode: java **
239: * c-basic-offset: 4 **
240: * indent-tabs-mode: nil **
241: * End: **
242: */
|