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
19 import java.text.ParseException;
20
21 import org.apache.commons.net.ftp.FTPClientConfig;
22 import org.apache.commons.net.ftp.FTPFile;
23
24 /**
25 * Implementation of FTPFileEntryParser and FTPFileListParser for Netware Systems. Note that some of the proprietary
26 * extensions for Novell-specific operations are not supported. See
27 * <a href="http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html">http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html</a>
28 * for more details.
29 *
30 * @author <a href="rwinston@apache.org">Rory Winston</a>
31 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
32 * @version $Id: NetwareFTPEntryParser.java 633028 2008-03-03 10:22:57Z niallp $
33 */
34 public class NetwareFTPEntryParser extends ConfigurableFTPFileEntryParserImpl {
35
36 /**
37 * Default date format is e.g. Feb 22 2006
38 */
39 private static final String DEFAULT_DATE_FORMAT = "MMM dd yyyy";
40
41 /**
42 * Default recent date format is e.g. Feb 22 17:32
43 */
44 private static final String DEFAULT_RECENT_DATE_FORMAT = "MMM dd HH:mm";
45
46 /**
47 * this is the regular expression used by this parser.
48 * Example: d [-W---F--] SCION_VOL2 512 Apr 13 23:12 VOL2
49 */
50 private static final String REGEX = "(d|-){1}\\s+" // Directory/file flag
51 + "\\[(.*)\\]\\s+" // Attributes
52 + "(\\S+)\\s+" + "(\\d+)\\s+" // Owner and size
53 + "(\\S+\\s+\\S+\\s+((\\d+:\\d+)|(\\d{4})))" // Long/short date format
54 + "\\s+(.*)"; // Filename (incl. spaces)
55
56 /**
57 * The default constructor for a NetwareFTPEntryParser object.
58 *
59 * @exception IllegalArgumentException
60 * Thrown if the regular expression is unparseable. Should not be seen
61 * under normal conditions. It it is seen, this is a sign that
62 * <code>REGEX</code> is not a valid regular expression.
63 */
64 public NetwareFTPEntryParser() {
65 this(null);
66 }
67
68 /**
69 * This constructor allows the creation of an NetwareFTPEntryParser object
70 * with something other than the default configuration.
71 *
72 * @param config The {@link FTPClientConfig configuration} object used to
73 * configure this parser.
74 * @exception IllegalArgumentException
75 * Thrown if the regular expression is unparseable. Should not be seen
76 * under normal conditions. It it is seen, this is a sign that
77 * <code>REGEX</code> is not a valid regular expression.
78 * @since 1.4
79 */
80 public NetwareFTPEntryParser(FTPClientConfig config) {
81 super(REGEX);
82 configure(config);
83 }
84
85 /**
86 * Parses a line of an NetwareFTP server file listing and converts it into a
87 * usable format in the form of an <code> FTPFile </code> instance. If the
88 * file listing line doesn't describe a file, <code> null </code> is
89 * returned, otherwise a <code> FTPFile </code> instance representing the
90 * files in the directory is returned.
91 * <p>
92 * <p>
93 * Netware file permissions are in the following format: RWCEAFMS, and are explained as follows:
94 * <ul>
95 * <li><b>S</b> - Supervisor; All rights.
96 * <li><b>R</b> - Read; Right to open and read or execute.
97 * <li><b>W</b> - Write; Right to open and modify.
98 * <li><b>C</b> - Create; Right to create; when assigned to a file, allows a deleted file to be recovered.
99 * <li><b>E</b> - Erase; Right to delete.
100 * <li><b>M</b> - Modify; Right to rename a file and to change attributes.
101 * <li><b>F</b> - File Scan; Right to see directory or file listings.
102 * <li><b>A</b> - Access Control; Right to modify trustee assignments and the Inherited Rights Mask.
103 * </ul>
104 *
105 * See <a href="http://www.novell.com/documentation/nfap10/index.html?page=/documentation/nfap10/nfaubook/data/abxraws.html">here</a>
106 * for more details
107 *
108 * @param entry A line of text from the file listing
109 * @return An FTPFile instance corresponding to the supplied entry
110 */
111 public FTPFile parseFTPEntry(String entry) {
112
113 FTPFile f = new FTPFile();
114 if (matches(entry)) {
115 String dirString = group(1);
116 String attrib = group(2);
117 String user = group(3);
118 String size = group(4);
119 String datestr = group(5);
120 String name = group(9);
121
122 try {
123 f.setTimestamp(super.parseTimestamp(datestr));
124 } catch (ParseException e) {
125 return null; // this is a parsing failure too.
126 }
127
128 //is it a DIR or a file
129 if (dirString.trim().equals("d")) {
130 f.setType(FTPFile.DIRECTORY_TYPE);
131 } else // Should be "-"
132 {
133 f.setType(FTPFile.FILE_TYPE);
134 }
135
136 f.setUser(user);
137
138 //set the name
139 f.setName(name.trim());
140
141 //set the size
142 f.setSize(Long.parseLong(size.trim()));
143
144 // Now set the permissions (or at least a subset thereof - full permissions would probably require
145 // subclassing FTPFile and adding extra metainformation there)
146 if (attrib.indexOf("R") != -1) {
147 f.setPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION,
148 true);
149 }
150 if (attrib.indexOf("W") != -1) {
151 f.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION,
152 true);
153 }
154
155 return (f);
156 }
157 return null;
158
159 }
160
161 /**
162 * Defines a default configuration to be used when this class is
163 * instantiated without a {@link FTPClientConfig FTPClientConfig}
164 * parameter being specified.
165 * @return the default configuration for this parser.
166 */
167 protected FTPClientConfig getDefaultConfiguration() {
168 return new FTPClientConfig(FTPClientConfig.SYST_NETWARE,
169 DEFAULT_DATE_FORMAT, DEFAULT_RECENT_DATE_FORMAT, null, null,
170 null);
171 }
172
173 }