001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.ftp.parser;
019import java.io.BufferedReader;
020import java.io.IOException;
021import java.text.ParseException;
022import java.util.StringTokenizer;
023
024import org.apache.commons.net.ftp.FTPClientConfig;
025import org.apache.commons.net.ftp.FTPFile;
026
027/**
028 * Implementation FTPFileEntryParser and FTPFileListParser for VMS Systems.
029 * This is a sample of VMS LIST output
030 *
031 *  "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
032 *  "1-JUN.LIS;2              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
033 *  "DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
034 * <P><B>
035 * Note: VMSFTPEntryParser can only be instantiated through the
036 * DefaultFTPParserFactory by classname.  It will not be chosen
037 * by the autodetection scheme.
038 * </B>
039 * <P>
040 *
041 * @author  <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
042 * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
043 * @author <a href="sestegra@free.fr">Stephane ESTE-GRACIAS</a>
044 * @version $Id: VMSFTPEntryParser.java 1489361 2013-06-04 09:48:36Z sebb $
045 *
046 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
047 * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
048 */
049public class VMSFTPEntryParser extends ConfigurableFTPFileEntryParserImpl
050{
051
052    private static final String DEFAULT_DATE_FORMAT
053        = "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 =
059        "(.*;[0-9]+)\\s*"                                                   //1  file and version
060        + "(\\d+)/\\d+\\s*"                                                 //2  size/allocated
061        +"(\\S+)\\s+(\\S+)\\s+"                                             //3+4 date and time
062        + "\\[(([0-9$A-Za-z_]+)|([0-9$A-Za-z_]+),([0-9$a-zA-Z_]+))\\]?\\s*" //5(6,7,8) owner
063        + "\\([a-zA-Z]*,([a-zA-Z]*),([a-zA-Z]*),([a-zA-Z]*)\\)";            //9,10,11 Permissions (O,G,W)
064    // TODO - perhaps restrict permissions to [RWED]* ?
065
066
067
068    /**
069     * Constructor for a VMSFTPEntryParser object.
070     *
071     * @exception IllegalArgumentException
072     * Thrown if the regular expression is unparseable.  Should not be seen
073     * under normal conditions.  It it is seen, this is a sign that
074     * <code>REGEX</code> is  not a valid regular expression.
075     */
076    public VMSFTPEntryParser()
077    {
078        this(null);
079    }
080
081    /**
082     * This constructor allows the creation of a VMSFTPEntryParser object with
083     * something other than the default configuration.
084     *
085     * @param config The {@link FTPClientConfig configuration} object used to
086     * configure this parser.
087     * @exception IllegalArgumentException
088     * Thrown if the regular expression is unparseable.  Should not be seen
089     * under normal conditions.  It it is seen, this is a sign that
090     * <code>REGEX</code> is  not a valid regular expression.
091     * @since 1.4
092     */
093    public VMSFTPEntryParser(FTPClientConfig config)
094    {
095        super(REGEX);
096        configure(config);
097    }
098
099    /**
100     * Parses a line of a VMS FTP server file listing and converts it into a
101     * usable format in the form of an <code> FTPFile </code> instance.  If the
102     * file listing line doesn't describe a file, <code> null </code> is
103     * returned, otherwise a <code> FTPFile </code> instance representing the
104     * files in the directory is returned.
105     * <p>
106     * @param entry A line of text from the file listing
107     * @return An FTPFile instance corresponding to the supplied entry
108     */
109//    @Override
110    public FTPFile parseFTPEntry(String entry)
111    {
112        //one block in VMS equals 512 bytes
113        long longBlock = 512;
114
115        if (matches(entry))
116        {
117            FTPFile f = new FTPFile();
118            f.setRawListing(entry);
119            String name = group(1);
120            String size = group(2);
121            String datestr = group(3)+" "+group(4);
122            String owner = group(5);
123            String permissions[] = new String[3];
124            permissions[0]= group(9);
125            permissions[1]= group(10);
126            permissions[2]= group(11);
127            try
128            {
129                f.setTimestamp(super.parseTimestamp(datestr));
130            }
131            catch (ParseException e)
132            {
133                 // intentionally do nothing
134            }
135
136
137            String grp;
138            String user;
139            StringTokenizer t = new StringTokenizer(owner, ",");
140            switch (t.countTokens()) {
141                case 1:
142                    grp  = null;
143                    user = t.nextToken();
144                    break;
145                case 2:
146                    grp  = t.nextToken();
147                    user = t.nextToken();
148                    break;
149                default:
150                    grp  = null;
151                    user = null;
152            }
153
154            if (name.lastIndexOf(".DIR") != -1)
155            {
156                f.setType(FTPFile.DIRECTORY_TYPE);
157            }
158            else
159            {
160                f.setType(FTPFile.FILE_TYPE);
161            }
162            //set FTPFile name
163            //Check also for versions to be returned or not
164            if (isVersioning())
165            {
166                f.setName(name);
167            }
168            else
169            {
170                name = name.substring(0, name.lastIndexOf(";"));
171                f.setName(name);
172            }
173            //size is retreived in blocks and needs to be put in bytes
174            //for us humans and added to the FTPFile array
175            long sizeInBytes = Long.parseLong(size) * longBlock;
176            f.setSize(sizeInBytes);
177
178            f.setGroup(grp);
179            f.setUser(user);
180            //set group and owner
181
182            //Set file permission.
183            //VMS has (SYSTEM,OWNER,GROUP,WORLD) users that can contain
184            //R (read) W (write) E (execute) D (delete)
185
186            //iterate for OWNER GROUP WORLD permissions
187            for (int access = 0; access < 3; access++)
188            {
189                String permission = permissions[access];
190
191                f.setPermission(access, FTPFile.READ_PERMISSION, permission.indexOf('R')>=0);
192                f.setPermission(access, FTPFile.WRITE_PERMISSION, permission.indexOf('W')>=0);
193                f.setPermission(access, FTPFile.EXECUTE_PERMISSION, permission.indexOf('E')>=0);
194            }
195
196            return f;
197        }
198        return null;
199    }
200
201
202    /**
203     * Reads the next entry using the supplied BufferedReader object up to
204     * whatever delemits one entry from the next.   This parser cannot use
205     * the default implementation of simply calling BufferedReader.readLine(),
206     * because one entry may span multiple lines.
207     *
208     * @param reader The BufferedReader object from which entries are to be
209     * read.
210     *
211     * @return A string representing the next ftp entry or null if none found.
212     * @exception IOException thrown on any IO Error reading from the reader.
213     */
214    @Override
215    public String readNextEntry(BufferedReader reader) throws IOException
216    {
217        String line = reader.readLine();
218        StringBuilder entry = new StringBuilder();
219        while (line != null)
220        {
221            if (line.startsWith("Directory") || line.startsWith("Total")) {
222                line = reader.readLine();
223                continue;
224            }
225
226            entry.append(line);
227            if (line.trim().endsWith(")"))
228            {
229                break;
230            }
231            line = reader.readLine();
232        }
233        return (entry.length() == 0 ? null : entry.toString());
234    }
235
236    protected boolean isVersioning() {
237        return false;
238    }
239
240    /**
241     * Defines a default configuration to be used when this class is
242     * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
243     * parameter being specified.
244     * @return the default configuration for this parser.
245     */
246    @Override
247    protected FTPClientConfig getDefaultConfiguration() {
248        return new FTPClientConfig(
249                FTPClientConfig.SYST_VMS,
250                DEFAULT_DATE_FORMAT,
251                null, null, null, null);
252    }
253
254    // DEPRECATED METHODS - for API compatibility only - DO NOT USE
255
256    /**
257     * DO NOT USE
258     * @deprecated (2.2) No other FTPFileEntryParser implementations have this method.
259     */
260    @Deprecated
261    public FTPFile[] parseFileList(java.io.InputStream listStream) throws IOException {
262        org.apache.commons.net.ftp.FTPListParseEngine engine = new org.apache.commons.net.ftp.FTPListParseEngine(this);
263        engine.readServerList(listStream, null);
264        return engine.getFiles();
265    }
266
267}
268
269/* Emacs configuration
270 * Local variables:        **
271 * mode:             java  **
272 * c-basic-offset:   4     **
273 * indent-tabs-mode: nil   **
274 * End:                    **
275 */