KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > ftp > parser > EnterpriseUnixFTPEntryParser


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.util.Calendar JavaDoc;
18
19 import org.apache.commons.net.ftp.FTPFile;
20
21 /**
22  * Parser for the Connect Enterprise Unix FTP Server From Sterling Commerce.
23  * Here is a sample of the sort of output line this parser processes:
24  * "-C--E-----FTP B QUA1I1 18128 41 Aug 12 13:56 QUADTEST"
25  * <P><B>
26  * Note: EnterpriseUnixFTPEntryParser can only be instantiated through the
27  * DefaultFTPParserFactory by classname. It will not be chosen
28  * by the autodetection scheme.
29  * </B>
30  * @version $Id: EnterpriseUnixFTPEntryParser.java 165675 2005-05-02 20:09:55Z rwinston $
31  * @author <a HREF="Winston.Ojeda@qg.com">Winston Ojeda</a>
32  * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
33  * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
34  */

35 public class EnterpriseUnixFTPEntryParser extends RegexFTPFileEntryParserImpl
36 {
37
38     /**
39      * months abbreviations looked for by this parser. Also used
40      * to determine <b>which</b> month has been matched by the parser.
41      */

42     private static final String JavaDoc MONTHS =
43         "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
44
45     /**
46      * this is the regular expression used by this parser.
47      */

48     private static final String JavaDoc REGEX =
49         "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])"
50         + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))"
51         + "(\\S*)\\s*"
52         + "(\\S+)\\s*"
53         + "(\\S*)\\s*"
54         + "(\\d*)\\s*"
55         + "(\\d*)\\s*"
56         + MONTHS
57         + "\\s*"
58         + "((?:[012]\\d*)|(?:3[01]))\\s*"
59         + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s"
60         + "(\\S*)(\\s*.*)";
61
62     /**
63      * The sole constructor for a EnterpriseUnixFTPEntryParser object.
64      *
65      */

66     public EnterpriseUnixFTPEntryParser()
67     {
68         super(REGEX);
69     }
70
71     /**
72      * Parses a line of a unix FTP server file listing and converts it into a
73      * usable format in the form of an <code> FTPFile </code> instance. If
74      * the file listing line doesn't describe a file, <code> null </code> is
75      * returned, otherwise a <code> FTPFile </code> instance representing the
76      * files in the directory is returned.
77      *
78      * @param entry A line of text from the file listing
79      * @return An FTPFile instance corresponding to the supplied entry
80      */

81     public FTPFile parseFTPEntry(String JavaDoc entry)
82     {
83
84         FTPFile file = new FTPFile();
85         file.setRawListing(entry);
86
87         if (matches(entry))
88         {
89             String JavaDoc usr = group(14);
90             String JavaDoc grp = group(15);
91             String JavaDoc filesize = group(16);
92             String JavaDoc mo = group(17);
93             String JavaDoc da = group(18);
94             String JavaDoc yr = group(20);
95             String JavaDoc hr = group(21);
96             String JavaDoc min = group(22);
97             String JavaDoc name = group(23);
98
99             file.setType(FTPFile.FILE_TYPE);
100             file.setUser(usr);
101             file.setGroup(grp);
102             try
103             {
104                 file.setSize(Long.parseLong(filesize));
105             }
106             catch (NumberFormatException JavaDoc e)
107             {
108                 // intentionally do nothing
109
}
110
111             Calendar JavaDoc cal = Calendar.getInstance();
112         cal.set(Calendar.MILLISECOND, 0);
113             cal.set(Calendar.SECOND,
114                     0);
115             cal.set(Calendar.MINUTE,
116                     0);
117             cal.set(Calendar.HOUR_OF_DAY,
118                     0);
119             try
120             {
121
122                 int pos = MONTHS.indexOf(mo);
123                 int month = pos / 4;
124                 if (yr != null)
125                 {
126                     // it's a year
127
cal.set(Calendar.YEAR,
128                             Integer.parseInt(yr));
129                 }
130                 else
131                 {
132                     // it must be hour/minute or we wouldn't have matched
133
int year = cal.get(Calendar.YEAR);
134
135                     // if the month we're reading is greater than now, it must
136
// be last year
137
if (cal.get(Calendar.MONTH) < month)
138                     {
139                         year--;
140                     }
141                     cal.set(Calendar.YEAR,
142                             year);
143                     cal.set(Calendar.HOUR_OF_DAY,
144                             Integer.parseInt(hr));
145                     cal.set(Calendar.MINUTE,
146                             Integer.parseInt(min));
147                 }
148                 cal.set(Calendar.MONTH,
149                         month);
150                 cal.set(Calendar.DATE,
151                         Integer.parseInt(da));
152                 file.setTimestamp(cal);
153             }
154             catch (NumberFormatException JavaDoc e)
155             {
156                 // do nothing, date will be uninitialized
157
}
158             file.setName(name);
159
160             return file;
161         }
162         return null;
163     }
164 }
165
Popular Tags