
org.daisy.pipeline.braille.common.BundledResourcePath Maven / Gradle / Ivy
package org.daisy.pipeline.braille.common;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import static com.google.common.base.Functions.toStringFunction;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import static com.google.common.base.Predicates.alwaysTrue;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import static org.daisy.pipeline.braille.common.util.Files;
import static org.daisy.pipeline.braille.common.util.Iterators.partition;
import static org.daisy.pipeline.braille.common.util.Predicates.matchesGlobPattern;
import static org.daisy.pipeline.braille.common.util.Tuple2;
import static org.daisy.pipeline.braille.common.util.URIs;
import static org.daisy.pipeline.braille.common.util.Files.asFile;
import static org.daisy.pipeline.braille.common.util.Files.normalize;
import static org.daisy.pipeline.braille.common.util.URIs.asURI;
import static org.daisy.pipeline.braille.common.util.URLs.asURL;
import static org.daisy.pipeline.braille.common.util.URLs.decode;
import org.osgi.framework.Bundle;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class BundledResourcePath implements ResourcePath {
protected static final String IDENTIFIER = "identifier";
protected static final String PATH = "path";
protected static final String UNPACK = "unpack";
protected static final String INCLUDES = "includes";
protected URI identifier = null;
protected URL path = null;
protected File unpackDir = null;
/* The included resources as relative paths */
protected Collection resources = null;
/**
* {@inheritDoc}
*/
public URI getIdentifier() {
return identifier;
}
private final ResourceResolver resolver = new util.MemoizingResolver() {
public URL _apply(URI resource) {
logger.trace("Resolving " + resource + " within " + identifier + " (real path: " + path + "; unpack dir: " + unpackDir + ")");
resource = resource.normalize();
if (resource.equals(identifier) || resource.equals(path) || resource.equals(unpackDir))
return maybeUnpack(asURI("."));
URI relativePath = resource;
if (relativePath.isAbsolute()) {
relativePath = identifier.relativize(resource);
if (relativePath.isAbsolute()) {
relativePath = asURI(path).relativize(resource);
if (relativePath.isAbsolute() && unpackDir != null) {
relativePath = asURI(unpackDir).relativize(resource); }}}
if (resources.contains(relativePath))
return maybeUnpack(relativePath);
if (!resource.toString().endsWith("/"))
return resolve(asURI(resource.toString() + "/"));
return null;
}
};
/**
* Resolve a resource from a URI.
* @param resource The URI can be one of the following:
* - a relative URI, or
* - an absolute URI that can be relativized against, or equal to:
* - the BundledResourcePath's identifier,
* - the BundledResourcePath's actual path in the bundle, or
* - the directory where resources are unpacked.
* @return The resolved URL, or null if the resource cannot be resolved.
* The URL will be a file URL if the BundledResourcePath is "unpacking".
*/
public URL resolve(URI resource) {
return resolver.resolve(resource);
}
public URI canonicalize(URI resource) {
URL resolved = resolve(resource);
if (resolved == null)
return null;
return identifier.resolve(URIs.relativize(unpackDir != null ? unpackDir : path, resolved));
}
protected void activate(ComponentContext context, final Map, ?> properties) throws Exception {
if (properties.get(IDENTIFIER) == null || properties.get(IDENTIFIER).toString().isEmpty()) {
throw new IllegalArgumentException(IDENTIFIER + " property must not be empty"); }
String identifierAsString = properties.get(IDENTIFIER).toString();
if (!identifierAsString.endsWith("/")) identifierAsString += "/";
try { identifier = asURI(identifierAsString); }
catch (Exception e) {
throw new IllegalArgumentException(IDENTIFIER + " could not be parsed into a URI"); }
if (!identifier.isAbsolute())
throw new IllegalArgumentException(IDENTIFIER + " must be an absolute URI");
if (properties.get(PATH) == null || properties.get(PATH).toString().isEmpty()) {
throw new IllegalArgumentException(PATH + " property must not be empty"); }
final Bundle bundle = context.getBundleContext().getBundle();
String pathAsRelativeFilePath = properties.get(PATH).toString();
if (!pathAsRelativeFilePath.endsWith("/")) pathAsRelativeFilePath += "/";
path = bundle.getEntry(pathAsRelativeFilePath);
if (path == null)
throw new IllegalArgumentException("Resource path at location " + pathAsRelativeFilePath + " could not be found");
final Predicate
© 2015 - 2025 Weber Informatics LLC | Privacy Policy