001package com.hfg.setting;
002
003import com.hfg.xml.XMLTag;
004
005import java.util.ArrayList;
006import java.util.Collection;
007import java.util.List;
008
009//------------------------------------------------------------------------------
010/**
011 * Base class for XML-serializable list settings.
012 *
013 * @author J. Alex Taylor, hairyfatguy.com
014 */
015//------------------------------------------------------------------------------
016// com.hfg XML/HTML Coding Library
017//
018// This library is free software; you can redistribute it and/or
019// modify it under the terms of the GNU Lesser General Public
020// License as published by the Free Software Foundation; either
021// version 2.1 of the License, or (at your option) any later version.
022//
023// This library is distributed in the hope that it will be useful,
024// but WITHOUT ANY WARRANTY; without even the implied warranty of
025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026// Lesser General Public License for more details.
027//
028// You should have received a copy of the GNU Lesser General Public
029// License along with this library; if not, write to the Free Software
030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031//
032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
033// jataylor@hairyfatguy.com
034//------------------------------------------------------------------------------
035
036public abstract class ListSettingImpl extends SettingImpl
037{
038   //---------------------------------------------------------------------------
039   public ListSettingImpl(String inName)
040   {
041      super(inName);
042   }
043
044   //---------------------------------------------------------------------------
045   public ListSettingImpl(String inName, Object inValue)
046   {
047      this(inName);
048      if (inValue != null)
049      {
050         if (! (inValue instanceof Collection))
051         {
052            throw new RuntimeException("The specified value (" + inValue.getClass().getSimpleName() + ") must be a Collection!");
053         }
054
055         List listValue;
056         if (! (inValue instanceof List))
057         {
058            listValue = new ArrayList((Collection) inValue);
059         }
060         else
061         {
062            listValue = (List) inValue;
063         }
064
065         setObjectValue(listValue);
066      }
067   }
068
069   //---------------------------------------------------------------------------
070   public ListSettingImpl(XMLTag inXMLTag)
071   {
072      super(inXMLTag);
073
074      List<XMLTag> valueSubtags = inXMLTag.getSubtagsByName(SettingXML.VALUE);
075      if (valueSubtags != null)
076      {
077         for (XMLTag subtag : valueSubtags)
078         {
079            addValueFromString(subtag.getUnescapedContent());
080         }
081      }
082   }
083
084   //---------------------------------------------------------------------------
085   protected abstract void addValueFromString(String inValue);
086
087   //---------------------------------------------------------------------------
088   protected void setValueFromString(String inValue)
089   {
090      setObjectValue(inValue);
091   }
092
093}