All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.setting.SettingImpl Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.setting;

import com.hfg.xml.XMLTag;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

//------------------------------------------------------------------------------
/**
 * Base class for XML-serializable settings.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public abstract class SettingImpl
{
   private String      mName;
   private Object      mValue;

   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   public SettingImpl(String inName)
   {
      mName = inName;
   }

   //---------------------------------------------------------------------------
   public SettingImpl(String inName, Object inValue)
   {
      this(inName);
      setObjectValue(inValue);
   }

   //---------------------------------------------------------------------------
   public SettingImpl(XMLTag inXMLTag)
   {
      inXMLTag.verifyTagName(SettingXML.SETTING);
      mName = inXMLTag.getAttributeValue(SettingXML.NAME_ATT);

      if (inXMLTag.hasContent())
      {
         setValueFromString(inXMLTag.getUnescapedContent());
      }
   }

   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   public XMLTag toXMLTag()
   {
      XMLTag tag = new XMLTag(SettingXML.SETTING);
      tag.setAttribute(SettingXML.NAME_ATT, name());
      tag.setAttribute(SettingXML.TYPE_ATT, getClass().getName());

      if (getObjectValue() != null)
      {
         if (getObjectValue() instanceof List)
         {
            for (Object object : (List) getObjectValue())
            {
               XMLTag subtag = new XMLTag(SettingXML.VALUE);
               tag.addSubtag(subtag);

               subtag.setContent(object.toString());
            }
         }
         else if (getObjectValue() instanceof Map)
         {
            Map map = (Map) getObjectValue();
            for (Object key : map.keySet())
            {
               XMLTag subtag = new XMLTag(SettingXML.VALUE);
               tag.addSubtag(subtag);
               subtag.setAttribute(SettingXML.NAME_ATT, key.toString());
               subtag.setContent(map.get(key).toString());
            }
         }
         else
         {
            try
            {
               Method method = getObjectValue().getClass().getMethod("toXMLTag");
               tag.addSubtag((XMLTag) method.invoke(getObjectValue()));
            }
            catch (Exception e)
            {
               // Ignore exceptions. Just set tag contents using the string value.
               tag.setContent(getStringValue());
            }
         }
      }

      return tag;
   }

   //---------------------------------------------------------------------------
   public String name()
   {
      return mName;
   }

   //---------------------------------------------------------------------------
   @Override
   public String toString()
   {
      return (mValue != null ? mValue.toString() : "");
   }

   //---------------------------------------------------------------------------
   protected void setObjectValue(Object inValue)
   {
      mValue = inValue;
   }

   //---------------------------------------------------------------------------
   protected Object getObjectValue()
   {
      return mValue;
   }

   //---------------------------------------------------------------------------
   public String getStringValue()
   {
      return (getObjectValue() != null ? getObjectValue().toString() : null);
   }

   //---------------------------------------------------------------------------
   protected void addValueFromString(String inValue)
   {
      setValueFromString(inValue);
   }

   //---------------------------------------------------------------------------
   protected abstract void setValueFromString(String inValue);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy