示例#1
0
        public CreateNTriplesPrefixTarget(NTriplesPrefixReference reference)
        {
            this.myElement = reference.GetTreeNode();
            string name = reference.GetName();

            var uri = this.TryFindUri(reference);

            this.myDeclaration =
                NTriplesElementFactory.GetInstance(this.myElement.GetPsiModule()).CreatePrefixDeclarationSentence(name, uri);
            this.Anchor = this.FindAnchor();
        }
示例#2
0
        private string TryFindUri(NTriplesPrefixReference reference)
        {
            var file = (INTriplesFile)this.myElement.GetContainingFile();

            if (file == null)
            {
                throw new FormatException("The element has no file assigned.");
            }

            var name = reference.GetName();

            if (!string.IsNullOrEmpty(name))
            {
                // Looking for declarations of the same name
                var cache = this.myElement.GetSolution().GetComponent <NTriplesCache>();
                var uris  = cache.GetPrefixDeclarationSymbols(name).Select(s => s.Uri).Distinct().ToArray();
                if (uris.Length == 0)
                {
                    return(null);
                }

                if (uris.Length == 1)
                {
                    return(uris[0]);
                }

                // narrowing the search using local names
                var prefixesWithSameName =
                    new RecursiveElementCollector <IPrefixName>(p => p.GetText() == name).ProcessElement(file).GetResults();
                var localNamesInThePrefix =
                    prefixesWithSameName.Select(p => ((IUriIdentifier)p.Parent).GetLocalName())
                    .Distinct()
                    .Where(n => !string.IsNullOrEmpty(n))
                    .ToArray();

                if (localNamesInThePrefix.Length == 0)
                {
                    return(uris[0]);
                }

                var bestUri    = uris[0];
                int bestMetric = 0;
                foreach (var uri in uris)
                {
                    var metric =
                        cache.GetAllUriIdentifiersInNamespace(uri)
                        .Select(s => s.LocalName)
                        .Intersect(localNamesInThePrefix)
                        .Count();
                    if (metric > bestMetric)
                    {
                        bestMetric = metric;
                        bestUri    = uri;
                    }
                }

                return(bestUri);
            }

            return(null);
        }