KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > propertyeditor > StringArrayEditor


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.propertyeditor;
23
24 import java.beans.PropertyEditorSupport JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 /**
29  * A property editor for String[]. The text format of a string array is a
30  * comma or \n, \r seperated list with \, representing an escaped comma to
31  * include in the string element.
32  *
33  * @version <tt>$Revision: 1958 $</tt>
34  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
35  * @author Scott.Stark@jboss.org
36  */

37 public class StringArrayEditor
38    extends PropertyEditorSupport JavaDoc
39 {
40    Pattern JavaDoc commaDelim = Pattern.compile("','|[^,\r\n]+");
41
42    static String JavaDoc[] parseList(String JavaDoc text)
43    {
44       ArrayList JavaDoc list = new ArrayList JavaDoc();
45       StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
46       for(int n = 0; n < text.length(); n ++)
47       {
48          char c = text.charAt(n);
49          switch( c)
50          {
51             case '\\':
52                tmp.append(c);
53                if( n < text.length() && text.charAt(n+1) == ',' )
54                {
55                   tmp.setCharAt(tmp.length()-1, ',');
56                   n ++;
57                }
58                break;
59             case ',':
60             case '\n':
61             case '\r':
62                if( tmp.length() > 0 )
63                   list.add(tmp.toString());
64                tmp.setLength(0);
65                break;
66             default:
67                tmp.append(c);
68                break;
69          }
70       }
71       if( tmp.length() > 0 )
72          list.add(tmp.toString());
73
74       String JavaDoc[] x = new String JavaDoc[list.size()];
75       list.toArray(x);
76       return x;
77    }
78
79    /** Build a String[] from comma or eol seperated elements with a \,
80     * representing a ',' to include in the current string element.
81     *
82     */

83    public void setAsText(final String JavaDoc text)
84    {
85       String JavaDoc[] theValue = parseList(text);
86       setValue(theValue);
87    }
88
89    /**
90     * @return a comma seperated string of the array elements
91     */

92    public String JavaDoc getAsText()
93    {
94       String JavaDoc[] theValue = (String JavaDoc[]) getValue();
95       StringBuffer JavaDoc text = new StringBuffer JavaDoc();
96       int length = theValue == null ? 0 : theValue.length;
97       for(int n = 0; n < length; n ++)
98       {
99          String JavaDoc s = theValue[n];
100          if( s.equals(",") )
101             text.append('\\');
102          text.append(s);
103          text.append(',');
104       }
105       // Remove the trailing ','
106
if( text.length() > 0 )
107          text.setLength(text.length()-1);
108       return text.toString();
109    }
110 }
111
Popular Tags