11 changed files with 296 additions and 13 deletions
@ -0,0 +1,52 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
using System; |
||||||
|
using System.Collections.ObjectModel; |
||||||
|
using ICSharpCode.Reporting.Globals; |
||||||
|
using ICSharpCode.Reporting.PageBuilder.ExportColumns; |
||||||
|
|
||||||
|
namespace ICSharpCode.Reporting.Exporter.Visitors |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Description of FormatVisitor.
|
||||||
|
/// </summary>
|
||||||
|
public class FormatVisitor: AbstractVisitor |
||||||
|
{ |
||||||
|
readonly Collection<ExportPage> pages; |
||||||
|
public FormatVisitor(Collection<ExportPage> pages) |
||||||
|
{ |
||||||
|
if (pages == null) |
||||||
|
throw new ArgumentNullException("pages"); |
||||||
|
this.pages = pages; |
||||||
|
Console.WriteLine("Start FormatVisitor"); |
||||||
|
} |
||||||
|
|
||||||
|
public override void Visit(ICSharpCode.Reporting.PageBuilder.ExportColumns.ExportPage page) |
||||||
|
{ |
||||||
|
foreach (var element in page.ExportedItems) { |
||||||
|
var ac = element as IAcceptor; |
||||||
|
ac.Accept(this); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override void Visit(ICSharpCode.Reporting.PageBuilder.ExportColumns.ExportContainer exportColumn) |
||||||
|
{ |
||||||
|
foreach (var element in exportColumn.ExportedItems) { |
||||||
|
var ac = element as IAcceptor; |
||||||
|
ac.Accept(this); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public override void Visit(ICSharpCode.Reporting.PageBuilder.ExportColumns.ExportText exportColumn) |
||||||
|
{ |
||||||
|
if (!String.IsNullOrEmpty(exportColumn.FormatString)) { |
||||||
|
Console.WriteLine("Format {0} - {1}",exportColumn.Name,exportColumn.Text); |
||||||
|
exportColumn.Text = StandardFormatter.FormatOutput(exportColumn.Text, |
||||||
|
exportColumn.FormatString, |
||||||
|
"System.Int16", |
||||||
|
"no format"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,150 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
using System; |
||||||
|
using System.Globalization; |
||||||
|
|
||||||
|
namespace ICSharpCode.Reporting.Globals |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Description of StandardFormatter.
|
||||||
|
/// </summary>
|
||||||
|
class StandardFormatter |
||||||
|
{ |
||||||
|
|
||||||
|
|
||||||
|
public static string FormatOutput(string valueToFormat,string format, |
||||||
|
string dataType, string nullValue ) |
||||||
|
{ |
||||||
|
if (String.IsNullOrEmpty(format)) { |
||||||
|
return valueToFormat; |
||||||
|
} |
||||||
|
|
||||||
|
if (String.IsNullOrEmpty(valueToFormat)) { |
||||||
|
return nullValue; |
||||||
|
} |
||||||
|
|
||||||
|
TypeCode typeCode = TypeHelper.TypeCodeFromString(dataType); |
||||||
|
return FormatItem(valueToFormat,format,typeCode,nullValue); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private static string FormatItem (string valueToFormat,string format, |
||||||
|
TypeCode typeCode,string nullValue) |
||||||
|
{ |
||||||
|
string retValue = String.Empty; |
||||||
|
|
||||||
|
switch (typeCode) { |
||||||
|
case TypeCode.Int16: |
||||||
|
case TypeCode.Int32: |
||||||
|
retValue = FormatIntegers (valueToFormat,format); |
||||||
|
break; |
||||||
|
case TypeCode.DateTime: |
||||||
|
retValue = FormatDate(valueToFormat,format); |
||||||
|
break; |
||||||
|
case TypeCode.Boolean: |
||||||
|
retValue = FormatBool (valueToFormat,format); |
||||||
|
break; |
||||||
|
case TypeCode.Decimal: |
||||||
|
retValue = FormatDecimal (valueToFormat,format); |
||||||
|
break; |
||||||
|
|
||||||
|
case TypeCode.Double: |
||||||
|
case TypeCode.Single: |
||||||
|
break; |
||||||
|
|
||||||
|
case TypeCode.String: |
||||||
|
case TypeCode.Char: |
||||||
|
retValue = valueToFormat; |
||||||
|
break; |
||||||
|
default: |
||||||
|
retValue = valueToFormat; |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
return retValue; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private static string FormatBool (string toFormat, string format) |
||||||
|
{ |
||||||
|
if (CheckValue(toFormat)) { |
||||||
|
bool b = bool.Parse (toFormat); |
||||||
|
return b.ToString (CultureInfo.CurrentCulture); |
||||||
|
} |
||||||
|
return toFormat; |
||||||
|
} |
||||||
|
|
||||||
|
private static string FormatIntegers(string toFormat, string format) |
||||||
|
{ |
||||||
|
string str = String.Empty; |
||||||
|
if (CheckValue (toFormat)) { |
||||||
|
try { |
||||||
|
int number = Int32.Parse (toFormat, |
||||||
|
System.Globalization.NumberStyles.Any, |
||||||
|
CultureInfo.CurrentCulture.NumberFormat); |
||||||
|
|
||||||
|
str = number.ToString (format,CultureInfo.CurrentCulture); |
||||||
|
} catch (System.FormatException e) { |
||||||
|
throw e; |
||||||
|
} |
||||||
|
return str; |
||||||
|
} else { |
||||||
|
str = (0.0M).ToString(CultureInfo.CurrentCulture); |
||||||
|
} |
||||||
|
return str; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private static string FormatDecimal(string toFormat, string format) |
||||||
|
{ |
||||||
|
string str = String.Empty; |
||||||
|
if (CheckValue (toFormat)) { |
||||||
|
try { |
||||||
|
decimal dec = Decimal.Parse(toFormat, |
||||||
|
System.Globalization.NumberStyles.Any, |
||||||
|
CultureInfo.CurrentCulture.NumberFormat); |
||||||
|
str = dec.ToString (format,CultureInfo.CurrentCulture); |
||||||
|
|
||||||
|
} catch (System.FormatException e) { |
||||||
|
throw e; |
||||||
|
} |
||||||
|
return str; |
||||||
|
} else { |
||||||
|
str = (0.0M).ToString(CultureInfo.CurrentCulture); |
||||||
|
} |
||||||
|
return str; |
||||||
|
} |
||||||
|
|
||||||
|
// http://stackoverflow.com/questions/4710455/i-need-code-to-validate-any-time-in-c-sharp-in-hhmmss-format
|
||||||
|
|
||||||
|
private static string FormatDate(string toFormat, string format) |
||||||
|
{ |
||||||
|
DateTime date; |
||||||
|
if (DateTime.TryParse(toFormat, out date)) |
||||||
|
{ |
||||||
|
string str = date.ToString(format, |
||||||
|
DateTimeFormatInfo.CurrentInfo); |
||||||
|
return str.Trim(); |
||||||
|
} |
||||||
|
|
||||||
|
TimeSpan time; |
||||||
|
bool valid = TimeSpan.TryParseExact(toFormat, |
||||||
|
"g", |
||||||
|
CultureInfo.CurrentCulture, |
||||||
|
out time); |
||||||
|
if (valid) { |
||||||
|
return time.ToString("g"); |
||||||
|
} |
||||||
|
return toFormat; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private static bool CheckValue (string toFormat) |
||||||
|
{ |
||||||
|
if (String.IsNullOrEmpty(toFormat)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,67 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
using System; |
||||||
|
using System.Data; |
||||||
|
|
||||||
|
namespace ICSharpCode.Reporting.Globals |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Description of TypeHelper.
|
||||||
|
/// </summary>
|
||||||
|
class TypeHelper |
||||||
|
{ |
||||||
|
public static DbType DbTypeFromStringRepresenation(string type) |
||||||
|
{ |
||||||
|
switch (type.ToLower()) |
||||||
|
{ |
||||||
|
case "int": |
||||||
|
return DbType.Int16; |
||||||
|
case "int16": |
||||||
|
return DbType.Int16; |
||||||
|
case "int32": |
||||||
|
return DbType.Int32; |
||||||
|
case "int64": |
||||||
|
return DbType.Int64; |
||||||
|
case "uint16": |
||||||
|
return DbType.UInt16; |
||||||
|
case "uint32": |
||||||
|
return DbType.UInt32; |
||||||
|
case "uint64": |
||||||
|
return DbType.UInt64; |
||||||
|
case "single": |
||||||
|
return DbType.Single; |
||||||
|
case "double": |
||||||
|
return DbType.Double; |
||||||
|
case "decimal": |
||||||
|
return DbType.Decimal; |
||||||
|
case "datetime" : |
||||||
|
return DbType.DateTime; |
||||||
|
case "datetime2" : |
||||||
|
return DbType.DateTime2; |
||||||
|
case "boolean" : |
||||||
|
return DbType.Boolean; |
||||||
|
case "nvarchar": |
||||||
|
return DbType.String; |
||||||
|
case "varchar": |
||||||
|
return DbType.AnsiString; |
||||||
|
case "binary": |
||||||
|
return DbType.Binary; |
||||||
|
case "currency": |
||||||
|
return DbType.Currency; |
||||||
|
case "guid": |
||||||
|
return DbType.Guid; |
||||||
|
case "xml": |
||||||
|
return DbType.Xml; |
||||||
|
default: |
||||||
|
return DbType.Object; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static TypeCode TypeCodeFromString (string type) { |
||||||
|
if (String.IsNullOrEmpty(type)) { |
||||||
|
throw new ArgumentNullException("type"); |
||||||
|
} |
||||||
|
return Type.GetTypeCode( Type.GetType(type)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue