private static Resource getContents(XElement content) { string contentType = SerializationUtil.StringValueOrNull(content.Attribute(XATOM_CONTENT_TYPE)); if (contentType != "text/xml" && contentType != "application/xml+fhir") { throw Error.Format("Bundle Entry should have contents of type 'text/xml'", XmlDomFhirReader.GetLineInfo(content)); } #if DEBUG if (contentType == "application/xml+fhir") { System.Diagnostics.Debug.WriteLine("Bundle Entry should have contents of type 'text/xml'", XmlDomFhirReader.GetLineInfo(content)); } #endif XElement resource = null; try { resource = content.Elements().Single(); } catch { throw Error.Format("Entry <content> node should have a single child: the resource", XmlDomFhirReader.GetLineInfo(content)); } return((Resource)(new ResourceReader(new XmlDomFhirReader(resource)).Deserialize())); }
private static UriLinkList getLinks(IEnumerable <XElement> links) { return(new UriLinkList( links.Select(el => new UriLinkEntry { Rel = SerializationUtil.StringValueOrNull(el.Attribute(XATOM_LINK_REL)), Uri = SerializationUtil.UriValueOrNull(el.Attribute(XATOM_LINK_HREF)) }))); }
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 IList<Tag> ParseTags(XmlReader xr) //{ // xr.MoveToContent(); // var taglist = (XElement)XElement.ReadFrom(xr); // if (taglist.Name == BundleXmlParser.XFHIRNS + TagListSerializer.TAGLIST_TYPE) // { // if (taglist.Elements().All(xe => xe.Name == BundleXmlParser.XFHIRNS + BundleXmlParser.XATOM_CATEGORY)) // return ParseTags(taglist.Elements()); // else // throw Error.Format("TagList contains unexpected child elements"); // } // else // throw Error.Format("Unexpected element name {0} found at start of TagList", taglist.Name); //} //internal static IList<Tag> ParseTags(JsonReader xr) //{ // var tagObj = JObject.Load(xr); // var tagType = tagObj[SerializationConfig.RESOURCETYPE_MEMBER_NAME]; // if(tagType == null || tagType.Value<string>() != TagListSerializer.TAGLIST_TYPE) // throw Error.Format("TagList should start with a resourceType member TagList"); // var categoryArray = tagObj[BundleXmlParser.XATOM_CATEGORY] as JArray; // if (categoryArray != null) // return ParseTags(categoryArray); // else // return new List<Tag>(); //} internal static IList <Tag> ParseTags(IEnumerable <XElement> tags) { var result = new List <Tag>(); if (tags != null) { foreach (var tag in tags) { var scheme = SerializationUtil.StringValueOrNull(tag.Attribute(BundleXmlParser.XATOM_CAT_SCHEME)); var term = SerializationUtil.StringValueOrNull(tag.Attribute(BundleXmlParser.XATOM_CAT_TERM)); var label = SerializationUtil.StringValueOrNull(tag.Attribute(BundleXmlParser.XATOM_CAT_LABEL)); result.Add(new Tag(term, scheme, label)); } } return(result); }
private static Resource getContents(XElement content) { string contentType = SerializationUtil.StringValueOrNull(content.Attribute(XATOM_CONTENT_TYPE)); if (contentType != "text/xml") { throw Error.Format("Entry should have contents of type 'text/xml'", XmlDomFhirReader.GetLineInfo(content)); } XElement resource = null; try { resource = content.Elements().Single(); } catch { throw Error.Format("Entry <content> node should have a single child: the resource", XmlDomFhirReader.GetLineInfo(content)); } return((Resource)(new ResourceReader(new XmlDomFhirReader(resource)).Deserialize())); }
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); }