1
2
3
4 package net.sourceforge.pmd.cpd;
5
6 import java.util.Properties;
7
8 public class LanguageFactory {
9
10
11 public static String[] supportedLanguages = new String[]{"java", "jsp", "cpp", "c", "php", "ruby","fortran", "ecmascript","cs" };
12
13 private static final String SUFFIX = "Language";
14 public static final String EXTENSION = "extension";
15 public static final String BY_EXTENSION = "by_extension";
16 private static final String PACKAGE = "net.sourceforge.pmd.cpd.";
17
18 public Language createLanguage(String language) {
19 return createLanguage(language, new Properties());
20 }
21
22 public Language createLanguage(String language, Properties properties)
23 {
24 language = this.languageAliases(language);
25
26 Language implementation;
27 try {
28 implementation = this.dynamicLanguageImplementationLoad(this.languageConventionSyntax(language));
29 if ( implementation == null )
30 {
31
32 implementation = this.dynamicLanguageImplementationLoad(language.toUpperCase());
33
34
35 if ( implementation == null )
36 {
37
38
39 return new AnyLanguage(language);
40 }
41 }
42 return implementation;
43 } catch (InstantiationException e) {
44 e.printStackTrace();
45 } catch (IllegalAccessException e) {
46 e.printStackTrace();
47 }
48 return null;
49 }
50
51 private String languageAliases(String language)
52 {
53
54 if ( "c".equals(language) ) {
55 return "cpp";
56 }
57 return language;
58 }
59
60 private Language dynamicLanguageImplementationLoad(String language) throws InstantiationException, IllegalAccessException
61 {
62 try {
63 return (Language) this.getClass().getClassLoader().loadClass(
64 PACKAGE + language + SUFFIX).newInstance();
65 } catch (ClassNotFoundException e) {
66
67
68 return null;
69 } catch (NoClassDefFoundError e) {
70
71
72
73
74 return null;
75 }
76 }
77
78
79
80
81
82
83
84 private String languageConventionSyntax(String language) {
85 return Character.toUpperCase(language.charAt(0)) + language.substring(1, language.length()).toLowerCase();
86 }
87 }