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{
34 out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
35 out.println("<stigmata>");
39 exportWellknownClasses(out);
41 exportProperties(out);
43 out.println("</stigmata>");
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>");
57 out.println(" </properties>");
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());
65 out.println(" </classpath-list>");
68 private void exportWellknownClasses(PrintWriter out) throws IOException{
69 out.println(" <wellknown-classes>");
70 for(WellknownClassJudgeRule rule: environment.getWellknownClassManager()){
71 String value = rule.getPattern();
74 switch(rule.getMatchPartType()){
85 throw new InternalError("unknown part type: " + rule.getMatchPartType());
87 switch(rule.getMatchType()){
92 matchtag = "not-match";
101 throw new InternalError("unknown match type: " + rule.getMatchType());
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>");
110 out.println(" </wellknown-classes>");
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());
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()));
138 out.println(" <value></value>%n");
141 out.printf(" <value>%s</value>%n", String.valueOf(entry.getValue()));
143 out.println(" </attribute>");
145 out.println(" </attributes>");
146 } catch(Exception e){
149 out.println(" </filter>");
151 out.println(" </filter-list>");
152 out.println(" </filterset>");
154 out.println(" </filterset-list>");
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());
168 out.println(" </birthmark-service>");
171 out.println(" </birthmark-services>");