NetwareFTPEntryParser.java

  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. package org.apache.commons.net.ftp.parser;

  18. import java.text.ParseException;

  19. import org.apache.commons.net.ftp.Configurable;
  20. import org.apache.commons.net.ftp.FTPClientConfig;
  21. import org.apache.commons.net.ftp.FTPFile;
  22. import org.apache.commons.net.ftp.FTPFileEntryParser;

  23. /**
  24.  * Implements {@link FTPFileEntryParser} and {@link Configurable} for Netware Systems. Note that some proprietary extensions for Novell-specific operations
  25.  * are not supported. See <a href="http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html">
  26.  * http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html</a> for more details.
  27.  *
  28.  * @see FTPFileEntryParser Usage instructions.
  29.  * @since 1.5
  30.  */
  31. public class NetwareFTPEntryParser extends ConfigurableFTPFileEntryParserImpl {

  32.     /**
  33.      * Default date format is e.g. Feb 22 2006
  34.      */
  35.     private static final String DEFAULT_DATE_FORMAT = "MMM dd yyyy";

  36.     /**
  37.      * Default recent date format is e.g. Feb 22 17:32
  38.      */
  39.     private static final String DEFAULT_RECENT_DATE_FORMAT = "MMM dd HH:mm";

  40.     /**
  41.      * this is the regular expression used by this parser. Example: d [-W---F--] SCION_VOL2 512 Apr 13 23:12 VOL2
  42.      */
  43.     private static final String REGEX = "(d|-){1}\\s+" // Directory/file flag
  44.             + "\\[([-A-Z]+)\\]\\s+" // Attributes RWCEAFMS or -
  45.             + "(\\S+)\\s+" + "(\\d+)\\s+" // Owner and size
  46.             + "(\\S+\\s+\\S+\\s+((\\d+:\\d+)|(\\d{4})))" // Long/short date format
  47.             + "\\s+(.*)"; // Filename (incl. spaces)

  48.     /**
  49.      * The default constructor for a NetwareFTPEntryParser object.
  50.      *
  51.      * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
  52.      *                                  sign that <code>REGEX</code> is not a valid regular expression.
  53.      */
  54.     public NetwareFTPEntryParser() {
  55.         this(null);
  56.     }

  57.     /**
  58.      * This constructor allows the creation of an NetwareFTPEntryParser object with something other than the default configuration.
  59.      *
  60.      * @param config The {@link FTPClientConfig configuration} object used to configure this parser.
  61.      * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
  62.      *                                  sign that <code>REGEX</code> is not a valid regular expression.
  63.      * @since 1.4
  64.      */
  65.     public NetwareFTPEntryParser(final FTPClientConfig config) {
  66.         super(REGEX);
  67.         configure(config);
  68.     }

  69.     /**
  70.      * Defines a default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified.
  71.      *
  72.      * @return the default configuration for this parser.
  73.      */
  74.     @Override
  75.     protected FTPClientConfig getDefaultConfiguration() {
  76.         return new FTPClientConfig(FTPClientConfig.SYST_NETWARE, DEFAULT_DATE_FORMAT, DEFAULT_RECENT_DATE_FORMAT);
  77.     }

  78.     /**
  79.      * Parses a line of an NetwareFTP server file listing and converts it into a usable format in the form of an <code>FTPFile</code> instance. If the file
  80.      * listing line doesn't describe a file, <code>null</code> is returned, otherwise a <code>FTPFile</code> instance representing the files in the
  81.      * directory is returned.
  82.      * <p>
  83.      * Netware file permissions are in the following format: RWCEAFMS, and are explained as follows:
  84.      * <ul>
  85.      * <li><b>S</b> - Supervisor; All rights.
  86.      * <li><b>R</b> - Read; Right to open and read or execute.
  87.      * <li><b>W</b> - Write; Right to open and modify.
  88.      * <li><b>C</b> - Create; Right to create; when assigned to a file, allows a deleted file to be recovered.
  89.      * <li><b>E</b> - Erase; Right to delete.
  90.      * <li><b>M</b> - Modify; Right to rename a file and to change attributes.
  91.      * <li><b>F</b> - File Scan; Right to see directory or file listings.
  92.      * <li><b>A</b> - Access Control; Right to modify trustee assignments and the Inherited Rights Mask.
  93.      * </ul>
  94.      *
  95.      * See <a href="http://www.novell.com/documentation/nfap10/index.html?page=/documentation/nfap10/nfaubook/data/abxraws.html"> here</a> for more details
  96.      *
  97.      * @param entry A line of text from the file listing
  98.      * @return An FTPFile instance corresponding to the supplied entry
  99.      */
  100.     @Override
  101.     public FTPFile parseFTPEntry(final String entry) {

  102.         final FTPFile f = new FTPFile();
  103.         if (matches(entry)) {
  104.             final String dirString = group(1);
  105.             final String attrib = group(2);
  106.             final String user = group(3);
  107.             final String size = group(4);
  108.             final String datestr = group(5);
  109.             final String name = group(9);

  110.             try {
  111.                 f.setTimestamp(super.parseTimestamp(datestr));
  112.             } catch (final ParseException e) {
  113.                 // intentionally do nothing
  114.             }

  115.             // is it a DIR or a file
  116.             if (dirString.trim().equals("d")) {
  117.                 f.setType(FTPFile.DIRECTORY_TYPE);
  118.             } else // Should be "-"
  119.             {
  120.                 f.setType(FTPFile.FILE_TYPE);
  121.             }

  122.             f.setUser(user);

  123.             // set the name
  124.             f.setName(name.trim());

  125.             // set the size
  126.             f.setSize(Long.parseLong(size.trim()));

  127.             // Now set the permissions (or at least a subset thereof - full permissions would probably require
  128.             // subclassing FTPFile and adding extra metainformation there)
  129.             if (attrib.indexOf('R') != -1) {
  130.                 f.setPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION, true);
  131.             }
  132.             if (attrib.indexOf('W') != -1) {
  133.                 f.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION, true);
  134.             }

  135.             return f;
  136.         }
  137.         return null;

  138.     }

  139. }