001package Torello.Browser.JsonAST;
002
003/**
004 * Converts a single type reference as a {@code String} into a genuine {@link TypeNode} instance.
005 * <EMBED CLASS='external-html' DATA-FILE-ID=L_FIND_TYPENODE>
006 */
007@Torello.JavaDoc.Annotations.StaticFunctional
008@Torello.JavaDoc.Annotations.JDHeaderBackgroundImg(EmbedTagFileID="LINKER_JDHBI")
009public class Helper$FindTypeNode
010{
011    private Helper$FindTypeNode() { }
012
013    static TypeNode find(final PPR ppr, final String ref)
014    {
015        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
016        // Identify the Domain in which the reference is located (as instance of Domain, not String
017        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
018        // 
019        // Since e.ref is some String, first figure out whether or not the Domain to which
020        // the relevant TCE belongs is the domain in which this PPR resides, or if it is another
021        // domain.
022        // 
023        // All that the following two lines are doing is checking if:
024        // 
025        // this.ref = "SomeTypeTypeNode.name"
026        // this.ref = "OtherDomain.SomeOtherTypeNode.name"
027        // 
028        // In the former case, use the Domain that owns this Entity
029        // In the later case, convert the Domain-Name (currently as a String), into an actual
030        // pointer to the JsonAST.Domain reference.
031
032        final int pos = ref.indexOf('.');
033
034        final Domain linkedDomain = (pos == -1)
035            ? ppr.ownerDomain
036            : Helper$FindDomain.find(ref.substring(0, pos));
037
038
039        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
040        // Search the Domain, to find the TypeNode of the reference
041        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
042
043        final String expectedName = (pos == -1)
044            ? ref
045            : ref.substring(pos + 1);
046
047        for (final TypeNode tn : linkedDomain.types)
048            if (tn.name.equals(expectedName))
049                return tn;
050
051        throw new LinkingStateError(
052            Helper$FindTypeNode.class.getSimpleName() + ": \n" +
053            "Could not link:     [" + ref + "]\n" +
054            "Expected in domain: [" + linkedDomain.name  + "]\n" +
055            ppr.exceptionLocationSummary()
056        );
057    }
058}