1 22 23 package org.xquark.mapper.util; 24 25 import java.io.PrintWriter ; 26 import java.io.Writer ; 27 28 31 public class Tabulator 32 { 33 private static final String RCSRevision = "$Revision: 1.1 $"; 34 private static final String RCSName = "$Name: $"; 35 36 private PrintWriter out; 37 private int[] offsets; 38 private int lineLen; 39 private StringBuffer buf; 40 private int itemCount = 0; 41 42 43 public Tabulator(Writer out, int lineLen, int[] offsets) 44 { 45 this.out = new PrintWriter (out); 46 this.offsets = offsets; 47 this.lineLen = lineLen; 48 buf = new StringBuffer (lineLen); 49 } 50 51 public void addItem(int n) 52 { 53 addItem(new Integer (n)); 54 } 55 56 public void addItem(Object item) 57 { 58 fillWithBlank(offsets[itemCount]); 59 60 if (item != null) 61 { 62 String s = item.toString(); 63 int pad = 0; 64 if (itemCount == (offsets.length - 1)) 65 pad = lineLen - offsets[itemCount - 1]; 66 else 67 pad = offsets[itemCount + 1] - offsets[itemCount]; 68 if (s.length() > pad) 69 { 70 buf.append(s.substring(0, pad - 3)); 71 buf.append("..."); 72 } 73 else 74 buf.append(s); 75 } 76 77 itemCount++; 78 79 if (itemCount == offsets.length) 81 flush(); 82 } 83 84 public void flush() 85 { 86 out.print(buf.toString()); 88 newLine(); 89 buf.setLength(0); 90 itemCount = 0; 91 } 92 93 public void newLine() 94 { 95 out.println(); 96 out.flush(); 97 } 98 99 private void fillWithBlank(int offset) 100 { 101 for(int i = buf.length(); i < offset; i++) 102 buf.append(' '); 103 } 104 } 105 | Popular Tags |