1 package jp.sourceforge.stigmata.utils;
3 import java.io.IOException;
4 import java.io.PrintWriter;
6 import java.util.Iterator;
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;
15 import org.apache.commons.beanutils.BeanUtils;
18 * Export birthmark environment to xml file.
20 * @author Haruaki TAMADA
22 public class ConfigFileExporter{
23 private BirthmarkEnvironment environment;
25 public ConfigFileExporter(BirthmarkEnvironment environment){
26 this.environment = environment;
29 public void export(BirthmarkEnvironment environment, PrintWriter out) throws IOException{
30 new ConfigFileExporter(environment).export(out);
33 public void export(PrintWriter out) throws IOException{
35 out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
36 out.println("<stigmata>");
40 exportWellknownClasses(out);
42 exportProperties(out);
44 out.println("</stigmata>");
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>");
61 out.println(" </properties>");
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());
69 out.println(" </classpath-list>");
72 private void exportWellknownClasses(PrintWriter out) throws IOException{
73 out.println(" <wellknown-classes>");
74 for(WellknownClassJudgeRule rule: environment.getWellknownClassManager()){
75 String value = rule.getPattern();
78 switch(rule.getMatchPartType()){
89 throw new InternalError("unknown part type: " + rule.getMatchPartType());
91 switch(rule.getMatchType()){
96 matchtag = "not-match";
105 throw new InternalError("unknown match type: " + rule.getMatchType());
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>");
114 out.println(" </wellknown-classes>");
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());
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()));
142 out.println(" <value></value>%n");
145 out.printf(" <value>%s</value>%n", String.valueOf(entry.getValue()));
147 out.println(" </attribute>");
149 out.println(" </attributes>");
150 } catch(Exception e){
153 out.println(" </filter>");
155 out.println(" </filter-list>");
156 out.println(" </filterset>");
158 out.println(" </filterset-list>");
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());
173 out.println(" </birthmark-service>");
176 out.println(" </birthmark-services>");