/** * Ctor. * * @param txt Text of the link * @todo #558:30min Href ctor. According to new qulice version, constructor must contain only * variables initialization and other constructor calls. Refactor code according to that rule * and remove `ConstructorOnlyInitializesOrCallOtherConstructors` warning suppression. */ @SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", "PMD.ConstructorOnlyInitializesOrCallOtherConstructors" }) public Href(final CharSequence txt) { this.params = new ConcurrentHashMap<String, List<String>>(0); final URI link = Href.createURI(txt.toString()); final String query = link.getRawQuery(); if (query == null) { this.uri = link; } else { final String href = link.toString(); this.uri = URI.create(href.substring(0, href.length() - query.length() - 1)); final String[] pairs = query.split("&"); for (final String pair : pairs) { final String[] parts = pair.split("=", 2); final String key = Href.decode(parts[0]); final String value; if (parts.length > 1) { value = Href.decode(parts[1]); } else { value = ""; } this.params.putIfAbsent(key, new LinkedList<String>()); this.params.get(key).add(value); } } }
/** * 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; }