OSDN Git Service

c94e433fb75092bbb8085bc42e323770234e6149
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / utils / ConfigFileImporter.java
1 package jp.sourceforge.stigmata.utils;
2
3 /*
4  * $Id$
5  */
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import javax.xml.parsers.ParserConfigurationException;
15 import javax.xml.parsers.SAXParser;
16 import javax.xml.parsers.SAXParserFactory;
17
18 import jp.sourceforge.stigmata.BirthmarkEnvironment;
19 import jp.sourceforge.stigmata.ComparisonPairFilter;
20 import jp.sourceforge.stigmata.ComparisonPairFilterSet;
21 import jp.sourceforge.stigmata.Stigmata;
22 import jp.sourceforge.stigmata.birthmarks.BirthmarkService;
23
24 import org.xml.sax.Attributes;
25 import org.xml.sax.SAXException;
26 import org.xml.sax.helpers.DefaultHandler;
27
28 /**
29  * configuration file parser.
30  * 
31  * @author Haruaki TAMADA
32  * @version $Revision$ 
33  */
34 public class ConfigFileImporter{
35     private BirthmarkEnvironment environment;
36
37     public ConfigFileImporter(BirthmarkEnvironment environment){
38         this.environment = environment;
39     }
40
41     public BirthmarkEnvironment parse(InputStream in) throws IOException{
42         try{
43             SAXParserFactory factory = SAXParserFactory.newInstance();
44             SAXParser parser = factory.newSAXParser();
45             Handler handler = new Handler(getEnvironment());
46             parser.parse(in, handler);
47             this.environment = handler.getEnvironment();
48
49             return environment;
50         }catch(ParserConfigurationException e){
51             throw new IOException(e.getMessage());
52         }catch(SAXException e){
53             throw new IOException(e.getMessage());
54         }
55     }
56
57     public BirthmarkEnvironment getEnvironment(){
58         return environment;
59     }
60
61     private static enum Part{
62         WELLKNOWN_CLASSES, PROPERTIES, CLASSPATH, SERVICES, FILTER_SET, FILTER_DEFINITION,
63     }
64
65     private static class Handler extends DefaultHandler{
66         private BirthmarkEnvironment environment;
67         private WellknownClassManager manager;
68         private BirthmarkService service;
69         private ComparisonPairFilterSet filter;
70         private Part part;
71         private boolean exclude;
72         private WellknownClassJudgeRule.MatchType matchType;
73         private WellknownClassJudgeRule.MatchPartType partType;
74         private String qname;
75         private String key;
76         private String filterType, filterCriterion, attributeName;
77         private Map<String, String> filterAttributes = new HashMap<String, String>();
78
79         public Handler(BirthmarkEnvironment environment){
80             if(environment == null){
81                 environment = Stigmata.getInstance().createEnvironment();
82             }
83             this.environment = environment;
84             this.manager = environment.getWellknownClassManager();
85         }
86
87         public BirthmarkEnvironment getEnvironment(){
88             return environment;
89         }
90
91         @Override
92         public void startElement(String uri, String localName, String qname,
93                                  Attributes attributes) throws SAXException{
94             this.qname = qname;
95
96             if(qname.equals("wellknown-classes")){
97                 part = Part.WELLKNOWN_CLASSES;
98             }
99             else if(qname.equals("property")){
100                 part = Part.PROPERTIES;
101             }
102             else if(qname.equals("classpath-list")){
103                 part = Part.CLASSPATH;
104             }
105             else if(qname.equals("birthmark-service")){
106                 part = Part.SERVICES;
107                 service = new BirthmarkService();
108                 service.setUserDefined(false);
109             }
110             else if(qname.equals("filterset-list")){
111                 part = Part.FILTER_SET;
112             }
113
114             if(part == Part.FILTER_SET){
115                 if(qname.equals("filterset")){
116                     filter = new ComparisonPairFilterSet();
117                 }
118                 else if(qname.equals("filter")){
119                     part = Part.FILTER_DEFINITION;
120                     filterAttributes.clear();
121                 }
122             }
123             else if(part == Part.WELLKNOWN_CLASSES){
124                 if(qname.equals("exclude")){
125                     exclude = true;
126                 }
127                 else if(qname.equals("package-name")){
128                     partType = WellknownClassJudgeRule.MatchPartType.PACKAGE_NAME;
129                 }
130                 else if(qname.equals("class-name")){
131                     partType = WellknownClassJudgeRule.MatchPartType.CLASS_NAME;
132                 }
133                 else if(qname.equals("fully-name")){
134                     partType = WellknownClassJudgeRule.MatchPartType.FULLY_NAME;
135                 }
136                 else if(qname.equals("suffix")){
137                     matchType = WellknownClassJudgeRule.MatchType.SUFFIX;
138                 }
139                 else if(qname.equals("prefix")){
140                     matchType = WellknownClassJudgeRule.MatchType.PREFIX;
141                 }
142                 else if(qname.equals("match")){
143                     matchType = WellknownClassJudgeRule.MatchType.EXACT;
144                 }
145                 else if(qname.equals("not-match")){
146                     matchType = WellknownClassJudgeRule.MatchType.NOT_MATCH;
147                 }
148             }
149         }
150
151         @Override
152         public void characters(char[] data, int offset, int length) throws SAXException{
153             String value = new String(data, offset, length).trim();
154
155             if(value.length() > 0){
156                 if(part == Part.PROPERTIES){
157                     if(qname.equals("name")){
158                         key = value;
159                     }
160                     else if(qname.equals("value")){
161                         environment.addProperty(key, value);
162                     }
163                 }
164                 else if(part == Part.WELLKNOWN_CLASSES
165                         && (qname.equals("suffix") || qname.equals("prefix") || qname.equals("match"))){
166                     manager.add(new WellknownClassJudgeRule(value, matchType, partType, exclude));
167                     exclude = false;
168                 }
169                 else if(part == Part.CLASSPATH && qname.equals("classpath")){
170                     try{
171                         environment.getClasspathContext().addClasspath(
172                                 new URL(value));
173                     }catch(MalformedURLException e){
174                         throw new SAXException(e);
175                     }
176                 }
177                 else if(part == Part.SERVICES){
178                     if(qname.equals("type"))
179                         service.setType(value);
180                     else if(qname.equals("display-name"))
181                         service.setDisplayType(value);
182                     else if(qname.equals("description"))
183                         service.setDescription(value);
184                     else if(qname.equals("extractor"))
185                         service.setExtractorClassName(value);
186                     else if(qname.equals("comparator"))
187                         service.setComparatorClassName(value);
188                 }
189                 else if(part == Part.FILTER_SET){
190                     if(qname.equals("name")){
191                         filter.setName(value);
192                     }
193                     else if(qname.equals("match")){
194                         if(value.equals("all")){
195                             filter.setMatchAll();
196                         }
197                         else{
198                             filter.setMatchAny();
199                         }
200                     }
201                 }
202                 else if(part == Part.FILTER_DEFINITION){
203                     if(qname.equals("filter-type")){
204                         filterType = value;
205                     }
206                     else if(qname.equals("criterion")){
207                         filterCriterion = value;
208                     }
209                     else if(qname.equals("name")){
210                         attributeName = value;
211                     }
212                     else{
213                         filterAttributes.put(attributeName, value);
214                     }
215                 }
216             }
217         }
218
219         @Override
220         public void endElement(String uri, String localname, String qname){
221             if(part == Part.SERVICES && qname.equals("birthmark-service")){
222                 environment.addService(service);
223                 service = null;
224             }
225             else if(part == Part.FILTER_DEFINITION && qname.equals("filter")){
226                 ComparisonPairFilter f = environment.getFilterManager().buildFilter(
227                     filterType, filterCriterion, filterAttributes
228                 );
229                 filter.addFilter(f);
230                 part = Part.FILTER_SET;
231             }
232             else if(part == Part.FILTER_SET && qname.equals("filterset")){
233                 environment.getFilterManager().addFilterSet(filter);
234             }
235         }
236     }
237 }