-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
FormatsManager.java
53 lines (42 loc) · 1.87 KB
/
FormatsManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.drools.compiler.kie.builder.impl;
import org.kie.api.io.ResourceType;
import java.util.HashMap;
import java.util.Map;
public class FormatsManager {
private FormatsManager() { }
private static final FormatsManager INSTANCE = new FormatsManager();
static FormatsManager get() {
return INSTANCE;
}
private Map<String, String> registry = new HashMap<String, String>() {{
put("gdst", "org.drools.guvnor.models.guided.dtable.backend.GuidedDecisionTableConverter");
put("scgd", "org.drools.guvnor.models.guided.scorecard.backend.GuidedScoreCardConverter");
put("template", "org.drools.guvnor.models.guided.template.backend.GuidedRuleTemplateConverter");
}};
private Map<String, FormatConverter> converters = new HashMap<String, FormatConverter>();
public FormatConverter getConverterFor(String fileName) {
return isKieExtension(fileName) ? FormatConverter.DummyConverter.INSTANCE : getExternalConverter(fileName);
}
private FormatConverter getExternalConverter(String fileName) {
int dotPos = fileName.lastIndexOf('.');
if (dotPos < 0) {
return null;
}
String extension = fileName.substring(dotPos+1);
FormatConverter converter = converters.get(extension);
if (converter == null) {
String converterClassName = registry.get(extension);
if (converterClassName != null) {
try {
converter = ((Class<? extends FormatConverter>)Class.forName(converterClassName)).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return converter;
}
static boolean isKieExtension(String fileName) {
return !fileName.endsWith( ".java" ) && ResourceType.determineResourceType(fileName) != null;
}
}