Пример #1
0
        /// <summary>
        /// A Function which attempts to reduce a Uri to a QName
        /// </summary>
        /// <param name="uri">The Uri to attempt to reduce</param>
        /// <param name="qname">The value to output the QName to if possible</param>
        /// <returns></returns>
        /// <remarks>This function will return a Boolean indicated whether it succeeded in reducing the Uri to a QName.  If it did then the out parameter qname will contain the reduction, otherwise it will be the empty string.</remarks>
        public override bool ReduceToQName(string uri, out string qname)
        {
            // See if we've cached this mapping
            QNameMapping mapping;

            if (_mapping.TryGetValue(uri, out mapping))
            {
                qname = mapping.QName;
                return(true);
            }
            mapping = new QNameMapping(uri);

            foreach (Uri u in _uris.Values)
            {
                String baseuri = u.AbsoluteUri;

                // Does the Uri start with the Base Uri
                if (uri.StartsWith(baseuri))
                {
                    // Remove the Base Uri from the front of the Uri
                    qname = uri.Substring(baseuri.Length);
                    // Add the Prefix back onto the front plus the colon to give a QName
                    if (_prefixes.ContainsKey(u.GetEnhancedHashCode()))
                    {
                        qname = _prefixes[u.GetEnhancedHashCode()] + ":" + qname;
                        if (qname.Equals(":"))
                        {
                            continue;
                        }
                        if (qname.Contains("/") || qname.Contains("#"))
                        {
                            continue;
                        }
                        // Cache the Mapping
                        mapping.QName = qname;
                        AddToCache(uri, mapping);
                        return(true);
                    }
                }
            }

            // Failed to find a Reduction
            qname = String.Empty;
            return(false);
        }
Пример #2
0
 /// <summary>
 /// Adds a QName mapping to the cache
 /// </summary>
 /// <param name="uri">URI</param>
 /// <param name="mapping">Mapping</param>
 protected virtual void AddToCache(String uri, QNameMapping mapping)
 {
     this._mapping.Add(uri, mapping);
 }
Пример #3
0
        /// <summary>
        /// A Function which attempts to reduce a Uri to a QName and issues a Temporary Namespace if required
        /// </summary>
        /// <param name="uri">The Uri to attempt to reduce</param>
        /// <param name="qname">The value to output the QName to if possible</param>
        /// <param name="tempNamespace">The Temporary Namespace issued (if any)</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// This function will always returns a possible QName for the URI if the format of the URI permits it.  It doesn't guarentee that the QName will be valid for the syntax it is being written to - it is up to implementers of writers to validate the QNames returned.
        /// </para>
        /// <para>
        /// Where necessary a Temporary Namespace will be issued and the <paramref name="tempNamespace">tempNamespace</paramref> parameter will be set to the prefix of the new temporary namespace
        /// </para>
        /// </remarks>
        public bool ReduceToQName(String uri, out String qname, out String tempNamespace)
        {
            tempNamespace = String.Empty;

            //See if we've cached this mapping
            QNameMapping mapping;

            if (this._mapping.TryGetValue(uri, out mapping))
            {
                qname = mapping.QName;
                return(true);
            }
            mapping = new QNameMapping(uri);

            //Try and find a Namespace URI that is the prefix of the URI
            foreach (Uri u in this._uris.Values)
            {
                String baseuri = u.AbsoluteUri;

                //Does the Uri start with the Base Uri
                if (uri.StartsWith(baseuri))
                {
                    //Remove the Base Uri from the front of the Uri
                    qname = uri.Substring(baseuri.Length);
                    //Add the Prefix back onto the front plus the colon to give a QName
                    if (this._prefixes.ContainsKey(u.GetEnhancedHashCode()))
                    {
                        qname = this._prefixes[u.GetEnhancedHashCode()] + ":" + qname;
                        if (qname.Equals(":"))
                        {
                            continue;
                        }
                        if (qname.Contains("/") || qname.Contains("#"))
                        {
                            continue;
                        }
                        //Cache the Mapping
                        mapping.QName = qname;
                        this.AddToCache(uri, mapping);
                        return(true);
                    }
                }
            }

            //Try and issue a Temporary Namespace
            String nsUri, nsPrefix;

            if (uri.Contains('#'))
            {
                nsUri    = uri.Substring(0, uri.LastIndexOf('#') + 1);
                nsPrefix = this.GetNextTemporaryNamespacePrefix();
            }
            else if (uri.LastIndexOf('/') > 8)
            {
                nsUri    = uri.Substring(0, uri.LastIndexOf('/') + 1);
                nsPrefix = this.GetNextTemporaryNamespacePrefix();
            }
            else
            {
                //Failed to find a Reduction and unable to issue a Temporary Namespace
                qname = String.Empty;
                return(false);
            }

            //Add to Namespace Map
            this.AddNamespace(nsPrefix, UriFactory.Create(nsUri));

            //Cache mapping and return
            mapping.QName = nsPrefix + ":" + uri.Replace(nsUri, String.Empty);
            this.AddToCache(uri, mapping);
            qname         = mapping.QName;
            tempNamespace = nsPrefix;
            return(true);
        }
Пример #4
0
 /// <summary>
 /// Adds a QName Mapping to the Cache in a Thread Safe way
 /// </summary>
 /// <param name="key">Key</param>
 /// <param name="value">Value</param>
 protected override void AddToCache(int key, QNameMapping value)
 {
     try
     {
         Monitor.Enter(this._mapping);
         base.AddToCache(key, value);
     }
     finally
     {
         Monitor.Exit(this._mapping);
     }
 }
Пример #5
0
 /// <summary>
 /// Adds a URI to QName Mapping to the Cache
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 protected virtual void AddToCache(int key, QNameMapping value)
 {
     this._mapping.Add(key, value);
 }
Пример #6
0
        /// <summary>
        /// A Function which attempts to reduce a Uri to a QName and issues a Temporary Namespace if required
        /// </summary>
        /// <param name="uri">The Uri to attempt to reduce</param>
        /// <param name="qname">The value to output the QName to if possible</param>
        /// <param name="tempNamespace">The Temporary Namespace issued (if any)</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// This function will always returns a possible QName for the URI if the format of the URI permits it.  It doesn't guarentee that the QName will be valid for the syntax it is being written to - it is up to implementers of writers to validate the QNames returned.
        /// </para>
        /// <para>
        /// Where necessary a Temporary Namespace will be issued and the <paramref name="tempNamespace">tempNamespace</paramref> parameter will be set to the prefix of the new temporary namespace
        /// </para>
        /// </remarks>
        public bool ReduceToQName(String uri, out String qname, out String tempNamespace)
        {
            tempNamespace = String.Empty;

            //See if we've cached this mapping
            QNameMapping mapping = new QNameMapping(uri);
            if (this._mapping.Contains(uri.GetHashCode(), mapping))
            {
                qname = this._mapping[uri.GetHashCode()].QName;
                return true;
            }

            //Try and find a Namespace URI that is the prefix of the URI
            foreach (Uri u in this._uris.Values)
            {
                String baseuri = u.ToString();

                //Does the Uri start with the Base Uri
                if (uri.StartsWith(baseuri))
                {
                    //Remove the Base Uri from the front of the Uri
                    qname = uri.Substring(baseuri.Length);
                    //Add the Prefix back onto the front plus the colon to give a QName
                    if (this._prefixes.ContainsKey(u.GetEnhancedHashCode()))
                    {
                        qname = this._prefixes[u.GetEnhancedHashCode()] + ":" + qname;
                        if (qname.Equals(":")) continue;
                        if (qname.Contains("/") || qname.Contains("#")) continue;
                        //Cache the Mapping
                        mapping.QName = qname;
                        this.AddToCache(uri.GetHashCode(), mapping);
                        return true;
                    }
                }
            }

            //Try and issue a Temporary Namespace
            String nsUri, nsPrefix;
            if (uri.Contains('#'))
            {
                nsUri = uri.Substring(0, uri.LastIndexOf('#') + 1);
                nsPrefix = this.GetNextTemporaryNamespacePrefix();
            }
            else if (uri.LastIndexOf('/') > 8)
            {
                nsUri = uri.Substring(0, uri.LastIndexOf('/') + 1);
                nsPrefix = this.GetNextTemporaryNamespacePrefix();
            }
            else
            {

                //Failed to find a Reduction and unable to issue a Temporary Namespace
                qname = String.Empty;
                return false;
            }

            //Add to Namespace Map
            this.AddNamespace(nsPrefix, UriFactory.Create(nsUri));

            //Cache mapping and return
            mapping.QName = nsPrefix + ":" + uri.Replace(nsUri, String.Empty);
            this.AddToCache(uri.GetHashCode(), mapping);
            qname = mapping.QName;
            tempNamespace = nsPrefix;
            return true;
        }
Пример #7
0
        /// <summary>
        /// A Function which attempts to reduce a Uri to a QName
        /// </summary>
        /// <param name="uri">The Uri to attempt to reduce</param>
        /// <param name="qname">The value to output the QName to if possible</param>
        /// <returns></returns>
        /// <remarks>This function will return a Boolean indicated whether it succeeded in reducing the Uri to a QName.  If it did then the out parameter qname will contain the reduction, otherwise it will be the empty string.</remarks>
        public override bool ReduceToQName(string uri, out string qname)
        {
            //See if we've cached this mapping
            QNameMapping mapping = new QNameMapping(uri);
            if (this._mapping.Contains(uri.GetHashCode(), mapping))
            {
                qname = this._mapping[uri.GetHashCode()].QName;
                return true;
            }

            foreach (Uri u in this._uris.Values)
            {
                String baseuri = u.ToString();

                //Does the Uri start with the Base Uri
                if (uri.StartsWith(baseuri))
                {
                    //Remove the Base Uri from the front of the Uri
                    qname = uri.Substring(baseuri.Length);
                    //Add the Prefix back onto the front plus the colon to give a QName
                    if (this._prefixes.ContainsKey(u.GetEnhancedHashCode()))
                    {
                        qname = this._prefixes[u.GetEnhancedHashCode()] + ":" + qname;
                        if (qname.Equals(":")) continue;
                        if (qname.Contains("/") || qname.Contains("#")) continue;
                        //Cache the Mapping
                        mapping.QName = qname;
                        this.AddToCache(uri.GetHashCode(), mapping);
                        return true;
                    }
                }
            }

            //Failed to find a Reduction
            qname = String.Empty;
            return false;
        }
Пример #8
0
 /// <summary>
 /// Adds a URI to QName Mapping to the Cache
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 protected virtual void AddToCache(int key, QNameMapping value)
 {
     this._mapping.Add(key, value);
 }