001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.util;
018
019import java.beans.PropertyEditorSupport;
020import java.util.ArrayList;
021import java.util.List;
022import java.util.StringTokenizer;
023
024
025public class StringArrayEditor extends PropertyEditorSupport {
026
027    public void setAsText(String text) {
028        if (text == null || text.length() == 0) {
029            setValue(null);
030        } else {
031            StringTokenizer stok = new StringTokenizer(text, ",");
032            final List<String> list = new ArrayList<String>();
033
034            while (stok.hasMoreTokens()) {
035                list.add(stok.nextToken());
036            }
037
038            Object array = list.toArray(new String[list.size()]);
039
040            setValue(array);
041        }
042    }
043
044    public String getAsText() {
045        Object[] objects = (Object[]) getValue();
046        if (objects == null || objects.length == 0) {
047            return null;
048        }
049
050        StringBuffer result = new StringBuffer(String.valueOf(objects[0]));
051        for (int i = 1; i < objects.length; i++) {
052            result.append(",").append(objects[i]);
053        }
054
055        return result.toString();
056
057    }
058
059}
060