View Javadoc

1   package rydeen.io;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.ArrayList;
6   import java.util.Iterator;
7   import java.util.List;
8   
9   import rydeen.ProcessTarget;
10  import rydeen.TargetSource;
11  import rydeen.utils.NullIterator;
12  
13  /**
14   * 与えられた文字列配列や,リストに格納された文字列から
15   * {@link TargetSource <code>TargetSource</code>}
16   * オブジェクトを構築するためのクラス.
17   *
18   * @author Haruaki Tamada
19   */
20  public class TargetSourceBuilder{
21      /**
22       * 与えられた文字列配列の各要素に対応するファイルを読み込むための
23       * TargetSourceを作成し,返します.
24       * 
25       * @see DirectoryTargetSource
26       * @see JarFileTargetSource
27       * @see FileProcessTarget
28       * @see MultipleTargetSource
29       */
30      public TargetSource build(String[] targets) throws IOException{
31          List<TargetSource> list = new ArrayList<TargetSource>();
32          for(String target: targets){
33              File file = new File(target);
34              if(file.isDirectory()){
35                  list.add(new DirectoryTargetSource(file));
36              }
37              else if(file.getName().endsWith(".jar") || file.getName().endsWith(".zip")){
38                  list.add(new JarFileTargetSource(file));
39              }
40              else if(file.getName().endsWith(".class")){
41                  MemoryTargetSource source = new MemoryTargetSource();
42                  source.addTarget(new FileProcessTarget(source, new File(target)));
43                  list.add(source);
44              }
45          }
46          return build(list);
47      }
48  
49      private TargetSource build(List<TargetSource> list){
50          TargetSource source;
51          if(list.size() == 0){
52              source = new NullTargetSource();
53          }
54          else if(list.size() == 1){
55              source = list.get(0);
56          }
57          else{
58              source = new MultipleTargetSource(list.toArray(new TargetSource[list.size()]));
59          }
60          return source;
61      }
62  
63      private static final class NullTargetSource implements TargetSource{
64          @Override
65          public String getName(){
66              return "";
67          }
68  
69          @Override
70          public Iterator<ProcessTarget> iterator(){
71              return new NullIterator<ProcessTarget>();
72          }
73  
74          @Override
75          public void close(){
76          }
77  
78          @Override
79          public boolean contains(String target){
80              return false;
81          }
82      }
83  }