1 package jp.sourceforge.stigmata.utils;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.net.MalformedURLException;
11 import java.util.HashMap;
14 import javax.xml.parsers.ParserConfigurationException;
15 import javax.xml.parsers.SAXParser;
16 import javax.xml.parsers.SAXParserFactory;
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;
24 import org.xml.sax.Attributes;
25 import org.xml.sax.SAXException;
26 import org.xml.sax.helpers.DefaultHandler;
29 * configuration file parser.
31 * @author Haruaki TAMADA
34 public class ConfigFileImporter{
35 private BirthmarkEnvironment environment;
37 public ConfigFileImporter(BirthmarkEnvironment environment){
38 this.environment = environment;
41 public BirthmarkEnvironment parse(InputStream in) throws IOException{
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();
50 }catch(ParserConfigurationException e){
51 throw new IOException(e.getMessage());
52 }catch(SAXException e){
53 throw new IOException(e.getMessage());
57 public BirthmarkEnvironment getEnvironment(){
61 private static enum Part{
62 WELLKNOWN_CLASSES, PROPERTIES, CLASSPATH, SERVICES, FILTER_SET, FILTER_DEFINITION,
65 private static class Handler extends DefaultHandler{
66 private BirthmarkEnvironment environment;
67 private WellknownClassManager manager;
68 private BirthmarkService service;
69 private ComparisonPairFilterSet filter;
71 private boolean exclude;
72 private WellknownClassJudgeRule.MatchType matchType;
73 private WellknownClassJudgeRule.MatchPartType partType;
76 private String filterType, filterCriterion, attributeName;
77 private Map<String, String> filterAttributes = new HashMap<String, String>();
79 public Handler(BirthmarkEnvironment environment){
80 if(environment == null){
81 environment = Stigmata.getInstance().createEnvironment();
83 this.environment = environment;
84 this.manager = environment.getWellknownClassManager();
87 public BirthmarkEnvironment getEnvironment(){
92 public void startElement(String uri, String localName, String qname,
93 Attributes attributes) throws SAXException{
96 if(qname.equals("wellknown-classes")){
97 part = Part.WELLKNOWN_CLASSES;
99 else if(qname.equals("property")){
100 part = Part.PROPERTIES;
102 else if(qname.equals("classpath-list")){
103 part = Part.CLASSPATH;
105 else if(qname.equals("birthmark-service")){
106 part = Part.SERVICES;
107 service = new BirthmarkService();
108 service.setUserDefined(false);
110 else if(qname.equals("filterset-list")){
111 part = Part.FILTER_SET;
114 if(part == Part.FILTER_SET){
115 if(qname.equals("filterset")){
116 filter = new ComparisonPairFilterSet();
118 else if(qname.equals("filter")){
119 part = Part.FILTER_DEFINITION;
120 filterAttributes.clear();
123 else if(part == Part.WELLKNOWN_CLASSES){
124 if(qname.equals("exclude")){
127 else if(qname.equals("package-name")){
128 partType = WellknownClassJudgeRule.MatchPartType.PACKAGE_NAME;
130 else if(qname.equals("class-name")){
131 partType = WellknownClassJudgeRule.MatchPartType.CLASS_NAME;
133 else if(qname.equals("fully-name")){
134 partType = WellknownClassJudgeRule.MatchPartType.FULLY_NAME;
136 else if(qname.equals("suffix")){
137 matchType = WellknownClassJudgeRule.MatchType.SUFFIX;
139 else if(qname.equals("prefix")){
140 matchType = WellknownClassJudgeRule.MatchType.PREFIX;
142 else if(qname.equals("match")){
143 matchType = WellknownClassJudgeRule.MatchType.EXACT;
145 else if(qname.equals("not-match")){
146 matchType = WellknownClassJudgeRule.MatchType.NOT_MATCH;
152 public void characters(char[] data, int offset, int length) throws SAXException{
153 String value = new String(data, offset, length).trim();
155 if(value.length() > 0){
156 if(part == Part.PROPERTIES){
157 if(qname.equals("name")){
160 else if(qname.equals("value")){
161 environment.addProperty(key, value);
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));
169 else if(part == Part.CLASSPATH && qname.equals("classpath")){
171 environment.getClasspathContext().addClasspath(
173 }catch(MalformedURLException e){
174 throw new SAXException(e);
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);
189 else if(part == Part.FILTER_SET){
190 if(qname.equals("name")){
191 filter.setName(value);
193 else if(qname.equals("match")){
194 if(value.equals("all")){
195 filter.setMatchAll();
198 filter.setMatchAny();
202 else if(part == Part.FILTER_DEFINITION){
203 if(qname.equals("filter-type")){
206 else if(qname.equals("criterion")){
207 filterCriterion = value;
209 else if(qname.equals("name")){
210 attributeName = value;
213 filterAttributes.put(attributeName, value);
220 public void endElement(String uri, String localname, String qname){
221 if(part == Part.SERVICES && qname.equals("birthmark-service")){
222 environment.addService(service);
225 else if(part == Part.FILTER_DEFINITION && qname.equals("filter")){
226 ComparisonPairFilter f = environment.getFilterManager().buildFilter(
227 filterType, filterCriterion, filterAttributes
230 part = Part.FILTER_SET;
232 else if(part == Part.FILTER_SET && qname.equals("filterset")){
233 environment.getFilterManager().addFilterSet(filter);