KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > BreakIteratorSplitCharacter


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import java.text.BreakIterator JavaDoc;
31 import java.text.CharacterIterator JavaDoc;
32 import java.text.StringCharacterIterator JavaDoc;
33
34 import net.sf.jasperreports.engine.JRRuntimeException;
35
36 import com.lowagie.text.SplitCharacter;
37 import com.lowagie.text.pdf.PdfChunk;
38
39
40 /**
41  * Implementation of {@link com.lowagie.text.SplitCharacter SplitCharacter} that
42  * uses the same logic as AWT to break texts into lines.
43  *
44  * @author Lucian Chirita (lucianc@users.sourceforge.net)
45  * @version $Id: BreakIteratorSplitCharacter.java 1276 2006-06-01 20:36:30 +0300 (Thu, 01 Jun 2006) lucianc $
46  *
47  * @see net.sf.jasperreports.engine.export.JRPdfExporterParameter#FORCE_LINEBREAK_POLICY
48  * @see net.sf.jasperreports.engine.util.JRProperties#PDF_FORCE_LINEBREAK_POLICY
49  */

50 public class BreakIteratorSplitCharacter implements SplitCharacter
51 {
52
53     private char[] chars;
54     private int start, end;
55     private boolean[] boundary;
56     private int lastBoundary;
57     private final BreakIterator JavaDoc breakIter;
58     
59     public BreakIteratorSplitCharacter()
60     {
61         this(BreakIterator.getLineInstance());
62     }
63     
64     public BreakIteratorSplitCharacter(BreakIterator JavaDoc breakIter)
65     {
66         this.breakIter = breakIter;
67     }
68
69     public boolean isSplitCharacter(int startIdx, int current, int endIdx, char[] cc, PdfChunk[] ck)
70     {
71         ++current;
72         if (current == endIdx)
73         {
74             return true;
75         }
76
77         if (!(chars == cc && this.start == startIdx && this.end == endIdx))
78         {
79             chars = cc;
80             this.start = startIdx;
81             this.end = endIdx;
82
83             breakIter.setText(new ArrayCharIterator(cc, startIdx, endIdx));
84
85             boundary = new boolean[endIdx - startIdx + 1];
86
87             lastBoundary = breakIter.first();
88             if (lastBoundary != BreakIterator.DONE)
89             {
90                 boundary[lastBoundary - startIdx] = true;
91             }
92         }
93
94         while (current > lastBoundary)
95         {
96             lastBoundary = breakIter.next();
97
98             if (lastBoundary == BreakIterator.DONE)
99             {
100                 lastBoundary = Integer.MAX_VALUE;
101             }
102             else
103             {
104                 boundary[lastBoundary - startIdx] = true;
105             }
106         }
107
108         return boundary[current - startIdx];
109     }
110
111     protected static class ArrayCharIterator implements CharacterIterator JavaDoc
112     {
113
114         private char[] chars;
115         private int start;
116         private int end;
117         private int curr;
118
119         public ArrayCharIterator(char[] chars, int start, int end)
120         {
121             this.chars = chars;
122             this.start = start;
123             this.end = end;
124         }
125
126         public char first()
127         {
128             curr = start;
129             return current();
130         }
131
132         public char last()
133         {
134             if (end == start)
135             {
136                 curr = end;
137             }
138             else
139             {
140                 curr = end - 1;
141             }
142             return current();
143         }
144
145         public char setIndex(int position)
146         {
147             if (position < start || position > end)
148             {
149                 throw new JRRuntimeException("Invalid index " + position + " (start = " + start + ", end = " + end + ")");
150             }
151             curr = position;
152             return current();
153         }
154
155         public char current()
156         {
157             if (curr < start || curr >= end)
158             {
159                 return DONE;
160             }
161             return chars[curr];
162         }
163
164         public char next()
165         {
166             if (curr >= end - 1)
167             {
168                 curr = end;
169                 return DONE;
170             }
171             curr++;
172             return chars[curr];
173         }
174
175         public char previous()
176         {
177             if (curr <= start)
178             {
179                 return DONE;
180             }
181             curr--;
182             return chars[curr];
183         }
184
185         public int getBeginIndex()
186         {
187             return start;
188         }
189
190         public int getEndIndex()
191         {
192             return end;
193         }
194
195         public int getIndex()
196         {
197             return curr;
198         }
199
200         public Object JavaDoc clone()
201         {
202             try
203             {
204                 StringCharacterIterator JavaDoc other = (StringCharacterIterator JavaDoc) super.clone();
205                 return other;
206             }
207             catch (CloneNotSupportedException JavaDoc e)
208             {
209                 throw new InternalError JavaDoc();
210             }
211         }
212     }
213
214 }
215
Popular Tags