1 package jp.sourceforge.stigmata.utils;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.MalformedURLException;
7 import java.util.HashMap;
10 import javax.xml.parsers.ParserConfigurationException;
11 import javax.xml.parsers.SAXParser;
12 import javax.xml.parsers.SAXParserFactory;
14 import jp.sourceforge.stigmata.BirthmarkEnvironment;
15 import jp.sourceforge.stigmata.ComparisonPairFilter;
16 import jp.sourceforge.stigmata.ComparisonPairFilterSet;
17 import jp.sourceforge.stigmata.Stigmata;
18 import jp.sourceforge.stigmata.birthmarks.BirthmarkService;
20 import org.xml.sax.Attributes;
21 import org.xml.sax.SAXException;
22 import org.xml.sax.helpers.DefaultHandler;
25 * configuration file parser.
27 * @author Haruaki TAMADA
29 public class ConfigFileImporter{
30 private BirthmarkEnvironment environment;
32 public ConfigFileImporter(BirthmarkEnvironment environment){
33 this.environment = environment;
36 public BirthmarkEnvironment parse(InputStream in) throws IOException{
38 SAXParserFactory factory = SAXParserFactory.newInstance();
39 SAXParser parser = factory.newSAXParser();
40 Handler handler = new Handler(getEnvironment());
41 parser.parse(in, handler);
42 this.environment = handler.getEnvironment();
45 }catch(ParserConfigurationException e){
46 throw new IOException(e.getMessage());
47 }catch(SAXException e){
48 throw new IOException(e.getMessage());
52 public BirthmarkEnvironment getEnvironment(){
56 private static enum Part{
57 WELLKNOWN_CLASSES, PROPERTIES, CLASSPATH, SERVICES, FILTER_SET, FILTER_DEFINITION,
60 private static class Handler extends DefaultHandler{
61 private BirthmarkEnvironment environment;
62 private WellknownClassManager manager;
63 private BirthmarkService service;
64 private ComparisonPairFilterSet filter;
66 private boolean exclude;
67 private WellknownClassJudgeRule.MatchType matchType;
68 private WellknownClassJudgeRule.MatchPartType partType;
71 private String filterType, filterCriterion, attributeName;
72 private Map<String, String> filterAttributes = new HashMap<String, String>();
74 public Handler(BirthmarkEnvironment environment){
75 if(environment == null){
76 environment = Stigmata.getInstance().createEnvironment();
78 this.environment = environment;
79 this.manager = environment.getWellknownClassManager();
82 public BirthmarkEnvironment getEnvironment(){
87 public void startElement(String uri, String localName, String qname,
88 Attributes attributes) throws SAXException{
91 if(qname.equals("wellknown-classes")){
92 part = Part.WELLKNOWN_CLASSES;
94 else if(qname.equals("property")){
95 part = Part.PROPERTIES;
97 else if(qname.equals("classpath-list")){
98 part = Part.CLASSPATH;
100 else if(qname.equals("birthmark-service")){
101 part = Part.SERVICES;
102 service = new BirthmarkService();
103 service.setUserDefined(false);
105 else if(qname.equals("filterset-list")){
106 part = Part.FILTER_SET;
109 if(part == Part.FILTER_SET){
110 if(qname.equals("filterset")){
111 filter = new ComparisonPairFilterSet();
113 else if(qname.equals("filter")){
114 part = Part.FILTER_DEFINITION;
115 filterAttributes.clear();
118 else if(part == Part.WELLKNOWN_CLASSES){
119 if(qname.equals("exclude")){
122 else if(qname.equals("package-name")){
123 partType = WellknownClassJudgeRule.MatchPartType.PACKAGE_NAME;
125 else if(qname.equals("class-name")){
126 partType = WellknownClassJudgeRule.MatchPartType.CLASS_NAME;
128 else if(qname.equals("fully-name")){
129 partType = WellknownClassJudgeRule.MatchPartType.FULLY_NAME;
131 else if(qname.equals("suffix")){
132 matchType = WellknownClassJudgeRule.MatchType.SUFFIX;
134 else if(qname.equals("prefix")){
135 matchType = WellknownClassJudgeRule.MatchType.PREFIX;
137 else if(qname.equals("match")){
138 matchType = WellknownClassJudgeRule.MatchType.EXACT;
140 else if(qname.equals("not-match")){
141 matchType = WellknownClassJudgeRule.MatchType.NOT_MATCH;
147 public void characters(char[] data, int offset, int length) throws SAXException{
148 String value = new String(data, offset, length).trim();
150 if(value.length() > 0){
151 if(part == Part.PROPERTIES){
152 if(qname.equals("name")){
155 else if(qname.equals("value")){
156 environment.addProperty(key, value);
159 else if(part == Part.WELLKNOWN_CLASSES
160 && (qname.equals("suffix") || qname.equals("prefix") || qname.equals("match"))){
161 manager.add(new WellknownClassJudgeRule(value, matchType, partType, exclude));
164 else if(part == Part.CLASSPATH && qname.equals("classpath")){
166 environment.getClasspathContext().addClasspath(
168 }catch(MalformedURLException e){
169 throw new SAXException(e);
172 else if(part == Part.SERVICES){
173 if(qname.equals("type"))
174 service.setType(value);
175 else if(qname.equals("display-name"))
176 service.setDisplayType(value);
177 else if(qname.equals("description"))
178 service.setDescription(value);
179 else if(qname.equals("extractor"))
180 service.setExtractorClassName(value);
181 else if(qname.equals("comparator"))
182 service.setComparatorClassName(value);
184 else if(part == Part.FILTER_SET){
185 if(qname.equals("name")){
186 filter.setName(value);
188 else if(qname.equals("match")){
189 if(value.equals("all")){
190 filter.setMatchAll();
193 filter.setMatchAny();
197 else if(part == Part.FILTER_DEFINITION){
198 if(qname.equals("filter-type")){
201 else if(qname.equals("criterion")){
202 filterCriterion = value;
204 else if(qname.equals("name")){
205 attributeName = value;
208 filterAttributes.put(attributeName, value);
215 public void endElement(String uri, String localname, String qname){
216 if(part == Part.SERVICES && qname.equals("birthmark-service")){
217 environment.addService(service);
220 else if(part == Part.FILTER_DEFINITION && qname.equals("filter")){
221 ComparisonPairFilter f = environment.getFilterManager().buildFilter(
222 filterType, filterCriterion, filterAttributes
225 part = Part.FILTER_SET;
227 else if(part == Part.FILTER_SET && qname.equals("filterset")){
228 environment.getFilterManager().addFilterSet(filter);