File Source: Command.java
/*
P/P * Method: com.dmdirc.commandparser.commands.Command__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.commandparser.commands;
24
25 import com.dmdirc.commandparser.CommandManager;
26 import com.dmdirc.ui.interfaces.InputWindow;
27 import com.dmdirc.ui.messages.Styliser;
28
29 /**
30 * Represents a generic command.
31 *
32 * @author chris
33 */
/*
P/P * Method: void com.dmdirc.commandparser.commands.Command()
*/
34 public abstract class Command {
35
36 /** The format name used for command output. */
37 protected static final String FORMAT_OUTPUT = "commandOutput";
38
39 /** The format name used for command errors. */
40 protected static final String FORMAT_ERROR = "commandError";
41
42 /**
43 * Sends a line, if appropriate, to the specified target.
44 * @param target The command window to send the line to
45 * @param isSilent Whether this command is being silenced or not
46 * @param type The type of message to send
47 * @param args The arguments of the message
48 */
49 protected final void sendLine(final InputWindow target,
50 final boolean isSilent, final String type, final Object ... args) {
/*
P/P * Method: void sendLine(InputWindow, bool, String, Object[])
*
* Test Vectors:
* isSilent: {1}, {0}
* target: Addr_Set{null}, Inverse{null}
*/
51 if (!isSilent && target != null) {
52 target.addLine(type, args);
53 }
54 }
55
56 /**
57 * Sends a usage line, if appropriate, to the specified target.
58 *
59 * @param target The command window to send the line to
60 * @param isSilent Whether this command is being silenced or not
61 * @param name The name of the command that's raising the error
62 * @param args The arguments that the command accepts or expects
63 */
64 protected final void showUsage(final InputWindow target,
65 final boolean isSilent, final String name, final String args) {
/*
P/P * Method: void showUsage(InputWindow, bool, String, String)
*
* Preconditions:
* init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
*/
66 sendLine(target, isSilent, "commandUsage", CommandManager.getCommandChar(),
67 name, args);
68 }
69
70 /**
71 * Formats the specified data into a table suitable for output in the
72 * textpane. It is expected that each String[] in data has the same number
73 * of elements as the headers array.
74 *
75 * @param headers The headers of the table.
76 * @param data The contents of the table.
77 * @return A string containing an ASCII table
78 */
79 protected static String doTable(final String[] headers, final String[][] data) {
/*
P/P * Method: String doTable(String[], String[][])
*
* Preconditions:
* data != null
* data.length <= 232-1
* headers != null
* (soft) data[...] != null
* (soft) data[...].length in {1..232-1}
* (soft) data[...].length - headers.length in {0..232-2}
* (soft) data[...][...] != null
* (soft) headers.length in {1..232-1}
* (soft) headers[...] != null
*
* Presumptions:
* java.lang.String:length(...)@87 <= 232-4
* java.lang.String:length(...)@90 <= 232-4
*
* Postconditions:
* init'ed(java.lang.StringBuilder:toString(...)._tainted)
* return_value == &java.lang.StringBuilder:toString(...)
*/
80 final StringBuilder res = new StringBuilder();
81 res.append(Styliser.CODE_FIXED);
82 res.append(Styliser.CODE_BOLD);
83
84 int[] maxsizes = new int[headers.length];
85
86 for (int i = 0; i < headers.length; i++) {
87 maxsizes[i] = headers[i].length() + 3;
88
89 for (int j = 0; j < data.length; j++) {
90 maxsizes[i] = Math.max(maxsizes[i], data[j][i].length() + 3);
91 }
92
93 doPadding(res, headers[i], maxsizes[i]);
94 }
95
96 for (String[] source : data) {
97 res.append('\n');
98 res.append(Styliser.CODE_FIXED);
99
100 for (int i = 0; i < source.length; i++) {
101 doPadding(res, source[i], maxsizes[i]);
102 }
103 }
104
105 return res.toString();
106 }
107
108 /**
109 * Adds the specified data to the stringbuilder, padding with spaces to
110 * the specified size.
111 *
112 * @param builder The stringbuilder to append data to
113 * @param data The data to be added
114 * @param size The minimum size that should be used
115 */
116 private static void doPadding(final StringBuilder builder, final String data,
117 final int size) {
/*
P/P * Method: void doPadding(StringBuilder, String, int)
*
* Preconditions:
* builder != null
* data != null
*
* Postconditions:
* builder._tainted == old builder._tainted | data._tainted
* init'ed(builder._tainted)
*/
118 builder.append(data);
119
120 for (int i = 0; i < size - data.length(); i++) {
121 builder.append(' ');
122 }
123 }
124
125 }
SofCheck Inspector Build Version : 2.17854
| Command.java |
2009-Jun-25 01:54:24 |
| Command.class |
2009-Sep-02 17:04:12 |