KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004-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
18 import java.text.ParseException JavaDoc;
19
20 import org.apache.commons.net.ftp.FTPClientConfig;
21 import org.apache.commons.net.ftp.FTPFile;
22
23 /**
24  * @version $Id: OS400FTPEntryParser.java 155429 2005-02-26 13:13:04Z dirkv $
25  */

26
27 public class OS400FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
28 {
29     private static final String JavaDoc DEFAULT_DATE_FORMAT
30         = "yy/MM/dd HH:mm:ss"; //01/11/09 12:30:24
31

32
33
34     private static final String JavaDoc REGEX =
35         "(\\S+)\\s+" // user
36
+ "(\\d+)\\s+" // size
37
+ "(\\S+)\\s+(\\S+)\\s+" // date stuff
38
+ "(\\*\\S+)\\s+" // *STMF/*DIR
39
+ "(\\S+/?)\\s*"; // filename
40

41     
42     /**
43      * The default constructor for a OS400FTPEntryParser object.
44      *
45      * @exception IllegalArgumentException
46      * Thrown if the regular expression is unparseable. Should not be seen
47      * under normal conditions. It it is seen, this is a sign that
48      * <code>REGEX</code> is not a valid regular expression.
49      */

50     public OS400FTPEntryParser()
51     {
52         this(null);
53     }
54
55     /**
56      * This constructor allows the creation of an OS400FTPEntryParser object
57      * with something other than the default configuration.
58      *
59      * @param config The {@link FTPClientConfig configuration} object used to
60      * configure this parser.
61      * @exception IllegalArgumentException
62      * Thrown if the regular expression is unparseable. Should not be seen
63      * under normal conditions. It it is seen, this is a sign that
64      * <code>REGEX</code> is not a valid regular expression.
65      * @since 1.4
66      */

67     public OS400FTPEntryParser(FTPClientConfig config)
68     {
69         super(REGEX);
70         configure(config);
71     }
72
73
74     public FTPFile parseFTPEntry(String JavaDoc entry)
75     {
76
77         FTPFile file = new FTPFile();
78         file.setRawListing(entry);
79         int type;
80
81         if (matches(entry))
82         {
83             String JavaDoc usr = group(1);
84             String JavaDoc filesize = group(2);
85             String JavaDoc datestr = group(3)+" "+group(4);
86             String JavaDoc typeStr = group(5);
87             String JavaDoc name = group(6);
88             
89             try
90             {
91                 file.setTimestamp(super.parseTimestamp(datestr));
92             }
93             catch (ParseException JavaDoc e)
94             {
95                 return null; // this is a parsing failure too.
96
}
97
98
99             if (typeStr.equalsIgnoreCase("*STMF"))
100             {
101                 type = FTPFile.FILE_TYPE;
102             }
103             else if (typeStr.equalsIgnoreCase("*DIR"))
104             {
105                 type = FTPFile.DIRECTORY_TYPE;
106             }
107             else
108             {
109                 type = FTPFile.UNKNOWN_TYPE;
110             }
111
112             file.setType(type);
113
114             file.setUser(usr);
115
116             try
117             {
118                 file.setSize(Long.parseLong(filesize));
119             }
120             catch (NumberFormatException JavaDoc e)
121             {
122                 // intentionally do nothing
123
}
124
125             if (name.endsWith("/"))
126             {
127                 name = name.substring(0, name.length() - 1);
128             }
129             int pos = name.lastIndexOf('/');
130             if (pos > -1)
131             {
132                 name = name.substring(pos + 1);
133             }
134
135             file.setName(name);
136
137             return file;
138         }
139         return null;
140     }
141
142     /**
143      * Defines a default configuration to be used when this class is
144      * instantiated without a {@link FTPClientConfig FTPClientConfig}
145      * parameter being specified.
146      * @return the default configuration for this parser.
147      */

148     protected FTPClientConfig getDefaultConfiguration() {
149         return new FTPClientConfig(
150                 FTPClientConfig.SYST_OS400,
151                 DEFAULT_DATE_FORMAT,
152                 null, null, null, null);
153     }
154
155 }
156
Popular Tags