Example #1
0
 private bool IsCurie(String value)
 {
     if (value.Contains(':'))
     {
         String prefix    = value.Substring(0, value.IndexOf(':'));
         String reference = (prefix.Length + 1 < value.Length ? value.Substring(value.IndexOf(':') + 1) : String.Empty);
         if (XmlSpecsHelper.IsNCName(prefix))
         {
             if (reference.Equals(String.Empty))
             {
                 return(true);
             }
             else if (IriSpecsHelper.IsIrelativeRef(reference))
             {
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(value.Equals(String.Empty) || IriSpecsHelper.IsIrelativeRef(value));
     }
 }
Example #2
0
        /// <summary>
        /// Gets the Data Type Uri of the given Node if it has a supported type
        /// </summary>
        /// <param name="n">Node</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// Only <see cref="ILiteralNode">ILiteralNode</see>'s can have a Data Type
        /// </para>
        /// <para>
        /// The function only returns the Data Type Uri (as a String) if the Data Type of the Literal is one of the supported Data Types
        /// </para>
        /// </remarks>
        public static String GetSupportedDataType(INode n)
        {
            if (n == null)
            {
                throw new RdfException("Data Type cannot be determined for Nulls");
            }

            switch (n.NodeType)
            {
            case NodeType.Blank:
            case NodeType.GraphLiteral:
            case NodeType.Uri:
                throw new RdfException("Data Type cannot be determined for non-Literal Nodes");

            case NodeType.Literal:
                ILiteralNode l = (ILiteralNode)n;
                if (l.DataType == null)
                {
                    if (!l.Language.Equals(String.Empty))
                    {
                        throw new RdfException("Literals with Language Specifiers do not have a Data Type");
                    }
                    else
                    {
                        return(XmlSchemaDataTypeString);
                    }
                }
                else
                {
                    String type = l.DataType.ToString();
                    if (XmlSpecsHelper.IsSupportedType(type))
                    {
                        return(type);
                    }
                    else
                    {
                        return(String.Empty);
                    }
                }

            default:
                throw new RdfException("Data Type cannot be determined for unknown Node types");
            }
        }
 /// <summary>
 /// Determines whether a QName is valid for use in RDF/XML.
 /// </summary>
 /// <param name="qname">QName.</param>
 /// <returns></returns>
 public static bool IsValidQName(String qname)
 {
     if (qname.Contains(":"))
     {
         String[] parts = qname.Split(':');
         if (parts[0].Length == 0)
         {
             // Empty Prefix is permitted
             return(XmlSpecsHelper.IsNCName(parts[1]));
         }
         else
         {
             return(XmlSpecsHelper.IsNCName(parts[0]) && XmlSpecsHelper.IsNCName(parts[1]));
         }
     }
     else
     {
         return(XmlSpecsHelper.IsNCName(qname));
     }
 }
Example #4
0
 /// <summary>
 /// Validates that an ID is a valid NCName
 /// </summary>
 /// <param name="value">ID Value to Test</param>
 /// <returns>True if the ID is valid</returns>
 /// <remarks>Actual Validation conditions on IDs are stricter than this and this is handled separately by the <see cref="ValidateID()">ValidateID</see> method</remarks>
 public static bool IsRdfID(String value)
 {
     //Must be a valid NCName as defined in the XML Namespace Specification
     //Which is itself defined in terms of the XML Specification
     return(XmlSpecsHelper.IsNCName(value));
 }
Example #5
0
        /// <summary>
        /// Determines whether a given String is a valid QName
        /// </summary>
        /// <param name="value">String to test</param>
        /// <returns></returns>
        public static bool IsValidQName(String value)
        {
            if (value.Contains(':'))
            {
                String ns, localname;
                ns        = value.Substring(0, value.IndexOf(':'));
                localname = value.Substring(value.IndexOf(':') + 1);

                //Namespace Validation
                if (!ns.Equals(String.Empty))
                {
                    //Allowed empty Namespace
                    if (ns.StartsWith("-"))
                    {
                        //Can't start with a -
                        return(false);
                    }
                    else
                    {
                        char[] nchars = ns.ToCharArray();
                        if (XmlSpecsHelper.IsNameStartChar(nchars[0]) && nchars[0] != '_')
                        {
                            if (nchars.Length > 1)
                            {
                                for (int i = 1; i < nchars.Length; i++)
                                {
                                    //Not a valid Name Char
                                    if (!XmlSpecsHelper.IsNameChar(nchars[i]))
                                    {
                                        return(false);
                                    }
                                    if (nchars[i] == '.')
                                    {
                                        return(false);
                                    }
                                }
                                //If we reach here the Namespace is OK
                            }
                            else
                            {
                                //Only 1 Character which was valid so OK
                            }
                        }
                        else
                        {
                            //Doesn't start with a valid Name Start Char
                            return(false);
                        }
                    }
                }

                //Local Name Validation
                if (!localname.Equals(String.Empty))
                {
                    //Allowed empty Local Name
                    char[] lchars = localname.ToCharArray();

                    if (XmlSpecsHelper.IsNameStartChar(lchars[0]))
                    {
                        if (lchars.Length > 1)
                        {
                            for (int i = 1; i < lchars.Length; i++)
                            {
                                //Not a valid Name Char
                                if (!XmlSpecsHelper.IsNameChar(lchars[i]))
                                {
                                    return(false);
                                }
                                if (lchars[i] == '.')
                                {
                                    return(false);
                                }
                            }
                            //If we reach here the Local Name is OK
                        }
                        else
                        {
                            //Only 1 Character which was valid so OK
                        }
                    }
                    else
                    {
                        //Not a valid Name Start Char
                        return(false);
                    }
                }

                //If we reach here then it's all valid
                return(true);
            }
            else
            {
                //Must contain a colon
                return(false);
            }
        }
Example #6
0
        private Uri ParseUri(RdfACoreParserContext context, String value, RdfACurieMode mode)
        {
            switch (mode)
            {
            case RdfACurieMode.Uri:
                //Resolve as a URI which may be relative
                return(this.ResolveUri(context, value));

            case RdfACurieMode.SafeCurieOrCurieOrUri:
                if (this.IsSafeCurie(value))
                {
                    //If a Safe CURIE must resolve as a CURIE ignoring if not resolvable
                    return(this.ResolveSafeCurie(context, value));
                }
                else
                {
                    //Otherwise try resolving as a CURIE and if not resolvable try as a URI
                    Uri u = this.ResolveCurie(context, value);
                    if (u == null)
                    {
                        //Try resolving as a URI
                        return(this.ResolveUri(context, value));
                    }
                    else
                    {
                        //Resolved as a CURIE
                        return(u);
                    }
                }

            case RdfACurieMode.TermOrCurieOrAbsUri:
                if (XmlSpecsHelper.IsNCName(value))
                {
                    //If a Term try resolving as a term and ignore if not resolvable
                    return(this.ResolveTerm(context, value));
                }
                else if (this.IsCurie(value))
                {
                    //If a CURIE try resolving as a CURIE first
                    Uri u = this.ResolveCurie(context, value);
                    if (u == null)
                    {
                        //If not resolvable as a CURIE try as an absolute URI
                        return(this.ResolveAbsoluteUri(value));
                    }
                    else
                    {
                        //Resolved as a CURIE
                        return(u);
                    }
                }
                else
                {
                    //Try resolving as an absolute URI
                    return(this.ResolveAbsoluteUri(value));
                }

            default:
                return(null);
            }
        }
Example #7
0
        private bool ParseProfileAttribute(RdfACoreParserContext context, IRdfAEvent evt)
        {
            foreach (Uri u in this.ParseUris(context, evt["profile"]))
            {
                try
                {
                    Graph g = new Graph();
#if !SILVERLIGHT
                    UriLoader.Load(g, u);
#else
                    throw new PlatformNotSupportedException("The @profile attribute is not currently supported under Silverlight/Windows Phone 7");
#endif

                    String prefixQuery = "PREFIX rdfa: <" + RdfAParser.RdfANamespace + "> SELECT SAMPLE(?prefix) AS ?NamespacePrefix SAMPLE(?uri) AS ?NamespaceURI WHERE { ?s rdfa:prefix ?prefix ; rdfa:uri ?uri } GROUP BY ?s HAVING (COUNT(?prefix) = 1 && COUNT(?uri) = 1)";
                    String termQuery   = "PREFIX rdfa: <" + RdfAParser.RdfANamespace + "> SELECT SAMPLE(?term) AS ?Term SAMPLE(?uri) AS ?URI WHERE {?s rdfa:term ?term ; rdfa:uri ?uri } GROUP BY ?s HAVING (COUNT(?term) = 1 && COUNT(?uri) = 1)";

                    //Namespace Mappings
                    Object results = g.ExecuteQuery(prefixQuery);
                    if (results is SparqlResultSet)
                    {
                        SparqlResultSet rset = (SparqlResultSet)results;
                        foreach (SparqlResult r in rset.Results)
                        {
                            INode prefixNode = r["NamespacePrefix"];
                            INode nsNode     = r["NamespaceURI"];
                            if (prefixNode.NodeType == NodeType.Literal && nsNode.NodeType == NodeType.Literal)
                            {
                                String prefix = ((ILiteralNode)prefixNode).Value.ToLower();
                                String ns     = ((ILiteralNode)nsNode).Value;
                                context.Namespaces.AddNamespace(prefix, new Uri(ns));
                            }
                        }
                    }

                    //Term Mappings
                    results = g.ExecuteQuery(termQuery);
                    if (results is SparqlResultSet)
                    {
                        SparqlResultSet rset = (SparqlResultSet)results;
                        foreach (SparqlResult r in rset.Results)
                        {
                            INode termNode = r["Term"];
                            INode uriNode  = r["URI"];
                            if (termNode.NodeType == NodeType.Literal && uriNode.NodeType == NodeType.Literal)
                            {
                                String term = ((ILiteralNode)termNode).Value;
                                String uri  = ((ILiteralNode)uriNode).Value;
                                if (XmlSpecsHelper.IsNCName(term))
                                {
                                    context.Terms.AddNamespace(term, new Uri(uri));
                                }
                            }
                        }
                    }

                    //Vocabulary Setting
                    INode vocabNode = g.GetTriplesWithPredicate(g.CreateUriNode(new Uri(RdfAParser.RdfANamespace + "vocabulary"))).Select(t => t.Object).FirstOrDefault();
                    if (vocabNode != null)
                    {
                        if (vocabNode.NodeType == NodeType.Literal)
                        {
                            context.DefaultVocabularyUri = new Uri(((ILiteralNode)vocabNode).Value);
                        }
                        else if (vocabNode.NodeType == NodeType.Uri)
                        {
                            context.DefaultVocabularyUri = ((IUriNode)vocabNode).Uri;
                        }
                    }
                }
                catch
                {
                    return(false);
                }
            }
            return(true);
        }