SimpleConsole.java
001 /*
002  *  Licensed to the Apache Software Foundation (ASF) under one
003  *  or more contributor license agreements.  See the NOTICE file
004  *  distributed with this work for additional information
005  *  regarding copyright ownership.  The ASF licenses this file
006  *  to you under the Apache License, Version 2.0 (the
007  *  "License"); you may not use this file except in compliance
008  *  with the License.  You may obtain a copy of the License at
009  *
010  *    http://www.apache.org/licenses/LICENSE-2.0
011  *
012  *  Unless required by applicable law or agreed to in writing,
013  *  software distributed under the License is distributed on an
014  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015  *  KIND, either express or implied.  See the License for the
016  *  specific language governing permissions and limitations
017  *  under the License.    
018  *
019  
020  */
021 package org.apache.qpid.tools.utils;
022 
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025 
026 import java.io.BufferedReader;
027 import java.io.BufferedWriter;
028 import java.io.IOException;
029 import java.util.LinkedList;
030 import java.util.List;
031 
032 public class SimpleConsole implements Console
033 {
034     /** SLF4J Logger. */
035     private static Logger _devlog = LoggerFactory.getLogger(SimpleConsole.class);
036 
037     /** Console Writer. */
038     protected static BufferedWriter _consoleWriter;
039 
040     /** Console Reader. */
041     protected static BufferedReader _consoleReader;
042 
043     /** Parser for command-line input. */
044     protected CommandParser _parser;
045 
046     public SimpleConsole(BufferedWriter writer, BufferedReader reader)
047     {
048         _consoleWriter = writer;
049         _consoleReader = reader;
050         _parser = new SimpleCommandParser(_consoleReader);
051     }
052 
053     public void print(String... message)
054     {
055         try
056         {
057             for (String s : message)
058             {
059                 _consoleWriter.write(s);
060             }
061             _consoleWriter.flush();
062         }
063         catch (IOException e)
064         {
065             _devlog.error(e.getMessage() ": Occured whilst trying to write:" + message);
066         }
067 
068     }
069 
070     public void println(String... message)
071     {
072         print(message);
073         print(System.getProperty("line.separator"));
074     }
075 
076 
077     public String readln()
078     {
079         try
080         {
081             return _consoleReader.readLine();
082         }
083         catch (IOException e)
084         {
085             _devlog.debug("Unable to read input due to:" + e.getMessage());
086             return null;
087         }
088     }
089 
090     public String[] readCommand()
091     {
092         try
093         {
094             return _parser.parse();
095         }
096         catch (IOException e)
097         {
098             _devlog.error("Error reading command:" + e.getMessage());
099             return new String[0];
100         }
101     }
102 
103     public CommandParser getCommandParser()
104     {
105         return _parser;
106     }
107 
108     public void setCommandParser(CommandParser parser)
109     {
110         _parser = parser;
111     }
112 
113     public void displayList(boolean hasTitle, String... list)
114     {
115         java.util.List<java.util.List> data = new LinkedList<List>();
116 
117         java.util.List<String> values = new LinkedList<String>();
118 
119         data.add(values);
120 
121         for (String value : list)
122         {
123             values.add(value);
124         }
125 
126         if (hasTitle)
127         {
128             values.add(1"*divider");
129         }
130 
131         printMap(null, data);
132     }
133 
134     /**
135      *
136      *  Prints the list of String nicely.
137      *
138      *  +----------------------------+
139      *  |  Heading                   |
140      *  +----------------------------+
141      *  |  title      |  title       |   ..
142      *  +----------------------------+
143      *  |  Item 2     |  value 2     |   ..
144      *  |  Item 3     |  value 2     |   ..
145      *  +----------------------------+
146      *
147      @param title The title to display if any
148      @param entries the entries to display in a map.
149      */
150     public void printMap(String title, java.util.List<java.util.List> entries)
151     {
152         try
153         {
154             int columns = entries.size();
155 
156             int[] columnWidth = new int[columns];
157 
158             // calculate row count
159             int rowMax = 0;
160 
161             //the longest item
162             int itemMax = 0;
163 
164             for (int i = 0; i < columns; i++)
165             {
166                 int columnIRowMax = entries.get(i).size();
167 
168                 if (columnIRowMax > rowMax)
169                 {
170                     rowMax = columnIRowMax;
171                 }
172                 for (Object values : entries.get(i))
173                 {
174                     if (values.toString().equals(Console.ROW_DIVIDER))
175                     {
176                         continue;
177                     }
178 
179                     int itemLength = values.toString().length();
180 
181                     //note for single width
182                     if (itemLength > itemMax)
183                     {
184                         itemMax = itemLength;
185                     }
186 
187                     //note for mulit width
188                     if (itemLength > columnWidth[i])
189                     {
190                         columnWidth[i= itemLength;
191                     }
192 
193                 }
194             }
195 
196             int tableWidth = 0;
197 
198 
199             for (int i = 0; i < columns; i++)
200             {
201                 // plus 2 for the space padding
202                 columnWidth[i+= 2;
203             }
204             for (int size : columnWidth)
205             {
206                 tableWidth += size;
207             }
208             tableWidth += (columns - 1);
209 
210             if (title != null)
211             {
212                 if (title.length() > tableWidth)
213                 {
214                     tableWidth = title.length();
215                 }
216 
217                 printCellRow("+""-", tableWidth);
218 
219                 printCell(CellFormat.CENTRED, "|", tableWidth, " " + title + " "0);
220                 _consoleWriter.newLine();
221 
222             }
223 
224             //put top line | or bottom of title
225             printCellRow("+""-", tableWidth);
226 
227             //print the table data
228             int row = 0;
229 
230             for (; row < rowMax; row++)
231             {
232                 for (int i = 0; i < columns; i++)
233                 {
234                     java.util.List columnData = entries.get(i);
235 
236                     String value;
237                     // does this column have a value for this row
238                     if (columnData.size() > row)
239                     {
240                         value = " " + columnData.get(row).toString() " ";
241                     }
242                     else
243                     {
244                         value = "  ";
245                     }
246 
247                     if (i == && value.equals(" " + Console.ROW_DIVIDER + " "))
248                     {
249                         printCellRow("+""-", tableWidth);
250                         //move on to the next row
251                         break;
252                     }
253                     else
254                     {
255                         printCell(CellFormat.LEFT, "|", columnWidth[i], value, i);
256                     }
257 
258                     // if it is the last row then do a new line.
259                     if (i == columns - 1)
260                     {
261                         _consoleWriter.newLine();
262                     }
263                 }
264             }
265 
266             printCellRow("+""-", tableWidth);
267 
268         }
269         catch (IOException e)
270         {
271             _devlog.error(e.getMessage() ": Occured whilst trying to write.");
272         }
273     }
274 
275     public void close()
276     {
277 
278         try
279         {
280             _consoleReader.close();
281         }
282         catch (IOException e)
283         {
284             _devlog.error(e.getMessage() ": Occured whilst trying to close reader.");
285         }
286 
287         try
288         {
289 
290             _consoleWriter.close();
291         }
292         catch (IOException e)
293         {
294             _devlog.error(e.getMessage() ": Occured whilst trying to close writer.");
295         }
296 
297     }
298 
299     private void printCell(CellFormat format, String edge, int cellWidth, String cell, int columnthrows IOException
300     {
301         int pad = cellWidth - cell.length();
302 
303         if (column == 0)
304         {
305             _consoleWriter.write(edge);
306         }
307 
308         switch (format)
309         {
310             case CENTRED:
311                 printPad(" ", pad / 2);
312                 break;
313             case RIGHT:
314                 printPad(" ", pad);
315                 break;
316         }
317 
318         _consoleWriter.write(cell);
319 
320 
321         switch (format)
322         {
323             case CENTRED:
324                 // if pad isn't even put the extra one on the right
325                 if (pad % == 0)
326                 {
327                     printPad(" ", pad / 2);
328                 }
329                 else
330                 {
331                     printPad(" "(pad / 21);
332                 }
333                 break;
334             case LEFT:
335                 printPad(" ", pad);
336                 break;
337         }
338 
339 
340         _consoleWriter.write(edge);
341 
342     }
343 
344     private void printCellRow(String edge, String mid, int cellWidththrows IOException
345     {
346         _consoleWriter.write(edge);
347 
348         printPad(mid, cellWidth);
349 
350         _consoleWriter.write(edge);
351         _consoleWriter.newLine();
352     }
353 
354     private void printPad(String padChar, int countthrows IOException
355     {
356         for (int i = 0; i < count; i++)
357         {
358             _consoleWriter.write(padChar);
359         }
360     }
361 
362 
363 }