Example usage for java.net URI relativize

List of usage examples for java.net URI relativize

Introduction

In this page you can find the example usage for java.net URI relativize.

Prototype

public URI relativize(URI uri) 

Source Link

Document

Relativizes the given URI against this URI.

Usage

From source file:doc.doclets.WorkbenchHelpDoclet.java

/**
 * Given two dot separated classpath names, return a relative path to the corresponding doc file. This method is used to create relative paths
 * //from w w w .  j  a  v a  2  s . c o m
 * @param directory The top level directory where the classes are written.
 * @param destinationClassName
 * @param programElementDoc The documentation for the base class.
 * @param isIncluded True if the destination class is included in the set of classes we are documenting. If isIncluded is true, we create a link to the .xml
 *          file. If isIncluded is false, we create a linke to the javadoc .html file.
 * @return a relative path from the base class to the destination class.
 */
private static String _relativizePath(final String baseDirectory, final String destinationClassName,
        final ProgramElementDoc programElementDoc, final boolean isIncluded) {
    // Use / here because these will be used in URLS
    // String baseFileName = baseClassName.replace('.', "/");
    final String baseClassName = programElementDoc.qualifiedName();
    String destinationFileName = destinationClassName.replace('.', '/');
    if (baseDirectory != null) {
        // FIXME: will this work if baseDirectory is null?
        // baseFileName = baseDirectory + "/" + baseFileName;
        destinationFileName = baseDirectory + "/" + destinationFileName;
    }
    // URI baseURI = new File(baseFileName).toURI();
    final URI destinationURI = new File(destinationFileName).toURI();
    final URI baseDirectoryURI = new File(baseDirectory).toURI();
    final URI relativeURI = baseDirectoryURI.relativize(destinationURI);

    // Determine offsite from baseClassName to baseDirectory
    final String baseClassParts[] = baseClassName.split("\\.");
    final StringBuffer relativePath = new StringBuffer();

    int offset = 1;
    if (programElementDoc instanceof FieldDoc) {
        // Fields have names like foo.bar.bif, where bif is the method
        offset = 2;
    }
    for (int i = 0; i < baseClassParts.length - offset; i++) {
        relativePath.append("../");
    }

    // If the target is not in the list of actors we are creating
    // documentation for, then link to the .html file that
    // presumably was generated by javadoc; otherwise, link to the
    // .xml file
    final String extension = isIncluded ? ".xml" : ".html";

    System.out.println("PtDoclet: relativize: " + baseDirectory + " " + baseClassName + " "
            + baseClassParts.length + " " + offset + " " + relativePath + relativeURI.getPath() + extension);

    return relativePath + relativeURI.getPath() + extension;
}

From source file:fr.afcepf.atod.wine.data.parser.XmlParser.java

private static String getResourcePath() {
    try {//w  w w  . j  a  v  a  2 s .  c om
        URI resourcePathFile = System.class.getResource("/RESOURCE_PATH").toURI();
        String resourcePath = Files.readAllLines(Paths.get(resourcePathFile)).get(0);
        URI rootURI = new File("").toURI();
        URI resourceURI = new File(resourcePath).toURI();
        URI relativeResourceURI = rootURI.relativize(resourceURI);
        return relativeResourceURI.getPath();
    } catch (Exception e) {
        return null;
    }
}

From source file:info.servertools.core.util.FileUtils.java

public static void zipDirectory(File directory, File zipfile, @Nullable Collection<String> fileBlacklist,
        @Nullable Collection<String> folderBlacklist) throws IOException {
    URI baseDir = directory.toURI();
    Deque<File> queue = new LinkedList<>();
    queue.push(directory);/*from   ww  w .j a v a2 s  .c  om*/
    OutputStream out = new FileOutputStream(zipfile);
    Closeable res = out;
    try {
        ZipOutputStream zout = new ZipOutputStream(out);
        res = zout;
        while (!queue.isEmpty()) {
            directory = queue.removeFirst();
            File[] dirFiles = directory.listFiles();
            if (dirFiles != null && dirFiles.length != 0) {
                for (File child : dirFiles) {
                    if (child != null) {
                        String name = baseDir.relativize(child.toURI()).getPath();
                        if (child.isDirectory()
                                && (folderBlacklist == null || !folderBlacklist.contains(child.getName()))) {
                            queue.push(child);
                            name = name.endsWith("/") ? name : name + "/";
                            zout.putNextEntry(new ZipEntry(name));
                        } else {
                            if (fileBlacklist != null && !fileBlacklist.contains(child.getName())) {
                                zout.putNextEntry(new ZipEntry(name));
                                copy(child, zout);
                                zout.closeEntry();
                            }
                        }
                    }
                }
            }
        }
    } finally {
        res.close();
    }
}

From source file:net.sourceforge.dita4publishers.tools.dxp.DitaDxpHelper.java

/**
 * Given a DITA map bounded object set, zips it up into a DXP Zip package.
 * @param mapBos//from   w  w  w  .ja v a 2s. co  m
 * @param outputZipFile
 * @throws Exception 
 */
public static void zipMapBos(DitaBoundedObjectSet mapBos, File outputZipFile, MapBosProcessorOptions options)
        throws Exception {
    /*
     *  Some potential complexities:
     *  
     *  - DXP package spec requires either a map manifest or that 
     *  there be exactly one map at the root of the zip package. This 
     *  means that the file structure of the BOS needs to be checked to
     *  see if the files already conform to this structure and, if not,
     *  they need to be reorganized and have pointers rewritten if a 
     *  map manifest has not been requested.
     *  
     *  - If the file structure of the original data includes files not
     *  below the root map, the file organization must be reworked whether
     *  or not a map manifest has been requested.
     *  
     *  - Need to generate DXP map manifest if a manifest is requested.
     *  
     *  - If user has requested that the original file structure be 
     *  remembered, a manifest must be generated.
     */

    log.debug("Determining zip file organization...");

    BosVisitor visitor = new DxpFileOrganizingBosVisitor();
    visitor.visit(mapBos);

    if (!options.isQuiet())
        log.info("Creating DXP package \"" + outputZipFile.getAbsolutePath() + "\"...");
    OutputStream outStream = new FileOutputStream(outputZipFile);
    ZipOutputStream zipOutStream = new ZipOutputStream(outStream);

    ZipEntry entry = null;

    // At this point, URIs of all members should reflect their
    // storage location within the resulting DXP package. There
    // must also be a root map, either the original map
    // we started with or a DXP manifest map.

    URI rootMapUri = mapBos.getRoot().getEffectiveUri();
    URI baseUri = null;
    try {
        baseUri = AddressingUtil.getParent(rootMapUri);
    } catch (URISyntaxException e) {
        throw new BosException("URI syntax exception getting parent URI: " + e.getMessage());
    }

    Set<String> dirs = new HashSet<String>();

    if (!options.isQuiet())
        log.info("Constructing DXP package...");
    for (BosMember member : mapBos.getMembers()) {
        if (!options.isQuiet())
            log.info("Adding member " + member + " to zip...");
        URI relativeUri = baseUri.relativize(member.getEffectiveUri());
        File temp = new File(relativeUri.getPath());
        String parentPath = temp.getParent();
        if (parentPath != null && !"".equals(parentPath) && !parentPath.endsWith("/")) {
            parentPath += "/";
        }
        log.debug("parentPath=\"" + parentPath + "\"");
        if (!"".equals(parentPath) && parentPath != null && !dirs.contains(parentPath)) {
            entry = new ZipEntry(parentPath);
            zipOutStream.putNextEntry(entry);
            zipOutStream.closeEntry();
            dirs.add(parentPath);
        }
        entry = new ZipEntry(relativeUri.getPath());
        zipOutStream.putNextEntry(entry);
        IOUtils.copy(member.getInputStream(), zipOutStream);
        zipOutStream.closeEntry();
    }

    zipOutStream.close();
    if (!options.isQuiet())
        log.info("DXP package \"" + outputZipFile.getAbsolutePath() + "\" created.");
}

From source file:org.eclipse.winery.repository.Utils.java

/**
 * @param baseURI the URI from which the path should start
 * @param id the generic id to resolve/*from  w  w  w .  j av a  2  s.c  om*/
 *
 * @return the relative path for the given id
 */
public static String getRelativeURL(URI baseURI, GenericId id) {
    String absolutePath = Prefs.INSTANCE.getResourcePath() + "/"
            + Utils.getURLforPathInsideRepo(BackendUtils.getPathInsideRepo(id));
    return baseURI.relativize(URI.create(absolutePath)).toString();
}

From source file:org.dita.dost.util.JobMapper.java

@Override
public String[] mapFileName(String sourceFileName) {
    final Job.FileInfo fi = job.getFileInfo(toURI(sourceFileName));
    final String res;
    if (fi.result == null) {
        res = sourceFileName;//from w w w.  j a v  a 2 s  . com
    } else {
        final URI base = toURI(job.getProperty("user.input.dir.uri"));
        final URI rel = base.relativize(fi.result);
        res = toFile(rel).getPath();
    }
    return new String[] { extension != null ? (FilenameUtils.removeExtension(res) + extension) : res };
}

From source file:com.textocat.textokit.commons.cpe.FileDirectoryCollectionReader.java

private URI getURIForMetadata(File f) {
    URI fURI = f.toURI();//from   w  w  w . java2  s .c  om
    if (setRelativeURI) {
        URI dirURI = directory.toURI();
        return dirURI.relativize(fURI);
    } else {
        return fURI;
    }
}

From source file:org.dita.dost.ant.types.JobMapper.java

@Override
public String[] mapFileName(String sourceFileName) {
    final URI uri = toURI(sourceFileName);
    Job.FileInfo fi = job.getFileInfo(uri);
    if (fi == null) {
        fi = job.getFileInfo(job.getInputDir().resolve(uri));
    }/*w  ww  .  j  a  v a  2s .c o m*/
    final String res;
    switch (type) {
    case TEMP:
        res = fi.file.getPath();
        break;
    case RESULT:
        if (fi.result == null) {
            res = sourceFileName;
        } else {
            final URI base = job.getInputDir();
            final URI rel = base.relativize(fi.result);
            res = toFile(rel).getPath();
        }
        break;
    default:
        throw new IllegalArgumentException();
    }
    return new String[] { extension != null ? (FilenameUtils.removeExtension(res) + extension) : res };
}

From source file:org.eclipse.winery.repository.rest.RestUtils.java

/**
 * @param baseURI the URI from which the path should start
 * @param id      the generic id to resolve
 * @return the relative path for the given id
 */// ww  w  .  j a  va  2 s  . c  om
public static String getRelativeURL(URI baseURI, GenericId id) {
    String absolutePath = Environment.getUrlConfiguration().getRepositoryApiUrl() + "/" + Util.getUrlPath(id);
    return baseURI.relativize(URI.create(absolutePath)).toString();
}

From source file:com.asakusafw.runtime.directio.hadoop.HadoopDataSourceUtil.java

/**
 * Returns whether the parent path contains the child path, or not.
 * If the parent and child is same, this returns {@code false}.
 * @param parent the parent path/*from   ww  w .  j av a  2  s .  c  o m*/
 * @param child the child path
 * @return {@code true} if parent path strictly contains the child, otherwise {@code false}
 * @throws IllegalArgumentException if some parameters were {@code null}
 */
public static boolean contains(Path parent, Path child) {
    if (parent == null) {
        throw new IllegalArgumentException("parent must not be null"); //$NON-NLS-1$
    }
    if (child == null) {
        throw new IllegalArgumentException("child must not be null"); //$NON-NLS-1$
    }
    if (parent.depth() >= child.depth()) {
        return false;
    }
    URI parentUri = parent.toUri();
    URI childUri = child.toUri();
    URI relative = parentUri.relativize(childUri);
    if (relative.equals(childUri) == false) {
        return true;
    }
    return false;
}