001: /**
002: *
003: * edtFTPj
004: *
005: * Copyright (C) 2000-2004 Enterprise Distributed Technologies Ltd
006: *
007: * www.enterprisedt.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: *
023: * Bug fixes, suggestions and comments should be should posted on
024: * http://www.enterprisedt.com/forums/index.php
025: *
026: * Change Log:
027: *
028: * $Log: FTPFileParser.java,v $
029: * Revision 1.9 2007-12-18 07:53:20 bruceb
030: * trimStart() changes
031: *
032: * Revision 1.8 2007-10-12 05:20:44 bruceb
033: * permit ignoring date parser errors
034: *
035: * Revision 1.7 2007-01-15 23:03:22 bruceb
036: * more splitter methods
037: *
038: * Revision 1.6 2005/07/22 10:25:12 bruceb
039: * upped MAX_FIELDS
040: *
041: * Revision 1.5 2005/06/03 11:26:25 bruceb
042: * comment change
043: *
044: * Revision 1.4 2004/10/18 15:57:51 bruceb
045: * setLocale added
046: *
047: * Revision 1.3 2004/07/23 08:29:57 bruceb
048: * updated comment
049: *
050: * Revision 1.2 2004/06/25 11:48:30 bruceb
051: * changed MAX_FIELDS to 20
052: *
053: * Revision 1.1 2004/04/17 23:42:07 bruceb
054: * file parsing part II
055: *
056: * Revision 1.1 2004/04/17 18:37:23 bruceb
057: * new parse functionality
058: *
059: */package com.enterprisedt.net.ftp;
060:
061: import java.text.ParseException;
062: import java.util.Locale;
063:
064: /**
065: * Root class of all file parsers
066: *
067: * @author Bruce Blackshaw
068: * @version $Revision: 1.9 $
069: */
070: abstract public class FTPFileParser {
071:
072: /**
073: * Maximum number of fields in raw string
074: */
075: private final static int MAX_FIELDS = 100;
076:
077: /**
078: * Ignore date parsing errors
079: */
080: protected boolean ignoreDateParseErrors = false;
081:
082: /**
083: * Parse server supplied string
084: *
085: * @param raw raw string to parse
086: */
087: abstract public FTPFile parse(String raw) throws ParseException;
088:
089: /**
090: * Set the locale for date parsing of listings
091: *
092: * @param locale locale to set
093: */
094: abstract public void setLocale(Locale locale);
095:
096: /**
097: * Ignore date parse errors
098: *
099: * @param ignore
100: */
101: public void setIgnoreDateParseErrors(boolean ignore) {
102: this .ignoreDateParseErrors = ignore;
103: }
104:
105: /**
106: * Trim the start of the supplied string
107: *
108: * @param str string to trim
109: * @return string trimmed of whitespace at the start
110: */
111: protected String trimStart(String str) {
112: StringBuffer buf = new StringBuffer();
113: boolean found = false;
114: for (int i = 0; i < str.length(); i++) {
115: char ch = str.charAt(i);
116: if (!found & Character.isWhitespace(ch))
117: continue;
118: found = true;
119: buf.append(ch);
120: }
121: return buf.toString();
122: }
123:
124: /**
125: * Splits string consisting of fields separated by
126: * whitespace into an array of strings. Yes, we could
127: * use String.split() but this would restrict us to 1.4+
128: *
129: * @param str string to split
130: * @return array of fields
131: */
132: protected String[] split(String str) {
133: return split(str, new WhitespaceSplitter());
134: }
135:
136: /**
137: * Splits string consisting of fields separated by
138: * whitespace into an array of strings. Yes, we could
139: * use String.split() but this would restrict us to 1.4+
140: *
141: * @param str string to split
142: * @return array of fields
143: */
144: protected String[] split(String str, char token) {
145: return split(str, new CharSplitter(token));
146: }
147:
148: /**
149: * Splits string consisting of fields separated by
150: * whitespace into an array of strings. Yes, we could
151: * use String.split() but this would restrict us to 1.4+
152: *
153: * @param str string to split
154: * @return array of fields
155: */
156: protected String[] split(String str, Splitter splitter) {
157: String[] fields = new String[MAX_FIELDS];
158: int pos = 0;
159: StringBuffer field = new StringBuffer();
160: for (int i = 0; i < str.length(); i++) {
161: char ch = str.charAt(i);
162: if (!splitter.isSeparator(ch))
163: field.append(ch);
164: else {
165: if (field.length() > 0) {
166: fields[pos++] = field.toString();
167: field.setLength(0);
168: }
169: }
170: }
171: // pick up last field
172: if (field.length() > 0) {
173: fields[pos++] = field.toString();
174: }
175: String[] result = new String[pos];
176: System.arraycopy(fields, 0, result, 0, pos);
177: return result;
178: }
179:
180: interface Splitter {
181: boolean isSeparator(char ch);
182: }
183:
184: class CharSplitter implements Splitter {
185: private char token;
186:
187: CharSplitter(char token) {
188: this .token = token;
189: }
190:
191: public boolean isSeparator(char ch) {
192: if (ch == token)
193: return true;
194: return false;
195: }
196:
197: }
198:
199: class WhitespaceSplitter implements Splitter {
200:
201: public boolean isSeparator(char ch) {
202: if (Character.isWhitespace(ch))
203: return true;
204: return false;
205: }
206: }
207: }
|