1 28 package net.sf.jasperreports.engine.util; 29 30 import java.text.BreakIterator ; 31 import java.text.CharacterIterator ; 32 import java.text.StringCharacterIterator ; 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 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 breakIter; 58 59 public BreakIteratorSplitCharacter() 60 { 61 this(BreakIterator.getLineInstance()); 62 } 63 64 public BreakIteratorSplitCharacter(BreakIterator 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 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 clone() 201 { 202 try 203 { 204 StringCharacterIterator other = (StringCharacterIterator ) super.clone(); 205 return other; 206 } 207 catch (CloneNotSupportedException e) 208 { 209 throw new InternalError (); 210 } 211 } 212 } 213 214 } 215 | Popular Tags |