Java Code Examples for java.net.URI.relativize()

The following are Jave code examples for showing how to use relativize() of the java.net.URI class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: sling-org-apache-sling-testing-clients   File: AbstractSlingClient.java   View Source Code Vote up 6 votes
/**
 * <p>Transforms an external {@code url} into a sling path, by subtracting the {@code server url} (incl. contextPath).
 * The returned path will not contain the context path, so it can be used with {@link #getUrl(String)}</p>
 *
 * <p>The url can be absolute (incl. hostname) or relative to root (starts with "/").</p>
 *
 * <p>If the server url is not a prefix of the given url, it returns the given url</p>
 *
 * <p>If the url is just a path, it returns the path (with leading slash if not already present)</p>
 *
 * @param url full url
 * @return sling path
 */
public URI getPath(URI url) {
    // special case for urls that are server urls, but without trailing slash
    if (url.relativize(getUrl()).equals(URI.create(""))) {
        return slash;
    }

    URI contextPath = URI.create(getUrl().getPath());
    URI relativeUrl = contextPath.relativize(slash.resolve(url));

    if (relativeUrl.relativize(contextPath).equals(URI.create(""))) {
        return slash;
    }

    return slash.resolve(getUrl().relativize(relativeUrl));
}
 
Example 2
Project: Reer   File: PublishArtifactToFileBuildOutcomeTransformer.java   View Source Code Vote up 5 votes
private String getId(PublishArtifact artifact, Project project) {
    // Assume that each artifact points to a unique file, and use the relative path from the project as the id
    URI artifactUri = artifact.getFile().toURI();
    URI projectDirUri = project.getProjectDir().toURI();
    URI relativeUri = projectDirUri.relativize(artifactUri);
    return relativeUri.getPath();
}
 
Example 3
Project: incubator-netbeans   File: NbMavenProjectImpl.java   View Source Code Vote up 5 votes
public URI[] getResources(boolean test) {
        List<URI> toRet = new ArrayList<URI>();
        URI projectroot = getProjectDirectory().toURI();
        Set<URI> sourceRoots = null;
        List<Resource> res = test ? getOriginalMavenProject().getTestResources() : getOriginalMavenProject().getResources();
        LBL : for (Resource elem : res) {
            String dir = elem.getDirectory();
            if (dir == null) {
                continue; // #191742
            }
            URI uri = FileUtilities.getDirURI(getProjectDirectory(), dir);
            if (elem.getTargetPath() != null || !elem.getExcludes().isEmpty() || !elem.getIncludes().isEmpty()) {
                URI rel = projectroot.relativize(uri);
                if (rel.isAbsolute()) { //outside of project directory
                    continue;// #195928, #231517
                }
                if (sourceRoots == null) {
                    sourceRoots = new HashSet<URI>();
                    sourceRoots.addAll(Arrays.asList(getSourceRoots(true)));
                    sourceRoots.addAll(Arrays.asList(getSourceRoots(false)));
                    //should we also consider generated sources? most like not necessary
                }
                for (URI sr : sourceRoots) {
                    if (!uri.relativize(sr).isAbsolute()) {
                        continue LBL;// #195928, #231517
                    }
                }
                //hope for the best now
            }
//            if (new File(uri).exists()) {
            toRet.add(uri);
//            }
        }
        return toRet.toArray(new URI[toRet.size()]);
    }
 
Example 4
Project: incubator-netbeans   File: AbstractSvnTestCase.java   View Source Code Vote up 5 votes
protected String getPathRelativeToWC(File file) {
    URI wcURI = getWC().toURI();
    URI fileURI = file.toURI();
    URI relativePathURI = wcURI.relativize(fileURI);
    if (relativePathURI == fileURI) {
        throw new IllegalArgumentException(
                "The given file is not in the working directory.");
    }
    return relativePathURI.getPath();
}
 
Example 5
Project: jdk8u-jdk   File: Test.java   View Source Code Vote up 5 votes
Test rtvz(URI base) {
    if (!parsed())
        return this;
    this.base = base;
    op = "rtvz";
    uri = base.relativize(uri);
    checked = 0;
    failed = 0;
    return this;
}
 
Example 6
Project: openjdk-jdk10   File: Test.java   View Source Code Vote up 5 votes
Test rtvz(URI base) {
    if (!parsed())
        return this;
    this.base = base;
    op = "rtvz";
    uri = base.relativize(uri);
    checked = 0;
    failed = 0;
    return this;
}
 
Example 7
Project: Reer   File: JsHint.java   View Source Code Vote up 4 votes
@TaskAction
public void doJsHint() {
    RhinoWorkerHandleFactory handleFactory = new DefaultRhinoWorkerHandleFactory(getWorkerProcessBuilderFactory());

    LogLevel logLevel = getProject().getGradle().getStartParameter().getLogLevel();
    JsHintProtocol worker = handleFactory.create(getRhinoClasspath(), JsHintProtocol.class, JsHintWorker.class, logLevel, getProject().getProjectDir());

    JsHintSpec spec = new JsHintSpec();
    spec.setSource(getSource().getFiles()); // flatten because we need to serialize
    spec.setEncoding(getEncoding());
    spec.setJsHint(getJsHint().getSingleFile());

    JsHintResult result = worker.process(spec);
    setDidWork(true);

    // TODO - this is all terribly lame. We need some proper reporting here (which means implementing Reporting).

    Logger logger = getLogger();
    boolean anyErrors = false;

    Map<String, Map<?, ?>> reportData = new LinkedHashMap<String, Map<?, ?>>(result.getResults().size());
    for (Map.Entry<File, Map<String, Object>> fileEntry: result.getResults().entrySet()) {
        File file = fileEntry.getKey();
        Map<String, Object> data = fileEntry.getValue();

        reportData.put(file.getAbsolutePath(), data);

        if (data.containsKey("errors")) {
            anyErrors = true;

            URI projectDirUri = getProject().getProjectDir().toURI();
            @SuppressWarnings("unchecked") Map<String, Object> errors = (Map<String, Object>) data.get("errors");
            if (!errors.isEmpty()) {
                URI relativePath = projectDirUri.relativize(file.toURI());
                logger.warn("JsHint errors for file: {}", relativePath.getPath());
                for (Map.Entry<String, Object> errorEntry : errors.entrySet()) {
                    @SuppressWarnings("unchecked") Map<String, Object> error = (Map<String, Object>) errorEntry.getValue();
                    int line = Float.valueOf(error.get("line").toString()).intValue();
                    int character = Float.valueOf(error.get("character").toString()).intValue();
                    String reason = error.get("reason").toString();

                    logger.warn("  {}:{} > {}", new Object[] {line, character, reason});
                }
            }
        }
    }

    File jsonReportFile = getJsonReport();
    if (jsonReportFile != null) {
        try {
            FileWriter reportWriter = new FileWriter(jsonReportFile);
            new GsonBuilder().setPrettyPrinting().create().toJson(reportData, reportWriter);
            reportWriter.close();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    if (anyErrors) {
        throw new TaskExecutionException(this, new GradleException("JsHint detected errors"));
    }
}
 
Example 8
Project: Reer   File: ManifestUtil.java   View Source Code Vote up 4 votes
private static String constructRelativeClasspathUri(File jarFile, File file) {
    URI jarFileUri = jarFile.getParentFile().toURI();
    URI fileUri = file.toURI();
    URI relativeUri = jarFileUri.relativize(fileUri);
    return relativeUri.getRawPath();
}
 
Example 9
Project: incubator-netbeans   File: BookmarkUtils.java   View Source Code Vote up 4 votes
public static URI getRelativeURI(ProjectBookmarks projectBookmarks, URI fileURI) {
    URI projectURI = projectBookmarks.getProjectURI();
    return (projectURI != null) ? projectURI.relativize(fileURI) : fileURI;
}