Here you can find the source of resolveBagUri(final Path baseDir, final URI bagUri)
Resolves the supplied bag:// URI against a platform-specific base directory.
Parameter | Description |
---|---|
baseDir | the base directory that contains the bag |
bagUri | a URI identifying a resource in a bag |
Parameter | Description |
---|---|
IllegalArgumentException | if the supplied bagUri is null or empty, if baseDir is null, if bagUri does not have scheme bag |
RuntimeException | if the supplied base directory cannot be normalized |
public static Path resolveBagUri(final Path baseDir, final URI bagUri)
//package com.java2s; /*/* w ww.j a v a 2 s. c o m*/ * Copyright 2016 Johns Hopkins University * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.net.URI; import java.nio.file.Path; import java.nio.file.Paths; public class Main { private static final String ERR_RESOLVE_BAGURI = "Unable to resolve bag uri %s against base directory %s: "; static String BAG_URI_SCHEME = "bag"; /** * Resolves the supplied {@code bag://} URI against a platform-specific base directory. This method is used to * resolve resources in a bag to a platform-specific {@code Path} used by the caller to access the content of the * resource. * <p> * Example usage: Given a bag that contains a resource identified by the URI {@code bag://my-bag/data/bar}, and * the bag has been exploded into the directory {@code /tmp/foo/my-bag} (where the bag payload directory is * located at {@code /tmp/foo/my-bag/data}) then the base directory of the bag is {@code /tmp/foo}. If the caller * wishes to resolve the URI {@code bag://my-bag/data/bar}, they would invoke this method: * </p> * <pre> * Path result = UriUtility.resolveBagUri(Paths.get("/tmp/foo"), new URI("bag://my-bag/data/bar")); * assert Paths.get("/tmp/foo/my-bag/data/bar").equals(result); * </pre> * <p> * The base directory does not need to exist. This implementation will {@link Path#normalize() normalize} the * supplied directory. * </p> * <p> * The {@code bag://} URI is converted to a path by concatenating the authority portion of the URI with the path * portion. * </p> * <p> * If the supplied {@code bagUri} is <em>not</em> a URI with the {@code bag} scheme, an * {@code IllegalArgumentException} is thrown. * </p> * * @param baseDir the base directory that contains the bag * @param bagUri a URI identifying a resource in a bag * @return a platform-specific {@code Path}, used to access the contents of the resource identified by * {@code bagUri} * @throws IllegalArgumentException if the supplied bagUri is null or empty, if {@code baseDir} is null, if * {@code bagUri} does not have scheme {@code bag} * @throws RuntimeException if the supplied base directory cannot be normalized */ public static Path resolveBagUri(final Path baseDir, final URI bagUri) { if (bagUri == null) { throw new IllegalArgumentException( String.format(ERR_RESOLVE_BAGURI + "bag uri was null.", "null", baseDir)); } if (!bagUri.getScheme().equals(BAG_URI_SCHEME)) { throw new IllegalArgumentException( String.format(ERR_RESOLVE_BAGURI + "bag uri had incorrect scheme.", bagUri, baseDir)); } if (baseDir == null) { throw new IllegalArgumentException( String.format(ERR_RESOLVE_BAGURI + "base directory was null", bagUri, "null")); } // normalize the base directory path final Path originalDir = baseDir; final Path normalizedDir = baseDir.normalize(); if (normalizedDir == null) { throw new RuntimeException(String.format(ERR_RESOLVE_BAGURI + "failed to normalize the base directory.", bagUri, originalDir)); } final Path bagPath = Paths.get(bagUri.getAuthority(), bagUri.getPath()); return normalizedDir.resolve(bagPath); } }