01: /*
02: * FirstWordFormatter.java
03: *
04: * Copyright (C) 2005 Anupam Sengupta (anupamsg@users.sourceforge.net)
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19: *
20: * Version: $Revision: 1.3 $
21: */
22: package net.sf.anupam.csv.formatters;
23:
24: import org.apache.commons.lang.StringUtils;
25:
26: /**
27: * A {@link CSVFieldFormatter formatter} that returns the first word
28: * of the specified CSV value. This is useful in situations such as
29: * extracting the first name.
30: *
31: * @author Anupam Sengupta
32: * @version $Revision: 1.3 $
33: * @csv.formatter-mapping name="firstWord"
34: * @see LastWordFormatter
35: * @since 1.5
36: */
37: final class FirstWordFormatter implements CSVFieldFormatter {
38:
39: /**
40: * Constructor for FirstWordFormatter.
41: */
42: public FirstWordFormatter() {
43: super ();
44: }
45:
46: /**
47: * Formats the value and returns the first word.
48: *
49: * @param value the value to be transformed
50: * @return the first word from the input value
51: * @see CSVFieldFormatter#format(String)
52: */
53: public String format(final String value) {
54:
55: if (value == null) {
56: return null;
57: }
58: return StringUtils.trim(value.split(" ")[0]);
59: }
60:
61: }
|