%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.net.ftp.parser.NTFTPEntryParser |
|
|
1 | /* |
|
2 | * Copyright 2001-2005 The Apache Software Foundation |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package org.apache.commons.net.ftp.parser; |
|
17 | import java.text.ParseException; |
|
18 | ||
19 | import org.apache.commons.net.ftp.FTPClientConfig; |
|
20 | import org.apache.commons.net.ftp.FTPFile; |
|
21 | ||
22 | /** |
|
23 | * Implementation of FTPFileEntryParser and FTPFileListParser for NT Systems. |
|
24 | * |
|
25 | * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a> |
|
26 | * @author <a href="mailto:scohen@apache.org">Steve Cohen</a> |
|
27 | * @version $Id: NTFTPEntryParser.java 155429 2005-02-26 13:13:04Z dirkv $ |
|
28 | * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions) |
|
29 | */ |
|
30 | public class NTFTPEntryParser extends ConfigurableFTPFileEntryParserImpl |
|
31 | { |
|
32 | ||
33 | private static final String DEFAULT_DATE_FORMAT |
|
34 | = "MM-dd-yy hh:mma"; //11-09-01 12:30PM |
|
35 | ||
36 | ||
37 | /** |
|
38 | * this is the regular expression used by this parser. |
|
39 | */ |
|
40 | private static final String REGEX = |
|
41 | "(\\S+)\\s+(\\S+)\\s+" |
|
42 | + "(<DIR>)?\\s*" |
|
43 | + "([0-9]+)?\\s+" |
|
44 | + "(\\S.*)"; |
|
45 | ||
46 | /** |
|
47 | * The sole constructor for an NTFTPEntryParser object. |
|
48 | * |
|
49 | * @exception IllegalArgumentException |
|
50 | * Thrown if the regular expression is unparseable. Should not be seen |
|
51 | * under normal conditions. It it is seen, this is a sign that |
|
52 | * <code>REGEX</code> is not a valid regular expression. |
|
53 | */ |
|
54 | public NTFTPEntryParser() |
|
55 | { |
|
56 | 18 | this(null); |
57 | 18 | } |
58 | ||
59 | /** |
|
60 | * This constructor allows the creation of an NTFTPEntryParser object |
|
61 | * with something other than the default configuration. |
|
62 | * |
|
63 | * @param config The {@link FTPClientConfig configuration} object used to |
|
64 | * configure this parser. |
|
65 | * @exception IllegalArgumentException |
|
66 | * Thrown if the regular expression is unparseable. Should not be seen |
|
67 | * under normal conditions. It it is seen, this is a sign that |
|
68 | * <code>REGEX</code> is not a valid regular expression. |
|
69 | * @since 1.4 |
|
70 | */ |
|
71 | public NTFTPEntryParser(FTPClientConfig config) |
|
72 | { |
|
73 | 18 | super(REGEX); |
74 | 18 | configure(config); |
75 | 18 | } |
76 | ||
77 | /** |
|
78 | * Parses a line of an NT FTP server file listing and converts it into a |
|
79 | * usable format in the form of an <code> FTPFile </code> instance. If the |
|
80 | * file listing line doesn't describe a file, <code> null </code> is |
|
81 | * returned, otherwise a <code> FTPFile </code> instance representing the |
|
82 | * files in the directory is returned. |
|
83 | * <p> |
|
84 | * @param entry A line of text from the file listing |
|
85 | * @return An FTPFile instance corresponding to the supplied entry |
|
86 | */ |
|
87 | public FTPFile parseFTPEntry(String entry) |
|
88 | { |
|
89 | 42 | FTPFile f = new FTPFile(); |
90 | 42 | f.setRawListing(entry); |
91 | ||
92 | 42 | if (matches(entry)) |
93 | { |
|
94 | 37 | String datestr = group(1)+" "+group(2); |
95 | 37 | String dirString = group(3); |
96 | 37 | String size = group(4); |
97 | 37 | String name = group(5); |
98 | try |
|
99 | { |
|
100 | 37 | f.setTimestamp(super.parseTimestamp(datestr)); |
101 | } |
|
102 | 10 | catch (ParseException e) |
103 | { |
|
104 | 10 | return null; // this is a parsing failure too. |
105 | 27 | } |
106 | ||
107 | 27 | if (null == name || name.equals(".") || name.equals("..")) |
108 | { |
|
109 | 0 | return (null); |
110 | } |
|
111 | 27 | f.setName(name); |
112 | ||
113 | ||
114 | 27 | if ("<DIR>".equals(dirString)) |
115 | { |
|
116 | 9 | f.setType(FTPFile.DIRECTORY_TYPE); |
117 | 9 | f.setSize(0); |
118 | } |
|
119 | else |
|
120 | { |
|
121 | 18 | f.setType(FTPFile.FILE_TYPE); |
122 | 18 | if (null != size) |
123 | { |
|
124 | 18 | f.setSize(Long.parseLong(size)); |
125 | } |
|
126 | } |
|
127 | 27 | return (f); |
128 | } |
|
129 | 5 | return null; |
130 | } |
|
131 | ||
132 | /** |
|
133 | * Defines a default configuration to be used when this class is |
|
134 | * instantiated without a {@link FTPClientConfig FTPClientConfig} |
|
135 | * parameter being specified. |
|
136 | * @return the default configuration for this parser. |
|
137 | */ |
|
138 | public FTPClientConfig getDefaultConfiguration() { |
|
139 | 18 | return new FTPClientConfig( |
140 | FTPClientConfig.SYST_NT, |
|
141 | DEFAULT_DATE_FORMAT, |
|
142 | null, class="keyword">null, class="keyword">null, class="keyword">null); |
|
143 | } |
|
144 | ||
145 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |