PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/WpfCSharp/AutodeskReCapClient/AdskReCapResponse.cs

https://github.com/PandazZ/Autodesk-ReCap-Samples
C# | 127 lines | 93 code | 14 blank | 20 comment | 26 complexity | 329f9c1ee7c32d3346c488e562e1bdde MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. // (C) Copyright 2014 by Autodesk, Inc.
  2. //
  3. // Permission to use, copy, modify, and distribute this software in
  4. // object code form for any purpose and without fee is hereby granted,
  5. // provided that the above copyright notice appears in all copies and
  6. // that both that copyright notice and the limited warranty and
  7. // restricted rights notice below appear in all supporting
  8. // documentation.
  9. //
  10. // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
  11. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
  12. // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
  13. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
  14. // UNINTERRUPTED OR ERROR FREE.
  15. //- Written by Cyrille Fauvel, Autodesk Developer Network (ADN)
  16. //- http://www.autodesk.com/joinadn
  17. //- April 20th, 2014
  18. //
  19. using System;
  20. using System.Collections.Generic;
  21. using System.ComponentModel;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25. using System.Xml;
  26. using System.Xml.Linq;
  27. using System.Dynamic;
  28. using Newtonsoft.Json;
  29. using Newtonsoft.Json.Linq;
  30. namespace Autodesk.ADN.Toolkit.ReCap {
  31. public class AdskReCapResponse : AdskDynamicDictionary {
  32. public AdskReCapResponse (XDocument doc) : base () {
  33. ProcessElement (doc.Element ("Response"), this) ;
  34. }
  35. public AdskReCapResponse (JObject obj) : base () {
  36. ProcessObject (obj, this) ;
  37. }
  38. public AdskReCapResponse (JArray obj) : base () {
  39. ProcessArray (obj, this) ;
  40. }
  41. #region Reading object utilities
  42. internal static void ProcessElement (XElement obj, AdskDynamicDictionary dict) {
  43. var elList =from el in obj.Elements () select el ;
  44. foreach ( XElement elt in elList ) {
  45. if ( elt.HasElements == false ) {
  46. dict.Dictionary [elt.Name.LocalName] =(string)elt ;
  47. } else {
  48. // Careful, might be an array
  49. bool isArray =(elt.Elements ().Count () != elt.Elements().Select (el => el.Name).Distinct ().Count ()) ;
  50. AdskDynamicDictionary subDict =new AdskDynamicDictionary () ;
  51. if ( isArray )
  52. ProcessElementArray (elt, subDict) ;
  53. else
  54. ProcessElement (elt, subDict) ;
  55. dict.Dictionary [elt.Name.LocalName] =subDict ;
  56. }
  57. }
  58. }
  59. internal static void ProcessElementArray (XElement obj, AdskDynamicDictionary dict) {
  60. var elList = from el in obj.Elements () select el ;
  61. int i =0 ;
  62. foreach ( XElement elt in elList ) {
  63. if ( elt.HasElements == false ) {
  64. dict.Dictionary [i.ToString ()] =(string)elt ;
  65. } else {
  66. // Careful, might be an array
  67. bool isArray =(elt.Elements ().Count () != elt.Elements().Select (el => el.Name).Distinct ().Count ()) ;
  68. AdskDynamicDictionary subDict =new AdskDynamicDictionary () ;
  69. ProcessElement (elt, subDict) ;
  70. if ( isArray )
  71. ProcessElementArray (elt, subDict) ;
  72. else
  73. ProcessElement (elt, subDict) ;
  74. dict.Dictionary [i.ToString ()] = subDict ;
  75. }
  76. i++ ;
  77. }
  78. }
  79. internal static void ProcessObject (JObject obj, AdskDynamicDictionary dict) {
  80. foreach ( KeyValuePair<string, JToken> pair in obj ) {
  81. if ( pair.Value.GetType () == typeof (JValue) ) {
  82. dict.Dictionary [pair.Key] =((JValue)pair.Value).Value ;
  83. } else if ( pair.Value.GetType () == typeof (JObject) ) {
  84. AdskDynamicDictionary subDict =new AdskDynamicDictionary () ;
  85. ProcessObject ((JObject)(pair.Value), subDict) ;
  86. dict.Dictionary [pair.Key] =subDict ;
  87. } else if ( pair.Value.GetType () == typeof (JArray) ) {
  88. AdskDynamicDictionary subDict =new AdskDynamicDictionary () ;
  89. ProcessArray ((JArray)(pair.Value), subDict) ;
  90. dict.Dictionary [pair.Key] =subDict ;
  91. }
  92. }
  93. }
  94. internal static void ProcessArray (JArray obj, AdskDynamicDictionary dict) {
  95. int i =0 ;
  96. foreach ( JToken item in obj ) {
  97. if ( item.GetType () == typeof (JValue) ) {
  98. dict.Dictionary [i.ToString ()] =((JValue)item).Value ;
  99. } else if ( item.GetType () == typeof (JObject) ) {
  100. AdskDynamicDictionary subDict =new AdskDynamicDictionary ();
  101. ProcessObject ((JObject)(item), subDict) ;
  102. dict.Dictionary [i.ToString ()] =subDict ;
  103. } else if ( item.GetType () == typeof (JArray) ) {
  104. AdskDynamicDictionary subDict =new AdskDynamicDictionary ();
  105. ProcessArray ((JArray)item, subDict) ;
  106. dict.Dictionary [i.ToString ()] =subDict ;
  107. }
  108. i++ ;
  109. }
  110. }
  111. #endregion
  112. }
  113. }