Beispiel #1
0
 /// <summary>Parses the authority for the pre-parsed given Url.</summary>
 /// <remarks>Parses the authority for the pre-parsed given Url.</remarks>
 /// <param name="parsed">the pre-parsed Url.</param>
 private static void ParseAuthority(Url parsed)
 {
     // parse authority for unparsed relative network-path reference
     if (parsed.href.IndexOf(":") == -1 && parsed.href.IndexOf("//") == 0 && string.Empty
         .Equals(parsed.host))
     {
         // must parse authority from pathname
         parsed.pathname = JavaCompat.Substring(parsed.pathname, 2);
         var idx = parsed.pathname.IndexOf("/");
         if (idx == -1)
         {
             parsed.authority = parsed.pathname;
             parsed.pathname  = string.Empty;
         }
         else
         {
             parsed.authority = JavaCompat.Substring(parsed.pathname, 0, idx);
             parsed.pathname  = JavaCompat.Substring(parsed.pathname, idx);
         }
     }
     else
     {
         // construct authority
         parsed.authority = parsed.host;
         if (!string.Empty.Equals(parsed.auth))
         {
             parsed.authority = parsed.auth + "@" + parsed.authority;
         }
     }
 }
Beispiel #2
0
            /// <exception cref="JsonLD.Core.JsonLdError"></exception>
            private void AdvanceLineNumber()
            {
                var match = Regex.NextEoln.Matcher(line);

                if (match.Find())
                {
                    var split = match.Group(0).Split(string.Empty + Regex.Eoln);
                    lineNumber   += split.Length - 1;
                    linePosition += split[split.Length - 1].Length;
                    line          = JavaCompat.Substring(line, match.Group(0).Length);
                }
            }
Beispiel #3
0
        private string UnquoteString(string value)
        {
            if (value.StartsWith("\"\"\"") || value.StartsWith("'''"))
            {
                return(JavaCompat.Substring(value, 3, value.Length - 3));
            }

            if (value.StartsWith("\"") || value.StartsWith("'"))
            {
                return(JavaCompat.Substring(value, 1, value.Length - 1));
            }
            return(value);
        }
Beispiel #4
0
        /// <summary>
        ///     checks the URI for a prefix, and if one is found, set used prefixes to
        ///     true
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        private string GetURI(string uri)
        {
            // check for bnode
            if (uri.StartsWith("_:"))
            {
                return(uri);
            }
            foreach (var prefix in availableNamespaces.Keys)
            {
                if (uri.StartsWith(prefix))
                {
                    usedNamespaces.Add(prefix);

                    // return the prefixed URI
                    return(availableNamespaces[prefix] + ":" + JavaCompat.Substring(uri, prefix.Length));
                }
            }

            // return the full URI
            return("<" + uri + ">");
        }
Beispiel #5
0
            /// <exception cref="JsonLD.Core.JsonLdError"></exception>
            public virtual void AdvanceLinePosition(int len)
            {
                if (len > 0)
                {
                    linePosition += len;
                    line          = JavaCompat.Substring(line, len);
                }

                while (!string.Empty.Equals(line))
                {
                    // clear any whitespace
                    var match = Regex.CommentOrWs.Matcher(line);
                    if (match.Find() && match.Group(0).Length > 0)
                    {
                        var eoln = Regex.Eoln.Matcher(match.Group(0));
                        var end  = 0;
                        while (eoln.Find())
                        {
                            lineNumber += 1;
                            end         = eoln.End();
                        }

                        linePosition = match.Group(0).Length - end;
                        line         = JavaCompat.Substring(line, match.Group(0).Length);
                    }
                    else
                    {
                        break;
                    }
                }

                if (string.Empty.Equals(line) && !EndIsOK())
                {
                    throw new JsonLdError(JsonLdError.Error.ParseError,
                                          "Error while parsing Turtle; unexpected end of input. {line: "
                                          + lineNumber + ", position:" + linePosition + "}");
                }
            }
Beispiel #6
0
        public static string RemoveBase(JToken baseobj, string iri)
        {
            if (baseobj.IsNull())
            {
                return(iri);
            }
            Url @base;

            if (baseobj.Type == JTokenType.String)
            {
                @base = Parse((string)baseobj);
            }
            else
            {
                throw new Exception("Arrgggghhh!");
            }

            // establish base root
            var root = string.Empty;

            if (!string.Empty.Equals(@base.href))
            {
                root += @base.protocol + "//" + @base.authority;
            }
            else
            {
                // support network-path reference with empty base
                if (iri.IndexOf("//") != 0)
                {
                    root += "//";
                }
            }

            // IRI not relative to base
            if (iri.IndexOf(root) != 0)
            {
                return(iri);
            }

            // remove root from IRI and parse remainder
            var rel = Parse(JavaCompat.Substring(iri, root.Length));

            // remove path segments that match
            IList <string> baseSegments = new List <string>(@base
                                                            .normalizedPath.Split("/").ToList());

            baseSegments = baseSegments.Where(seg => seg != "").ToList();
            if (@base.normalizedPath.EndsWith("/"))
            {
                baseSegments.Add(string.Empty);
            }
            IList <string> iriSegments = new List <string>(rel.normalizedPath
                                                           .Split("/").ToList());

            iriSegments = iriSegments.Where(seg => seg != "").ToList();
            if (rel.normalizedPath.EndsWith("/"))
            {
                iriSegments.Add(string.Empty);
            }
            while (baseSegments.Count > 0 && iriSegments.Count > 0)
            {
                if (!baseSegments[0].Equals(iriSegments[0]))
                {
                    break;
                }
                if (baseSegments.Count > 0)
                {
                    baseSegments.RemoveAt(0);
                }
                if (iriSegments.Count > 0)
                {
                    iriSegments.RemoveAt(0);
                }
            }

            // use '../' for each non-matching base segment
            var rval = string.Empty;

            if (baseSegments.Count > 0)
            {
                // don't count the last segment if it isn't a path (doesn't end in
                // '/')
                // don't count empty first segment, it means base began with '/'
                if ([email protected]("/") || string.Empty.Equals(baseSegments[0]))
                {
                    baseSegments.RemoveAt(baseSegments.Count - 1);
                }
                for (var i = 0; i < baseSegments.Count; ++i)
                {
                    rval += "../";
                }
            }

            // prepend remaining segments
            if (iriSegments.Count > 0)
            {
                rval += iriSegments[0];
            }
            for (var i_1 = 1; i_1 < iriSegments.Count; i_1++)
            {
                rval += "/" + iriSegments[i_1];
            }

            // add query and hash
            if (!string.Empty.Equals(rel.query))
            {
                rval += "?" + rel.query;
            }
            if (!string.Empty.Equals(rel.hash))
            {
                rval += rel.hash;
            }
            if (string.Empty.Equals(rval))
            {
                rval = "./";
            }
            return(rval);
        }
Beispiel #7
0
        /// <summary>Removes a base IRI from the given absolute IRI.</summary>
        /// <remarks>Removes a base IRI from the given absolute IRI.</remarks>
        /// <param name="base">the base IRI.</param>
        /// <param name="iri">the absolute IRI.</param>
        /// <returns>the relative IRI if relative to base, otherwise the absolute IRI.</returns>
        private static string RemoveBase(JToken baseobj, string iri)
        {
            Url @base;

            if (IsString(baseobj))
            {
                @base = Url.Parse((string)baseobj);
            }
            else
            {
                @base = baseobj.Value <Url>();
            }

            // establish base root
            var root = string.Empty;

            if (!string.Empty.Equals(@base.href))
            {
                root += @base.protocol + "//" + @base.authority;
            }
            else
            {
                // support network-path reference with empty base
                if (iri.IndexOf("//") != 0)
                {
                    root += "//";
                }
            }

            // IRI not relative to base
            if (iri.IndexOf(root) != 0)
            {
                return(iri);
            }

            // remove root from IRI and parse remainder
            var rel = Url.Parse(JavaCompat.Substring(iri, root.Length));

            // remove path segments that match
            var baseSegments = _split(@base.normalizedPath, "/");
            var iriSegments  = _split(rel.normalizedPath, "/");

            while (baseSegments.Count > 0 && iriSegments.Count > 0)
            {
                if (!baseSegments[0].Equals(iriSegments[0]))
                {
                    break;
                }
                if (baseSegments.Count > 0)
                {
                    baseSegments.RemoveAt(0);
                }
                if (iriSegments.Count > 0)
                {
                    iriSegments.RemoveAt(0);
                }
            }

            // use '../' for each non-matching base segment
            var rval = string.Empty;

            if (baseSegments.Count > 0)
            {
                // don't count the last segment if it isn't a path (doesn't end in
                // '/')
                // don't count empty first segment, it means base began with '/'
                if ([email protected]("/") || string.Empty.Equals(baseSegments[0]))
                {
                    baseSegments.RemoveAt(baseSegments.Count - 1);
                }
                for (var i = 0; i < baseSegments.Count; ++i)
                {
                    rval += "../";
                }
            }

            // prepend remaining segments
            rval += _join(iriSegments, "/");

            // add query and hash
            if (!string.Empty.Equals(rel.query))
            {
                rval += "?" + rel.query;
            }
            if (!string.Empty.Equals(rel.hash))
            {
                rval += rel.hash;
            }
            if (string.Empty.Equals(rval))
            {
                rval = "./";
            }
            return(rval);
        }
Beispiel #8
0
        /// <summary>Prepends a base IRI to the given relative IRI.</summary>
        /// <remarks>Prepends a base IRI to the given relative IRI.</remarks>
        /// <param name="base">the base IRI.</param>
        /// <param name="iri">the relative IRI.</param>
        /// <returns>
        ///     the absolute IRI.
        ///     TODO: the Url class isn't as forgiving as the Node.js url parser,
        ///     we may need to re-implement the parser here to support the
        ///     flexibility required
        /// </returns>
        private static string PrependBase(JToken baseobj, string iri)
        {
            // already an absolute IRI
            if (iri.IndexOf(":") != -1)
            {
                return(iri);
            }

            // parse base if it is a string
            Url @base;

            if (IsString(baseobj))
            {
                @base = Url.Parse((string)baseobj);
            }
            else
            {
                @base = baseobj.Value <Url>();
            }
            var rel = Url.Parse(iri);

            // start hierarchical part
            var hierPart = @base.protocol;

            if (!string.Empty.Equals(rel.authority))
            {
                hierPart += "//" + rel.authority;
            }
            else
            {
                if (!string.Empty.Equals(@base.href))
                {
                    hierPart += "//" + @base.authority;
                }
            }

            // per RFC3986 normalize
            string path;

            // IRI represents an absolute path
            if (rel.pathname.IndexOf("/") == 0)
            {
                path = rel.pathname;
            }
            else
            {
                path = @base.pathname;

                // append relative path to the end of the last directory from base
                if (!string.Empty.Equals(rel.pathname))
                {
                    path = JavaCompat.Substring(path, 0, path.LastIndexOf("/") + 1);
                    if (path.Length > 0 && !path.EndsWith("/"))
                    {
                        path += "/";
                    }
                    path += rel.pathname;
                }
            }

            // remove slashes anddots in path
            path = Url.RemoveDotSegments(path, !string.Empty.Equals(hierPart));

            // add query and hash
            if (!string.Empty.Equals(rel.query))
            {
                path += "?" + rel.query;
            }
            if (!string.Empty.Equals(rel.hash))
            {
                path += rel.hash;
            }
            var rval = hierPart + path;

            if (string.Empty.Equals(rval))
            {
                return("./");
            }
            return(rval);
        }