Exemplo n.º 1
0
        /// <summary>
        /// Gives the collection of "xmlns" attributes of the "rdf:RDF" root node
        /// </summary>
        internal static XmlAttributeCollection GetXmlnsNamespaces(XmlNode rdfRDF, XmlNamespaceManager nsMgr)
        {
            XmlAttributeCollection xmlns = rdfRDF.Attributes;

            if (xmlns != null && xmlns.Count > 0)
            {
                IEnumerator iEnum = xmlns.GetEnumerator();
                while (iEnum != null && iEnum.MoveNext())
                {
                    XmlAttribute attr = (XmlAttribute)iEnum.Current;
                    if (attr.LocalName.ToUpperInvariant() != "XMLNS")
                    {
                        //Try to resolve the current namespace against the namespace register;
                        //if not resolved, create new namespace with scope limited to actual node
                        RDFNamespace ns =
                            (RDFNamespaceRegister.GetByPrefix(attr.LocalName) ??
                             RDFNamespaceRegister.GetByNamespace(attr.Value) ??
                             new RDFNamespace(attr.LocalName, attr.Value));

                        nsMgr.AddNamespace(ns.Prefix, ns.Namespace.ToString());
                    }
                }
            }
            return(xmlns);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finds if the given token contains a recognizable namespace and, if so, abbreviates it with its prefix.
        /// It also prepares the result in a format useful for serialization.
        /// </summary>
        internal static String AbbreviateNamespace(String token)
        {
            //Null or Space token: it's a trick, give empty result
            if (token == null || token.Trim() == String.Empty)
            {
                return(String.Empty);
            }

            //Blank token: abbreviate it with "_"
            if (token.StartsWith("bnode:"))
            {
                return(token.Replace("bnode:", "_:"));
            }

            //Prefixed token: check if it starts with a known prefix, if so just return it
            if (RDFNamespaceRegister.GetByPrefix(token.Split(':')[0]) != null)
            {
                return(token);
            }

            //Uri token: search a known namespace, if found replace it with its prefix
            String  tokenBackup = token;
            Boolean abbrev      = false;

            RDFNamespaceRegister.Instance.Register.ForEach(ns => {
                if (!abbrev)
                {
                    String nS = ns.ToString();
                    if (!token.Equals(nS, StringComparison.OrdinalIgnoreCase))
                    {
                        if (token.StartsWith(nS))
                        {
                            token = token.Replace(nS, ns.NamespacePrefix + ":").TrimEnd(new Char[] { '/' });

                            //Accept the abbreviation only if it has generated a valid XSD QName
                            try {
                                var qn = new RDFTypedLiteral(token, RDFModelEnums.RDFDatatypes.XSD_QNAME);
                                abbrev = true;
                            }
                            catch {
                                token  = tokenBackup;
                                abbrev = false;
                            }
                        }
                    }
                }
            });

            //Search done, let's analyze results:
            if (abbrev)
            {
                return(token);        //token is a relative or a blank uri
            }
            if (token.Contains("^^")) //token is a typedLiteral absolute uri
            {
                return(token.Replace("^^", "^^<") + ">");
            }
            return("<" + token + ">"); //token is an absolute uri
        }