1 22 23 package org.cofax.cms; 24 25 import org.apache.commons.net.ftp.*; 26 import java.io.*; 27 import java.util.*; 28 29 37 38 public class CofaxToolsNTFileListParser implements FTPFileListParser { 39 40 43 public int timeStart = 0; 44 45 48 public int timeStop = 17; 49 50 53 public int isDirStart = 24; 54 55 58 public int isDirStop = 29; 59 60 63 public int fileSizeStart = 30; 64 65 68 public int fileSizeStop = 38; 69 70 73 public int fileNameStart = 39; 74 75 78 public int fileNameStop = 150; 79 80 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 s = new StringBuffer (); 91 String thisLine; 92 HashMap ht = new HashMap(); 93 94 while ((thisLine = br.readLine()) != null) { 95 s.append(thisLine + "\n"); 96 ht = parseLine(thisLine); 97 String fileName = (String ) ht.get("FILENAME"); 98 if (fileName != null) { 99 FTPFile file = new FTPFile(); 100 file.setGroup((String ) ht.get("group")); 101 file.setName((String ) ht.get("FILENAME")); 102 String sSize = (String ) 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 nfe) { 110 CofaxToolsUtil.log("NTFileParseError parseFileList ERROR: " + nfe.getMessage()); 111 } 112 } 113 file.setUser((String ) ht.get("owner")); 114 115 String type = (String ) 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 138 139 public String preTrim(String input) { 140 StringBuffer sb = new StringBuffer (); 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 157 public HashMap parseLine(String line) { 158 HashMap ht = new HashMap(); 159 int length = line.length(); 160 try { 161 162 String time = line.substring(timeStart, timeStop); 163 String isDir = line.substring(isDirStart, isDirStop); 164 String fileSize = line.substring(fileSizeStart, fileSizeStop); 165 String 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 e) { 183 CofaxToolsUtil.log("NTFileParseError parseLine ERROR: " + e); 184 ht.put("Other", line); 185 } catch (StringIndexOutOfBoundsException e) { 186 CofaxToolsUtil.log("NTFileParseError parseLine ERROR: " + e); 187 ht.put("Other", line); 188 } 189 return (ht); 190 } 191 192 } 193 | Popular Tags |