public Template parseTemplate(String template) throws URISyntaxException { this.template = template; builder = new Builder(template); builder.setHasScheme(false); builder.setHasAuthority( false); // Assume no until found otherwise. If true, will cause // in output URL. builder.setIsAuthorityOnly(false); builder.setIsAbsolute( false); // Assume relative until found otherwise. If true, will cause leading / in output // URL. builder.setIsDirectory( false); // Assume a file path until found otherwise. If true, will cause trailing / in // output URL. builder.setHasQuery( false); // Assume no ? until found otherwise. If true, will cause ? in output URL. builder.setHasFragment( false); // Assume no # until found otherwise. If true, will cause # in output URL. Matcher match = PATTERN.matcher(template); if (match.matches()) { consumeSchemeMatch(match); consumeAuthorityMatch(match); consumePathMatch(match); consumeQueryMatch(match); consumeFragmentMatch(match); fixNakedAuthority(); } else { throw new URISyntaxException(template, RES.parseTemplateFailureReason(template)); } return builder.build(); }
private final void fixNakedAuthority() { if (builder.getHashScheme() && !builder.getHasAuthority() && !builder.getIsAbsolute() && !builder.getIsDirectory() && (builder.getPath().size() == 1) && !builder.getHasQuery() && !builder.getHasFragment()) { final Scheme scheme = builder.getScheme(); builder.setHasScheme(false); builder.setHost(makeTokenSingular(scheme.getToken())); Path path = builder.getPath().remove(0); builder.setPort(makeTokenSingular(path.getToken())); builder.setIsAuthorityOnly(true); } }
private void consumeSchemeMatch(Matcher match) { if (match.group(MATCH_GROUP_SCHEME) != null) { builder.setHasScheme(true); consumeSchemeToken(match.group(MATCH_GROUP_SCHEME_NAKED)); } }