source: trunk/java-utils/src/main/java/de/ugoe/cs/util/console/defaultcommands/CMDman.java

Last change on this file was 1348, checked in by pharms, 11 years ago
  • removed find bugs warnings
  • Property svn:mime-type set to text/plain
File size: 3.2 KB
Line 
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.
14
15package de.ugoe.cs.util.console.defaultcommands;
16
17import java.io.BufferedReader;
18import java.io.IOException;
19import java.io.InputStream;
20import java.io.InputStreamReader;
21import java.util.List;
22
23import org.apache.commons.lang.WordUtils;
24
25import de.ugoe.cs.util.console.Command;
26import de.ugoe.cs.util.console.CommandExecuter;
27import 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 */
37public 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 {
51            throw new IllegalArgumentException();
52        }
53
54        Command cmd = CommandExecuter.getInstance().getCMD(command);
55
56        if (cmd != null) {
57            InputStream manStream =
58                ClassLoader.getSystemResourceAsStream("manuals/" + command);
59           
60            if (manStream != null) {
61                BufferedReader reader = null;
62                try {
63                    reader = new BufferedReader(new InputStreamReader(manStream, "UTF-8"));
64                    String line = reader.readLine();
65                   
66                    while (line != null) {
67                        line = line.replace("$USAGE$", "Usage: " + cmd.help());
68                        Console.println(WordUtils.wrap(line, 100));
69                        line = reader.readLine();
70                    }
71                }
72                catch (IOException e) {
73                    Console.printerrln("Failure reading man page");
74                    Console.logException(e);
75                }
76                finally {
77                    try {
78                        if (reader != null) {
79                            reader.close();
80                        }
81                    }
82                    catch (IOException e) {
83                        // Ignore, as this is unimportant to the user
84                    }
85                }
86            }
87            else {
88                Console.println("No man page for command " + command + " available");
89            }
90        }
91        else {
92            Console.println("Command " + command + " not found.");
93        }
94    }
95
96    /*
97     * (non-Javadoc)
98     *
99     * @see de.ugoe.cs.util.console.Command#help()
100     */
101    @Override
102    public String help() {
103        return "man <commandName>";
104    }
105
106}
Note: See TracBrowser for help on using the repository browser.