1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
#if !WindowsCE
using System;
using System.Collections.Specialized ;
using System.Xml;
namespace FlickrNet
{
/// <summary>
/// Configuration settings for the Flickr.Net API Library.
/// </summary>
/// <remarks>
/// <p>First, register the configuration section in the configSections section:</p>
/// <p><code><configSections>
/// <section name="flickrNet" type="FlickrNet.FlickrConfigurationManager,FlickrNet"/>
/// </configSections></code>
/// </p>
/// <p>
/// Next, include the following config section:
/// </p>
/// <p><code>
/// <flickrNet
/// apiKey="1234567890abc" // optional
/// secret="2134123" // optional
/// token="234234" // optional
/// cacheSize="1234" // optional, in bytes (defaults to 50 * 1024 * 1024 = 50MB)
/// cacheTimeout="[d.]HH:mm:ss" // optional, defaults to 1 day (1.00:00:00) - day component is optional
/// // e.g. 10 minutes = 00:10:00 or 1 hour = 01:00:00 or 2 days, 12 hours = 2.12:00:00
/// >
/// <proxy // proxy element is optional, but if included the attributes below are mandatory as mentioned
/// ipaddress="127.0.0.1" // mandatory
/// port="8000" // mandatory
/// username="username" // optional, but must have password if included
/// password="password" // optional, see username
/// domain="domain" // optional, used for Microsoft authenticated proxy servers
/// />
/// </flickrNet>
/// </code></p>
/// </remarks>
internal class FlickrConfigurationSettings
{
#region Private Variables
private string _apiKey;
private string _apiSecret;
private string _apiToken;
private int _cacheSize;
private TimeSpan _cacheTimeout = TimeSpan.MinValue;
private string _proxyAddress;
private int _proxyPort;
private bool _proxyDefined;
private string _proxyUsername;
private string _proxyPassword;
private string _proxyDomain;
private string _cacheLocation;
private bool _cacheDisabled;
private SupportedService _service;
#endregion
#region FlickrConfigurationSettings Constructor
/// <summary>
/// Loads FlickrConfigurationSettings with the settings in the config file.
/// </summary>
/// <param name="configNode">XmlNode containing the configuration settings.</param>
public FlickrConfigurationSettings(XmlNode configNode)
{
if( configNode == null ) throw new ArgumentNullException("configNode");
foreach(XmlAttribute attribute in configNode.Attributes)
{
switch(attribute.Name)
{
case "apiKey":
_apiKey = attribute.Value;
break;
case "secret":
_apiSecret = attribute.Value;
break;
case "token":
_apiToken = attribute.Value;
break;
case "cacheDisabled":
try
{
_cacheDisabled = bool.Parse(attribute.Value);
break;
}
catch(FormatException ex)
{
throw new System.Configuration.ConfigurationException("cacheDisbled should be \"true\" or \"false\"", ex, configNode);
}
case "cacheSize":
try
{
_cacheSize = int.Parse(attribute.Value);
break;
}
catch(FormatException ex)
{
throw new System.Configuration.ConfigurationException("cacheSize should be integer value", ex, configNode);
}
case "cacheTimeout":
try
{
_cacheTimeout = TimeSpan.Parse(attribute.Value);
break;
}
catch(FormatException ex)
{
throw new System.Configuration.ConfigurationException("cacheTimeout should be TimeSpan value ([d:]HH:mm:ss)", ex, configNode);
}
case "cacheLocation":
_cacheLocation = attribute.Value;
break;
case "service":
try
{
_service = (SupportedService)Enum.Parse(typeof(SupportedService), attribute.Value, true);
break;
}
catch(ArgumentException ex)
{
throw new System.Configuration.ConfigurationException("service must be one of the supported services (See SupportedServices enum)", ex, configNode);
}
default:
throw new System.Configuration.ConfigurationException(String.Format("Unknown attribute '{0}' in flickrNet node", attribute.Name), configNode);
}
}
foreach(XmlNode node in configNode.ChildNodes)
{
switch(node.Name)
{
case "proxy":
ProcessProxyNode(node, configNode);
break;
default:
throw new System.Configuration.ConfigurationException(String.Format("Unknown node '{0}' in flickrNet node", node.Name), configNode);
}
}
}
#endregion
#region ProcessProxyNode - Constructor Helper Method
private void ProcessProxyNode(XmlNode proxy, XmlNode configNode)
{
if( proxy.ChildNodes.Count > 0 )
throw new System.Configuration.ConfigurationException("proxy element does not support child elements");
_proxyDefined = true;
foreach(XmlAttribute attribute in proxy.Attributes)
{
switch(attribute.Name)
{
case "ipaddress":
_proxyAddress = attribute.Value;
break;
case "port":
try
{
_proxyPort = int.Parse(attribute.Value);
}
catch(FormatException ex)
{
throw new System.Configuration.ConfigurationException("proxy port should be integer value", ex, configNode);
}
break;
case "username":
_proxyUsername = attribute.Value;
break;
case "password":
_proxyPassword = attribute.Value;
break;
case "domain":
_proxyDomain = attribute.Value;
break;
default:
throw new System.Configuration.ConfigurationException(String.Format("Unknown attribute '{0}' in flickrNet/proxy node", attribute.Name), configNode);
}
}
if( _proxyAddress == null )
throw new System.Configuration.ConfigurationException("proxy ipaddress is mandatory if you specify the proxy element");
if( _proxyPort == 0 )
throw new System.Configuration.ConfigurationException("proxy port is mandatory if you specify the proxy element");
if( _proxyUsername != null && _proxyPassword == null )
throw new System.Configuration.ConfigurationException("proxy password must be specified if proxy username is specified");
if( _proxyUsername == null && _proxyPassword != null )
throw new System.Configuration.ConfigurationException("proxy username must be specified if proxy password is specified");
if( _proxyDomain != null && _proxyUsername == null )
throw new System.Configuration.ConfigurationException("proxy username/password must be specified if proxy domain is specified");
}
#endregion
#region Public Properties
/// <summary>
/// API key. Null if not present. Optional.
/// </summary>
public string ApiKey
{
get { return _apiKey; }
}
/// <summary>
/// Shared Secret. Null if not present. Optional.
/// </summary>
public string SharedSecret
{
get { return _apiSecret; }
}
/// <summary>
/// API token. Null if not present. Optional.
/// </summary>
public string ApiToken
{
get { return _apiToken; }
}
/// <summary>
/// Cache size in bytes. 0 if not present. Optional.
/// </summary>
public bool CacheDisabled
{
get { return _cacheDisabled; }
}
/// <summary>
/// Cache size in bytes. 0 if not present. Optional.
/// </summary>
public int CacheSize
{
get { return _cacheSize; }
}
/// <summary>
/// Cache timeout. Equals TimeSpan.MinValue is not present. Optional.
/// </summary>
public TimeSpan CacheTimeout
{
get { return _cacheTimeout; }
}
public string CacheLocation
{
get { return _cacheLocation; }
}
public SupportedService Service
{
get { return _service; }
}
/// <summary>
/// If the proxy is defined in the configuration section.
/// </summary>
public bool IsProxyDefined
{
get { return _proxyDefined; }
}
/// <summary>
/// If <see cref="IsProxyDefined"/> is true then this is mandatory.
/// </summary>
public string ProxyIPAddress
{
get { return _proxyAddress; }
}
/// <summary>
/// If <see cref="IsProxyDefined"/> is true then this is mandatory.
/// </summary>
public int ProxyPort
{
get { return _proxyPort; }
}
/// <summary>
/// The username for the proxy. Optional.
/// </summary>
public string ProxyUsername
{
get { return _proxyUsername; }
}
/// <summary>
/// The password for the proxy. Optional.
/// </summary>
public string ProxyPassword
{
get { return _proxyPassword; }
}
/// <summary>
/// The domain for the proxy. Optional.
/// </summary>
public string ProxyDomain
{
get { return _proxyDomain; }
}
#endregion
}
}
#endif
|