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.spi.BirthmarkService;
19 import jp.sourceforge.stigmata.spi.ReflectedBirthmarkService;
21 import org.xml.sax.Attributes;
22 import org.xml.sax.SAXException;
23 import org.xml.sax.helpers.DefaultHandler;
26 * configuration file parser.
28 * @author Haruaki TAMADA
30 public class ConfigFileImporter{
31 private BirthmarkEnvironment environment;
33 public ConfigFileImporter(BirthmarkEnvironment environment){
34 this.environment = environment;
37 public BirthmarkEnvironment parse(InputStream in) throws IOException{
39 SAXParserFactory factory = SAXParserFactory.newInstance();
40 SAXParser parser = factory.newSAXParser();
41 Handler handler = new Handler(getEnvironment());
42 parser.parse(in, handler);
43 this.environment = handler.getEnvironment();
46 }catch(ParserConfigurationException e){
47 throw new IOException(e.getMessage());
48 }catch(SAXException e){
49 throw new IOException(e.getMessage());
53 public BirthmarkEnvironment getEnvironment(){
57 private static enum Part{
58 WELLKNOWN_CLASSES, PROPERTIES, CLASSPATH, SERVICES, FILTER_SET, FILTER_DEFINITION,
61 private static class Handler extends DefaultHandler{
62 private BirthmarkEnvironment environment;
63 private WellknownClassManager manager;
64 private Map<String, String> serviceMap;
65 private ComparisonPairFilterSet filter;
67 private boolean exclude;
68 private WellknownClassJudgeRule.MatchType matchType;
69 private WellknownClassJudgeRule.MatchPartType partType;
72 private String filterType, filterCriterion, attributeName;
73 private Map<String, String> filterAttributes = new HashMap<String, String>();
75 public Handler(BirthmarkEnvironment environment){
76 if(environment == null){
77 environment = Stigmata.getInstance().createEnvironment();
79 this.environment = environment;
80 this.manager = environment.getWellknownClassManager();
83 public BirthmarkEnvironment getEnvironment(){
88 public void startElement(String uri, String localName, String qname,
89 Attributes attributes) throws SAXException{
92 if(qname.equals("wellknown-classes")){
93 part = Part.WELLKNOWN_CLASSES;
95 else if(qname.equals("property")){
96 part = Part.PROPERTIES;
98 else if(qname.equals("classpath-list")){
99 part = Part.CLASSPATH;
101 else if(qname.equals("birthmark-service")){
102 part = Part.SERVICES;
104 else if(qname.equals("filterset-list")){
105 part = Part.FILTER_SET;
108 if(part == Part.FILTER_SET){
109 if(qname.equals("filterset")){
110 filter = new ComparisonPairFilterSet();
112 else if(qname.equals("filter")){
113 part = Part.FILTER_DEFINITION;
114 filterAttributes.clear();
117 else if(part == Part.WELLKNOWN_CLASSES){
118 if(qname.equals("exclude")){
121 else if(qname.equals("package-name")){
122 partType = WellknownClassJudgeRule.MatchPartType.PACKAGE_NAME;
124 else if(qname.equals("class-name")){
125 partType = WellknownClassJudgeRule.MatchPartType.CLASS_NAME;
127 else if(qname.equals("fully-name")){
128 partType = WellknownClassJudgeRule.MatchPartType.FULLY_NAME;
130 else if(qname.equals("suffix")){
131 matchType = WellknownClassJudgeRule.MatchType.SUFFIX;
133 else if(qname.equals("prefix")){
134 matchType = WellknownClassJudgeRule.MatchType.PREFIX;
136 else if(qname.equals("match")){
137 matchType = WellknownClassJudgeRule.MatchType.EXACT;
139 else if(qname.equals("not-match")){
140 matchType = WellknownClassJudgeRule.MatchType.NOT_MATCH;
146 public void characters(char[] data, int offset, int length) throws SAXException{
147 String value = new String(data, offset, length).trim();
149 if(value.length() > 0){
150 if(part == Part.PROPERTIES){
151 if(qname.equals("name")){
154 else if(qname.equals("value")){
155 environment.addProperty(key, value);
158 else if(part == Part.WELLKNOWN_CLASSES
159 && (qname.equals("suffix") || qname.equals("prefix") || qname.equals("match"))){
160 manager.add(new WellknownClassJudgeRule(value, matchType, partType, exclude));
163 else if(part == Part.CLASSPATH && qname.equals("classpath")){
165 environment.getClasspathContext().addClasspath(new URL(value));
166 }catch(MalformedURLException e){
167 throw new SAXException(e);
170 else if(part == Part.SERVICES){
171 if(serviceMap == null){
172 serviceMap = new HashMap<String, String>();
174 serviceMap.put(qname, value);
176 else if(part == Part.FILTER_SET){
177 if(qname.equals("name")){
178 filter.setName(value);
180 else if(qname.equals("match")){
181 if(value.equals("all")){
182 filter.setMatchAll();
185 filter.setMatchAny();
189 else if(part == Part.FILTER_DEFINITION){
190 if(qname.equals("filter-type")){
193 else if(qname.equals("criterion")){
194 filterCriterion = value;
196 else if(qname.equals("name")){
197 attributeName = value;
200 filterAttributes.put(attributeName, value);
207 public void endElement(String uri, String localname, String qname){
208 if(part == Part.SERVICES && qname.equals("birthmark-service")){
209 BirthmarkService service = new ReflectedBirthmarkService(
210 serviceMap.get("type"),
211 serviceMap.get("description"),
212 serviceMap.get("extractor"),
213 serviceMap.get("comparator")
215 environment.addService(service);
217 else if(part == Part.FILTER_DEFINITION && qname.equals("filter")){
218 ComparisonPairFilter f = environment.getFilterManager().buildFilter(
219 filterType, filterCriterion, filterAttributes
222 part = Part.FILTER_SET;
224 else if(part == Part.FILTER_SET && qname.equals("filterset")){
225 environment.getFilterManager().addFilterSet(filter);