com.hn.translation.factory.AbstractTranslatorFactory Maven / Gradle / Ivy
package com.hn.translation.factory;
import com.hn.translation.*;
import com.hn.translation.annotation.TranslatorComponent;
import com.hn.translation.exception.TranslationException;
import com.hn.translation.impl.*;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
public abstract class AbstractTranslatorFactory
implements TFactory {
protected Map translatorMap = new HashMap();
private List workPackages = new ArrayList();
private List> translatorClasses = Arrays.asList(new Class[]{BaiduTranslator.class,
JinshanTranslator.class, TencentTranslator.class,GoogleTranslator.class, OmiTranslator.class, YoudaoTranslator.class});
public AbstractTranslatorFactory()
throws ClassNotFoundException, InstantiationException, IllegalAccessException, TranslationException, URISyntaxException {
initSystemTranslator();
initUserTranslator();
}
public AbstractTranslatorFactory(String[] workPackages)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, TranslationException, URISyntaxException {
this();
}
public AbstractTranslatorFactory(String workPackage)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, TranslationException, URISyntaxException {
this(new String[]{workPackage});
}
private void initSystemTranslator()
throws ClassNotFoundException, InstantiationException, IllegalAccessException, TranslationException, URISyntaxException {
for (Class> translatorClass : this.translatorClasses) {
TranslatorComponent component = (TranslatorComponent) translatorClass.getAnnotation(TranslatorComponent.class);
putTranslator(translatorClass, component);
}
}
private void putTranslator(Class> translatorClass, TranslatorComponent component) throws InstantiationException, IllegalAccessException {
if (component != null) {
String id = component.id();
if (this.translatorMap.containsKey(id)) {
throw new TranslationException("Id duplication exception");
}
this.translatorMap.put(component.id(), (Translator) translatorClass.newInstance());
}
}
private void initUserTranslator()
throws ClassNotFoundException, InstantiationException, IllegalAccessException, TranslationException, URISyntaxException {
for (String workPackage : this.workPackages) {
List workClassesName = getClassNameByPackage(workPackage);
for (String workClassName : workClassesName) {
Class> workClass = Class.forName(workClassName);
TranslatorComponent component = workClass.getAnnotation(TranslatorComponent.class);
putTranslator(workClass, component);
}
}
}
private List getClassNameByPackage(String packageName)
throws URISyntaxException {
List classesName = new ArrayList();
ClassLoader loader = getClass().getClassLoader();
URL url = loader.getResource(packageName.replace(".", "/"));
File packageDir = new File(new URI(url.getPath()).getPath());
for (File classFile : packageDir.listFiles()) {
String classNickName = classFile.getName();
classNickName = classNickName.substring(0, classNickName.indexOf('.'));
classesName.add(packageName + "." + classNickName);
}
return classesName;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy