001package com.hfg.setting; 002 003import java.lang.reflect.Method; 004import java.util.List; 005import java.util.Map; 006 007import com.hfg.xml.XMLTag; 008 009//------------------------------------------------------------------------------ 010/** 011 * Base class for XML-serializable 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 SettingImpl<V extends Object> 037{ 038 private String mName; 039 private V mValue; 040 041 //########################################################################### 042 // CONSTRUCTORS 043 //########################################################################### 044 045 //--------------------------------------------------------------------------- 046 public SettingImpl(String inName) 047 { 048 mName = inName; 049 } 050 051 //--------------------------------------------------------------------------- 052 public SettingImpl(String inName, V inValue) 053 { 054 this(inName); 055 setObjectValue(inValue); 056 } 057 058 //--------------------------------------------------------------------------- 059 public SettingImpl(XMLTag inXMLTag) 060 { 061 inXMLTag.verifyTagName(SettingXML.SETTING); 062 mName = inXMLTag.getAttributeValue(SettingXML.NAME_ATT); 063 064 if (inXMLTag.hasContent()) 065 { 066 setValueFromString(inXMLTag.getUnescapedContent()); 067 } 068 } 069 070 //########################################################################### 071 // PUBLIC METHODS 072 //########################################################################### 073 074 //--------------------------------------------------------------------------- 075 public XMLTag toXMLTag() 076 { 077 XMLTag tag = new XMLTag(SettingXML.SETTING); 078 tag.setAttribute(SettingXML.NAME_ATT, name()); 079 tag.setAttribute(SettingXML.TYPE_ATT, getClass().getName()); 080 081 if (getObjectValue() != null) 082 { 083 if (getObjectValue() instanceof List) 084 { 085 for (Object object : (List) getObjectValue()) 086 { 087 XMLTag subtag = new XMLTag(SettingXML.VALUE); 088 tag.addSubtag(subtag); 089 090 subtag.setContent(object.toString()); 091 } 092 } 093 else if (getObjectValue() instanceof Map) 094 { 095 Map map = (Map) getObjectValue(); 096 for (Object key : map.keySet()) 097 { 098 XMLTag subtag = new XMLTag(SettingXML.VALUE); 099 tag.addSubtag(subtag); 100 subtag.setAttribute(SettingXML.NAME_ATT, key.toString()); 101 subtag.setContent(map.get(key).toString()); 102 } 103 } 104 else 105 { 106 try 107 { 108 Method method = getObjectValue().getClass().getMethod("toXMLTag"); 109 tag.addSubtag((XMLTag) method.invoke(getObjectValue())); 110 } 111 catch (Exception e) 112 { 113 // Ignore exceptions. Just set tag contents using the string value. 114 tag.setContent(getStringValue()); 115 } 116 } 117 } 118 119 return tag; 120 } 121 122 //--------------------------------------------------------------------------- 123 public String name() 124 { 125 return mName; 126 } 127 128 //--------------------------------------------------------------------------- 129 @Override 130 public String toString() 131 { 132 return (mValue != null ? mValue.toString() : ""); 133 } 134 135 //--------------------------------------------------------------------------- 136 protected void setObjectValue(V inValue) 137 { 138 mValue = inValue; 139 } 140 141 //--------------------------------------------------------------------------- 142 protected V getObjectValue() 143 { 144 return mValue; 145 } 146 147 //--------------------------------------------------------------------------- 148 public String getStringValue() 149 { 150 return (getObjectValue() != null ? getObjectValue().toString() : null); 151 } 152 153 //--------------------------------------------------------------------------- 154 protected void addValueFromString(String inValue) 155 { 156 setValueFromString(inValue); 157 } 158 159 //--------------------------------------------------------------------------- 160 protected abstract void setValueFromString(String inValue); 161}