KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > util > Tabulator


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.util;
24
25 import java.io.PrintWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27
28 /**
29  *
30  */

31 public class Tabulator
32 {
33     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
34     private static final String JavaDoc RCSName = "$Name: $";
35
36     private PrintWriter JavaDoc out;
37     private int[] offsets;
38     private int lineLen;
39     private StringBuffer JavaDoc buf;
40     private int itemCount = 0;
41     
42     /** Creates a new instance of Tabulator */
43     public Tabulator(Writer JavaDoc out, int lineLen, int[] offsets)
44     {
45         this.out = new PrintWriter JavaDoc(out);
46         this.offsets = offsets;
47         this.lineLen = lineLen;
48         buf = new StringBuffer JavaDoc(lineLen);
49     }
50     
51     public void addItem(int n)
52     {
53         addItem(new Integer JavaDoc(n));
54     }
55     
56     public void addItem(Object JavaDoc item)
57     {
58         fillWithBlank(offsets[itemCount]);
59             
60         if (item != null)
61         {
62             String JavaDoc 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         // new line
80
if (itemCount == offsets.length)
81             flush();
82     }
83     
84     public void flush()
85     {
86 // String value = buf.toString();
87
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