Description
Enhanced version of URI.relativize, the target can now be in parent folder of base URI.
License
Open Source License
Parameter
Parameter | Description |
---|
base | Base uri, location from where to relativize. |
target | Target uri, final destination of returned URI. |
Exception
Parameter | Description |
---|
IllegalArgumentException | an exception |
Return
Non-absolute URI, or target if target scheme is different than base scheme.
Declaration
public static URI relativize(URI base, URI target)
Method Source Code
//package com.java2s;
/**/*from w w w. j av a2s .c om*/
* H2GIS is a library that brings spatial support to the H2 Database Engine
* <http://www.h2database.com>. H2GIS is developed by CNRS
* <http://www.cnrs.fr/>.
*
* This code is part of the H2GIS project. H2GIS is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation;
* version 3.0 of the License.
*
* H2GIS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details <http://www.gnu.org/licenses/>.
*
*
* For more information, please consult: <http://www.h2gis.org/>
* or contact directly: info_at_h2gis.org
*/
import java.net.URI;
import java.net.URISyntaxException;
import java.util.StringTokenizer;
public class Main {
/**
* Enhanced version of URI.relativize, the target can now be in parent folder of base URI.
* @param base Base uri, location from where to relativize.
* @param target Target uri, final destination of returned URI.
* @return Non-absolute URI, or target if target scheme is different than base scheme.
* @throws IllegalArgumentException
*/
public static URI relativize(URI base, URI target) {
if (!base.getScheme().equals(target.getScheme())) {
return target;
}
StringBuilder rel = new StringBuilder();
String path = base.getPath();
String separator = "/";
StringTokenizer tokenizer = new StringTokenizer(target.getPath(), separator);
String targetPart = "";
if (tokenizer.hasMoreTokens()) {
targetPart = tokenizer.nextToken();
}
if (path.startsWith(separator)) {
path = path.substring(1);
}
StringTokenizer baseTokenizer = new StringTokenizer(path, separator, true);
while (baseTokenizer.hasMoreTokens()) {
String basePart = baseTokenizer.nextToken();
if (baseTokenizer.hasMoreTokens()) {
// Has a / after this folder name
baseTokenizer.nextToken(); // return separator
if (!basePart.isEmpty()) {
while (targetPart.isEmpty() && tokenizer.hasMoreTokens()) {
targetPart = tokenizer.nextToken();
}
if (!basePart.equals(targetPart)) {
rel.append("..");
rel.append(separator);
} else if (tokenizer.hasMoreTokens()) {
targetPart = tokenizer.nextToken();
}
}
}
}
// Add part of target path that is not in base path
rel.append(targetPart);
while (tokenizer.hasMoreTokens()) {
targetPart = tokenizer.nextToken();
rel.append(separator);
rel.append(targetPart);
}
try {
return new URI(null, null, rel.toString(), null, null);
} catch (URISyntaxException ex) {
throw new IllegalArgumentException(
"Illegal URI provided:\n" + base.toString() + "\n" + target.toString());
}
}
}
Related
- relativeFileToURI(File file)
- relativePath(final URI baseURI, final URI pathURI)
- relativeURI(String basePath, String path)
- relativize(URI base, URI child)
- relativize(URI base, URI child)
- relativize(URI basePath, File path)
- relativize(URI baseUri, File f)
- relativize(URI baseURI, URI uriToRelativize)
- relativizeFromBase(String uri, URI base)