/*******************************************************************************
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and
* limitations under the License.
* *****************************************************************************
* __ _ _ ___
* ( )( \/\/ )/ __)
* /__\ \ / \__ \
* (_)(_) \/\/ (___/
*
* AWS SDK for .NET
* API Version: 2009-04-15
*/
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Amazon.SimpleDB.Util
{
///
/// Provides a collection of static functions to:
/// 1. Convert various values into strings that may be compared lexicographically
/// 2. Decode a Base64 Encoded string
/// 3. Decode an Amazon SimpleDB Attribute's properties
/// 4. Decode an Amazon SimpleDB Item's properties and constituent Item(s)
///
public static class AmazonSimpleDBUtil
{
///
/// Date format String, e.g. 2007-12-06T10:32:43.141-08:00
///
private static string dateFormat = "yyyy-MM-ddTHH:mm:ss.fffzzzz";
private static string base64Str = "base64";
///
/// Encodes positive integer value into a string by zero-padding it up to the specified number of digits.
///
///
/// For example, the integer 123 encoded with a 6 digit maximum would be represented as 000123
///
/// positive integer to be encoded
/// maximum number of digits in the largest value in the data set
/// A string representation of the zero-padded integer
public static string EncodeZeroPadding(int number, int maxNumDigits)
{
return number.ToString(CultureInfo.InvariantCulture).PadLeft(maxNumDigits, '0');
}
///
/// Encodes positive single-precision floating point value into a string by zero-padding it to the specified number of digits.
///
///
/// This function only zero-pads digits to the left of the decimal point.
///
/// For example, the value 123.456 encoded with a 6 digit maximum would be represented as 000123.456
///
/// positive floating point value to be encoded
/// maximum number of digits in the largest value in the data set
/// A string representation of the zero-padded floating point value
public static string EncodeZeroPadding(float number, int maxNumDigits)
{
string fltStr = number.ToString(CultureInfo.InvariantCulture);
int decPt = fltStr.IndexOf('.');
if (decPt == -1)
{
return fltStr.PadLeft(maxNumDigits, '0');
}
else
{
return fltStr.PadLeft(maxNumDigits + (fltStr.Length - decPt), '0');
}
}
///
/// Encodes real integer value into a string by offsetting and zero-padding
/// number up to the specified number of digits. Use this encoding method if the data
/// range set includes both positive and negative values.
///
///
/// For example, the integer value -123 offset by 1000 with a maximum of 6 digits would be:
/// -123 + 1000, padded to 6 digits: 000877
///
/// integer to be encoded
/// maximum number of digits in the largest absolute value in the data set
/// offset value, has to be greater than absolute value of any negative number in the data set.
/// A string representation of the integer
public static string EncodeRealNumberRange(int number, int maxNumDigits, int offsetValue)
{
return (number + offsetValue).ToString(CultureInfo.InvariantCulture).PadLeft(maxNumDigits, '0');
}
///
/// Encodes real float value into a string by offsetting and zero-padding
/// number up to the specified number of digits. Use this encoding method if the data
/// range set includes both positive and negative values.
///
///
/// For example, the floating point value -123.456 offset by 1000 with
/// a maximum of 6 digits to the left, and 4 to the right would be:
/// 0008765440
///
/// floating point value to be encoded
/// maximum number of digits left of the decimal point in the largest absolute value in the data set
/// maximum number of digits right of the decimal point in the largest absolute value in the data set, i.e. precision
/// offset value, has to be greater than absolute value of any negative number in the data set.
/// A string representation of the integer
public static string EncodeRealNumberRange(float number, int maxDigitsLeft, int maxDigitsRight, int offsetValue)
{
long shiftMultiplier = (long)Math.Pow(10, maxDigitsRight);
long shiftedNumber = (long)Math.Round((number + offsetValue) * shiftMultiplier);
return shiftedNumber.ToString(CultureInfo.InvariantCulture).PadLeft(maxDigitsLeft + maxDigitsRight, '0');
}
///
/// Decodes zero-padded positive float value from the string representation
///
/// zero-padded string representation of the float value
/// original float value
public static float DecodeZeroPaddingFloat(string value)
{
return float.Parse(value, CultureInfo.InvariantCulture);
}
///
/// Decodes zero-padded positive integer value from the string representation
///
/// zero-padded string representation of the integer
/// original integer value
public static int DecodeZeroPaddingInt(string value)
{
return int.Parse(value, CultureInfo.InvariantCulture);
}
///
/// Decodes float value from the string representation that was created by using encodeRealNumberRange(..) function.
///
/// string representation of the integer value
/// offset value that was used in the original encoding
/// original integer value
public static int DecodeRealNumberRangeInt(string value, int offsetValue)
{
return (int)(long.Parse(value, CultureInfo.InvariantCulture) - offsetValue);
}
///
/// Decodes float value from the string representation that was created by using encodeRealNumberRange(..) function.
///
/// string representation of the integer value
/// maximum number of digits left of the decimal point in the largest absolute
/// value in the data set (must be the same as the one used for encoding).
/// offset value that was used in the original encoding
/// original float value
public static float DecodeRealNumberRangeFloat(string value, int maxDigitsRight, int offsetValue)
{
return (float)(long.Parse(value, CultureInfo.InvariantCulture) / Math.Pow(10, maxDigitsRight) - offsetValue);
}
///
/// Encodes date value into string format that can be compared lexicographically
///
/// date value to be encoded
/// string representation of the date value
public static string EncodeDate(DateTime date)
{
return date.ToString(dateFormat, CultureInfo.InvariantCulture);
}
///
/// Decodes date value from the string representation created using encodeDate(..) function.
///
/// string representation of the date value
/// original date value
public static DateTime DecodeDate(string value)
{
return DateTime.ParseExact(value, dateFormat, CultureInfo.InvariantCulture);
}
///
/// Gets the Current Date as an ISO8601 formatted Timestamp
///
/// ISO8601 formatted current timestamp String
public static string FormattedCurrentTimestamp
{
get
{
return Amazon.Util.AWSSDKUtils.FormattedCurrentTimestampISO8601;
}
}
///
/// Decodes the base64 encoded properties of the Attribute.
/// The Name and/or Value properties of an Attribute can be base64 encoded.
///
/// The properties of this Attribute will be decoded
///
///
public static void DecodeAttribute(Amazon.SimpleDB.Model.Attribute inputAttribute)
{
if (null == inputAttribute)
{
throw new ArgumentNullException("inputAttribute", "The Attribute passed in was null");
}
string encoding = inputAttribute.AlternateNameEncoding;
if (null != encoding)
{
if (string.Equals(encoding, base64Str, StringComparison.OrdinalIgnoreCase))
{
// The Name is base64 encoded
inputAttribute.Name = AmazonSimpleDBUtil.DecodeBase64String(inputAttribute.Name);
inputAttribute.AlternateNameEncoding = "";
}
}
encoding = inputAttribute.AlternateValueEncoding;
if (null != encoding)
{
if (string.Equals(encoding, base64Str, StringComparison.OrdinalIgnoreCase))
{
// The Value is base64 encoded
inputAttribute.Value = AmazonSimpleDBUtil.DecodeBase64String(inputAttribute.Value);
inputAttribute.AlternateValueEncoding = "";
}
}
}
///
/// Decodes the base64 properties of every SimpleDB Attribute specified in
/// list of attributes specified as input.
///
/// The Attributes in this list will be decoded
///
///
///
public static void DecodeAttributes(List attributes)
{
if (attributes != null &&
attributes.Count > 0)
{
foreach (Amazon.SimpleDB.Model.Attribute at in attributes)
{
AmazonSimpleDBUtil.DecodeAttribute(at);
}
}
}
///
/// Decodes the base64 encoded members of the Item if necessary.
/// The Name property of an Item can be base64 encoded.
/// This method also decodes any encoded properties of the Attributes
/// associated with the Input Item.
///
/// The Item to be decoded
///
///
public static void DecodeItem(Amazon.SimpleDB.Model.Item inputItem)
{
if (null == inputItem)
{
throw new ArgumentNullException("inputItem", "The Item passed in was null");
}
string encoding = inputItem.AlternateNameEncoding;
if (null != encoding)
{
if (string.Equals(encoding, base64Str, StringComparison.OrdinalIgnoreCase))
{
// The Name is base64 encoded
inputItem.Name = AmazonSimpleDBUtil.DecodeBase64String(inputItem.Name);
inputItem.AlternateNameEncoding = "";
}
}
AmazonSimpleDBUtil.DecodeAttributes(inputItem.Attributes);
}
///
/// Decodes the base64 encoded members of the Item List.
///
/// The Item List to be decoded
///
///
///
public static void DecodeItems(List inputItems)
{
if (inputItems != null &&
inputItems.Count > 0)
{
foreach (Amazon.SimpleDB.Model.Item it in inputItems)
{
AmazonSimpleDBUtil.DecodeItem(it);
}
}
}
///
/// Returns the Base64 decoded version of the input string.
///
/// The Base64 encoded string
/// Decoded version of the Base64 input string
public static string DecodeBase64String(string encoded)
{
if (null == encoded)
{
throw new ArgumentNullException("encoded", "The Encoded String passed in was null");
}
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encoded);
return System.Text.UTF8Encoding.UTF8.GetString(encodedDataAsBytes, 0, encodedDataAsBytes.Length);
}
}
}