• R/O
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Revision12 (tree)
Zeit2015-06-23 23:43:10
Autorki-chi

Log Message

コマンド定義・判断方法のリファクタリング中

Ändern Zusammenfassung

Diff

--- trunk/src/com/nissy_ki_chi/ConsolePicocalc/BookCommand.java (revision 11)
+++ trunk/src/com/nissy_ki_chi/ConsolePicocalc/BookCommand.java (revision 12)
@@ -18,96 +18,98 @@
1818
1919 public class BookCommand {
2020
21- String getCurrentBookName() {
22- return _currentBookName;
23- }
21+// String getCurrentBookName() {
22+// return _currentBookName;
23+// }
24+//
25+// void setCurrentBookName(String bookName) {
26+// this._currentBookName = bookName;
27+// }
28+//
29+// Map<String, Book> getBooks() {
30+// return _books;
31+// }
32+//
33+// Book getCurrentBook() {
34+// return this._books.get(_currentBookName);
35+// }
36+//
37+// Book getBook(String bookName) {
38+// return this._books.get(bookName);
39+// }
40+//
41+// Map<String, Book> _books = new HashMap<String, Book>();
42+// String _currentBookName = null;
2443
25- void setCurrentBookName(String bookName) {
26- this._currentBookName = bookName;
27- }
28-
29- Map<String, Book> getBooks() {
30- return _books;
31- }
3244
33- Book getCurrentBook() {
34- return this._books.get(_currentBookName);
35- }
3645
37- Book getBook(String bookName) {
38- return this._books.get(bookName);
39- }
46+// void processBookCmd(String[] cmdArr, String argStr) {
47+//
48+// if (cmdArr.length == 1) {
49+// return;
50+// }
51+//
52+// // 新しいブックの追加
53+// if (cmdArr[1].equals("add")) {
54+// Book newBook = new Book(argStr);
55+// newBook.addSheet("sheet1");
56+// _books.put(newBook.getName(), newBook);
57+// }
58+//
59+// // カレントブックの変更
60+// if (cmdArr[1].equals("current")) {
61+// if (_books.containsKey(argStr)) {
62+// this._currentBookName = argStr;
63+// }
64+// }
65+//
66+// // ブックの一覧を表示
67+// if (cmdArr[1].equals("list")) {
68+// for (String bookName: new TreeSet<String>(_books.keySet())) {
69+// System.out.println(bookName);
70+// }
71+// }
72+//
73+// // カレントブックで使用可能な関数一覧を表示
74+// if (cmdArr[1].equals("functions")) {
75+// String[] functionNames = _books.get(_currentBookName).getFunctionNames();
76+// Arrays.sort(functionNames);
77+// for (String funcName: Arrays.asList(functionNames)) {
78+// System.out.println(funcName);
79+// }
80+// }
81+//
82+// // ファイル読み込み
83+// if (cmdArr[1].equals("load")) {
84+// String filePath = argStr;
85+// Book book = _books.get(_currentBookName);
86+// load(book, filePath);
87+// }
88+//
89+// // ファイル書き込み
90+// if (cmdArr[1].equals("save")) {
91+// String filePath = argStr;
92+// Book book = _books.get(_currentBookName);
93+// SimpleWriter sw = new SimpleWriter(new ConcurrentBookWrapper(book));
94+// BufferedWriter w = null;
95+// try {
96+// w = new BufferedWriter(new FileWriter(filePath));
97+// sw.write(w);
98+// } catch (Exception e) {
99+// e.printStackTrace();
100+// } finally {
101+// if (w != null) {
102+// try {
103+// w.close();
104+// } catch (IOException e) {
105+// e.printStackTrace();
106+// }
107+// }
108+// }
109+// }
110+//
111+// }
40112
41- Map<String, Book> _books = new HashMap<String, Book>();
42- String _currentBookName = null;
43-
44- void processBookCmd(String[] cmdArr, String argStr) {
45-
46- if (cmdArr.length == 1) {
47- return;
48- }
49-
50- // 新しいブックの追加
51- if (cmdArr[1].equals("add")) {
52- Book newBook = new Book(argStr);
53- newBook.addSheet("sheet1");
54- _books.put(newBook.getName(), newBook);
55- }
56-
57- // カレントブックの変更
58- if (cmdArr[1].equals("current")) {
59- if (_books.containsKey(argStr)) {
60- this._currentBookName = argStr;
61- }
62- }
63-
64- // ブックの一覧を表示
65- if (cmdArr[1].equals("list")) {
66- for (String bookName: new TreeSet<String>(_books.keySet())) {
67- System.out.println(bookName);
68- }
69- }
70-
71- // カレントブックで使用可能な関数一覧を表示
72- if (cmdArr[1].equals("functions")) {
73- String[] functionNames = _books.get(_currentBookName).getFunctionNames();
74- Arrays.sort(functionNames);
75- for (String funcName: Arrays.asList(functionNames)) {
76- System.out.println(funcName);
77- }
78- }
79-
80- // ファイル読み込み
81- if (cmdArr[1].equals("load")) {
82- String filePath = argStr;
83- Book book = _books.get(_currentBookName);
84- load(book, filePath);
85- }
86-
87- // ファイル書き込み
88- if (cmdArr[1].equals("save")) {
89- String filePath = argStr;
90- Book book = _books.get(_currentBookName);
91- SimpleWriter sw = new SimpleWriter(new ConcurrentBookWrapper(book));
92- BufferedWriter w = null;
93- try {
94- w = new BufferedWriter(new FileWriter(filePath));
95- sw.write(w);
96- } catch (Exception e) {
97- e.printStackTrace();
98- } finally {
99- if (w != null) {
100- try {
101- w.close();
102- } catch (IOException e) {
103- e.printStackTrace();
104- }
105- }
106- }
107- }
108-
109- }
110-
111113 void load(Book book, String filePath) {
112114 SimpleReader sr = new SimpleReader(new ConcurrentBookWrapper(book));
113115 BufferedReader fileBR = null;
--- trunk/src/com/nissy_ki_chi/ConsolePicocalc/Command.java (nonexistent)
+++ trunk/src/com/nissy_ki_chi/ConsolePicocalc/Command.java (revision 12)
@@ -0,0 +1,34 @@
1+package com.nissy_ki_chi.ConsolePicocalc;
2+
3+public class Command {
4+
5+
6+ static String[][] commandDefine = new String[][] {
7+ {".quit", "doQuit"},
8+
9+ {".book.add", "doBookAdd"},
10+ {".book.current", "doBookCurrent"},
11+ {".book.list", "doBookList"},
12+ {".book.functions", "doBookFunctions"},
13+ {".book.load", "doBookLoad"},
14+ {".book.save", "doBookSave"},
15+
16+
17+ {".sheet.add", "doSheetAdd"},
18+ {".sheet.list", "doSheetList"},
19+ {".sheet.current", "doSheetCurrent"},
20+
21+ {".list", "doCellList"},
22+
23+ {".group.list", "doGroupList"},
24+ {".group.list.cells", "doGroupListCells"},
25+ {".group.add", "doGroupAdd"},
26+ {".group.delete", "doGroupDelete"},
27+ {".group.cells.add", "doGroupCellsAdd"},
28+ {".group.cells.remove", "doGroupCellsRemove"},
29+
30+ {".list.tables", "doListTables"},
31+ {".list.table.cells", "doListTableCells"},
32+
33+ };
34+}
--- trunk/src/com/nissy_ki_chi/ConsolePicocalc/ConsolePicocalc.java (revision 11)
+++ trunk/src/com/nissy_ki_chi/ConsolePicocalc/ConsolePicocalc.java (revision 12)
@@ -1,25 +1,34 @@
11 package com.nissy_ki_chi.ConsolePicocalc;
22
33 import java.io.BufferedReader;
4+import java.io.BufferedWriter;
5+import java.io.FileInputStream;
6+import java.io.FileWriter;
47 import java.io.IOException;
58 import java.io.InputStreamReader;
69 import java.io.UnsupportedEncodingException;
710 import java.lang.reflect.Array;
11+import java.lang.reflect.InvocationTargetException;
12+import java.lang.reflect.Method;
813 import java.nio.file.Files;
914 import java.nio.file.Paths;
1015 import java.util.ArrayList;
16+import java.util.Arrays;
1117 import java.util.HashMap;
1218 import java.util.Map;
1319 import java.util.TreeSet;
1420
1521 import com.nissy_ki_chi.jpicosheet.core.Book;
22+import com.nissy_ki_chi.jpicosheet.core.ConcurrentBookWrapper;
1623 import com.nissy_ki_chi.jpicosheet.core.Sheet;
24+import com.nissy_ki_chi.jpicosheet.util.SimpleReader;
25+import com.nissy_ki_chi.jpicosheet.util.SimpleWriter;
1726
1827 public class ConsolePicocalc {
1928
2029
21-// Map<String, Book> _books = new HashMap<String, Book>();
22-// String _bookCommand.setCurrentBook(null;
30+ Map<String, Book> _books = new HashMap<String, Book>();
31+ String _currentBookName = null;
2332
2433 BookCommand _bookCommand = new BookCommand();
2534 SheetCommand _sheetCommand = new SheetCommand();
@@ -27,6 +36,11 @@
2736 GroupCommand _groupCommand = new GroupCommand();
2837 TableCommand _tableCommand = new TableCommand();
2938
39+ CommandFragment _commandStorage = new CommandFragment(".");
40+
41+ private static String DEFAULT_BOOK_NAME = "myBook";
42+ private static String DEFAULT_SHEET_NAME = "Sheet1";
43+
3044 /**
3145 * @param args
3246 */
@@ -34,6 +48,8 @@
3448 // TODO 自動生成されたメソッド・スタブ
3549
3650 ConsolePicocalc cp = new ConsolePicocalc();
51+ cp.setup();
52+
3753 String filePath = null;
3854 if (0 < args.length) {
3955 filePath = args[0];
@@ -48,21 +64,35 @@
4864 }
4965
5066
67+ private void setup() {
68+
69+ makeCommandStorage(_commandStorage);
70+ }
71+
72+ private void makeCommandStorage(CommandFragment commandStorage) {
73+ for (String[] cmd: Command.commandDefine) {
74+ String[] cmdArr = cmd[0].substring(1).split("\\.");
75+ String methodName = cmd[1];
76+ commandStorage.addCommand(cmdArr, methodName);
77+ }
78+ }
79+
80+
5181 public void start(String filePath) {
5282
5383 Book book = null;
5484 // 引数としてファイルが渡された場合はそれをロードする
5585 if (filePath != null && Files.exists(Paths.get(filePath))) {
56- _bookCommand.setCurrentBookName("myBook");
57- book = new Book(_bookCommand.getCurrentBookName());
86+ _currentBookName = DEFAULT_BOOK_NAME;
87+ book = new Book(_currentBookName);
5888 _bookCommand.load(book, filePath);
5989 } else {
6090 // そうでない場合はデフォルトシートを作成
61- book = new Book("myBook");
62- book.addSheet("Sheet1");
63- this._bookCommand.setCurrentBookName(book.getName());
91+ book = new Book(DEFAULT_BOOK_NAME);
92+ book.addSheet(DEFAULT_SHEET_NAME);
93+ this._currentBookName = book.getName();
6494 }
65- this._bookCommand.getBooks().put(book.getName(), book);
95+ this._books.put(book.getName(), book);
6696
6797 // コンソールから1行づつ読み込んで処理する
6898 BufferedReader br = null;
@@ -77,8 +107,8 @@
77107 while(true) {
78108 try {
79109 // プロンプト表示
80- Book currentBook = _bookCommand.getCurrentBook();
81- System.out.print("[" + _bookCommand.getCurrentBookName() + "]" + currentBook.getResolver().getCurrentSheet().getName() + ": ");
110+ Book currentBook = _books.get(_currentBookName);
111+ System.out.print("[" + _currentBookName + "]" + currentBook.getResolver().getCurrentSheet().getName() + ": ");
82112
83113 // 入力がなくなったら終了
84114 if ((line = br.readLine().trim()) == null) {
@@ -99,7 +129,7 @@
99129 if (line.charAt(0) == '.') {
100130 processCommand(line);
101131 } else {
102- _cellCommand.processCell(_bookCommand.getCurrentBook(), line);
132+ _cellCommand.processCell(_books.get(_currentBookName), line);
103133 }
104134 } catch (Exception e) {
105135 e.printStackTrace();
@@ -133,53 +163,165 @@
133163 }
134164 String[] cmdArr = cmdStr.split("\\.");
135165
136- if (cmdArr[0].equals("book")) {
137- _bookCommand.processBookCmd(cmdArr, argStr);
166+ String methodName = _commandStorage.getMethodName(cmdStr.split("\\."));
167+ if (methodName == null) {
168+ System.out.println("コマンド " + cmdStr + " はありません。");
138169 return;
170+ } else {
171+ try {
172+// Method m = this.getClass().getMethod(methodName);
173+ Method m = this.getClass().getDeclaredMethod(methodName, String.class);
174+ m.setAccessible(true);
175+ m.invoke(this, argStr);
176+ } catch (NoSuchMethodException e) {
177+ System.out.println("コマンド " + cmdStr + "に対応するメソッド " + methodName + " が定義されていません。");
178+ return;
179+ } catch (SecurityException e) {
180+ e.printStackTrace();
181+ return;
182+ } catch (IllegalAccessException e) {
183+ e.printStackTrace();
184+ return;
185+ } catch (IllegalArgumentException e) {
186+ e.printStackTrace();
187+ return;
188+ } catch (InvocationTargetException e) {
189+ e.printStackTrace();
190+ return;
191+ }
139192 }
140193
141- if (cmdArr[0].equals("sheet")) {
142- Book book = _bookCommand.getCurrentBook();
143- _sheetCommand.processSheetCmd(book, cmdArr, argStr);
144- return;
145- }
194+
195+// if (cmdArr[0].equals("book")) {
196+// _bookCommand.processBookCmd(cmdArr, argStr);
197+// return;
198+// }
199+//
200+// if (cmdArr[0].equals("sheet")) {
201+// Book book = _books.get(_currentBookName);
202+// _sheetCommand.processSheetCmd(book, cmdArr, argStr);
203+// return;
204+// }
205+//
206+// if (cmdArr[0].equals("group")) {
207+// Sheet sheet = _books.get(_currentBookName).getResolver().getCurrentSheet();
208+// _groupCommand.processGroupCmd(sheet, cmdArr, argStr);
209+// return;
210+// }
211+//
212+// if (cmdArr[0].equals("table")) {
213+// Sheet sheet = _books.get(_currentBookName).getResolver().getCurrentSheet();
214+// _tableCommand.processTableCmd(sheet, cmdArr, argStr);
215+// return;
216+// }
217+//
218+// if (cmdArr[0].equals("list") && cmdArr.length == 1) {
219+// Sheet sheet = _books.get(_currentBookName).getResolver().getCurrentSheet();
220+// _cellCommand.listCells(sheet);
221+// _groupCommand.listGroups(sheet);
222+// _tableCommand.listTables(sheet);
223+// return;
224+// } else {
225+// Sheet sheet = _books.get(_currentBookName).getResolver().getCurrentSheet();
226+// _cellCommand.processListCmd(sheet, cmdArr, argStr);
227+// return;
228+// }
146229
147- if (cmdArr[0].equals("group")) {
148- Sheet sheet = _bookCommand.getCurrentBook().getResolver().getCurrentSheet();
149- _groupCommand.processGroupCmd(sheet, cmdArr, argStr);
150- return;
151- }
152230
153- if (cmdArr[0].equals("table")) {
154- Sheet sheet = _bookCommand.getCurrentBook().getResolver().getCurrentSheet();
155- _tableCommand.processTableCmd(sheet, cmdArr, argStr);
156- return;
231+ }
232+
233+// private void listSheets(Book book) {
234+// TreeSet<String> sheetNames = new TreeSet<String>();
235+// for (Sheet s: book.getSheets()) {
236+// sheetNames.add(s.getName());
237+// }
238+// for (String sName: sheetNames) {
239+// System.out.println(sName);
240+// }
241+// }
242+
243+ private void doCellList(String argStr) {
244+ Sheet sheet = _books.get(_currentBookName).getResolver().getCurrentSheet();
245+ _cellCommand.listCells(sheet);
246+ _groupCommand.listGroups(sheet);
247+ _tableCommand.listTables(sheet);
248+ return;
249+ }
250+
251+ private void doBookAdd(String argStr) {
252+ Book newBook = new Book(argStr);
253+ newBook.addSheet("sheet1");
254+ _books.put(newBook.getName(), newBook);
255+ }
256+
257+ private void doBookCurrent(String argStr) {
258+ // カレントブックの変更
259+ if (_books.containsKey(argStr)) {
260+ this._currentBookName = argStr;
261+ }
262+ }
263+
264+ private void doBookList(String argStr) {
265+ for (String bookName: new TreeSet<String>(_books.keySet())) {
266+ System.out.println(bookName);
157267 }
268+ }
158269
159- if (cmdArr[0].equals("list") && cmdArr.length == 1) {
160- Sheet sheet = _bookCommand.getCurrentBook().getResolver().getCurrentSheet();
161- _cellCommand.listCells(sheet);
162- _groupCommand.listGroups(sheet);
163- _tableCommand.listTables(sheet);
164- return;
165- } else {
166- Sheet sheet = _bookCommand.getCurrentBook().getResolver().getCurrentSheet();
167- _cellCommand.processListCmd(sheet, cmdArr, argStr);
168- return;
270+ private void doBookFunctions(String argStr) {
271+ String[] functionNames = _books.get(_currentBookName).getFunctionNames();
272+ Arrays.sort(functionNames);
273+ for (String funcName: Arrays.asList(functionNames)) {
274+ System.out.println(funcName);
169275 }
170-
171-
172276 }
277+
278+ private void doBookLoad(String argStr) {
279+ String filePath = argStr;
280+ Book book = _books.get(_currentBookName);
281+ load(book, filePath);
282+ }
173283
174- private void listSheets(Book book) {
175- TreeSet<String> sheetNames = new TreeSet<String>();
176- for (Sheet s: book.getSheets()) {
177- sheetNames.add(s.getName());
284+ private void doBookSave(String argStr) {
285+ String filePath = argStr;
286+ Book book = _books.get(_currentBookName);
287+ SimpleWriter sw = new SimpleWriter(new ConcurrentBookWrapper(book));
288+ BufferedWriter w = null;
289+ try {
290+ w = new BufferedWriter(new FileWriter(filePath));
291+ sw.write(w);
292+ } catch (Exception e) {
293+ e.printStackTrace();
294+ } finally {
295+ if (w != null) {
296+ try {
297+ w.close();
298+ } catch (IOException e) {
299+ e.printStackTrace();
300+ }
301+ }
178302 }
179- for (String sName: sheetNames) {
180- System.out.println(sName);
303+ }
304+
305+
306+
307+ void load(Book book, String filePath) {
308+ SimpleReader sr = new SimpleReader(new ConcurrentBookWrapper(book));
309+ BufferedReader fileBR = null;
310+ try {
311+ fileBR = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
312+ sr.Read(fileBR);
313+ } catch (Exception e) {
314+ e.printStackTrace();
315+ } finally {
316+ if (fileBR != null) {
317+ try {
318+ fileBR.close();
319+ } catch (IOException e) {
320+ e.printStackTrace();
321+ }
322+ }
181323 }
324+
182325 }
183326
184-
185327 }
--- trunk/src/com/nissy_ki_chi/ConsolePicocalc/CommandFragment.java (nonexistent)
+++ trunk/src/com/nissy_ki_chi/ConsolePicocalc/CommandFragment.java (revision 12)
@@ -0,0 +1,95 @@
1+package com.nissy_ki_chi.ConsolePicocalc;
2+
3+import java.util.HashMap;
4+import java.util.Stack;
5+
6+public class CommandFragment {
7+
8+ private String _commandFragmentName = null;
9+ private String _methodName = null;
10+ private HashMap<String, CommandFragment> _subFragment = null;
11+
12+ private CommandFragment() {};
13+
14+// CommandFragment() {
15+//
16+// }
17+
18+ public CommandFragment(String commandFragmentName) {
19+ this._commandFragmentName = commandFragmentName;
20+ }
21+
22+ void setMethodName(String methodName) {
23+ _methodName = methodName;
24+ }
25+
26+ void addCommand(String[] commandArray, String methodName) {
27+ Stack<String> stack = new Stack<String>();
28+ for (int i = commandArray.length - 1; 0 <= i; i--) {
29+ stack.push(commandArray[i]);
30+ }
31+ addCommand(stack, methodName);
32+ }
33+
34+ private void addCommand(Stack<String> commandStack, String methodName) {
35+ if (commandStack.size() == 0) {
36+// this.put(cmdFragment, new CommandFragment(methodName));
37+ this._methodName = methodName;
38+ } else {
39+ String cmdFragment = commandStack.pop();
40+ if (this._subFragment == null) {
41+ this._subFragment = new HashMap<String, CommandFragment>();
42+ }
43+
44+ if (! this._subFragment.containsKey(cmdFragment)) {
45+ this._subFragment.put(cmdFragment, new CommandFragment(cmdFragment));
46+ }
47+ this._subFragment.get(cmdFragment).addCommand(commandStack, methodName);
48+ }
49+ }
50+
51+ void appendFragment(String commandFragmentName, CommandFragment fragment) {
52+ this._subFragment.put(commandFragmentName, fragment);
53+ }
54+
55+ String getMethodName(String[] commandArray) {
56+ Stack<String> stack = new Stack<String>();
57+ for (int i = commandArray.length - 1; 0 <= i; i--) {
58+ stack.push(commandArray[i]);
59+ }
60+ return getMethodName(stack);
61+ }
62+
63+ private String getMethodName(Stack<String> commandStack) {
64+ if (commandStack.size() == 0) {
65+ return this._methodName;
66+ } else {
67+ String cmdFragment = commandStack.pop();
68+ if (this._subFragment.containsKey(cmdFragment)) {
69+ return this._subFragment.get(cmdFragment).getMethodName(commandStack);
70+ } else {
71+ return null;
72+ }
73+ }
74+ }
75+
76+ @Override
77+ public String toString() {
78+ StringBuilder sb = new StringBuilder(this._commandFragmentName).append("=").append(this._methodName);
79+ if (this._subFragment != null) {
80+ sb.append("{");
81+ boolean firstOne = true;
82+ for (CommandFragment cf: this._subFragment.values()) {
83+ if (!firstOne) {
84+ sb.append(",");
85+ }
86+ firstOne = false;
87+ sb.append(cf.toString());
88+ }
89+ sb.append("}");
90+ }
91+ return sb.toString();
92+ }
93+
94+
95+}