1 /* 2 * Replica is published under the terms 3 * of the Apache Software License. 4 */ 5 package replica.server.mvc; 6 7 import java.sql.ResultSet; 8 import java.sql.ResultSetMetaData; 9 import java.sql.SQLException; 10 import java.util.logging.Level; 11 import java.util.logging.Logger; 12 13 /*** 14 * 15 * @author Pedro Costa 16 * @author Helder Silva 17 * @since 20/Jan/2004 18 */ 19 public class TableView implements View { 20 21 static Logger logger = Logger.getLogger(TableView.class.getName()); 22 23 final static char SPACE = ' '; 24 final static char DASH = '-'; 25 26 /*** 27 * 28 */ 29 public TableView() { 30 super(); 31 } 32 33 /* (non-Javadoc) 34 * @see replica.server.mvc.View#render(java.lang.Object) 35 */ 36 public Object render(Object model) { 37 return render((ResultSet)model); 38 } 39 40 /*** 41 * Render a ResultSet as a text string. 42 * 43 * @param model 44 * @return 45 */ 46 private String render(ResultSet model){ 47 48 try{ 49 StringBuffer res = new StringBuffer(); 50 51 ResultSetMetaData metaData = model.getMetaData(); 52 53 int columnCount = metaData.getColumnCount(); 54 55 writeHeader(res, columnCount, metaData); 56 57 writeDividerLine( res, columnCount, metaData, DASH); 58 59 while( model.next() ) 60 writeLine(res, columnCount, metaData, model); 61 62 return res.toString(); 63 } 64 catch(SQLException e){ 65 logger.log(Level.SEVERE, e.getMessage(), e); 66 return "Problem returning data : [" + e.getMessage() +"]."; 67 } 68 finally{ 69 try{ 70 model.getStatement().close(); 71 } 72 catch(Throwable t){ 73 } 74 } 75 } 76 77 private void writeHeader(StringBuffer res, int columnCount, ResultSetMetaData metaData) throws SQLException{ 78 79 res.append("|"); 80 for (int i = 1; i <= columnCount; i++) { 81 res.append( fillSpaces( metaData.getColumnLabel(i), metaData.getColumnDisplaySize(i), true ) ) 82 .append("|"); 83 } 84 res.append("\n"); 85 } 86 87 private void writeDividerLine(StringBuffer res, int columnCount, ResultSetMetaData metaData, char fillChar) throws SQLException{ 88 89 res.append("|"); 90 91 for (int i = 1; i <= columnCount; i++) { 92 res.append( fill( "", metaData.getColumnDisplaySize(i), true, fillChar) ); 93 if( i == columnCount ) 94 res.append("|"); 95 else 96 res.append("+"); 97 } 98 99 res.append("\n"); 100 } 101 102 private void writeLine(StringBuffer res, int columnCount, ResultSetMetaData metaData, ResultSet model) throws SQLException{ 103 104 res.append("|"); 105 for (int i = 1; i <= columnCount; i++) { 106 Object value = model.getObject(i); 107 res.append( fillSpaces( value!=null?value.toString():"", 108 metaData.getColumnDisplaySize(i), 109 metaData.getColumnClassName(i).equals( String.class.getName() ) ) ) 110 .append("|"); 111 } 112 res.append("\n"); 113 } 114 115 /*** 116 * Return the received string filled with spaces until the returned string has the indicated 117 * number of characters. 118 * 119 * @param value the string to be formated 120 * @param size the required size for the string 121 * @param appendRight if true spaces are appended to the right of the string, else, appended to the left 122 * @return the formated string 123 */ 124 private String fillSpaces(String value, int size, boolean appendRight){ 125 126 return fill( value, size, appendRight, SPACE); 127 } 128 129 /*** 130 * Return the received string filled with the given char until the returned string has the indicated 131 * number of characters. 132 * 133 * @param value the string to be formated 134 * @param size the required size for the string 135 * @param appendRight if true spaces are appended to the right of the string, else, appended to the left 136 * @param theChar the char to use to fill 137 * @return the formated string 138 */ 139 private String fill(String value, int size, boolean appendRight, char theChar){ 140 141 if( value.length() > size ) 142 return value.substring(0, size); 143 144 StringBuffer fillChars = new StringBuffer(); 145 146 int numChars = size - value.length(); 147 for (int i = 0; i < numChars; i++) { 148 fillChars.append(theChar); 149 } 150 151 return appendRight ? value + fillChars.toString() : fillChars.toString() + value; 152 } 153 }

This page was automatically generated by Maven