/MediaPortal/Source/UI/SkinEngine/Xaml/ParserHelper.cs

https://github.com/Thorsten1982/MediaPortal-2 · C# · 117 lines · 61 code · 7 blank · 49 comment · 11 complexity · 32d46aba10e4f3fa9e1e12814e6dcec8 MD5 · raw file

  1. #region Copyright (C) 2007-2011 Team MediaPortal
  2. /*
  3. Copyright (C) 2007-2011 Team MediaPortal
  4. http://www.team-mediaportal.com
  5. This file is part of MediaPortal 2
  6. MediaPortal 2 is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. MediaPortal 2 is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with MediaPortal 2. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #endregion
  18. using System.Collections.Generic;
  19. using System;
  20. using MediaPortal.UI.SkinEngine.Xaml.Exceptions;
  21. using MediaPortal.UI.SkinEngine.Xaml.Interfaces;
  22. namespace MediaPortal.UI.SkinEngine.Xaml
  23. {
  24. public class ParserHelper
  25. {
  26. /// <summary>
  27. /// Skips all connected space characters in the specified <paramref name="expr"/>
  28. /// beginning at the specified <paramref name="index"/>.
  29. /// </summary>
  30. public static int SkipSpaces(string expr, int index)
  31. {
  32. while (index < expr.Length && expr[index] == ' ')
  33. index++;
  34. return index;
  35. }
  36. /// <summary>
  37. /// Parses a property index expression in the form <c>(sys:Int32)42,(sys:Int32)24,SampleString</c>,
  38. /// where sys is mapped to the system namespace.
  39. /// Inside indexers, multiple indexer parameters are separated by commas (,).
  40. /// The type of each parameter can be specified with parentheses.
  41. /// </summary>
  42. /// <param name="context">The current parser context.</param>
  43. /// <param name="expression">The index expression to parse. The index expression is
  44. /// the expression between the <c>[</c> and <c>]</c> characters, for example in
  45. /// <c>PropertyName[10,20]</c>, the index expression is <c>10,20</c>.</param>
  46. /// <returns>Object array containing index parameters from the
  47. /// <paramref name="expression"/>.</returns>
  48. public static object[] ParseIndexExpression(IParserContext context, string expression)
  49. {
  50. IList<object> indices = new List<object>();
  51. string[] indexStrings = expression.Split(new char[] {','});
  52. foreach (string s in indexStrings)
  53. {
  54. string valStr = s;
  55. Type explicitType = null;
  56. if (valStr.StartsWith("("))
  57. { // Explicit type is given
  58. int i = valStr.IndexOf(')');
  59. explicitType = ParseType(context, s.Substring(1, i - 1));
  60. valStr = valStr.Substring(i + 1);
  61. }
  62. object indexResult = valStr;
  63. if (explicitType != null)
  64. if (!TypeConverter.Convert(indexResult, explicitType, out indexResult))
  65. throw new XamlParserException("Could not convert '{0}' to type '{1}'", indexResult, explicitType.Name);
  66. indices.Add(indexResult);
  67. }
  68. object[] result = new object[indexStrings.Length];
  69. indices.CopyTo(result, 0);
  70. return result;
  71. }
  72. /// <summary>
  73. /// Given a string specifying a type, this method returns the type.
  74. /// </summary>
  75. /// <param name="context">The current document context.</param>
  76. /// <param name="typeName">Type string in the form <c>sys:Int32</c>, where the prefix
  77. /// is a prefix currently registered in the <paramref name="context"/>, and the suffix
  78. /// is a type known by the namespace handler specified by the prefix.</param>
  79. /// <returns>Type object for the specified type string.</returns>
  80. public static Type ParseType(IParserContext context, string typeName)
  81. {
  82. string localName;
  83. string namespaceURI;
  84. context.LookupNamespace(typeName, out localName, out namespaceURI);
  85. return context.GetNamespaceHandler(namespaceURI).GetElementType(localName);
  86. }
  87. /// <summary>
  88. /// Given an element parsed from a XAML file, this method unwraps the include's content and returns it,
  89. /// if it is an <see cref="IInclude"/>.
  90. /// </summary>
  91. /// <param name="maybeIncludeElement">Element parsed from a XAML file.</param>
  92. /// <returns>The given <paramref name="maybeIncludeElement"/> or its content, if it is an include.</returns>
  93. public static object UnwrapIncludesAndCleanup(object maybeIncludeElement)
  94. {
  95. IInclude include = maybeIncludeElement as IInclude;
  96. if (include == null)
  97. return maybeIncludeElement;
  98. object content = include.PassContent();
  99. object result = UnwrapIncludesAndCleanup(content);
  100. IDisposable disposable = include as IDisposable;
  101. if (disposable != null)
  102. disposable.Dispose();
  103. return result;
  104. }
  105. }
  106. }