View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.ftp.parser;
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.text.ParseException;
22  import java.util.StringTokenizer;
23  
24  import org.apache.commons.net.ftp.FTPClientConfig;
25  import org.apache.commons.net.ftp.FTPFile;
26  
27  /**
28   * Implementation FTPFileEntryParser and FTPFileListParser for VMS Systems.
29   * This is a sample of VMS LIST output
30   *
31   *  "1-JUN.LIS;1              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
32   *  "1-JUN.LIS;2              9/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
33   *  "DATA.DIR;1               1/9           2-JUN-1998 07:32:04  [GROUP,OWNER]    (RWED,RWED,RWED,RE)",
34   * <P><B>
35   * Note: VMSFTPEntryParser can only be instantiated through the
36   * DefaultFTPParserFactory by classname.  It will not be chosen
37   * by the autodetection scheme.
38   * </B>
39   * <P>
40   *
41   * @version $Id: VMSFTPEntryParser.java 1697293 2015-08-24 01:01:00Z sebb $
42   *
43   * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
44   * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
45   */
46  public class VMSFTPEntryParser extends ConfigurableFTPFileEntryParserImpl
47  {
48  
49      private static final String DEFAULT_DATE_FORMAT
50          = "d-MMM-yyyy HH:mm:ss"; //9-NOV-2001 12:30:24
51  
52      /**
53       * this is the regular expression used by this parser.
54       */
55      private static final String REGEX =
56          "(.*?;[0-9]+)\\s*"                                                  //1  file and version
57          + "(\\d+)/\\d+\\s*"                                                 //2  size/allocated
58          +"(\\S+)\\s+(\\S+)\\s+"                                             //3+4 date and time
59          + "\\[(([0-9$A-Za-z_]+)|([0-9$A-Za-z_]+),([0-9$a-zA-Z_]+))\\]?\\s*" //5(6,7,8) owner
60          + "\\([a-zA-Z]*,([a-zA-Z]*),([a-zA-Z]*),([a-zA-Z]*)\\)";            //9,10,11 Permissions (O,G,W)
61      // TODO - perhaps restrict permissions to [RWED]* ?
62  
63  
64  
65      /**
66       * Constructor for a VMSFTPEntryParser object.
67       *
68       * @exception IllegalArgumentException
69       * Thrown if the regular expression is unparseable.  Should not be seen
70       * under normal conditions.  It it is seen, this is a sign that
71       * <code>REGEX</code> is  not a valid regular expression.
72       */
73      public VMSFTPEntryParser()
74      {
75          this(null);
76      }
77  
78      /**
79       * This constructor allows the creation of a VMSFTPEntryParser object with
80       * something other than the default configuration.
81       *
82       * @param config The {@link FTPClientConfig configuration} object used to
83       * configure this parser.
84       * @exception IllegalArgumentException
85       * Thrown if the regular expression is unparseable.  Should not be seen
86       * under normal conditions.  It it is seen, this is a sign that
87       * <code>REGEX</code> is  not a valid regular expression.
88       * @since 1.4
89       */
90      public VMSFTPEntryParser(FTPClientConfig config)
91      {
92          super(REGEX);
93          configure(config);
94      }
95  
96      /**
97       * Parses a line of a VMS FTP server file listing and converts it into a
98       * usable format in the form of an <code> FTPFile </code> instance.  If the
99       * file listing line doesn't describe a file, <code> null </code> is
100      * returned, otherwise a <code> FTPFile </code> instance representing the
101      * files in the directory is returned.
102      *
103      * @param entry A line of text from the file listing
104      * @return An FTPFile instance corresponding to the supplied entry
105      */
106 //    @Override
107     public FTPFile parseFTPEntry(String entry)
108     {
109         //one block in VMS equals 512 bytes
110         long longBlock = 512;
111 
112         if (matches(entry))
113         {
114             FTPFile f = new FTPFile();
115             f.setRawListing(entry);
116             String name = group(1);
117             String size = group(2);
118             String datestr = group(3)+" "+group(4);
119             String owner = group(5);
120             String permissions[] = new String[3];
121             permissions[0]= group(9);
122             permissions[1]= group(10);
123             permissions[2]= group(11);
124             try
125             {
126                 f.setTimestamp(super.parseTimestamp(datestr));
127             }
128             catch (ParseException e)
129             {
130                  // intentionally do nothing
131             }
132 
133 
134             String grp;
135             String user;
136             StringTokenizer t = new StringTokenizer(owner, ",");
137             switch (t.countTokens()) {
138                 case 1:
139                     grp  = null;
140                     user = t.nextToken();
141                     break;
142                 case 2:
143                     grp  = t.nextToken();
144                     user = t.nextToken();
145                     break;
146                 default:
147                     grp  = null;
148                     user = null;
149             }
150 
151             if (name.lastIndexOf(".DIR") != -1)
152             {
153                 f.setType(FTPFile.DIRECTORY_TYPE);
154             }
155             else
156             {
157                 f.setType(FTPFile.FILE_TYPE);
158             }
159             //set FTPFile name
160             //Check also for versions to be returned or not
161             if (isVersioning())
162             {
163                 f.setName(name);
164             }
165             else
166             {
167                 name = name.substring(0, name.lastIndexOf(";"));
168                 f.setName(name);
169             }
170             //size is retreived in blocks and needs to be put in bytes
171             //for us humans and added to the FTPFile array
172             long sizeInBytes = Long.parseLong(size) * longBlock;
173             f.setSize(sizeInBytes);
174 
175             f.setGroup(grp);
176             f.setUser(user);
177             //set group and owner
178 
179             //Set file permission.
180             //VMS has (SYSTEM,OWNER,GROUP,WORLD) users that can contain
181             //R (read) W (write) E (execute) D (delete)
182 
183             //iterate for OWNER GROUP WORLD permissions
184             for (int access = 0; access < 3; access++)
185             {
186                 String permission = permissions[access];
187 
188                 f.setPermission(access, FTPFile.READ_PERMISSION, permission.indexOf('R')>=0);
189                 f.setPermission(access, FTPFile.WRITE_PERMISSION, permission.indexOf('W')>=0);
190                 f.setPermission(access, FTPFile.EXECUTE_PERMISSION, permission.indexOf('E')>=0);
191             }
192 
193             return f;
194         }
195         return null;
196     }
197 
198 
199     /**
200      * Reads the next entry using the supplied BufferedReader object up to
201      * whatever delemits one entry from the next.   This parser cannot use
202      * the default implementation of simply calling BufferedReader.readLine(),
203      * because one entry may span multiple lines.
204      *
205      * @param reader The BufferedReader object from which entries are to be
206      * read.
207      *
208      * @return A string representing the next ftp entry or null if none found.
209      * @exception IOException thrown on any IO Error reading from the reader.
210      */
211     @Override
212     public String readNextEntry(BufferedReader reader) throws IOException
213     {
214         String line = reader.readLine();
215         StringBuilder entry = new StringBuilder();
216         while (line != null)
217         {
218             if (line.startsWith("Directory") || line.startsWith("Total")) {
219                 line = reader.readLine();
220                 continue;
221             }
222 
223             entry.append(line);
224             if (line.trim().endsWith(")"))
225             {
226                 break;
227             }
228             line = reader.readLine();
229         }
230         return (entry.length() == 0 ? null : entry.toString());
231     }
232 
233     protected boolean isVersioning() {
234         return false;
235     }
236 
237     /**
238      * Defines a default configuration to be used when this class is
239      * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
240      * parameter being specified.
241      * @return the default configuration for this parser.
242      */
243     @Override
244     protected FTPClientConfig getDefaultConfiguration() {
245         return new FTPClientConfig(
246                 FTPClientConfig.SYST_VMS,
247                 DEFAULT_DATE_FORMAT,
248                 null, null, null, null);
249     }
250 
251     // DEPRECATED METHODS - for API compatibility only - DO NOT USE
252 
253     /**
254      * DO NOT USE
255      * @param listStream the stream
256      * @return the array of files
257      * @throws IOException on error
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  */