private static UriPart unionUriParts ( UriPart a , UriPart b ) { Preconditions . checkArgument ( a != b ) ; if ( a == UriPart . DANGEROUS_SCHEME || b == UriPart . DANGEROUS_SCHEME ) { // Dangerous schemes (like javascript:) are poison -- if either side is dangerous, the whole // thing is. return UriPart . DANGEROUS_SCHEME ; } else if ( a == UriPart . FRAGMENT || b == UriPart . FRAGMENT || a == UriPart . UNKNOWN || b == UriPart . UNKNOWN ) { // UNKNOWN means one part is in the #fragment and one is not. This is the case if one is // FRAGMENT and the other is not, or if one of the branches was UNKNOWN to begin with. return UriPart . UNKNOWN ; } else if ( ( a == UriPart . MAYBE_VARIABLE_SCHEME || b == UriPart . MAYBE_VARIABLE_SCHEME ) && a != UriPart . UNKNOWN_PRE_FRAGMENT && b != UriPart . UNKNOWN_PRE_FRAGMENT ) { // This is the case you might see on a URL that starts with a print statement, and one // branch has a slash or ampersand but the other doesn't. Re-entering // MAYBE_VARIABLE_SCHEME allows us to pretend that the last branch was just part of the // leading print statement, which leaves us in a relatively-unknown state, but no more // unknown had it just been completely opaque. // // Good Example 1: {$urlWithQuery}{if $a}&a={$a}{/if}{if $b}&b={$b}{/if} // In this example, the first "if" statement has two branches: // - "true": {$urlWithQuery}&a={$a} looks like a QUERY due to hueristics // - "false": {$urlWithQuery} only, which Soy doesn't know at compile-time to actually // have a query, and it remains in MAYBE_VARIABLE_SCHEME. // Instead of yielding UNKNOWN, this yields MAYBE_VARIABLE_SCHEME, which the second // {if $b} can safely deal with. // // Good Example 2: {$base}{if $a}/a{/if}{if $b}/b{/if} // In this, one branch transitions definitely into an authority or path, but the other // might not. However, we can remain in MAYBE_VARIABLE_SCHEME safely. return UriPart . MAYBE_VARIABLE_SCHEME ; } else { // The part is unknown, but we think it's before the fragment. In this case, it's clearly // ambiguous at compile-time that it's not clear what to do. Examples: // // /foo/{if $cond}?a={/if} // {$base}{if $cond}?a={$a}{else}/b{/if} // {if $cond}{$base}{else}/a{if $cond2}?b=1{/if}{/if} // // Unlike MAYBE_VARIABLE_SCHEME, we don't need to try to gracefully recover here, because // the template author can easily disambiguate this. return UriPart . UNKNOWN_PRE_FRAGMENT ; } }
Determines the correct URI part if two branches are joined .