[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
[718] | 14 | |
---|
| 15 | package de.ugoe.cs.util.console.defaultcommands; |
---|
| 16 | |
---|
[1341] | 17 | import java.io.BufferedReader; |
---|
[718] | 18 | import java.io.IOException; |
---|
| 19 | import java.io.InputStream; |
---|
[1341] | 20 | import java.io.InputStreamReader; |
---|
[718] | 21 | import java.util.List; |
---|
| 22 | |
---|
| 23 | import org.apache.commons.lang.WordUtils; |
---|
| 24 | |
---|
| 25 | import de.ugoe.cs.util.console.Command; |
---|
| 26 | import de.ugoe.cs.util.console.CommandExecuter; |
---|
| 27 | import de.ugoe.cs.util.console.Console; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * <p> |
---|
| 31 | * Prints the manual of a command to the Console. |
---|
| 32 | * </p> |
---|
| 33 | * |
---|
| 34 | * @version $Revision: $ $Date: Aug 30, 2012$ |
---|
| 35 | * @author 2012, last modified by $Author: sherbold$ |
---|
| 36 | */ |
---|
| 37 | public class CMDman implements Command { |
---|
| 38 | |
---|
| 39 | /* |
---|
| 40 | * (non-Javadoc) |
---|
| 41 | * |
---|
| 42 | * @see de.ugoe.cs.util.console.Command#run(java.util.List) |
---|
| 43 | */ |
---|
| 44 | @Override |
---|
| 45 | public void run(List<Object> parameters) { |
---|
| 46 | String command; |
---|
| 47 | if (parameters.size() > 0) { |
---|
| 48 | command = (String) parameters.get(0); |
---|
| 49 | } |
---|
| 50 | else { |
---|
[766] | 51 | throw new IllegalArgumentException(); |
---|
[718] | 52 | } |
---|
| 53 | |
---|
| 54 | Command cmd = CommandExecuter.getInstance().getCMD(command); |
---|
| 55 | |
---|
| 56 | if (cmd != null) { |
---|
| 57 | InputStream manStream = |
---|
| 58 | ClassLoader.getSystemResourceAsStream("manuals/" + command); |
---|
[1341] | 59 | |
---|
[718] | 60 | if (manStream != null) { |
---|
[1341] | 61 | BufferedReader reader = new BufferedReader(new InputStreamReader(manStream)); |
---|
[718] | 62 | try { |
---|
[1341] | 63 | String line = reader.readLine(); |
---|
| 64 | |
---|
| 65 | while (line != null) { |
---|
| 66 | line = line.replace("$USAGE$", "Usage: " + cmd.help()); |
---|
| 67 | Console.println(WordUtils.wrap(line, 100)); |
---|
| 68 | line = reader.readLine(); |
---|
| 69 | } |
---|
[718] | 70 | } |
---|
| 71 | catch (IOException e) { |
---|
| 72 | Console.printerrln("Failure reading man page"); |
---|
| 73 | Console.logException(e); |
---|
| 74 | } |
---|
[1341] | 75 | finally { |
---|
| 76 | try { |
---|
| 77 | reader.close(); |
---|
| 78 | } |
---|
| 79 | catch (IOException e) { |
---|
| 80 | // Ignore, as this is unimportant to the user |
---|
| 81 | } |
---|
| 82 | } |
---|
[718] | 83 | } |
---|
| 84 | else { |
---|
[1341] | 85 | Console.println("No man page for command " + command + " available"); |
---|
[718] | 86 | } |
---|
| 87 | } |
---|
| 88 | else { |
---|
| 89 | Console.println("Command " + command + " not found."); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /* |
---|
| 94 | * (non-Javadoc) |
---|
| 95 | * |
---|
| 96 | * @see de.ugoe.cs.util.console.Command#help() |
---|
| 97 | */ |
---|
| 98 | @Override |
---|
| 99 | public String help() { |
---|
| 100 | return "man <commandName>"; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | } |
---|