Commit f46177d9 authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Merge branch 'release/2.0.0'

parents 32296caa 80d3355f
Pipeline #141 skipped
......@@ -3,7 +3,7 @@
<groupId>de.cebitec.common</groupId>
<artifactId>dbxref</artifactId>
<version>1.3.0</version>
<version>2.0.0</version>
<packaging>bundle</packaging>
<name>DbXRef library</name>
......@@ -20,8 +20,8 @@
<scm>
<developerConnection>scm:git:https://git.computational.bio.uni-giessen.de/gendb3/lib-dbxref.git</developerConnection>
<tag>HEAD</tag>
</scm>
<tag>HEAD</tag>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
......@@ -96,6 +96,11 @@
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
</dependencies>
<build>
......@@ -119,8 +124,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgument>-Xlint:unchecked</compilerArgument>
......@@ -147,6 +152,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>external.atlassian.jgitflow</groupId>
<artifactId>jgitflow-maven-plugin</artifactId>
<version>1.0-m5.1</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<localOnly>true</localOnly>
<noDeploy>true</noDeploy>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
......@@ -14,22 +14,40 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref;
package de.cebitec.common.dbxref.api;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.api.Resource.Type;
import java.util.Collection;
import java.util.Iterator;
import java.util.ServiceLoader;
/**
* A DbXRefService resolves dbxrefs to resources. In order to obtain a dbxref use the factory methods.
*
* @see #create(CharSequence)
* @see #create(CharSequence, CharSequence)
*
* @see #create(CharSequence)
* @see #create(CharSequence, CharSequence)
*
* @author Lukas Jelonek {@literal <Lukas.Jelonek at computational.bio.uni-giessen.de>}
*/
public interface DbXRefService {
/**
* Retrieves an instance of DbXRefService via the serviceloader.
*
* @return
*/
static DbXRefService getInstance() {
ServiceLoader<DbXRefService> loader = ServiceLoader.load(DbXRefService.class);
final Iterator<DbXRefService> it = loader.iterator();
if (it.hasNext()) {
return it.next();
} else {
throw new IllegalStateException("No DbXRefService provided");
}
}
/**
* Factory method for database cross references. Can instantiate any database cross reference, regardless of a valid
* database or valid identifier.
......@@ -61,11 +79,10 @@ public interface DbXRefService {
* Resolves resource links for the given reference and filtered by the given type. If multiple resolvers are
* available for the resource an aggregation of the resources is returned.
*
* @param <T> The type of the requested resources.
* @param ref The dbxref. Must not be null.
* @param targetClass The requested resource type. May be null. Then all resources are retrieved.
* @param targetType The requested resource type. May be null. Then all resources are retrieved.
* @return A collection of resources for the dbxref. If no resources can be resolved the collection will be empty.
*/
<T extends Resource> Collection<? extends T> resolve(DbXRef ref, Class<T> targetClass);
Collection<? extends Resource> resolve(DbXRef ref, Type targetType);
}
......@@ -27,12 +27,22 @@ import java.net.URI;
*/
public interface Resource {
public enum Type {
Xml,
Text,
Html
}
URI getUri();
/**
* Returns the Recourcetext.
*
* @return String that represents the Recourcetext.
*/
String getResourceText();
DbXRef getDbXRef();
Type getType();
}
/*
* Copyright (C) 2012 cb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.api;
import java.net.URI;
/**
* A resource that points to a plain text document.
*
* @author cb
*/
public class TextResource extends AbstractResource {
public TextResource(URI uri) {
super(uri);
}
}
/*
* Copyright (C) 2012 cb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.api;
import java.net.URI;
/**
* A resource that points to an xml document.
*
* @author cb
*/
public class XMLResource extends AbstractResource {
public XMLResource(URI uri) {
super(uri);
}
}
......@@ -14,11 +14,13 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref;
package de.cebitec.common.dbxref.impl;
import de.cebitec.common.dbxref.api.DbXRefService;
import com.google.common.collect.Iterables;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import java.util.Collection;
......@@ -37,21 +39,10 @@ import org.kohsuke.MetaInfServices;
*
* @author cb
* @author Lukas Jelonek {@literal <ljelonek at cebitec.uni-bielefeld.de>}
* @deprecated Don't use this class directly. Use the service interface instead. The visibility of this implementation
* will be changed to package private in a future release.
*/
@MetaInfServices
public class DbXRefResourceRegistry implements DbXRefService {
private static DbXRefResourceRegistry instance;
public static DbXRefResourceRegistry getDefault() {
if (instance == null) {
instance = new DbXRefResourceRegistry();
}
return instance;
}
Iterable<? extends DbXRefResolver> getResolvers() {
// service provider lookup von resolvern
Iterator<DbXRefResolver> it = ServiceLoader.load(DbXRefResolver.class, this.getClass().getClassLoader()).iterator();
......@@ -115,25 +106,25 @@ public class DbXRefResourceRegistry implements DbXRefService {
* @throws DbXrefResourceException If no handler can be found.
*/
@Override
public <T extends Resource> Collection<? extends T> resolve(DbXRef ref, Class<T> targetClass) {
Set<T> result = new HashSet<>();
public Collection<? extends Resource> resolve(DbXRef ref, Type targetType) {
Set<Resource> result = new HashSet<>();
if (!hasResolverFor(ref)) {
throw new DbXrefResourceException("No handler found for database '" + ref.getDatabase() + "'. Supported databases: " + getSupportedDatabases());
}
for (DbXRefResolver dbXResource : getResolversFor(ref)) {
result.addAll(filter(dbXResource.resolve(ref), targetClass));
result.addAll(filter(dbXResource.resolve(ref), targetType));
}
return result;
}
public <T extends Resource> Collection<? extends T> filter(Iterable<? extends Resource> iterable, Class<T> targetClass) {
List<T> filtered = new LinkedList<>();
public Collection<? extends Resource> filter(Iterable<? extends Resource> iterable, Type targetType) {
List<Resource> filtered = new LinkedList<>();
for (Resource r : iterable) {
if (targetClass.isInstance(r)) {
filtered.add(targetClass.cast(r));
if (r.getType() == targetType) {
filtered.add(r);
}
}
return filtered;
......
......@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref;
package de.cebitec.common.dbxref.impl;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import de.cebitec.common.dbxref.api.DbXRef;
......
......@@ -14,13 +14,13 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref;
package de.cebitec.common.dbxref.impl;
/**
*
* @author Lukas Jelonek {@literal <ljelonek at cebitec.uni-bielefeld.de>}
*/
public class DbXrefParserException extends RuntimeException {
class DbXrefParserException extends RuntimeException {
/**
* Creates a new instance of
......
......@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref;
package de.cebitec.common.dbxref.impl;
/**
*
......
/*
* Copyright (C) 2012 cb
* Copyright (C) 2016 Lukas Jelonek - Lukas.Jelonek at computational.bio.uni-giessen.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -14,8 +14,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.api;
package de.cebitec.common.dbxref.impl;
import com.google.common.base.Preconditions;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
......@@ -24,21 +27,37 @@ import org.apache.commons.io.IOUtils;
/**
*
* @author cb
*/
public abstract class AbstractResource implements Resource {
public class GenericResource implements Resource {
private final Type type;
private final DbXRef dbxref;
protected final URI uri;
public AbstractResource(URI uri) {
public GenericResource(Type type, DbXRef dbxref, URI uri) {
Preconditions.checkNotNull(type);
Preconditions.checkNotNull(dbxref);
Preconditions.checkNotNull(uri);
this.type = type;
this.dbxref = dbxref;
this.uri = uri;
}
@Override
public Type getType() {
return type;
}
@Override
public URI getUri() {
return uri;
}
@Override
public DbXRef getDbXRef() {
return dbxref;
}
@Override
public String getResourceText() {
try {
......@@ -60,4 +79,8 @@ public abstract class AbstractResource implements Resource {
}
}
@Override
public String toString() {
return "GenericResource{" + "type=" + type + ", dbxref=" + dbxref + ", uri=" + uri + '}';
}
}
/*
* Copyright (C) 2016 Lukas Jelonek - Lukas.Jelonek at computational.bio.uni-giessen.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.providers;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.kohsuke.MetaInfServices;
/**
*
*/
@MetaInfServices(DbXRefResolver.class)
public class CompositeProvider implements DbXRefResolver {
private final List<DbXRefResolver> providers = new ArrayList<>();
public CompositeProvider() {
Gson gson = new Gson();
InputStream is = this.getClass().getResourceAsStream("providers.json");
InputStreamReader isr = new InputStreamReader(is);
@SuppressWarnings("unchecked")
List<Map<String, List<String>>> fromJson = gson.fromJson(isr, List.class);
for (Map<String, List<String>> entry : fromJson) {
GenericDbXRefProvider.ResolverData data = new GenericDbXRefProvider.ResolverData();
for (String prefix : entry.get("prefixes")) {
data.addPrefix(prefix);
}
if (entry.containsKey("html")) {
for (String template : entry.get("html")) {
data.addResourceTemplate(Resource.Type.Html, template);
}
}
if (entry.containsKey("xml")) {
for (String template : entry.get("xml")) {
data.addResourceTemplate(Resource.Type.Xml, template);
}
}
if (entry.containsKey("text")) {
for (String template : entry.get("text")) {
data.addResourceTemplate(Resource.Type.Text, template);
}
}
GenericDbXRefProvider provider = new GenericDbXRefProvider(data);
providers.add(provider);
}
}
@Override
public Iterable<String> getDatabasePrefixes() {
List<String> prefixes = new ArrayList<>();
for (DbXRefResolver provider : providers) {
Iterables.addAll(prefixes, provider.getDatabasePrefixes());
}
return prefixes;
}
@Override
public boolean canHandle(DbXRef ref) {
for (DbXRefResolver provider : providers) {
if (provider.canHandle(ref)) {
return true;
}
}
return false;
}
@Override
public Collection<? extends Resource> resolve(DbXRef ref) {
for (DbXRefResolver provider : providers) {
if (provider.canHandle(ref)) {
return provider.resolve(ref);
}
}
return Collections.<Resource>emptyList();
}
}
/*
* Copyright (C) 2014 Lukas Jelonek <ljelonek at cebitec.uni-bielefeld.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
/**
*
* @author Lukas Jelonek {@literal <ljelonek at cebitec.uni-bielefeld.de>}
*/
@MetaInfServices(DbXRefResolver.class)
public class ECProvider extends GenericDbXRefProvider {
public ECProvider() {
super(createResolverData());
}
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("EC", "Enzyme");
resolverData.addResourceTemplate(HTMLResource.class, "http://enzyme.expasy.org/EC/%i");
return resolverData;
}
}
/*
* Copyright (C) 2012 cb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.XMLResource;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
/**
*
* @author cb
*/
@MetaInfServices(DbXRefResolver.class)
public class GIProvider extends GenericDbXRefProvider {
public GIProvider() {
super(createResolverData());
}
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("GI");
resolverData.addResourceTemplate(HTMLResource.class, "http://www.ncbi.nlm.nih.gov/protein/GI:%i");
resolverData.addResourceTemplate(XMLResource.class, "http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&db=protein&dopt=xml&sort=&val=%i&retmode=file");
return resolverData;
}
}
/*
* Copyright (C) 2012 cb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.XMLResource;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;