/** * Guarantees that the returned URI is absolute, no matter what the argument is. * * @param base An absolute base URI, can be null! * @param uri A string that either represents a relative or an already absolute URI * @return An absolute URI * @throws IllegalArgumentException If the base URI is null and the given URI string is not absolute */ public static URI createAbsoluteURI(URI base, String uri) throws IllegalArgumentException { return createAbsoluteURI(base, URI.create(uri)); }
public static URL createAbsoluteURL(URI base, URI relativeOrNot) throws IllegalArgumentException { try { return createAbsoluteURI(base, relativeOrNot).toURL(); } catch (Exception ex) { throw new IllegalArgumentException("Absolute URI can not be converted to URL", ex); } }
public static URL createAbsoluteURL(URL base, URI relativeOrNot) throws IllegalArgumentException { if (base == null && !relativeOrNot.isAbsolute()) { throw new IllegalArgumentException("Base URL is null and given URI is not absolute"); } else if (base == null && relativeOrNot.isAbsolute()) { try { return relativeOrNot.toURL(); } catch (Exception ex) { throw new IllegalArgumentException("Base URL was null and given URI can't be converted to URL"); } } else { try { assert base != null; URI baseURI = base.toURI(); URI absoluteURI = createAbsoluteURI(baseURI, relativeOrNot); return absoluteURI.toURL(); } catch (Exception ex) { throw new IllegalArgumentException( "Base URL is not an URI, or can't create absolute URI (null?), " + "or absolute URI can not be converted to URL", ex); } } }