OSDN Git Service

b8d492088d4e81e881dd375b77c78aa25dae51b4
[stigmata/stigmata-plugins.git] / kgram / src / main / java / jp / sourceforge / stigmata / birthmarks / kgram / KGramBasedBirthmarkExtractor.java
1 package jp.sourceforge.stigmata.birthmarks.kgram;
2
3 import java.util.Iterator;
4
5 import jp.sourceforge.stigmata.Birthmark;
6 import jp.sourceforge.stigmata.BirthmarkContext;
7 import jp.sourceforge.stigmata.BirthmarkElement;
8 import jp.sourceforge.stigmata.ExtractionUnit;
9 import jp.sourceforge.stigmata.birthmarks.ASMBirthmarkExtractor;
10 import jp.sourceforge.stigmata.birthmarks.BirthmarkExtractVisitor;
11 import jp.sourceforge.stigmata.spi.BirthmarkService;
12 import jp.sourceforge.stigmata.utils.ArrayIterator;
13
14 import org.objectweb.asm.ClassWriter;
15
16 /**
17  * @author Haruaki TAMADA
18  */
19 public class KGramBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
20     private int kvalue = 4;
21
22     public KGramBasedBirthmarkExtractor(BirthmarkService spi){
23         super(spi);
24     }
25
26     public KGramBasedBirthmarkExtractor(){
27         super();
28     }
29
30     @Override
31     public Iterator<String> getPropertyKeys(){
32         return new ArrayIterator<String>(new String[] { "KValue" });
33     }
34
35     @Override
36     public void setProperty(String key, Object value){
37         if(key.equalsIgnoreCase("kvalue")){
38             if(value instanceof Integer){
39                 kvalue = ((Integer)value).intValue();
40             }
41             else if(value instanceof String){
42                 kvalue = Integer.parseInt((String)value);
43             }
44         }
45     }
46
47     public void setKValue(int kvalue){
48         this.kvalue = kvalue;
49     }
50
51     public int getKValue(){
52         return kvalue;
53     }
54
55     @Override
56     public BirthmarkExtractVisitor createExtractVisitor(ClassWriter writer,
57             Birthmark birthmark, BirthmarkContext context){
58         KGramBasedBirthmarkExtractVisitor extractor =
59             new KGramBasedBirthmarkExtractVisitor(writer, birthmark, context);
60         extractor.setKValue(getKValue());
61         return extractor;
62     }
63
64     @Override
65     public ExtractionUnit[] getAcceptableUnits(){
66         return new ExtractionUnit[] {
67             ExtractionUnit.CLASS, ExtractionUnit.PACKAGE,
68             ExtractionUnit.ARCHIVE,
69         };
70     }
71
72
73     @Override
74     public BirthmarkElement buildElement(String value) {
75         value = value.trim();
76         String[] param =
77             value.substring(1, value.length() - 1).split(" *");
78         KGram<Integer> kgram = new KGram<Integer>(param.length);
79         for(int i = 0; i < param.length; i++){
80             kgram.set(i, new Integer(param[i].trim()));
81         }
82         return new KGramBasedBirthmarkElement<Integer>(kgram);
83     }
84 }