You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2016 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features and fast professional support,
  7. pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
  8. ***********************************************************************************/
  9. using System;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Xml.Linq;
  13. namespace Xceed.Words.NET
  14. {
  15. internal static class XElementHelpers
  16. {
  17. /// <summary>
  18. /// Get value from XElement and convert it to enum
  19. /// </summary>
  20. /// <typeparam name="T">Enum type</typeparam>
  21. internal static T GetValueToEnum<T>( XElement element )
  22. {
  23. if( element == null )
  24. throw new ArgumentNullException( "element" );
  25. var value = element.Attribute( XName.Get( "val" ) ).Value;
  26. foreach( T e in Enum.GetValues( typeof( T ) ) )
  27. {
  28. var fi = typeof( T ).GetField( e.ToString() );
  29. if( fi.GetCustomAttributes( typeof( XmlNameAttribute ), false ).Count() == 0 )
  30. throw new Exception( String.Format( "Attribute 'XmlNameAttribute' is not assigned to {0} fields!", typeof( T ).Name ) );
  31. var a = ( XmlNameAttribute )fi.GetCustomAttributes( typeof( XmlNameAttribute ), false ).First();
  32. if( a.XmlName == value )
  33. return e;
  34. }
  35. throw new ArgumentException( "Invalid element value!" );
  36. }
  37. /// <summary>
  38. /// Convert value to xml string and set it into XElement
  39. /// </summary>
  40. /// <typeparam name="T">Enum type</typeparam>
  41. internal static void SetValueFromEnum<T>( XElement element, T value )
  42. {
  43. if( element == null )
  44. throw new ArgumentNullException( "element" );
  45. element.Attribute( XName.Get( "val" ) ).Value = GetXmlNameFromEnum<T>( value );
  46. }
  47. /// <summary>
  48. /// Return xml string for this value
  49. /// </summary>
  50. /// <typeparam name="T">Enum type</typeparam>
  51. internal static String GetXmlNameFromEnum<T>( T value )
  52. {
  53. if( value == null )
  54. throw new ArgumentNullException( "value" );
  55. var fi = typeof( T ).GetField( value.ToString() );
  56. if( fi.GetCustomAttributes( typeof( XmlNameAttribute ), false ).Count() == 0 )
  57. throw new Exception( String.Format( "Attribute 'XmlNameAttribute' is not assigned to {0} fields!", typeof( T ).Name ) );
  58. var a = ( XmlNameAttribute )fi.GetCustomAttributes( typeof( XmlNameAttribute ), false ).First();
  59. return a.XmlName;
  60. }
  61. }
  62. /// <summary>
  63. /// This attribute applied to enum's fields for definition their's real xml names in DocX file.
  64. /// </summary>
  65. /// <example>
  66. /// public enum MyEnum
  67. /// {
  68. /// [XmlName("one")] // This means, that xml element has 'val="one"'
  69. /// ValueOne,
  70. /// [XmlName("two")] // This means, that xml element has 'val="two"'
  71. /// ValueTwo
  72. /// }
  73. /// </example>
  74. [AttributeUsage( AttributeTargets.Field, Inherited = false, AllowMultiple = false )]
  75. internal sealed class XmlNameAttribute : Attribute
  76. {
  77. /// <summary>
  78. /// Real xml name
  79. /// </summary>
  80. public String XmlName
  81. {
  82. get; private set;
  83. }
  84. public XmlNameAttribute( String xmlName )
  85. {
  86. XmlName = xmlName;
  87. }
  88. }
  89. }