001: /*
002: * Copyright 2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.ftp;
017:
018: import java.text.DateFormatSymbols;
019: import java.util.Collection;
020: import java.util.Locale;
021: import java.util.Map;
022: import java.util.StringTokenizer;
023: import java.util.TreeMap;
024:
025: /**
026: * <p>
027: * This class implements an alternate means of configuring the
028: * {@link org.apache.commons.net.ftp.FTPClient FTPClient} object and
029: * also subordinate objects which it uses. Any class implementing the
030: * {@link org.apache.commons.net.ftp.Configurable Configurable }
031: * interface can be configured by this object.
032: * </p><p>
033: * In particular this class was designed primarily to support configuration
034: * of FTP servers which express file timestamps in formats and languages
035: * other than those for the US locale, which although it is the most common
036: * is not universal. Unfortunately, nothing in the FTP spec allows this to
037: * be determined in an automated way, so manual configuration such as this
038: * is necessary.
039: * </p><p>
040: * This functionality was designed to allow existing clients to work exactly
041: * as before without requiring use of this component. This component should
042: * only need to be explicitly invoked by the user of this package for problem
043: * cases that previous implementations could not solve.
044: * </p>
045: * <h3>Examples of use of FTPClientConfig</h3>
046: * Use cases:
047: * You are trying to access a server that
048: * <ul>
049: * <li>lists files with timestamps that use month names in languages other
050: * than English</li>
051: * <li>lists files with timestamps that use date formats other
052: * than the American English "standard" <code>MM dd yyyy</code></li>
053: * <li>is in different timezone and you need accurate timestamps for
054: * dependency checking as in Ant</li>
055: * </ul>
056: * <p>
057: * Unpaged (whole list) access on a UNIX server that uses French month names
058: * but uses the "standard" <code>MMM d yyyy</code> date formatting
059: * <pre>
060: * FTPClient f=FTPClient();
061: * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
062: * conf.setServerLanguageCode("fr");
063: * f.configure(conf);
064: * f.connect(server);
065: * f.login(username, password);
066: * FTPFile[] files = listFiles(directory);
067: * </pre>
068: * </p>
069: * <p>
070: * Paged access on a UNIX server that uses Danish month names
071: * and "European" date formatting in Denmark's time zone, when you
072: * are in some other time zone.
073: * <pre>
074: * FTPClient f=FTPClient();
075: * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
076: * conf.setServerLanguageCode("da");
077: * conf.setDefaultDateFormat("d MMM yyyy");
078: * conf.setRecentDateFormat("d MMM HH:mm");
079: * conf.setTimeZoneId("Europe/Copenhagen");
080: * f.configure(conf);
081: * f.connect(server);
082: * f.login(username, password);
083: * FTPListParseEngine engine =
084: * f.initiateListParsing("com.whatever.YourOwnParser", directory);
085: *
086: * while (engine.hasNext()) {
087: * FTPFile[] files = engine.getNext(25); // "page size" you want
088: * //do whatever you want with these files, display them, etc.
089: * //expensive FTPFile objects not created until needed.
090: * }
091: * </pre>
092: * </p>
093: * <p>
094: * Unpaged (whole list) access on a VMS server that uses month names
095: * in a language not {@link #getSupportedLanguageCodes() supported} by the system.
096: * but uses the "standard" <code>MMM d yyyy</code> date formatting
097: * <pre>
098: * FTPClient f=FTPClient();
099: * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_VMS);
100: * conf.setShortMonthNames(
101: * "jan|feb|mar|apr|ma\u00ED|j\u00FAn|j\u00FAl|\u00e1g\u00FA|sep|okt|n\u00F3v|des");
102: * f.configure(conf);
103: * f.connect(server);
104: * f.login(username, password);
105: * FTPFile[] files = listFiles(directory);
106: * </pre>
107: * </p>
108: * <p>
109: * Unpaged (whole list) access on a Windows-NT server in a different time zone.
110: * (Note, since the NT Format uses numeric date formatting, language issues
111: * are irrelevant here).
112: * <pre>
113: * FTPClient f=FTPClient();
114: * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
115: * conf.setTimeZoneId("America/Denver");
116: * f.configure(conf);
117: * f.connect(server);
118: * f.login(username, password);
119: * FTPFile[] files = listFiles(directory);
120: * </pre>
121: * </p>
122: * Unpaged (whole list) access on a Windows-NT server in a different time zone
123: * but which has been configured to use a unix-style listing format.
124: * <pre>
125: * FTPClient f=FTPClient();
126: * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
127: * conf.setTimeZoneId("America/Denver");
128: * f.configure(conf);
129: * f.connect(server);
130: * f.login(username, password);
131: * FTPFile[] files = listFiles(directory);
132: * </pre>
133: * </p>
134: * @since 1.4
135: * @see org.apache.commons.net.ftp.Configurable
136: * @see org.apache.commons.net.ftp.FTPClient
137: * @see org.apache.commons.net.ftp.parser.FTPTimestampParserImpl#configure(FTPClientConfig)
138: * @see org.apache.commons.net.ftp.parser.ConfigurableFTPFileEntryParserImpl
139: */
140: public class FTPClientConfig {
141:
142: /**
143: * Identifier by which a unix-based ftp server is known throughout
144: * the commons-net ftp system.
145: */
146: public static final String SYST_UNIX = "UNIX";
147:
148: /**
149: * Identifier by which a vms-based ftp server is known throughout
150: * the commons-net ftp system.
151: */
152: public static final String SYST_VMS = "VMS";
153:
154: /**
155: * Identifier by which a WindowsNT-based ftp server is known throughout
156: * the commons-net ftp system.
157: */
158: public static final String SYST_NT = "WINDOWS";
159:
160: /**
161: * Identifier by which an OS/2-based ftp server is known throughout
162: * the commons-net ftp system.
163: */
164: public static final String SYST_OS2 = "OS/2";
165:
166: /**
167: * Identifier by which an OS/400-based ftp server is known throughout
168: * the commons-net ftp system.
169: */
170: public static final String SYST_OS400 = "OS/400";
171:
172: /**
173: * Identifier by which an MVS-based ftp server is known throughout
174: * the commons-net ftp system.
175: */
176: public static final String SYST_MVS = "MVS";
177:
178: private final String serverSystemKey;
179: private String defaultDateFormatStr = null;
180: private String recentDateFormatStr = null;
181: private String serverLanguageCode = null;
182: private String shortMonthNames = null;
183: private String serverTimeZoneId = null;
184:
185: /**
186: * The main constructor for an FTPClientConfig object
187: * @param systemKey key representing system type of the server being
188: * connected to. See {@link #getServerSystemKey() serverSystemKey}
189: */
190: public FTPClientConfig(String systemKey) {
191: this .serverSystemKey = systemKey;
192: }
193:
194: /**
195: * Convenience constructor mainly for use in testing.
196: * Constructs a UNIX configuration.
197: */
198: public FTPClientConfig() {
199: this (SYST_UNIX);
200: }
201:
202: /**
203: * Constructor which allows setting of all member fields
204: * @param systemKey key representing system type of the server being
205: * connected to. See
206: * {@link #getServerSystemKey() serverSystemKey}
207: * @param defaultDateFormatStr See
208: * {@link #setDefaultDateFormatStr(String) defaultDateFormatStr}
209: * @param recentDateFormatStr See
210: * {@link #setRecentDateFormatStr(String) recentDateFormatStr}
211: * @param serverLanguageCode See
212: * {@link #setServerLanguageCode(String) serverLanguageCode}
213: * @param shortMonthNames See
214: * {@link #setShortMonthNames(String) shortMonthNames}
215: * @param serverTimeZoneId See
216: * {@link #setServerTimeZoneId(String) serverTimeZoneId}
217: */
218: public FTPClientConfig(String systemKey,
219: String defaultDateFormatStr, String recentDateFormatStr,
220: String serverLanguageCode, String shortMonthNames,
221: String serverTimeZoneId) {
222: this (systemKey);
223: this .defaultDateFormatStr = defaultDateFormatStr;
224: this .recentDateFormatStr = recentDateFormatStr;
225: this .serverLanguageCode = serverLanguageCode;
226: this .shortMonthNames = shortMonthNames;
227: this .serverTimeZoneId = serverTimeZoneId;
228: }
229:
230: private static Map LANGUAGE_CODE_MAP = new TreeMap();
231: static {
232:
233: // if there are other commonly used month name encodings which
234: // correspond to particular locales, please add them here.
235:
236: // many locales code short names for months as all three letters
237: // these we handle simply.
238: LANGUAGE_CODE_MAP.put("en", Locale.ENGLISH);
239: LANGUAGE_CODE_MAP.put("de", Locale.GERMAN);
240: LANGUAGE_CODE_MAP.put("it", Locale.ITALIAN);
241: LANGUAGE_CODE_MAP.put("es", new Locale("es", "", "")); // spanish
242: LANGUAGE_CODE_MAP.put("pt", new Locale("pt", "", "")); // portuguese
243: LANGUAGE_CODE_MAP.put("da", new Locale("da", "", "")); // danish
244: LANGUAGE_CODE_MAP.put("sv", new Locale("sv", "", "")); // swedish
245: LANGUAGE_CODE_MAP.put("no", new Locale("no", "", "")); // norwegian
246: LANGUAGE_CODE_MAP.put("nl", new Locale("nl", "", "")); // dutch
247: LANGUAGE_CODE_MAP.put("ro", new Locale("ro", "", "")); // romanian
248: LANGUAGE_CODE_MAP.put("sq", new Locale("sq", "", "")); // albanian
249: LANGUAGE_CODE_MAP.put("sh", new Locale("sh", "", "")); // serbo-croatian
250: LANGUAGE_CODE_MAP.put("sk", new Locale("sk", "", "")); // slovak
251: LANGUAGE_CODE_MAP.put("sl", new Locale("sl", "", "")); // slovenian
252:
253: // some don't
254: LANGUAGE_CODE_MAP
255: .put("fr",
256: "jan|f\u00e9v|mar|avr|mai|jun|jui|ao\u00fb|sep|oct|nov|d\u00e9c"); //french
257:
258: }
259:
260: /**
261: * Getter for the serverSystemKey property. This property
262: * specifies the general type of server to which the client connects.
263: * Should be either one of the <code>FTPClientConfig.SYST_*</code> codes
264: * or else the fully qualified class name of a parser implementing both
265: * the <code>FTPFileEntryParser</code> and <code>Configurable</code>
266: * interfaces.
267: * @return Returns the serverSystemKey property.
268: */
269: public String getServerSystemKey() {
270: return serverSystemKey;
271: }
272:
273: /**
274: * getter for the {@link #setDefaultDateFormatStr(String) defaultDateFormatStr}
275: * property.
276: * @return Returns the defaultDateFormatStr property.
277: */
278: public String getDefaultDateFormatStr() {
279: return defaultDateFormatStr;
280: }
281:
282: /**
283: * getter for the {@link #setRecentDateFormatStr(String) recentDateFormatStr} property.
284: * @return Returns the recentDateFormatStr property.
285: */
286:
287: public String getRecentDateFormatStr() {
288: return recentDateFormatStr;
289: }
290:
291: /**
292: * getter for the {@link #setServerTimeZoneId(String) serverTimeZoneId} property.
293: * @return Returns the serverTimeZoneId property.
294: */
295: public String getServerTimeZoneId() {
296: return serverTimeZoneId;
297: }
298:
299: /**
300: * <p>
301: * getter for the {@link #setShortMonthNames(String) shortMonthNames}
302: * property.
303: * </p>
304: * @return Returns the shortMonthNames.
305: */
306: public String getShortMonthNames() {
307: return shortMonthNames;
308: }
309:
310: /**
311: * <p>
312: * getter for the {@link #setServerLanguageCode(String) serverLanguageCode} property.
313: * </p>
314: * * @return Returns the serverLanguageCode property.
315: */
316: public String getServerLanguageCode() {
317: return serverLanguageCode;
318: }
319:
320: /**
321: * <p>
322: * setter for the defaultDateFormatStr property. This property
323: * specifies the main date format that will be used by a parser configured
324: * by this configuration to parse file timestamps. If this is not
325: * specified, such a parser will use as a default value, the most commonly
326: * used format which will be in as used in <code>en_US</code> locales.
327: * </p><p>
328: * This should be in the format described for
329: * <code>java.text.SimpleDateFormat</code>.
330: * property.
331: * </p>
332: * @param defaultDateFormatStr The defaultDateFormatStr to set.
333: */
334: public void setDefaultDateFormatStr(String defaultDateFormatStr) {
335: this .defaultDateFormatStr = defaultDateFormatStr;
336: }
337:
338: /**
339: * <p>
340: * setter for the recentDateFormatStr property. This property
341: * specifies a secondary date format that will be used by a parser
342: * configured by this configuration to parse file timestamps, typically
343: * those less than a year old. If this is not specified, such a parser
344: * will not attempt to parse using an alternate format.
345: * </p>
346: * This is used primarily in unix-based systems.
347: * </p>
348: * This should be in the format described for
349: * <code>java.text.SimpleDateFormat</code>.
350: * </p>
351: * @param recentDateFormatStr The recentDateFormatStr to set.
352: */
353: public void setRecentDateFormatStr(String recentDateFormatStr) {
354: this .recentDateFormatStr = recentDateFormatStr;
355: }
356:
357: /**
358: * <p>
359: * setter for the serverTimeZoneId property. This property
360: * allows a time zone to be specified corresponding to that known to be
361: * used by an FTP server in file listings. This might be particularly
362: * useful to clients such as Ant that try to use these timestamps for
363: * dependency checking.
364: * </p><p>
365: * This should be one of the identifiers used by
366: * <code>java.util.TimeZone</code> to refer to time zones, for example,
367: * <code>America/Chicago</code> or <code>Asia/Rangoon</code>.
368: * </p>
369: * @param serverTimeZoneId The serverTimeZoneId to set.
370: */
371: public void setServerTimeZoneId(String serverTimeZoneId) {
372: this .serverTimeZoneId = serverTimeZoneId;
373: }
374:
375: /**
376: * <p>
377: * setter for the shortMonthNames property.
378: * This property allows the user to specify a set of month names
379: * used by the server that is different from those that may be
380: * specified using the {@link #setServerLanguageCode(String) serverLanguageCode}
381: * property.
382: * </p><p>
383: * This should be a string containing twelve strings each composed of
384: * three characters, delimited by pipe (|) characters. Currently,
385: * only 8-bit ASCII characters are known to be supported. For example,
386: * a set of month names used by a hypothetical Icelandic FTP server might
387: * conceivably be specified as
388: * <code>"jan|feb|mar|apr|maí|jún|júl|ágú|sep|okt|nóv|des"</code>.
389: * </p>
390: * @param shortMonthNames The value to set to the shortMonthNames property.
391: */
392: public void setShortMonthNames(String shortMonthNames) {
393: this .shortMonthNames = shortMonthNames;
394: }
395:
396: /**
397: * <p>
398: * setter for the serverLanguageCode property. This property allows
399: * user to specify a
400: * <a href="http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt">
401: * two-letter ISO-639 language code</a> that will be used to
402: * configure the set of month names used by the file timestamp parser.
403: * If neither this nor the {@link #setShortMonthNames(String) shortMonthNames}
404: * is specified, parsing will assume English month names, which may or
405: * may not be significant, depending on whether the date format(s)
406: * specified via {@link #setDefaultDateFormatStr(String) defaultDateFormatStr}
407: * and/or {@link #setRecentDateFormatStr(String) recentDateFormatStr} are using
408: * numeric or alphabetic month names.
409: * </p>
410: * <p>If the code supplied is not supported here, <code>en_US</code>
411: * month names will be used. We are supporting here those language
412: * codes which, when a <code> java.util.Locale</code> is constucted
413: * using it, and a <code>java.text.SimpleDateFormat</code> is
414: * constructed using that Locale, the array returned by the
415: * SimpleDateFormat's <code>getShortMonths()</code> method consists
416: * solely of three 8-bit ASCII character strings. Additionally,
417: * languages which do not meet this requirement are included if a
418: * common alternative set of short month names is known to be used.
419: * This means that users who can tell us of additional such encodings
420: * may get them added to the list of supported languages by contacting
421: * the jakarta-commons-net team.
422: * </p>
423: * <p><strong>
424: * Please note that this attribute will NOT be used to determine a
425: * locale-based date format for the language. </strong>
426: * Experience has shown that many if not most FTP servers outside the
427: * United States employ the standard <code>en_US</code> date format
428: * orderings of <code>MMM d yyyy</code> and <code>MMM d HH:mm</code>
429: * and attempting to deduce this automatically here would cause more
430: * problems than it would solve. The date format must be changed
431: * via the {@link #setDefaultDateFormatStr(String) defaultDateFormatStr} and/or
432: * {@link #setRecentDateFormatStr(String) recentDateFormatStr} parameters.
433: * </p>
434: * @param serverLanguageCode The value to set to the serverLanguageCode property.
435: */
436: public void setServerLanguageCode(String serverLanguageCode) {
437: this .serverLanguageCode = serverLanguageCode;
438: }
439:
440: /**
441: * Looks up the supplied language code in the internally maintained table of
442: * language codes. Returns a DateFormatSymbols object configured with
443: * short month names corresponding to the code. If there is no corresponding
444: * entry in the table, the object returned will be that for
445: * <code>Locale.US</code>
446: * @param languageCode See {@link #setServerLanguageCode(String) serverLanguageCode}
447: * @return a DateFormatSymbols object configured with short month names
448: * corresponding to the supplied code, or with month names for
449: * <code>Locale.US</code> if there is no corresponding entry in the internal
450: * table.
451: */
452: public static DateFormatSymbols lookupDateFormatSymbols(
453: String languageCode) {
454: Object lang = LANGUAGE_CODE_MAP.get(languageCode);
455: if (lang != null) {
456: if (lang instanceof Locale) {
457: return new DateFormatSymbols((Locale) lang);
458: } else if (lang instanceof String) {
459: return getDateFormatSymbols((String) lang);
460: }
461: }
462: return new DateFormatSymbols(Locale.US);
463: }
464:
465: /**
466: * Returns a DateFormatSymbols object configured with short month names
467: * as in the supplied string
468: * @param shortmonths This should be as described in
469: * {@link #setShortMonthNames(String) shortMonthNames}
470: * @return a DateFormatSymbols object configured with short month names
471: * as in the supplied string
472: */
473: public static DateFormatSymbols getDateFormatSymbols(
474: String shortmonths) {
475: String[] months = splitShortMonthString(shortmonths);
476: DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
477: dfs.setShortMonths(months);
478: return dfs;
479: }
480:
481: private static String[] splitShortMonthString(String shortmonths) {
482: StringTokenizer st = new StringTokenizer(shortmonths, "|");
483: int monthcnt = st.countTokens();
484: if (12 != monthcnt) {
485: throw new IllegalArgumentException(
486: "expecting a pipe-delimited string containing 12 tokens");
487: }
488: String[] months = new String[13];
489: int pos = 0;
490: while (st.hasMoreTokens()) {
491: months[pos++] = st.nextToken();
492: }
493: months[pos] = "";
494: return months;
495: }
496:
497: /**
498: * Returns a Collection of all the language codes currently supported
499: * by this class. See {@link #setServerLanguageCode(String) serverLanguageCode}
500: * for a functional descrption of language codes within this system.
501: *
502: * @return a Collection of all the language codes currently supported
503: * by this class
504: */
505: public static Collection getSupportedLanguageCodes() {
506: return LANGUAGE_CODE_MAP.keySet();
507: }
508:
509: }
|