mirror of
https://github.com/sismics/docs.git
synced 2024-11-25 23:27:57 +01:00
scan classpath for format handlers
This commit is contained in:
parent
af217b4831
commit
c9606f98d3
@ -1,6 +1,7 @@
|
|||||||
package com.sismics.docs.core.util.format;
|
package com.sismics.docs.core.util.format;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.sismics.util.ClasspathScanner;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -11,17 +12,10 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class FormatHandlerUtil {
|
public class FormatHandlerUtil {
|
||||||
/**
|
/**
|
||||||
* List of format handlers.
|
* List of format handlers scanned in the classpath.
|
||||||
*/
|
*/
|
||||||
private static final List<Class<? extends FormatHandler>> FORMAT_HANDLERS = Lists.newArrayList(
|
private static final List<Class<? extends FormatHandler>> FORMAT_HANDLERS = Lists.newArrayList(
|
||||||
DocxFormatHandler.class,
|
new ClasspathScanner<FormatHandler>().findClasses(FormatHandler.class, "com.sismics.docs.core.util.format"));
|
||||||
PptxFormatHandler.class,
|
|
||||||
OdtFormatHandler.class,
|
|
||||||
VideoFormatHandler.class,
|
|
||||||
PdfFormatHandler.class,
|
|
||||||
TextPlainFormatHandler.class,
|
|
||||||
ImageFormatHandler.class
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a suitable format handler for this MIME type.
|
* Find a suitable format handler for this MIME type.
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.sismics.util;
|
||||||
|
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
import com.google.common.reflect.ClassPath;
|
||||||
|
import com.sismics.docs.core.util.format.PdfFormatHandler;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Classes scanner.
|
||||||
|
*/
|
||||||
|
public class ClasspathScanner<T> {
|
||||||
|
/**
|
||||||
|
* Logger.
|
||||||
|
*/
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(PdfFormatHandler.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find classes assignable from another.
|
||||||
|
*
|
||||||
|
* @param topClass Top class or interface
|
||||||
|
* @param pkg In this package
|
||||||
|
* @return Set of classes
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public Set<Class<T>> findClasses(Class<T> topClass, String pkg) {
|
||||||
|
Set<Class<T>> classes = Sets.newHashSet();
|
||||||
|
try {
|
||||||
|
for (ClassPath.ClassInfo classInfo : ClassPath.from(topClass.getClassLoader()).getTopLevelClasses(pkg)) {
|
||||||
|
Class<?> clazz = classInfo.load();
|
||||||
|
if (topClass.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers()) && !clazz.isInterface()) {
|
||||||
|
classes.add((Class<T>) clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Error loading format handlers", e);
|
||||||
|
}
|
||||||
|
log.info("Found " + classes.size() + " classes for " + topClass.getSimpleName());
|
||||||
|
return classes;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user