/// <summary> /// Default ctor to build an empty container of the given flavor and given type /// </summary> public RDFContainer(RDFModelEnums.RDFContainerType containerType, RDFModelEnums.RDFItemType itemType) { this.ContainerType = containerType; this.ItemType = itemType; this.ReificationSubject = new RDFResource(); this.Items = new ArrayList(); }
/// <summary> /// List-based ctor to build a container of the given flavor and given type with given items /// </summary> public RDFContainer(RDFModelEnums.RDFContainerType containerType, RDFModelEnums.RDFItemType itemType, ArrayList items) : this(containerType, itemType) { if (items != null) { switch (this.ItemType) { case RDFModelEnums.RDFItemType.Resource: foreach (var item in items) { if (item is RDFResource) { this.AddItem(item); } else { throw new RDFModelException("Cannot create RDFContainer because all the elements must be resources."); } } break; case RDFModelEnums.RDFItemType.Literal: foreach (var item in items) { if (item is RDFLiteral) { this.AddItem(item); } else { throw new RDFModelException("Cannot create RDFContainer because all the elements must be literals."); } } break; } } }
/// <summary> /// Given an element representing a RDF container, iterates on its constituent elements /// to build its standard reification triples. /// </summary> internal static void ParseContainerElements(RDFModelEnums.RDFContainerType contType, XmlNode container, RDFResource subj, RDFResource pred, RDFGraph result) { //Attach the container as the blank object of the current pred RDFResource obj = new RDFResource(); result.AddTriple(new RDFTriple(subj, pred, obj)); //obj -> rdf:type -> rdf:[Bag|Seq|Alt] switch (contType) { case RDFModelEnums.RDFContainerType.Bag: result.AddTriple(new RDFTriple(obj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.BAG)); break; case RDFModelEnums.RDFContainerType.Seq: result.AddTriple(new RDFTriple(obj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.SEQ)); break; default: result.AddTriple(new RDFTriple(obj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.ALT)); break; } //Iterate on the container items if (container.HasChildNodes) { IEnumerator elems = container.ChildNodes.GetEnumerator(); List <String> elemVals = new List <String>(); while (elems != null && elems.MoveNext()) { XmlNode elem = (XmlNode)elems.Current; XmlAttribute elemUri = GetRdfResourceAttribute(elem); #region Container Resource Item //This is a container of resources if (elemUri != null) { //Sanitize eventual blank node value detected by presence of "nodeID" attribute if (elemUri.LocalName.Equals("nodeID", StringComparison.Ordinal)) { if (!elemUri.Value.StartsWith("bnode:")) { elemUri.Value = "bnode:" + elemUri.Value; } } //obj -> rdf:_N -> VALUE if (contType == RDFModelEnums.RDFContainerType.Alt) { if (!elemVals.Contains(elemUri.Value)) { elemVals.Add(elemUri.Value); result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), new RDFResource(elemUri.Value))); } } else { result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), new RDFResource(elemUri.Value))); } } #endregion #region Container Literal Item //This is a container of literals else { //Parse the literal contained in the item RDFLiteral literal = null; XmlAttribute attr = GetRdfDatatypeAttribute(elem); if (attr != null) { literal = new RDFTypedLiteral(elem.InnerText, RDFModelUtilities.GetDatatypeFromString(attr.InnerText)); } else { attr = GetXmlLangAttribute(elem); literal = new RDFPlainLiteral(elem.InnerText, (attr != null ? attr.InnerText : String.Empty)); } //obj -> rdf:_N -> VALUE if (contType == RDFModelEnums.RDFContainerType.Alt) { if (!elemVals.Contains(literal.ToString())) { elemVals.Add(literal.ToString()); result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), literal)); } } else { result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), literal)); } } #endregion } } }