OSDN Git Service

Add new source files
[armadillo/armadillo1.git] / src / jp / sfjp / armadillo / ui / console / CommandLine.java
1 package jp.sfjp.armadillo.ui.console;
2
3 import java.io.*;
4 import java.util.*;
5 import jp.sfjp.armadillo.*;
6 import jp.sfjp.armadillo.archive.*;
7
8 public final class CommandLine {
9
10     static final ResourceManager res = ResourceManager.getInstance("jp.sfjp.armadillo.ui.messages");
11
12     static void usage(int messageId) {
13         switch (messageId) {
14             case 1:
15                 System.out.println(res.get("msg.warn-nofunction"));
16                 break;
17             case 0:
18             default:
19                 // no message
20         }
21         System.out.println(res.get("msg.usage"));
22         System.exit(2);
23     }
24
25     static void execute(char function, String firstPath, List<String> paths) throws IOException {
26         switch (function) {
27             case 'c':
28                 // create archive
29                 if (paths.isEmpty())
30                     usage(0);
31                 createArchive(new File(firstPath), paths);
32                 break;
33             case 't':
34                 // list archive
35                 if (!paths.isEmpty())
36                     System.out.println(res.get("msg.warn-ignoresecondfile"));
37                 listArchive(new File(firstPath));
38                 break;
39             case 'x':
40                 // extract archive
41                 extractArchive(firstPath, paths);
42                 break;
43             case 'z':
44                 // compress
45                 break;
46             case 'd':
47                 // decompress
48                 break;
49             case 'v':
50                 // validate
51                 if (ArmadilloCommands.validate(new File(firstPath)))
52                     System.out.println("OK");
53                 else
54                     System.out.println("NG");
55                 break;
56             default:
57                 throw new IllegalStateException("function=" + function);
58         }
59     }
60
61     static void createArchive(File afile, List<String> targets) throws IOException {
62         ArchiveType type = ArchiveType.of(afile.getName());
63         ArchiveCreator ac = ArmadilloCommands.getArchiveCreator(afile, type);
64         try {
65             for (final String name : targets) {
66                 if (name.length() == 0)
67                     continue;
68                 final File file = new File(name);
69                 String s = name.replace('\\', '/');
70                 if (file.isDirectory() && !s.endsWith("/"))
71                     s += "/";
72                 if (file.isDirectory())
73                     for (final File child : ArmadilloCommands.getAllChildrenFile(file))
74                         addEntry(ac, child, s);
75                 else
76                     addEntry(ac, file, s);
77             }
78         }
79         finally {
80             ac.close();
81         }
82     }
83
84     private static ArchiveEntry addEntry(ArchiveCreator ac, File file, String name) throws IOException {
85         ArchiveEntry entry = ac.newEntry(name);
86         entry.setSize(file.length());
87         entry.setLastModified(file.lastModified());
88         assert !entry.isDirectory() || entry.getSize() == 0L;
89         ac.addEntry(entry, file);
90         return entry;
91     }
92
93     static void listArchive(File afile) throws IOException {
94         ArchiveType type = ArchiveType.of(afile.getName());
95         ArchiveExtractor ax = ArmadilloCommands.getArchiveExtractor(afile, type);
96         try {
97             while (true) {
98                 ArchiveEntry entry = ax.nextEntry();
99                 if (entry == ArchiveEntry.NULL)
100                     break;
101                 final String methodName = entry.getMethodName();
102                 System.out.print((methodName.length() < 8)
103                                  ? String.format("%-8s", methodName)
104                                          : methodName.substring(0, 8));
105                 System.out.printf("%9d", entry.getSize());
106                 System.out.printf(" %1$tF %1$tT ", new Date(entry.getLastModified()));
107                 System.out.println(entry.getName());
108             }
109         }
110         finally {
111             ax.close();
112         }
113     }
114
115     static void extractArchive(String firstPath, List<String> paths) throws IOException {
116         final String dstdir;
117         if (paths.isEmpty())
118             dstdir = firstPath + ".tmp";
119         else {
120             if (paths.size() > 1)
121                 System.out.println("msg.warn-ignorethirdfile");
122             dstdir = paths.get(0);
123         }
124         ArmadilloCommands.extractAll(new File(firstPath), new File(dstdir));
125     }
126
127     public static void main(String[] args) {
128         char function = ' ';
129         int functionCount = 0;
130         List<String> of = new ArrayList<String>();
131         List<String> paths = new ArrayList<String>();
132         for (final String arg : args)
133             if (arg.equals("--")) {
134                 function = '-';
135                 ++functionCount;
136             }
137             else if (arg.startsWith("--")) {
138                 if (arg.equals("--validate")) {
139                     function = 'v';
140                     ++functionCount;
141                 }
142                 else if (arg.equals("--version")) {
143                     System.out.println(res.get(".version", ArmadilloCommands.getVersionString(true)));
144                     return;
145                 }
146                 else
147                     usage(0);
148             }
149             else if (arg.equals("-"))
150                 usage(0);
151             else if (arg.startsWith("-"))
152                 for (int i = 1; i < arg.length(); i++) {
153                     final char c = arg.charAt(i);
154                     switch (c) {
155                         case 'c':
156                         case 't':
157                         case 'x':
158                         case 'z':
159                         case 'd':
160                         case 'e':
161                         case 'v':
162                             function = c;
163                             ++functionCount;
164                             break;
165                         default:
166                             of.add(String.valueOf(c));
167                     }
168                 }
169             else if (functionCount == 0)
170                 usage(1);
171             else
172                 paths.add(arg);
173         if (functionCount != 1)
174             usage(1);
175         if (paths.isEmpty())
176             usage(0);
177         // main
178         final String firstPath = paths.remove(0);
179         try {
180             execute(function, firstPath, paths);
181         }
182         catch (IOException ex) {
183             ex.printStackTrace();
184         }
185     }
186
187 }