/** * Add this path to the URI. * * @param suffix The suffix * @return New HREF */ public Href path(final Object suffix) { return new Href( URI.create( new StringBuilder(this.uri.toString().replaceAll("/$", "")) .append('/') .append(Href.encode(suffix.toString())) .toString()), this.params); }
@Override public String toString() { final StringBuilder text = new StringBuilder(this.bare()); if (!this.params.isEmpty()) { boolean first = true; for (final Map.Entry<String, List<String>> ent : this.params.entrySet()) { for (final String value : ent.getValue()) { if (first) { text.append('?'); first = false; } else { text.append('&'); } text.append(Href.encode(ent.getKey())); if (!value.isEmpty()) { text.append('=').append(Href.encode(value)); } } } } return text.toString(); }
/** * Parses the specified content to create the corresponding {@code URI} instance. In case of an * {@code URISyntaxException}, it will automatically encode the character that causes the issue * then it will try again if it is possible otherwise an {@code IllegalArgumentException} will be * thrown. * * @param txt The content to parse * @return The {@code URI} corresponding to the specified content. * @throws IllegalArgumentException in case the content could not be parsed * @throws IllegalStateException in case an invalid character could not be encoded properly. */ private static URI createURI(final String txt) { URI result; try { result = new URI(txt); } catch (final URISyntaxException ex) { final int index = ex.getIndex(); if (index == -1) { throw new IllegalArgumentException(ex.getMessage(), ex); } final StringBuilder value = new StringBuilder(txt); value.replace(index, index + 1, Href.encode(value.substring(index, index + 1))); result = Href.createURI(value.toString()); } return result; }