示例#1
0
 /// <summary>
 /// Normalizes the specified URI.
 /// </summary>
 /// <param name="uri">The URI.</param>
 /// <param name="prefix">The prefix.</param>
 /// <param name="localname">The localname.</param>
 /// <returns><c>true</c> if a normalization was successful; otherwise, <c>false</c>.</returns>
 public override bool Normalize(string uri, out string prefix, out string localname)
 {
     // performance optimization: check abbreviations can contain URI by checking its length
     if (uri.Length >= m_MinKeyLength && uri.Length <= m_MaxKeyLength &&
         m_Abbreviations.ContainsKey(uri))
     {
         prefix    = null;
         localname = (string)m_Abbreviations[uri];
         return(true);
     }
     else
     {
         return(m_OriginalNamespaceManager.Normalize(uri, out prefix, out localname));
     }
 }
        private string URI(Entity entity)
        {
            if (entity is Variable && ((Variable)entity).LocalName != null)
            {
                return("?" + ((Variable)entity).LocalName);
            }

            if (entity is BNode)
            {
                string name = ((BNode)entity).LocalName;
                if (name != null &&
                    (anonNameMap[name] == null || (BNode)anonNameMap[name] == entity) &&
                    !name.StartsWith("bnode"))
                {
                    return("_:" + name);
                }
                else if (anonNames[entity] != null)
                {
                    return((string)anonNames[entity]);
                }
                else
                {
                    string id = "_:bnode" + anonNames.Count;
                    anonNames[entity] = id;
                    return(id);
                }
            }

            string uri = entity.Uri;
            string effectiveBaseUri = BaseUri == null ? "#" : BaseUri;

            if (effectiveBaseUri != null && uri.StartsWith(effectiveBaseUri))
            {
                int  len = effectiveBaseUri.Length;
                bool ok  = true;
                for (int i = len; i < uri.Length; i++)
                {
                    if (!char.IsLetterOrDigit(uri[i]))
                    {
                        ok = false; break;
                    }
                }
                if (ok)
                {
                    return(":" + uri.Substring(len));
                }
            }
            if (Format == Formats.NTriples || ns == null)
            {
                return("<" + Escape(uri) + ">");
            }

            string ret = ns.Normalize(uri);

            if (ret[0] != '<')
            {
                return(ret);
            }

            return("<" + Escape(uri) + ">");
        }
示例#3
0
        private void Normalize(string uri, out string prefix, out string localname)
        {
            if (uri == "")
            {
                throw new InvalidOperationException("The empty URI cannot be used as an element node.");
            }

            if (BaseUri == null && uri.StartsWith("#"))
            {
                // This isn't quite right, but it prevents dieing
                // for something not uncommon in N3.  The hash
                // gets lost.
                prefix    = "";
                localname = uri.Substring(1);
                return;
            }

            if (ns.Normalize(uri, out prefix, out localname))
            {
                return;
            }

            // No namespace prefix was registered, so come up with something.

            int last = uri.LastIndexOfAny(normalizechars);

            if (last <= 0)
            {
                throw new InvalidOperationException("No namespace was registered and no prefix could be automatically generated for <" + uri + ">");
            }

            int prev = uri.LastIndexOfAny(normalizechars, last - 1);

            if (prev <= 0)
            {
                throw new InvalidOperationException("No namespace was registered and no prefix could be automatically generated for <" + uri + ">");
            }

            string n = uri.Substring(0, last + 1);

            localname = uri.Substring(last + 1);

            // TODO: Make sure the local name (here and anywhere in this
            // class) is a valid XML name.

            if (Namespaces.GetPrefix(n) != null)
            {
                prefix = Namespaces.GetPrefix(n);
                return;
            }

            prefix = uri.Substring(prev + 1, last - prev - 1);

            // Remove all non-xmlable (letter) characters.
            StringBuilder newprefix = new StringBuilder();

            foreach (char c in prefix)
            {
                if (char.IsLetter(c))
                {
                    newprefix.Append(c);
                }
            }
            prefix = newprefix.ToString();

            if (prefix.Length == 0 || prefix == "xmlns")
            {
                // There were no letters in the prefix or the prefix was "xmlns", which isn't valid!
                prefix = "ns";
            }

            if (Namespaces.GetNamespace(prefix) == null)
            {
                doc.DocumentElement.SetAttribute("xmlns:" + prefix, n);
                Namespaces.AddNamespace(n, prefix);
                return;
            }

            int ctr = 1;

            while (true)
            {
                if (Namespaces.GetNamespace(prefix + ctr) == null)
                {
                    prefix += ctr;
                    doc.DocumentElement.SetAttribute("xmlns:" + prefix, n);
                    Namespaces.AddNamespace(n, prefix);
                    return;
                }
                ctr++;
            }
        }