OSDN Git Service

fixed findbugs warnings.
[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         try{
35             out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
36             out.println("<stigmata>");
37
38             exportServices(out);
39             exportFilters(out);
40             exportWellknownClasses(out);
41             exportClasspath(out);
42             exportProperties(out);
43
44             out.println("</stigmata>");
45             out.flush();
46         } finally{
47             out.close();
48         }
49     }
50
51     private void exportProperties(PrintWriter out) throws IOException{
52         out.println("  <properties>");
53         for(Iterator<String> i = environment.propertyKeys(); i.hasNext(); ){
54             String key = i.next();
55             String value = environment.getProperty(key);
56             out.println("    <property>");
57             out.printf("      <name>%s</name>%n", key);
58             out.printf("      <value>%s</value>%n", value);
59             out.println("    </property>");
60         }
61         out.println("  </properties>");
62     }
63
64     private void exportClasspath(PrintWriter out) throws IOException{
65         out.println("  <classpath-list>");
66         for(URL location: environment.getClasspathContext()){
67             out.printf("    <classpath>%s</classpath>%n", location.toString());
68         }
69         out.println("  </classpath-list>");
70     }
71
72     private void exportWellknownClasses(PrintWriter out) throws IOException{
73         out.println("  <wellknown-classes>");
74         for(WellknownClassJudgeRule rule: environment.getWellknownClassManager()){
75             String value = rule.getPattern();
76             String tag;
77             String matchtag;
78             switch(rule.getMatchPartType()){
79             case CLASS_NAME:
80                 tag = "class-name";
81                 break;
82             case FULLY_NAME:
83                 tag = "fully-name";
84                 break;
85             case PACKAGE_NAME:
86                 tag = "package-name";
87                 break;
88             default:
89                 throw new InternalError("unknown part type: " + rule.getMatchPartType());
90             }
91             switch(rule.getMatchType()){
92             case EXACT:
93                 matchtag = "match";
94                 break;
95             case NOT_MATCH:
96                 matchtag = "not-match";
97                 break;
98             case PREFIX:
99                 matchtag = "prefix";
100                 break;
101             case SUFFIX:
102                 matchtag = "suffix";
103                 break;
104             default:
105                 throw new InternalError("unknown match type: " + rule.getMatchType());
106             }
107
108             out.print("    ");
109             if(rule.isExclude()) out.print("<exclude>");
110             out.printf("<%s><%s>%s</%s></%s>", tag, matchtag, value, matchtag, tag);
111             if(rule.isExclude()) out.print("</exclude>");
112             out.println();
113         }
114         out.println("  </wellknown-classes>");
115     }
116
117     @SuppressWarnings("rawtypes")
118     private void exportFilters(PrintWriter out) throws IOException{
119         out.println("  <filterset-list>");
120         for(ComparisonPairFilterSet filterset: environment.getFilterManager().getFilterSets()){
121             out.println("    <filterset>");
122             out.printf("      <name>%s</name>%n", filterset.getName());
123             out.printf("      <match>%s</match>%n", filterset.isMatchAll()? "all": "any");
124             out.println("      <filter-list>");
125             for(ComparisonPairFilter filter: filterset){
126                 out.println("        <filter>");
127                 out.printf("          <filter-type>%s</filter-type>%n", filter.getService().getFilterName());
128                 out.printf("          <criterion>%s</criterion>%n", filter.getCriterion());
129                 try{
130                     Map props = BeanUtils.describe(filter);
131                     props.remove("service");
132                     props.remove("class");
133                     props.remove("criterion");
134                     props.remove("acceptableCriteria");
135                     out.println("          <attributes>");
136                     for(Object object: props.entrySet()){
137                         Map.Entry entry = (Map.Entry)object;
138                         Object value = entry.getValue();
139                         out.println("            <attribute>");
140                         out.printf("              <name>%s</name>%n", String.valueOf(entry.getKey()));
141                         if(value == null){
142                             out.println("              <value></value>%n");
143                         }
144                         else{
145                             out.printf("              <value>%s</value>%n", String.valueOf(entry.getValue()));
146                         }
147                         out.println("            </attribute>");
148                     }
149                     out.println("          </attributes>");
150                 } catch(Exception e){
151                     e.printStackTrace();
152                 }
153                 out.println("        </filter>");
154             }
155             out.println("      </filter-list>");
156             out.println("    </filterset>");
157         }
158         out.println("  </filterset-list>");
159     }
160
161     private void exportServices(PrintWriter out) throws IOException{
162         out.println("  <birthmark-services>");
163         for(BirthmarkService service: environment.getServices()){
164             if(service.isExperimental()){
165                 out.println("    <birthmark-service>");
166                 out.printf("      <type>%s</type>%n", service.getType());
167                 if(service instanceof ReflectedBirthmarkService){
168                     ReflectedBirthmarkService rbs = (ReflectedBirthmarkService)service;
169                     out.printf("      <description>%s</description>%n", rbs.getDescription());
170                     out.printf("      <extractor>%s</extractor>%n", rbs.getExtractorClassName());
171                     out.printf("      <comparator>%s</comparator>%n", rbs.getComparatorClassName());
172                 }
173                 out.println("    </birthmark-service>");
174             }
175         }
176         out.println("  </birthmark-services>");
177     }
178 }