00001 #include <spl/configuration/ConfigurationSection.h> 00002 #include <spl/Environment.h> 00003 #include <spl/text/StringBuffer.h> 00004 #include <spl/xml/XmlText.h> 00005 00006 ConfigurationSection::ConfigurationSection() 00007 : m_name(), m_idx() 00008 { 00009 } 00010 00011 ConfigurationSection::ConfigurationSection(const String& name) 00012 : m_name(name), m_idx() 00013 { 00014 } 00015 00016 ConfigurationSection::ConfigurationSection(const ConfigurationSection& cs) 00017 : m_name(cs.m_name), m_idx(cs.m_idx) 00018 { 00019 } 00020 00021 ConfigurationSection::~ConfigurationSection() 00022 { 00023 } 00024 00025 ConfigurationSection& ConfigurationSection::operator =(const ConfigurationSection& cs) 00026 { 00027 m_name = cs.m_name; 00028 m_idx.Clear(); 00029 00030 for(Hashtable<String, List<StringPtr> >::Iterator iter = cs.m_idx.Begin(); iter.Next(); ) 00031 { 00032 m_idx.Set(iter.CurrentKeyRef(), List<StringPtr>()); 00033 00034 for (List<StringPtr>::Iterator vals(iter.CurrentRef().Begin()); vals.Next(); ) 00035 { 00036 m_idx.GetRef(iter.CurrentKeyRef()).Add(vals.CurrentRef()->Clone()); 00037 } 00038 } 00039 00040 return *this; 00041 } 00042 00043 Array<String> ConfigurationSection::Keys() 00044 { 00045 int pos = 0; 00046 Array<String> keys(m_idx.Count()); 00047 00048 for(Hashtable<String, List<StringPtr> >::Iterator iter = m_idx.Begin(); iter.Next(); ) 00049 { 00050 keys[pos] = iter.CurrentKey(); 00051 } 00052 00053 return keys; 00054 } 00055 00056 void ConfigurationSection::Set(const String& key, const String& value) 00057 { 00058 if (! m_idx.ContainsKey(key)) 00059 { 00060 m_idx.Set(key, List<StringPtr>()); 00061 } 00062 m_idx.GetRef(key).Add(value.Clone()); 00063 } 00064 00065 void ConfigurationSection::Remove(const String& key) 00066 { 00067 m_idx.Remove(key); 00068 } 00069 00070 StringPtr ConfigurationSection::Value(const String& key) 00071 { 00072 if (! m_idx.ContainsKey(key)) 00073 { 00074 return StringPtr(); 00075 } 00076 00077 List<StringPtr>& list = m_idx.GetRef(key); 00078 if (list.Count() == 0) 00079 { 00080 return StringPtr(); 00081 } 00082 if (list.Count() == 1) 00083 { 00084 return list.Peek(); 00085 } 00086 00087 StringBuffer val; 00088 for (List<StringPtr>::Iterator vals(list.Begin()); vals.Next(); ) 00089 { 00090 if (val.Length() > 0) 00091 { 00092 val.Append(','); 00093 } 00094 val.Append(vals.Current()); 00095 } 00096 00097 return val.ToString(); 00098 } 00099 00100 List<StringPtr>& ConfigurationSection::Values(const String& key) 00101 { 00102 if (! m_idx.ContainsKey(key)) 00103 { 00104 m_idx.Set(key, List<StringPtr>()); 00105 } 00106 return m_idx.GetRef(key); 00107 } 00108 00109 StringPtr ConfigurationSection::ToIniString() 00110 { 00111 StringBuffer ini; 00112 String nl(Environment::NewLine()); 00113 00114 ini.Append('['); 00115 ini.Append(m_name); 00116 ini.Append(']'); 00117 ini.Append(nl); 00118 00119 for(Hashtable<String, List<StringPtr> >::Iterator iter = m_idx.Begin(); iter.Next(); ) 00120 { 00121 for (List<StringPtr>::Iterator vals(iter.CurrentRef().Begin()); vals.Next(); ) 00122 { 00123 ini.Append(iter.CurrentKeyRef()); 00124 ini.Append('='); 00125 ini.Append(vals.Current()); 00126 ini.Append(nl); 00127 } 00128 } 00129 ini.Append(nl); 00130 00131 return ini.ToString(); 00132 } 00133 00134 StringPtr ConfigurationSection::ToXmlString() 00135 { 00136 StringBuffer xml; 00137 00138 xml.Append('<'); 00139 XmlText::XmlEncode(m_name, xml); 00140 xml.Append(">\n"); 00141 00142 for(Hashtable<String, List<StringPtr> >::Iterator iter = m_idx.Begin(); iter.Next(); ) 00143 { 00144 for (List<StringPtr>::Iterator vals(iter.CurrentRef().Begin()); vals.Next(); ) 00145 { 00146 xml.Append("<add key=\""); 00147 XmlText::XmlEncode(iter.CurrentKeyRef(), xml); 00148 xml.Append("\" value=\""); 00149 XmlText::XmlEncode(vals.Current(), xml); 00150 xml.Append("\" />\n"); 00151 } 00152 } 00153 00154 xml.Append("</"); 00155 XmlText::XmlEncode(m_name, xml); 00156 xml.Append('>'); 00157 xml.Append('\n'); 00158 00159 return xml.ToString(); 00160 } 00161 00162 #ifdef DEBUG 00163 void ConfigurationSection::ValidateMem() const 00164 { 00165 m_name.ValidateMem(); 00166 m_idx.ValidateMem(); 00167 } 00168 00169 void ConfigurationSection::CheckMem() const 00170 { 00171 m_name.CheckMem(); 00172 m_idx.CheckMem(); 00173 } 00174 #endif