Example #1
0
        private IToken TryGetVariable()
        {
            // Consume first Character which must be a ?/$
            ConsumeCharacter();

            // Consume other valid Characters
            char next = Peek();

            while (Char.IsLetterOrDigit(next) || UnicodeSpecsHelper.IsLetterOrDigit(next) || next == '-' || next == '_' || next == '\\')
            {
                if (next == '\\')
                {
                    // Check its a valid Escape
                    HandleEscapes(TokeniserEscapeMode.QName);
                }
                else
                {
                    ConsumeCharacter();
                }
                next = Peek();
            }

            // Validate
            String value = Value;

            if (value.EndsWith("."))
            {
                Backtrack();
                value = value.Substring(0, value.Length - 1);
            }

            if (SparqlSpecsHelper.IsValidVarName(value))
            {
                return(new VariableToken(value, CurrentLine, StartPosition, EndPosition));
            }
            else
            {
                throw Error("The value '" + value + "' is not valid a Variable Name");
            }
        }
        /// <summary>
        /// Get column name by using qname compression and replacing special chars with underscores
        /// </summary>
        /// <param name="node">Node</param>
        /// <param name="namespaces">Namespaces</param>
        /// <returns></returns>
        private string GetColumnName(INode node, INamespaceMapper namespaces)
        {
            if (node.NodeType != NodeType.Uri)
            {
                return(null);
            }
            Uri u = ((IUriNode)node).Uri;

            String qname;

            if (!namespaces.ReduceToQName(u.AbsoluteUri, out qname))
            {
                // Issue temporary namespaces
                String tempNsPrefix = "ns" + this._nextTempID;
                String nsUri        = u.AbsoluteUri;
                if (nsUri.Contains("#"))
                {
                    //Create a Hash Namespace Uri
                    nsUri = nsUri.Substring(0, nsUri.LastIndexOf("#") + 1);
                }
                else if (nsUri.Contains("/"))
                {
                    //Create a Slash Namespace Uri
                    nsUri = nsUri.Substring(0, nsUri.LastIndexOf("/") + 1);
                }
                Uri tempNsUri;
                if (!Uri.TryCreate(nsUri, UriKind.Absolute, out tempNsUri))
                {
                    tempNsUri = u;
                }
                while (namespaces.HasNamespace(tempNsPrefix))
                {
                    this._nextTempID++;
                    tempNsPrefix = "ns" + this._nextTempID;
                }
                namespaces.AddNamespace(tempNsPrefix, tempNsUri);
                if (!namespaces.ReduceToQName(u.AbsoluteUri, out qname))
                {
                    qname = null;
                }
            }
            if (qname == null)
            {
                // Issue temporary column name
                return("entityPredicateColumn" + (++this._nextTempColumnID));
            }
            // Sanitize qname column name
            qname = qname.StartsWith("_") ? qname.Substring(1) : qname.Replace(':', '_');
            if (SparqlSpecsHelper.IsValidVarName(qname))
            {
                return(qname);
            }
            if (qname.Length == 0)
            {
                // Issue temporary column name
                return("entityPredicateColumn " + (++this._nextTempColumnID));
            }

            // Clean up illegal characters
            // First Character must be from PN_CHARS_U or a digit
            // Add a leading p for invalid first characters
            char[] cs    = qname.ToCharArray();
            char   first = cs[0];

            if (!Char.IsDigit(first) || !SparqlSpecsHelper.IsPNCharsU(first))
            {
                qname = "p" + qname;
                if (SparqlSpecsHelper.IsValidVarName(qname))
                {
                    return(qname);
                }
                cs = qname.ToCharArray();
            }
            for (int i = 1; i < cs.Length; i++)
            {
                if (i >= cs.Length - 1)
                {
                    continue;
                }
                // Subsequent Chars must be from PN_CHARS (except -) or a '.'
                // Replace invalid characters with _
                if (cs[i] == '.' || cs[i] == '-')
                {
                    cs[i] = '_';
                }
                else if (!SparqlSpecsHelper.IsPNChars(cs[i]))
                {
                    cs[i] = '_';
                }
            }
            // Trim trailing underscores
            return(new string(cs).TrimEnd('_'));
        }