1 28 package net.sf.jasperreports.engine.export; 29 30 import java.io.File ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.OutputStream ; 34 import java.io.OutputStreamWriter ; 35 import java.io.StringWriter ; 36 import java.io.Writer ; 37 import java.util.Arrays ; 38 import java.util.List ; 39 import java.util.StringTokenizer ; 40 41 import net.sf.jasperreports.engine.JRAbstractExporter; 42 import net.sf.jasperreports.engine.JRAlignment; 43 import net.sf.jasperreports.engine.JRException; 44 import net.sf.jasperreports.engine.JRExporterParameter; 45 import net.sf.jasperreports.engine.JRPrintFrame; 46 import net.sf.jasperreports.engine.JRPrintPage; 47 import net.sf.jasperreports.engine.JRPrintText; 48 import net.sf.jasperreports.engine.JasperPrint; 49 import net.sf.jasperreports.engine.util.JRStyledText; 50 import net.sf.jasperreports.engine.util.JRStyledTextParser; 51 52 import org.xml.sax.SAXException ; 53 54 74 public class JRTextExporter extends JRAbstractExporter 75 { 76 protected int pageWidth; 77 protected int pageHeight; 78 protected int characterHeight; 79 protected int characterWidth; 80 protected JRExportProgressMonitor progressMonitor; 81 protected Writer writer; 82 char[][] pageData; 83 protected String betweenPagesText; 84 protected String lineSeparator; 85 86 protected static final String systemLineSeparator = System.getProperty("line.separator"); 87 90 protected JRStyledTextParser styledTextParser = new JRStyledTextParser(); 91 92 95 public void exportReport() throws JRException 96 { 97 progressMonitor = (JRExportProgressMonitor)parameters.get(JRExporterParameter.PROGRESS_MONITOR); 98 99 100 setOffset(); 101 102 103 setInput(); 104 105 106 if (!isModeBatch) 107 { 108 setPageRange(); 109 } 110 111 String encoding = (String )parameters.get(JRExporterParameter.CHARACTER_ENCODING); 112 if (encoding == null) 113 { 114 encoding = "ISO-8859-1"; 115 } 116 117 Integer characterWidthParam = (Integer ) parameters.get(JRTextExporterParameter.CHARACTER_WIDTH); 118 if (characterWidthParam != null) { 119 characterWidth = characterWidthParam.intValue(); 120 if (characterWidth < 0) 121 throw new JRException("Character width must be greater than 0"); 122 } 123 else { 124 Integer pageWidthParam = (Integer ) parameters.get(JRTextExporterParameter.PAGE_WIDTH); 125 if (pageWidthParam != null) { 126 pageWidth = pageWidthParam.intValue(); 127 if (pageWidth <= 0) 128 throw new JRException("Page width must be greater than 0"); 129 } else { 130 throw new JRException("Character or page width must be specified"); 131 } 132 } 133 134 135 Integer characterHeightParam = (Integer ) parameters.get(JRTextExporterParameter.CHARACTER_HEIGHT); 136 if (characterHeightParam != null) { 137 characterHeight = characterHeightParam.intValue(); 138 if (characterHeight < 0) 139 throw new JRException("Character height must be greater than 0"); 140 } 141 else { 142 Integer pageHeightParam = (Integer ) parameters.get(JRTextExporterParameter.PAGE_HEIGHT); 143 if (pageHeightParam != null) { 144 pageHeight = pageHeightParam.intValue(); 145 if (pageHeight <= 0) 146 throw new JRException("Page height must be greater than 0"); 147 } 148 else { 149 throw new JRException("Character or page height must be specified"); 150 } 151 } 152 153 154 betweenPagesText = (String ) parameters.get(JRTextExporterParameter.BETWEEN_PAGES_TEXT); 155 if (betweenPagesText == null) { 156 betweenPagesText = systemLineSeparator + systemLineSeparator; 157 } 158 159 lineSeparator = (String ) parameters.get(JRTextExporterParameter.LINE_SEPARATOR); 160 if (lineSeparator == null) { 161 lineSeparator = systemLineSeparator; 162 } 163 164 StringBuffer sb = (StringBuffer )parameters.get(JRExporterParameter.OUTPUT_STRING_BUFFER); 165 if (sb != null) 166 { 167 try 168 { 169 writer = new StringWriter (); 170 exportReportToWriter(); 171 sb.append(writer.toString()); 172 } 173 catch (IOException e) 174 { 175 throw new JRException("Error writing to StringBuffer writer : " + jasperPrint.getName(), e); 176 } 177 finally 178 { 179 if (writer != null) 180 { 181 try 182 { 183 writer.close(); 184 } 185 catch(IOException e) 186 { 187 } 188 } 189 } 190 } 191 else 192 { 193 writer = (Writer )parameters.get(JRExporterParameter.OUTPUT_WRITER); 194 if (writer != null) 195 { 196 try 197 { 198 exportReportToWriter(); 199 } 200 catch (IOException e) 201 { 202 throw new JRException("Error writing to writer : " + jasperPrint.getName(), e); 203 } 204 } 205 else 206 { 207 OutputStream os = (OutputStream )parameters.get(JRExporterParameter.OUTPUT_STREAM); 208 if (os != null) 209 { 210 try 211 { 212 writer = new OutputStreamWriter (os, encoding); 213 exportReportToWriter(); 214 } 215 catch (IOException e) 216 { 217 throw new JRException("Error writing to OutputStream writer : " + jasperPrint.getName(), e); 218 } 219 } 220 else 221 { 222 File destFile = (File )parameters.get(JRExporterParameter.OUTPUT_FILE); 223 if (destFile == null) 224 { 225 String fileName = (String )parameters.get(JRExporterParameter.OUTPUT_FILE_NAME); 226 if (fileName != null) 227 { 228 destFile = new File (fileName); 229 } 230 else 231 { 232 throw new JRException("No output specified for the exporter."); 233 } 234 } 235 236 try 237 { 238 os = new FileOutputStream (destFile); 239 writer = new OutputStreamWriter (os, encoding); 240 exportReportToWriter(); 241 } 242 catch (IOException e) 243 { 244 throw new JRException("Error writing to file writer : " + jasperPrint.getName(), e); 245 } 246 finally 247 { 248 if (writer != null) 249 { 250 try 251 { 252 writer.close(); 253 } 254 catch(IOException e) 255 { 256 } 257 } 258 } 259 } 260 } 261 } 262 } 263 264 265 268 protected void exportReportToWriter() throws JRException, IOException 269 { 270 for(int reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++) 271 { 272 jasperPrint = (JasperPrint)jasperPrintList.get(reportIndex); 273 274 List pages = jasperPrint.getPages(); 275 if (pages != null && pages.size() > 0) 276 { 277 if (isModeBatch) 278 { 279 startPageIndex = 0; 280 endPageIndex = pages.size() - 1; 281 } 282 283 if (characterWidth > 0) 284 pageWidth = jasperPrint.getPageWidth() / characterWidth; 285 if (characterHeight > 0) 286 pageHeight = jasperPrint.getPageHeight() / characterHeight; 287 288 for(int i = startPageIndex; i <= endPageIndex; i++) 289 { 290 if (Thread.currentThread().isInterrupted()) 291 { 292 throw new JRException("Current thread interrupted."); 293 } 294 295 JRPrintPage page = (JRPrintPage)pages.get(i); 296 297 298 exportPage(page); 299 } 300 } 301 } 302 303 writer.flush(); 304 } 305 306 307 312 protected void exportPage(JRPrintPage page) throws IOException 313 { 314 List elements = page.getElements(); 315 316 pageData = new char[pageHeight][]; 317 for (int i = 0; i < pageHeight; i++) { 318 pageData[i] = new char[pageWidth]; 319 Arrays.fill(pageData[i], ' '); 320 } 321 322 exportElements(elements); 323 324 for (int i = 0; i < pageHeight; i++) { 325 writer.write(pageData[i]); 326 writer.write(lineSeparator); 327 } 328 329 writer.write(betweenPagesText); 330 331 if (progressMonitor != null) 332 { 333 progressMonitor.afterPageExport(); 334 } 335 } 336 337 338 protected void exportElements(List elements) 339 { 340 for (int i = 0; i < elements.size();i++) 341 { 342 Object element = elements.get(i); 343 if (element instanceof JRPrintText) 344 { 345 exportText((JRPrintText) element); 346 } 347 else if (element instanceof JRPrintFrame) 348 { 349 JRPrintFrame frame = (JRPrintFrame) element; 350 setFrameElementsOffset(frame, false); 351 try 352 { 353 exportElements(frame.getElements()); 354 } 355 finally 356 { 357 restoreElementOffsets(); 358 } 359 } 360 } 361 } 362 363 364 367 protected void exportText(JRPrintText element) 368 { 369 int rowCount = calculateYCoord(element.getHeight()); 370 int columnCount = calculateXCoord(element.getWidth()); 371 int x = calculateXCoord(element.getX() + getOffsetX()); 372 int y = calculateYCoord(element.getY() + getOffsetY()); 373 374 String allText; 375 JRStyledText styledText = getStyledText(element); 376 if (styledText == null) 377 { 378 allText = ""; 379 } 380 else 381 { 382 allText = styledText.getText(); 383 } 384 385 if (rowCount <= 0 || columnCount <= 0) 387 return; 388 389 if (allText != null && allText.length() == 0) 390 return; 391 392 StringBuffer [] rows = new StringBuffer [rowCount]; 394 rows[0] = new StringBuffer (); 395 int rowIndex = 0; 396 int rowPosition = 0; 397 398 StringTokenizer lfTokenizer = new StringTokenizer (allText, "\n"); 400 label:while (lfTokenizer.hasMoreTokens()) { 401 String line = lfTokenizer.nextToken(); 402 StringTokenizer spaceTokenizer = new StringTokenizer (line, " ", true); 403 404 while (spaceTokenizer.hasMoreTokens()) { 406 String word = spaceTokenizer.nextToken(); 407 408 while (word.length() > columnCount) { 411 rows[rowIndex].append(word.substring(0, columnCount - rowPosition)); 412 word = word.substring(columnCount - rowPosition, word.length()); 413 rowIndex++; 414 if(rowIndex == rowCount) 415 break label; 416 rowPosition = 0; 417 rows[rowIndex] = new StringBuffer (); 418 } 419 420 if (rowPosition + word.length() > columnCount) { 423 rowIndex++; 424 if (rowIndex == rowCount) 425 break label; 426 rowPosition = 0; 427 rows[rowIndex] = new StringBuffer (); 428 } 429 430 if (rowIndex > 9 && rowPosition == 0 && word.equals(" ")) 433 break; 434 435 rows[rowIndex].append(word); 438 rowPosition += word.length(); 439 } 440 441 442 rowIndex++; 443 if(rowIndex == rowCount) 444 break; 445 rowPosition = 0; 446 rows[rowIndex] = new StringBuffer (); 447 } 448 449 450 int xOffset = 0; 451 int yOffset = 0; 452 453 if (element.getVerticalAlignment() == JRAlignment.VERTICAL_ALIGN_BOTTOM) 454 yOffset = rowCount - rowIndex; 455 if (element.getVerticalAlignment() == JRAlignment.VERTICAL_ALIGN_MIDDLE) 456 yOffset = (rowCount - rowIndex) / 2; 457 458 for (int i = 0; i < rowIndex; i++) { 459 String line = rows[i].toString(); 460 int pos = line.length() - 1; 461 while (pos >= 0 && line.charAt(pos) == ' ') 462 pos--; 463 line = line.substring(0, pos + 1); 464 if (element.getHorizontalAlignment() == JRAlignment.HORIZONTAL_ALIGN_RIGHT) 465 xOffset = columnCount - line.length(); 466 if (element.getHorizontalAlignment() == JRAlignment.HORIZONTAL_ALIGN_CENTER) 467 xOffset = (columnCount - line.length()) / 2; 468 469 if (element.getHorizontalAlignment() == JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED) 472 if (i < rowIndex -1) 473 line = justifyText(line, columnCount); 474 475 char[] chars = line.toCharArray(); 476 System.arraycopy(chars, 0, pageData[y + yOffset + i], x + xOffset, chars.length); 477 } 478 } 479 480 481 484 private String justifyText(String s, int width) 485 { 486 StringBuffer justified = new StringBuffer (); 487 488 StringTokenizer t = new StringTokenizer (s, " "); 489 int tokenCount = t.countTokens(); 490 if (tokenCount <= 1) 491 return s; 492 493 String [] words = new String [tokenCount]; 494 int i = 0; 495 while (t.hasMoreTokens()) 496 words[i++] = t.nextToken(); 497 498 int emptySpace = width - s.length() + (words.length - 1); 499 int spaceCount = emptySpace / (words.length - 1); 500 int remainingSpace = emptySpace % (words.length - 1); 501 502 char[] spaces = new char[spaceCount]; 503 Arrays.fill(spaces, ' '); 504 505 for (i = 0; i < words.length - 1; i++) 506 { 507 justified.append(words[i]); 508 justified.append(spaces); 509 if (i < remainingSpace) 510 justified.append(' '); 511 } 512 justified.append(words[words.length-1]); 513 514 return justified.toString(); 515 } 516 517 518 521 protected int calculateYCoord(int y) 522 { 523 return pageHeight * y / jasperPrint.getPageHeight(); 524 } 525 526 529 protected int calculateXCoord(int x) 530 { 531 return pageWidth * x / jasperPrint.getPageWidth(); 532 } 533 534 535 538 protected JRStyledText getStyledText(JRPrintText textElement) 539 { 540 JRStyledText styledText = null; 541 542 String text = textElement.getText(); 543 if (text != null) 544 { 545 if (textElement.isStyledText()) 546 { 547 try 548 { 549 styledText = styledTextParser.parse(null, text); 550 } 551 catch (SAXException e) 552 { 553 } 555 } 556 557 if (styledText == null) 558 { 559 styledText = new JRStyledText(); 560 styledText.append(text); 561 styledText.addRun(new JRStyledText.Run(null, 0, text.length())); 562 } 563 } 564 565 return styledText; 566 } 567 568 } 569 | Popular Tags |