Example #1
0
        internal static Bundle Load(XmlReader reader)
        {
            XElement feed;

            var settings = new XmlReaderSettings();

            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;
            settings.IgnoreWhitespace             = true;

            try
            {
                var internalReader = XmlReader.Create(reader, settings);
                feed = XDocument.Load(internalReader, LoadOptions.SetLineInfo).Root;
                if (feed.Name != XNamespace.Get(ATOMPUB_NS) + "feed")
                {
                    throw Error.Format("Input data is not an Atom feed", null);
                }
            }
            catch (Exception exc)
            {
                throw Error.Format("Exception while loading feed: " + exc.Message, null);
            }

            Bundle result;

            try
            {
                result = new Bundle()
                {
                    Title       = SerializationUtil.StringValueOrNull(feed.Element(XATOMNS + XATOM_TITLE)),
                    LastUpdated = SerializationUtil.InstantOrNull(feed.Element(XATOMNS + XATOM_UPDATED)),
                    Id          = SerializationUtil.UriValueOrNull(feed.Element(XATOMNS + XATOM_ID)),
                    Links       = getLinks(feed.Elements(XATOMNS + XATOM_LINK)),
                    Tags        = TagListParser.ParseTags(feed.Elements(XATOMNS + XATOM_CATEGORY)),
                    AuthorName  = feed.Elements(XATOMNS + XATOM_AUTHOR).Count() == 0 ? null :
                                  SerializationUtil.StringValueOrNull(feed.Element(XATOMNS + XATOM_AUTHOR)
                                                                      .Element(XATOMNS + XATOM_AUTH_NAME)),
                    AuthorUri = feed.Elements(XATOMNS + XATOM_AUTHOR).Count() == 0 ? null :
                                SerializationUtil.StringValueOrNull(feed.Element(XATOMNS + XATOM_AUTHOR)
                                                                    .Element(XATOMNS + XATOM_AUTH_URI)),
                    TotalResults = SerializationUtil.IntValueOrNull(feed.Element(XOPENSEARCHNS + XATOM_TOTALRESULTS))
                };
            }
            catch (Exception exc)
            {
                throw Error.Format("Exception while parsing xml feed attributes: " + exc.Message, null);
            }

            result.Entries = loadEntries(feed.Elements().Where(elem =>
                                                               (elem.Name == XATOMNS + XATOM_ENTRY ||
                                                                elem.Name == XTOMBSTONE + XATOM_DELETED_ENTRY)), result);

            return(result);
        }
        internal static Bundle Load(JsonReader reader)
        {
            JObject feed;

            try
            {
                reader.DateParseHandling  = DateParseHandling.None;
                reader.FloatParseHandling = FloatParseHandling.Decimal;
                feed = JObject.Load(reader);

                if (feed.Value <string>(JsonDomFhirReader.RESOURCETYPE_MEMBER_NAME) != "Bundle")
                {
                    throw Error.Format("Input data is not an json FHIR bundle", null);
                }
            }
            catch (Exception exc)
            {
                throw Error.Format("Exception while parsing feed: " + exc.Message, null);
            }

            Bundle result;

            try
            {
                result = new Bundle()
                {
                    Title       = feed.Value <string>(BundleXmlParser.XATOM_TITLE),
                    LastUpdated = instantOrNull(feed[BundleXmlParser.XATOM_UPDATED]),
                    Id          = SerializationUtil.UriValueOrNull(feed[BundleXmlParser.XATOM_ID]),
                    Links       = getLinks(feed[BundleXmlParser.XATOM_LINK]),
                    Tags        = TagListParser.ParseTags(feed[BundleXmlParser.XATOM_CATEGORY]),
                    AuthorName  = feed[BundleXmlParser.XATOM_AUTHOR] as JArray != null ?
                                  feed[BundleXmlParser.XATOM_AUTHOR]
                                  .Select(auth => auth.Value <string>(BundleXmlParser.XATOM_AUTH_NAME))
                                  .FirstOrDefault()
                                : null,
                    AuthorUri = feed[BundleXmlParser.XATOM_AUTHOR] as JArray != null ?
                                feed[BundleXmlParser.XATOM_AUTHOR]
                                .Select(auth => auth.Value <string>(BundleXmlParser.XATOM_AUTH_URI))
                                .FirstOrDefault() : null,
                    TotalResults = intValueOrNull(feed[BundleXmlParser.XATOM_TOTALRESULTS])
                };
            }
            catch (Exception exc)
            {
                throw Error.Format("Exception while parsing json feed attributes: " + exc.Message, null);
            }

            var entries = feed[BundleXmlParser.XATOM_ENTRY];

            if (entries != null)
            {
                if (!(entries is JArray))
                {
                    throw Error.Format("The json feed contains a single entry, instead of an array", null);
                }

                result.Entries = loadEntries((JArray)entries, result);
            }

            return(result);
        }
Example #3
0
        private static BundleEntry loadEntry(XElement entry)
        {
            BundleEntry result;

            try
            {
                if (entry.Name == XTOMBSTONE + XATOM_DELETED_ENTRY)
                {
                    result    = new DeletedEntry();
                    result.Id = SerializationUtil.UriValueOrNull(entry.Attribute(XATOM_DELETED_REF));
                }
                else
                {
                    XElement content = entry.Element(XATOMNS + XATOM_CONTENT);
                    var      id      = SerializationUtil.UriValueOrNull(entry.Element(XATOMNS + XATOM_ID));

                    if (content != null)
                    {
                        var parsed = getContents(content);
                        if (parsed != null)
                        {
                            result = ResourceEntry.Create(parsed);
                        }
                        else
                        {
                            throw Error.Format("BundleEntry has a content element without content", XmlDomFhirReader.GetLineInfo(content));
                        }
                    }
                    else
                    {
                        result = SerializationUtil.CreateResourceEntryFromId(id);
                    }

                    result.Id = id;
                }

                result.Links = getLinks(entry.Elements(XATOMNS + XATOM_LINK));
                result.Tags  = TagListParser.ParseTags(entry.Elements(XATOMNS + XATOM_CATEGORY));

                if (result is DeletedEntry)
                {
                    ((DeletedEntry)result).When = SerializationUtil.InstantOrNull(entry.Attribute(XATOM_DELETED_WHEN));
                }
                else
                {
                    ResourceEntry re = (ResourceEntry)result;
                    re.Title       = SerializationUtil.StringValueOrNull(entry.Element(XATOMNS + XATOM_TITLE));
                    re.LastUpdated = SerializationUtil.InstantOrNull(entry.Element(XATOMNS + XATOM_UPDATED));
                    re.Published   = SerializationUtil.InstantOrNull(entry.Element(XATOMNS + XATOM_PUBLISHED));
                    re.AuthorName  = entry.Elements(XATOMNS + XATOM_AUTHOR).Count() == 0 ? null :
                                     SerializationUtil.StringValueOrNull(entry.Element(XATOMNS + XATOM_AUTHOR)
                                                                         .Element(XATOMNS + XATOM_AUTH_NAME));
                    re.AuthorUri = entry.Elements(XATOMNS + XATOM_AUTHOR).Count() == 0 ? null :
                                   SerializationUtil.StringValueOrNull(entry.Element(XATOMNS + XATOM_AUTHOR)
                                                                       .Element(XATOMNS + XATOM_AUTH_URI));
                }
            }
            catch (Exception exc)
            {
                throw Error.Format("Exception while reading entry: " + exc.Message, XmlDomFhirReader.GetLineInfo(entry));
            }

            return(result);
        }
        private static BundleEntry loadEntry(JObject entry)
        {
            BundleEntry result;

            try
            {
                if (entry[JATOM_DELETED] != null)
                {
                    result    = new DeletedEntry();
                    result.Id = SerializationUtil.UriValueOrNull(entry[BundleXmlParser.XATOM_ID]);
                }
                else
                {
                    var content = entry[BundleXmlParser.XATOM_CONTENT];

                    var id = SerializationUtil.UriValueOrNull(entry[BundleXmlParser.XATOM_ID]);
                    if (id == null)
                    {
                        throw Error.Format("BundleEntry found without an id", null);
                    }

                    if (content != null)
                    {
                        var parsed = getContents(content);
                        if (parsed != null)
                        {
                            result = ResourceEntry.Create(parsed);
                        }
                        else
                        {
                            throw Error.Format("BundleEntry {0} has a content element without content", null, id);
                        }
                    }
                    else
                    {
                        result = SerializationUtil.CreateResourceEntryFromId(id);
                    }

                    result.Id = id;
                }

                result.Links = getLinks(entry[BundleXmlParser.XATOM_LINK]);
                result.Tags  = TagListParser.ParseTags(entry[BundleXmlParser.XATOM_CATEGORY]);

                if (result is DeletedEntry)
                {
                    ((DeletedEntry)result).When = instantOrNull(entry[JATOM_DELETED]);
                }
                else
                {
                    var re = (ResourceEntry)result;
                    re.Title       = entry.Value <string>(BundleXmlParser.XATOM_TITLE);
                    re.LastUpdated = instantOrNull(entry[BundleXmlParser.XATOM_UPDATED]);
                    re.Published   = instantOrNull(entry[BundleXmlParser.XATOM_PUBLISHED]);
                    re.AuthorName  = entry[BundleXmlParser.XATOM_AUTHOR] as JArray != null ?
                                     entry[BundleXmlParser.XATOM_AUTHOR]
                                     .Select(auth => auth.Value <string>(BundleXmlParser.XATOM_AUTH_NAME))
                                     .FirstOrDefault() : null;
                    re.AuthorUri = entry[BundleXmlParser.XATOM_AUTHOR] as JArray != null ?
                                   entry[BundleXmlParser.XATOM_AUTHOR]
                                   .Select(auth => auth.Value <string>(BundleXmlParser.XATOM_AUTH_URI))
                                   .FirstOrDefault() : null;
                }
            }
            catch (Exception exc)
            {
                throw Error.Format("Exception while reading entry: " + exc.Message, null);
            }

            return(result);
        }