OSDN Git Service

dfe50d73cbacc0af852de1448e0aec104f224d28
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / birthmarks / AbstractBirthmarkExtractor.java
1 package jp.sourceforge.stigmata.birthmarks;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5
6 import jp.sourceforge.stigmata.Birthmark;
7 import jp.sourceforge.stigmata.BirthmarkContext;
8 import jp.sourceforge.stigmata.BirthmarkExtractionFailedException;
9 import jp.sourceforge.stigmata.BirthmarkExtractor;
10 import jp.sourceforge.stigmata.ExtractionUnit;
11 import jp.sourceforge.stigmata.spi.BirthmarkService;
12
13 /**
14  * Abstract class for extracting birthmark.
15  * @author  Haruaki TAMADA
16  */
17 public abstract class AbstractBirthmarkExtractor implements BirthmarkExtractor{
18     /**
19      * provider.
20      */
21     private BirthmarkService spi;
22
23     /**
24      * default constructor.
25      * @deprecated this constructor does not support service provider.
26      */
27     public AbstractBirthmarkExtractor(){
28     }
29
30     /**
31      * constructor.
32      * @param spi service provider.
33      */
34     public AbstractBirthmarkExtractor(BirthmarkService spi){
35         this.spi = spi;
36     }
37
38     /**
39      * returns the provider of this extractor.
40      */
41     @Override
42     public BirthmarkService getProvider(){
43         return spi;
44     }
45
46     /**
47      * extract birthmark given stream with given environment.
48      */
49     @Override
50     public final Birthmark extract(InputStream in, BirthmarkContext context) throws BirthmarkExtractionFailedException{
51         return extract(createBirthmark(), in, context);
52     }
53
54     /**
55      * extract birthmark given byte array with given environment.
56      */
57     @Override
58     public final Birthmark extract(Birthmark birthmark, byte[] bytecode, BirthmarkContext context) throws BirthmarkExtractionFailedException{
59         return extract(birthmark, new ByteArrayInputStream(bytecode), context);
60     }
61
62     /**
63      * extract birthmark given byte array with given environment.
64      */
65     @Override
66     public final Birthmark extract(byte[] bytecode, BirthmarkContext context) throws BirthmarkExtractionFailedException{
67         return extract(createBirthmark(), new ByteArrayInputStream(bytecode), context);
68     }
69
70     /**
71      * extract birthmark given stream with given environment.
72      */
73     @Override
74     public abstract Birthmark extract(Birthmark birthmark, InputStream in, BirthmarkContext context) throws BirthmarkExtractionFailedException;
75
76     /**
77      * create birthmark.
78      * @see jp.sourceforge.stigmata.BirthmarkExtractor#createBirthmark()
79      */
80     @Override
81     public Birthmark createBirthmark(){
82         return new PlainBirthmark(getProvider().getType());
83     }
84
85     @Override
86     public abstract ExtractionUnit[] getAcceptableUnits();
87
88     @Override
89     public boolean isAcceptable(ExtractionUnit unit){
90         ExtractionUnit[] units = getAcceptableUnits();
91
92         for(int i = 0; i < units.length; i++){
93             if(units[i] == unit){
94                 return true;
95             }
96         }
97         return false;
98     }
99 }