+++ /dev/null
-/*--------------------------------------------------------------------------\r
- * Copyright 2009 Taro L. Saito\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *--------------------------------------------------------------------------*/\r
-//--------------------------------------\r
-// XerialJ\r
-//\r
-// SilkMain.java\r
-// Since: Apr 9, 2009 5:00:14 PM\r
-//\r
-// $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/silk/cui/SilkMain.java $\r
-// $Author: leo $\r
-//--------------------------------------\r
-package org.xerial.silk.cui;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.InputStreamReader;\r
-import java.net.URL;\r
-import java.util.HashSet;\r
-import java.util.Set;\r
-\r
-import org.xerial.core.XerialError;\r
-import org.xerial.core.XerialErrorCode;\r
-import org.xerial.core.XerialException;\r
-import org.xerial.util.FileResource;\r
-import org.xerial.util.log.LogLevel;\r
-import org.xerial.util.log.Logger;\r
-import org.xerial.util.log.SimpleLogWriter;\r
-import org.xerial.util.opt.Argument;\r
-import org.xerial.util.opt.Option;\r
-import org.xerial.util.opt.OptionParser;\r
-\r
-/**\r
- * Command-line interface for managing Silk data format.\r
- * \r
- * @author leo\r
- * \r
- */\r
-public class SilkMain {\r
- private static Logger _logger = Logger.getLogger(SilkMain.class);\r
-\r
- public static class SilkGlobalOption {\r
- @Option(symbol = "h", longName = "help", description = "display help message")\r
- boolean displayHelp = false;\r
-\r
- @Option(longName = "loglevel", description = "set loglevel to one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL or ALL")\r
- protected LogLevel logLevel = LogLevel.INFO;\r
-\r
- @Argument(index = 0, name = "sub command", required = false)\r
- String subCommand = "help";\r
- }\r
-\r
- static Set<Class<SilkCommand>> availableCommands = new HashSet<Class<SilkCommand>>();\r
-\r
- static {\r
- availableCommands.addAll(FileResource.findClasses(SilkMain.class.getPackage(),\r
- SilkCommand.class, SilkMain.class.getClassLoader()));\r
-\r
- }\r
-\r
- public static void main(String[] args) {\r
- Logger.getRootLogger().setLogWriter(new SimpleLogWriter(System.err));\r
-\r
- SilkGlobalOption globalOption = new SilkGlobalOption();\r
- OptionParser parser = new OptionParser(globalOption);\r
- Logger.getRootLogger().setLogLevel(globalOption.logLevel);\r
- try {\r
- parser.parse(args, true);\r
-\r
- if (globalOption.subCommand == null)\r
- throw new XerialException(XerialErrorCode.INVALID_INPUT, "no command");\r
-\r
- SilkCommand command = null;\r
- for (Class<SilkCommand> each : availableCommands) {\r
- try {\r
- command = each.newInstance();\r
- if (command.getName() != null\r
- && command.getName().equals(globalOption.subCommand))\r
- break;\r
- }\r
- catch (InstantiationException e) {\r
- _logger.warn(e);\r
- }\r
- catch (IllegalAccessException e) {\r
- _logger.warn(e);\r
- }\r
- }\r
-\r
- if (command == null) {\r
- _logger.error("command is not specified");\r
- return;\r
- }\r
-\r
- // \r
- OptionParser subCommandOptionParser = new OptionParser(command);\r
- // run the sub command\r
- if (globalOption.displayHelp) {\r
- // display help messsage of the command\r
- String helpFileName = String.format("help-%s.txt", command.getName());\r
- URL helpFileAddr = FileResource.find(SilkMain.class, helpFileName);\r
- if (helpFileAddr == null)\r
- throw new XerialError(XerialErrorCode.RESOURCE_NOT_FOUND,\r
- "help file not found: " + helpFileName);\r
-\r
- BufferedReader helpReader = new BufferedReader(new InputStreamReader(helpFileAddr\r
- .openStream()));\r
- String line;\r
- while ((line = helpReader.readLine()) != null) {\r
- System.out.println(line);\r
- }\r
- subCommandOptionParser.printUsage();\r
-\r
- return;\r
- }\r
-\r
- subCommandOptionParser.parse(parser.getUnusedArguments());\r
- command.execute();\r
-\r
- }\r
- catch (Exception e) {\r
- _logger.error(e);\r
- }\r
- catch (Error e) {\r
- e.printStackTrace();\r
- }\r
-\r
- }\r
-\r
-}\r