Beispiel #1
0
        INode IXmlNodeProcessor <INode> .ProcessElement <TProvider>(TProvider provider, INode baseNode, Uri originalBaseUri, INode defaultNamespace, ContentIterator <INode> content)
        {
            var  empty    = provider.IsEmpty;
            bool sameBase = provider.BaseUri == originalBaseUri;

            Action <INode> elementInit = null;
            var            elementType = CreateElementType(provider, defaultNamespace);

            elementInit += n => rdf.HandleTriple(n, a, elementType);

            string       id            = null;
            INode        innerBaseNode = baseNode;
            XmlValueInfo info          = default;

            while (provider.MoveToNextAttribute())
            {
                if (provider.IsDefault && !ExportDefault)
                {
                    continue;
                }
                var property = CreateAttributeType(provider, elementType);
                var value    = CreateAttributeValue(provider);
                elementInit += n => rdf.HandleTriple(n, property, value);

                if (value is ILiteralNode literal && UriComparer.Equals(literal.DataType, ID))
                {
                    id = XmlConvert.VerifyNCName(provider.Value);
                }
Beispiel #2
0
 protected string GetXmlValue(INode node)
 {
     if (node is ILiteralNode literal && UriComparer.Equals(literal.DataType, XMLLiteral))
     {
         return(literal.Value);
     }
     return(null);
 }
Beispiel #3
0
 protected string GetStringValue(INode node)
 {
     if (node is ILiteralNode literal && !UriComparer.Equals(literal.DataType, XMLLiteral))
     {
         return(literal.Value);
     }
     throw new NotImplementedException();
 }
Beispiel #4
0
            private IEnumerable <NodeInfo> ExpandNodes(INode node)
            {
                if (node == null)
                {
                    yield break;
                }

                // Comments on a node are turned to XML comments or PIs
                foreach (var comment in Context.FindObject(node, comment).OfType <ILiteralNode>())
                {
                    var type = comment.DataType;
                    if (type != null && !UriComparer.Equals(type, xstring) && !UriComparer.Equals(type, langString))
                    {
                        yield return(new NodeInfo(XPathNodeType.ProcessingInstruction, comment));
                    }
                    else
                    {
                        yield return(new NodeInfo(XPathNodeType.Comment, comment));
                    }
                }

                // Non-empty literal value is turned into text or significant whitespace
                if (node is ILiteralNode literal)
                {
                    if (!String.IsNullOrEmpty(literal.Value))
                    {
                        var whitespace = XmlConvert.VerifyWhitespace(literal.Value) != null;
                        yield return(new NodeInfo(whitespace ? XPathNodeType.SignificantWhitespace : XPathNodeType.Text, node));
                    }
                    yield break;
                }

                var elementName = Context.FindObject(node, a).Select(GetQualifiedName).FirstOrDefault(NotNull);

                if (elementName != null)
                {
                    // The node is an element; it will have its own node
                    yield return(new NodeInfo(XPathNodeType.Element, node));
                }
                else
                {
                    foreach (var value in Context.FindObject(node, value))
                    {
                        // Try to see if any other value is expandable
                        var enumerator = ExpandNodes(value).GetEnumerator();
                        if (enumerator.MoveNext())
                        {
                            yield return(enumerator.Current);

                            while (enumerator.MoveNext())
                            {
                                yield return(enumerator.Current);
                            }
                            break;
                        }
                    }
                }
            }
Beispiel #5
0
 public string FindPrefix(Uri ns)
 {
     if (UriComparer.Equals(ns, LocalNamespace))
     {
         return("");
     }
     try
     {
         return(Graph.NamespaceMap.GetPrefix(UriTools.GetNamespacePrefix(ns)));
     }catch (RdfException)
     {
         return(null);
     }
 }
Beispiel #6
0
            protected XmlQualifiedName GetQualifiedName(INode node)
            {
                var ncname = Context.FindObject(node, label).OfType <ILiteralNode>().FirstOrDefault(IsNCName);

                if (ncname != null)
                {
                    var ns = Context.FindObject(node, isDefinedBy).OfType <IUriNode>().FirstOrDefault();
                    if (ns != null)
                    {
                        if (UriComparer.Equals(ns.Uri, Context.LocalNamespace) || UriComparer.Equals(Context.FindObject(ns, isDefinedBy).OfType <IUriNode>().FirstOrDefault()?.Uri, Context.LocalNamespace))
                        {
                            return(new XmlQualifiedName(ncname.Value));
                        }
                        else
                        {
                            return(new XmlQualifiedName(ncname.Value, ns.Uri.GetString()));
                        }
                    }
                }
                return(null);
            }
Beispiel #7
0
 internal static bool sameTerm(Uri uri1, Uri uri2)
 {
     return(uriComparer.Equals(uri1, uri2));
 }
Beispiel #8
0
        private bool WriteValue(XmlWriter writer, INode node, Context context)
        {
            if (node == null)
            {
                return(true);
            }

            // If raw XML value is provided, use that
            var literalValue = graph.FindObject(node, label).OfType <ILiteralNode>().FirstOrDefault(n => UriComparer.Equals(n.DataType, XMLLiteral) == true);

            if (literalValue != null)
            {
                writer.WriteRaw(literalValue.Value);
                return(true);
            }

            // Write all comments first
            foreach (var comment in graph.FindObject(node, comment).OfType <ILiteralNode>())
            {
                if (comment.DataType != null)
                {
                    var piName = GetXmlName(graph.CreateUriNode(comment.DataType), context.DocumentNode, null);
                    if (piName != null)
                    {
                        writer.WriteProcessingInstruction(piName.Name, comment.Value);
                        continue;
                    }
                }
                writer.WriteComment(comment.Value);
            }

            if (node is ILiteralNode literal)
            {
                if (UriComparer.Equals(literal.DataType, XMLLiteral))
                {
                    writer.WriteRaw(literal.Value);
                }
                else
                {
                    writer.WriteString(literal.Value);
                }
                return(true);
            }

            var(elementType, elementName) = graph.FindObject(node, a).Select(t => (t, n: GetXmlName(t, context.DocumentNode, null))).FirstOrDefault(t => t.n != null);
            if (elementName != null)
            {
                if (context.FindRoot)
                {
                    throw new RootNameSignal(elementName.Name);
                }
                writer.WriteStartElement(null, elementName.Name, elementName.Namespace);

                foreach (var(pred, obj) in graph.FindPredicateObject(node))
                {
                    var attributeName = GetXmlName(pred, context.DocumentNode, elementType);
                    if (attributeName != null)
                    {
                        if (!(obj is ILiteralNode value))
                        {
                            value = graph.FindPredicateObject(obj).Select(t => t.obj).OfType <ILiteralNode>().FirstOrDefault(l => UriComparer.Equals(l.DataType, ID));
                            if (value == null)
                            {
                                continue;
                            }
                        }
                        writer.WriteAttributeString(attributeName.Name, attributeName.Namespace, value.Value);
                    }
                }
            }
            try{
                foreach (var value in GetValues(node))
                {
                    if (WriteValue(writer, value, context))
                    {
                        return(true);
                    }
                }
                var list = EnumerateList(node);
                if (list != null)
                {
                    foreach (var element in list)
                    {
                        if (!WriteValue(writer, element, context))
                        {
                            WriteFallback(writer, element, context);
                        }
                    }
                    return(true);
                }
                bool any = false;
                foreach (var member in graph.FindObject(node, member))
                {
                    if (WriteValue(writer, member, context))
                    {
                        any = true;
                    }
                }
                if (elementName != null)
                {
                    if (!any)
                    {
                        WriteFallback(writer, node, context);
                    }
                    return(true);
                }
                return(any);
            }finally{
                if (elementName != null)
                {
                    writer.WriteEndElement();
                }
            }
        }
Beispiel #9
0
 private XmlQualifiedName GetXmlName(INode node, INode documentNode, INode elementType)
 {
     if (graph.FindObject(node, label).OfType <ILiteralNode>().FirstOrDefault(n => UriComparer.Equals(n.DataType, NCName))?.Value is string name)
     {
         var ns = graph.FindObject(node, isDefinedBy).OfType <IUriNode>().FirstOrDefault();
         if (ns != null && !ns.Equals(elementType) && !graph.ContainsTriple(ns, isDefinedBy, documentNode))
         {
             return(new XmlQualifiedName(name, ns.Uri.GetString()));
         }
         return(new XmlQualifiedName(name));
     }
     return(null);
 }
Beispiel #10
0
 private bool IsNCName(ILiteralNode node)
 {
     return(UriComparer.Equals(node.DataType, NCName));
 }
Beispiel #11
0
        public void EqualsShouldBeEquivalentToUriCompare()
        {
            var target = new UriComparer();
            var url1 = new Uri( "about:blank" );
            var url2 = new Uri( "about:Blank" );
            var url3 = new Uri( "http://www.tempuri.org" );

            Assert.Equal( Uri.Compare( null, null, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ) == 0, target.Equals( null, null ) );
            Assert.Equal( Uri.Compare( url1, null, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ) == 0, target.Equals( url1, null ) );
            Assert.Equal( Uri.Compare( null, url2, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ) == 0, target.Equals( null, url2 ) );

            Assert.Equal( Uri.Compare( url1, url2, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ) == 0, target.Equals( url1, url2 ) );
            Assert.Equal( Uri.Compare( url1, url3, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase ) == 0, target.Equals( url1, url3 ) );

            target = new UriComparer( UriComponents.AbsoluteUri, UriFormat.Unescaped, false );
            Assert.Equal( Uri.Compare( url1, url2, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.Ordinal ) == 0, target.Equals( url1, url2 ) );
            Assert.Equal( Uri.Compare( url1, url3, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.Ordinal ) == 0, target.Equals( url1, url3 ) );

            target = new UriComparer( UriComponents.Scheme, UriFormat.SafeUnescaped );
            Assert.Equal( Uri.Compare( url1, url2, UriComponents.Scheme, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase ) == 0, target.Equals( url1, url2 ) );
            Assert.Equal( Uri.Compare( url1, url3, UriComponents.Scheme, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase ) == 0, target.Equals( url1, url3 ) );

            target = new UriComparer( UriComponents.Scheme, UriFormat.SafeUnescaped, false );
            Assert.Equal( Uri.Compare( url1, url2, UriComponents.Scheme, UriFormat.SafeUnescaped, StringComparison.Ordinal ) == 0, target.Equals( url1, url2 ) );
            Assert.Equal( Uri.Compare( url1, url3, UriComponents.Scheme, UriFormat.SafeUnescaped, StringComparison.Ordinal ) == 0, target.Equals( url1, url3 ) );
        }