KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > cms > CofaxToolsNTFileListParser


1 /*
2  * CofaxToolsNTFileListParser is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/cms/CofaxToolsNTFileListParser.java,v 1.4.2.1 2006/12/11 16:28:24 fxrobin Exp $
21  */

22
23 package org.cofax.cms;
24
25 import org.apache.commons.net.ftp.*;
26 import java.io.*;
27 import java.util.*;
28
29 /**
30  * CofaxToolsNTFileListParser: Replaces DefaultFileListParser for the Oroinc
31  * netComponents class - which does not seem to perform the ftp list() call
32  * correctly on NT machines. This class splits list() returns from an NT server
33  * and ports the appropriate portions into FTPFile objects.
34  *
35  * @author Charles Harvey
36  */

37
38 public class CofaxToolsNTFileListParser implements FTPFileListParser {
39
40     /**
41      * Point in the string at which time information starts.
42      */

43     public int timeStart = 0;
44
45     /**
46      * Point in the string at which time information stops.
47      */

48     public int timeStop = 17;
49
50     /**
51      * Point in the string at which dir/ folder information starts.
52      */

53     public int isDirStart = 24;
54
55     /**
56      * Point in the string at which dir/ folder information stops.
57      */

58     public int isDirStop = 29;
59
60     /**
61      * Point in the string at which file size information starts.
62      */

63     public int fileSizeStart = 30;
64
65     /**
66      * Point in the string at which file size information stops.
67      */

68     public int fileSizeStop = 38;
69
70     /**
71      * Point in the string at which file name information starts.
72      */

73     public int fileNameStart = 39;
74
75     /**
76      * Point in the string at which file name information stops.
77      */

78     public int fileNameStop = 150;
79
80     /**
81      * Parses all information returned by parseline (from a line return from an
82      * ftp list() call into a new array of Oroinc FTPFile objects.
83      */

84
85     public FTPFile[] parseFileList(InputStream in) throws IOException {
86         ArrayList al = new ArrayList();
87         InputStreamReader reader = new InputStreamReader(in);
88
89         BufferedReader br = new BufferedReader(reader);
90         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
91         String JavaDoc thisLine;
92         HashMap ht = new HashMap();
93
94         while ((thisLine = br.readLine()) != null) {
95             s.append(thisLine + "\n");
96             ht = parseLine(thisLine);
97             String JavaDoc fileName = (String JavaDoc) ht.get("FILENAME");
98             if (fileName != null) {
99                 FTPFile file = new FTPFile();
100                 file.setGroup((String JavaDoc) ht.get("group"));
101                 file.setName((String JavaDoc) ht.get("FILENAME"));
102                 String JavaDoc sSize = (String JavaDoc) ht.get("fileSize");
103                 if (!sSize.equals("")) {
104                     long size = 0;
105                     try {
106                         size = Long.parseLong(sSize.trim());
107                         size = (long) size;
108                         file.setSize(size);
109                     } catch (NumberFormatException JavaDoc nfe) {
110                         CofaxToolsUtil.log("NTFileParseError parseFileList ERROR: " + nfe.getMessage());
111                     }
112                 }
113                 file.setUser((String JavaDoc) ht.get("owner"));
114
115                 String JavaDoc type = (String JavaDoc) ht.get("TYPE");
116                 if (type != null) {
117                     if (type.equals("folder")) {
118                         file.setType(FTPFile.DIRECTORY_TYPE);
119                     } else {
120                         file.setType(FTPFile.FILE_TYPE);
121                     }
122                     al.add(file);
123                 }
124             }
125         }
126         FTPFile[] files = new FTPFile[al.size()];
127         for (int i = 0; i < al.size(); i++) {
128             files[i] = (FTPFile) al.get(i);
129         }
130         return (files);
131     }
132
133     /**
134      * Default trim() function does not operate on Strings returned by an NT
135      * server. Replaces NT whitespace characters with whitespace characters that
136      * we can perform default trim() on and then performs default trim();
137      */

138
139     public String JavaDoc preTrim(String JavaDoc input) {
140         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
141         char[] inputC = input.toCharArray();
142         for (int i = 0; i < inputC.length; i++) {
143             if (Character.isWhitespace(inputC[i])) {
144                 sb.append(" ");
145             } else {
146                 sb.append(inputC[i]);
147             }
148         }
149         return (sb.toString().trim());
150     }
151
152     /**
153      * Splits the line returned via an ftp list() command into elements
154      * according to class parameters and returns them in a HashMap.
155      *
156      */

157     public HashMap parseLine(String JavaDoc line) {
158         HashMap ht = new HashMap();
159         int length = line.length();
160         try {
161
162             String JavaDoc time = line.substring(timeStart, timeStop);
163             String JavaDoc isDir = line.substring(isDirStart, isDirStop);
164             String JavaDoc fileSize = line.substring(fileSizeStart, fileSizeStop);
165             String JavaDoc fileName = line.substring(fileNameStart, length);
166
167             time = preTrim(time);
168             isDir = preTrim(isDir);
169             fileSize = preTrim(fileSize);
170             fileName = preTrim(fileName);
171
172             ht.put("time", time);
173             ht.put("fileSize", fileSize);
174             ht.put("FILENAME", fileName);
175
176             if (isDir.indexOf("<DIR>") == 0) {
177                 ht.put("TYPE", "folder");
178             } else {
179                 ht.put("TYPE", "file");
180             }
181
182         } catch (java.lang.NullPointerException JavaDoc e) {
183             CofaxToolsUtil.log("NTFileParseError parseLine ERROR: " + e);
184             ht.put("Other", line);
185         } catch (StringIndexOutOfBoundsException JavaDoc e) {
186             CofaxToolsUtil.log("NTFileParseError parseLine ERROR: " + e);
187             ht.put("Other", line);
188         }
189         return (ht);
190     }
191
192 }
193
Popular Tags