Example #1
0
      /// <summary>Parses an input XML document into an output BPL object.</summary>
      public override bool Parse(String input) {
         Clear();
         Input = input;
         if (input.IsEmpty()) {
            AddError("Input XML string is empty");
            return false;
         }

         try {
            XDocument inputXml = null;
            using (var reader = XmlReader.Create(new StringReader(input), _getReaderSettings())) {
               inputXml = XDocument.Load(reader, PreserveErrorsInfo ? LoadOptions.SetLineInfo : LoadOptions.None);
            }
            var migrators = _customMigrators ?? _classMigrators;
            if (migrators != null) {
               foreach (var migrator in migrators) {
                  if (migrator(inputXml.Root)) WasMigrated = true;
               }
            }
            _mapping = new XmlNamespaceMap(inputXml);
            _resolver = new BplReferenceResolver();
            _resolver.CustomResolver = CustomResolver;

            if (inputXml.Root != null) {
               Output = _deserializeObject(inputXml.Root);
            }
            if (!Success) return false;

            if (ResolutionStrategy != BplResolutionStrategy.Manual) {
               var success = _resolver.Resolve();
               if (success || ResolutionStrategy == BplResolutionStrategy.Tolerant) {
                  Output.CompleteInitialization();
               } else {
                  foreach (var R in _resolver.Pending) {
                     AddError("Failed to resolve reference from '{0}' to '{1}' ('{2}' association).", R.Source, R.TargetId, R.Property.Name);
                  }
               }
            }
            Pending = _resolver.Pending;
            return true;
         } catch (Exception e) {
            AddException(e);
            return false;
         }
      }
Example #2
0
      private bool _parse(Hashtable inputJson, BplClass expectedClass) {
         try {
            _mapping = new XmlNamespaceMap();
            if (inputJson.Contains(NAMESPACE_ATTR)) {
               var inputMapping = inputJson[NAMESPACE_ATTR] as Hashtable;
               if (inputMapping != null) {
                  foreach (string prefix in inputMapping.Keys) {
                     var nsname = inputMapping[prefix] as string;
                     if (nsname.NotEmpty() && BplNamespace.Get(nsname) != null) {
                        _mapping.Add(prefix, nsname);
                     } else {
                        AddError("Invalid namespace '{0}'.", nsname);
                     }
                  }
               } else {
                  AddError("Invalid namespace mapping.");
               }
            }
            if (!Success) return false;

            _resolver = new BplReferenceResolver();
            _resolver.CustomResolver = CustomResolver;

            _errorPath = PreserveErrorsInfo ? new List<object>() : null;
            _errorDepth = 0;

            Output = _deserializeObject(inputJson, expectedClass);
            if (!Success) return false;

            if (ResolutionStrategy != BplResolutionStrategy.Manual) {
               var success = _resolver.Resolve();
               if (success || ResolutionStrategy == BplResolutionStrategy.Tolerant) {
                  Output.CompleteInitialization();
               } else {
                  foreach (var R in _resolver.Pending) {
                     AddError("Failed to resolve reference from '{0}' to '{1}' ('{2}' association).", R.Source, R.TargetId, R.Property.Name);
                  }
               }
            }
            Pending = _resolver.Pending;
            return true;
         } catch (Exception e) {
            AddException(e);
            return false;
         }
      }