OSDN Git Service

remove Revision tag
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / birthmarks / BirthmarkService.java
1 package jp.sourceforge.stigmata.birthmarks;
2
3 /*
4  * $Id$
5  */
6
7 import java.lang.reflect.Constructor;
8
9 import jp.sourceforge.stigmata.BirthmarkComparator;
10 import jp.sourceforge.stigmata.BirthmarkEnvironment;
11 import jp.sourceforge.stigmata.BirthmarkExtractor;
12 import jp.sourceforge.stigmata.spi.BirthmarkSpi;
13
14 /**
15  * Service provider interface for birthmarks which are defined in
16  * configuration files.
17  *
18  * @author Haruaki TAMADA
19  */
20 public class BirthmarkService extends AbstractBirthmarkService implements BirthmarkSpi{
21     private Class<? extends BirthmarkExtractor> extractorClass;
22     private Class<? extends BirthmarkComparator> comparatorClass;
23     private String type;
24     private String displayType;
25     private String description;
26     private BirthmarkExtractor extractorObject;
27     private BirthmarkComparator comparatorObject;
28     private boolean userDefined = true;
29     private BirthmarkEnvironment environment;
30
31     public BirthmarkService(BirthmarkEnvironment environment){
32         this.environment = environment;
33     }
34
35     public BirthmarkService(){
36     }
37
38     public void setBirthmarkEnvironment(BirthmarkEnvironment environment){
39         this.environment = environment;
40     }
41
42     public void setExtractorClassName(String extractor){
43         try{
44             Class<?> c;
45             if(environment == null){
46                 c = Class.forName(extractor);
47             }
48             else{
49                 c = environment.getClasspathContext().findClass(extractor);
50             }
51             extractorClass = c.asSubclass(BirthmarkExtractor.class);
52             extractorObject = null;
53         } catch(ClassNotFoundException e){
54             e.printStackTrace();
55         }
56     }
57
58     public void setComparatorClassName(String comparator){
59         try{
60             Class<?> c;
61             if(environment == null){
62                 c = Class.forName(comparator);
63             }
64             else{
65                 c = environment.getClasspathContext().findClass(comparator);
66             }
67             comparatorClass = c.asSubclass(BirthmarkComparator.class);
68             comparatorObject = null;
69         } catch(ClassNotFoundException e){
70             e.printStackTrace();
71         }
72     }
73
74     public void setType(String type){
75         this.type = type;
76     }
77
78     /**
79      * returns a type of the birthmark this service provides.
80      */
81     @Override
82     public String getType(){
83         return type;
84     }
85
86     public void setDisplayType(String displayType){
87         this.displayType = displayType;
88     }
89
90     @Override
91     public String getDisplayType(){
92         return displayType;
93     }
94
95     public void setDescription(String description){
96         this.description = description;
97     }
98
99     @Override
100     public String getDescription(){
101         String desc = description;
102         if(description == null){
103             desc = "";
104         }
105         return desc;
106     }
107
108     /**
109      * returns a description of the birthmark this service provides.
110      */
111     @Override
112     public String getDefaultDescription(){
113         return description;
114     }
115
116     @Override
117     public String getExtractorClassName(){
118         return extractorClass.getName();
119     }
120
121     /**
122      * returns a extractor for the birthmark of this service.
123      */
124     @Override
125     public BirthmarkExtractor getExtractor(){
126         if(extractorObject == null){
127             try{
128                 Constructor<? extends BirthmarkExtractor> c = extractorClass.getConstructor(BirthmarkSpi.class);
129                 extractorObject = c.newInstance(this);
130             } catch(Exception e){
131                 e.printStackTrace();
132             }
133         }
134         return extractorObject;
135     }
136
137     @Override
138     public String getComparatorClassName(){
139         return comparatorClass.getName();
140     }
141
142     /**
143      * returns a comparator for the birthmark of this service.
144      */
145     @Override
146     public BirthmarkComparator getComparator(){
147         if(comparatorObject == null){
148             try{
149                 Constructor<? extends BirthmarkComparator> c = comparatorClass.getConstructor(BirthmarkSpi.class);
150                 comparatorObject = c.newInstance(this);
151             } catch(Exception e){
152                 e.printStackTrace();
153             }
154         }
155         return comparatorObject;
156     }
157
158     @Override
159     public boolean isUserDefined(){
160         return userDefined;
161     }
162
163     public void setUserDefined(boolean userDefined){
164         this.userDefined = userDefined;
165     }
166 }
167