OSDN Git Service

BirthmarkSpi -> BirthmarkService
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / utils / ConfigFileExporter.java
1 package jp.sourceforge.stigmata.utils;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.net.URL;
6 import java.util.Iterator;
7 import java.util.Map;
8
9 import jp.sourceforge.stigmata.BirthmarkEnvironment;
10 import jp.sourceforge.stigmata.ComparisonPairFilter;
11 import jp.sourceforge.stigmata.ComparisonPairFilterSet;
12 import jp.sourceforge.stigmata.spi.BirthmarkService;
13 import jp.sourceforge.stigmata.spi.ReflectedBirthmarkService;
14
15 import org.apache.commons.beanutils.BeanUtils;
16
17 /**
18  * Export birthmark environment to xml file.
19  * 
20  * @author Haruaki TAMADA
21  */
22 public class ConfigFileExporter{
23     private BirthmarkEnvironment environment;
24
25     public ConfigFileExporter(BirthmarkEnvironment environment){
26         this.environment = environment;
27     }
28
29     public void export(BirthmarkEnvironment environment, PrintWriter out) throws IOException{
30         new ConfigFileExporter(environment).export(out);
31     }
32
33     public void export(PrintWriter out) throws IOException{
34         out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
35         out.println("<stigmata>");
36
37         exportServices(out);
38         exportFilters(out);
39         exportWellknownClasses(out);
40         exportClasspath(out);
41         exportProperties(out);
42
43         out.println("</stigmata>");
44         out.flush();
45     }
46
47     private void exportProperties(PrintWriter out) throws IOException{
48         out.println("  <properties>");
49         for(Iterator<String> i = environment.propertyKeys(); i.hasNext(); ){
50             String key = i.next();
51             String value = environment.getProperty(key);
52             out.println("    <property>");
53             out.printf("      <name>%s</name>%n", key);
54             out.printf("      <value>%s</value>%n", value);
55             out.println("    </property>");
56         }
57         out.println("  </properties>");
58     }
59
60     private void exportClasspath(PrintWriter out) throws IOException{
61         out.println("  <classpath-list>");
62         for(URL location: environment.getClasspathContext()){
63             out.printf("    <classpath>%s</classpath>%n", location.toString());
64         }
65         out.println("  </classpath-list>");
66     }
67
68     private void exportWellknownClasses(PrintWriter out) throws IOException{
69         out.println("  <wellknown-classes>");
70         for(WellknownClassJudgeRule rule: environment.getWellknownClassManager()){
71             String value = rule.getPattern();
72             String tag;
73             String matchtag;
74             switch(rule.getMatchPartType()){
75             case CLASS_NAME:
76                 tag = "class-name";
77                 break;
78             case FULLY_NAME:
79                 tag = "fully-name";
80                 break;
81             case PACKAGE_NAME:
82                 tag = "package-name";
83                 break;
84             default:
85                 throw new InternalError("unknown part type: " + rule.getMatchPartType());
86             }
87             switch(rule.getMatchType()){
88             case EXACT:
89                 matchtag = "match";
90                 break;
91             case NOT_MATCH:
92                 matchtag = "not-match";
93                 break;
94             case PREFIX:
95                 matchtag = "prefix";
96                 break;
97             case SUFFIX:
98                 matchtag = "suffix";
99                 break;
100             default:
101                 throw new InternalError("unknown match type: " + rule.getMatchType());
102             }
103
104             out.print("    ");
105             if(rule.isExclude()) out.print("<exclude>");
106             out.printf("<%s><%s>%s</%s></%s>", tag, matchtag, value, matchtag, tag);
107             if(rule.isExclude()) out.print("</exclude>");
108             out.println();
109         }
110         out.println("  </wellknown-classes>");
111     }
112
113     @SuppressWarnings("rawtypes")
114     private void exportFilters(PrintWriter out) throws IOException{
115         out.println("  <filterset-list>");
116         for(ComparisonPairFilterSet filterset: environment.getFilterManager().getFilterSets()){
117             out.println("    <filterset>");
118             out.printf("      <name>%s</name>%n", filterset.getName());
119             out.printf("      <match>%s</match>%n", filterset.isMatchAll()? "all": "any");
120             out.println("      <filter-list>");
121             for(ComparisonPairFilter filter: filterset){
122                 out.println("        <filter>");
123                 out.printf("          <filter-type>%s</filter-type>%n", filter.getService().getFilterName());
124                 out.printf("          <criterion>%s</criterion>%n", filter.getCriterion());
125                 try{
126                     Map props = BeanUtils.describe(filter);
127                     props.remove("service");
128                     props.remove("class");
129                     props.remove("criterion");
130                     props.remove("acceptableCriteria");
131                     out.println("          <attributes>");
132                     for(Object object: props.entrySet()){
133                         Map.Entry entry = (Map.Entry)object;
134                         Object value = entry.getValue();
135                         out.println("            <attribute>");
136                         out.printf("              <name>%s</name>%n", String.valueOf(entry.getKey()));
137                         if(value == null){
138                             out.println("              <value></value>%n");
139                         }
140                         else{
141                             out.printf("              <value>%s</value>%n", String.valueOf(entry.getValue()));
142                         }
143                         out.println("            </attribute>");
144                     }
145                     out.println("          </attributes>");
146                 } catch(Exception e){
147                     e.printStackTrace();
148                 }
149                 out.println("        </filter>");
150             }
151             out.println("      </filter-list>");
152             out.println("    </filterset>");
153         }
154         out.println("  </filterset-list>");
155     }
156
157     private void exportServices(PrintWriter out) throws IOException{
158         out.println("  <birthmark-services>");
159         for(BirthmarkService service: environment.getServices()){
160             if(service.isExperimental() && service instanceof BirthmarkService){
161                 out.println("    <birthmark-service>");
162                 out.printf("      <type>%s</type>%n", service.getType());
163                 if(service instanceof ReflectedBirthmarkService){
164                     out.printf("      <description>%s</description>%n", service.getDescription());
165                     out.printf("      <extractor>%s</extractor>%n", service.getExtractor().getClass().getName());
166                     out.printf("      <comparator>%s</comparator>%n", service.getComparator().getClass().getName());
167                 }
168                 out.println("    </birthmark-service>");
169             }
170         }
171         out.println("  </birthmark-services>");
172     }
173 }