1 51 package org.apache.fop.render.xml; 52 53 import org.apache.fop.svg.*; 55 import org.apache.fop.render.Renderer; 56 import org.apache.fop.image.FopImage; 57 import org.apache.fop.image.ImageArea; 58 import org.apache.fop.layout.*; 59 import org.apache.fop.layout.inline.*; 60 import org.apache.fop.fo.properties.LeaderPattern; 61 import org.apache.fop.apps.FOPException; 62 import org.apache.fop.datatypes.IDReferences; 63 64 import org.apache.avalon.framework.logger.Logger; 66 67 import java.io.IOException ; 69 import java.io.PrintWriter ; 70 import java.io.OutputStream ; 71 import java.util.List ; 72 73 79 public class XMLRenderer implements Renderer { 80 81 protected Logger log; 82 83 public void setLogger(Logger logger) { 84 log = logger; 85 } 86 87 90 protected int indent = 0; 91 92 95 protected String producer; 96 97 100 protected PrintWriter writer; 101 102 105 protected java.util.Map options; 106 private boolean consistentOutput = false; 107 108 protected IDReferences idReferences; 109 110 public XMLRenderer() {} 111 112 115 public void setOptions(java.util.Map options) { 116 this.options = options; 117 Boolean con = (Boolean )options.get("consistentOutput"); 118 if(con != null) { 119 consistentOutput = con.booleanValue(); 120 } 121 } 122 123 128 public void setProducer(String producer) { 129 this.producer = producer; 130 } 131 132 133 public void render(Page page, OutputStream outputStream) 134 throws IOException { 135 idReferences = page.getIDReferences(); 136 this.renderPage(page); 137 } 138 139 142 protected void writeIndent() { 143 StringBuffer s = new StringBuffer (); 144 for (int i = 0; i < this.indent; i++) { 145 s = s.append(" "); 146 } 147 this.writer.write(s.toString()); 148 } 149 150 155 protected void writeElement(String element) { 156 writeIndent(); 157 this.writer.write(element + "\n"); 158 } 159 160 165 protected void writeEmptyElementTag(String tag) { 166 writeIndent(); 167 this.writer.write(tag + "\n"); 168 } 169 170 175 protected void writeEndTag(String tag) { 176 this.indent--; 177 writeIndent(); 178 this.writer.write(tag + "\n"); 179 } 180 181 186 protected void writeStartTag(String tag) { 187 writeIndent(); 188 this.writer.write(tag + "\n"); 189 this.indent++; 190 } 191 192 197 public void setupFontInfo(FontInfo fontInfo) throws FOPException { 198 199 200 org.apache.fop.render.pdf.FontSetup.setup(fontInfo); 201 } 202 203 216 protected void drawImageScaled(int x, int y, int w, int h, 217 FopImage image, 218 FontState fs) { 219 } 221 222 235 protected void drawImageClipped(int x, int y, 236 int clipX, int clipY, 237 int clipW, int clipH, 238 FopImage image, 239 FontState fs) { 240 } 242 243 248 public void renderAreaContainer(AreaContainer area) { 249 writeStartTag("<AreaContainer name=\"" + area.getAreaName() + "\">"); 250 List children = area.getChildren(); 251 for (int i = 0; i < children.size(); i++) { 252 Box b = (Box)children.get(i); 253 b.render(this); 254 } 255 writeEndTag("</AreaContainer>"); 256 } 257 258 263 public void renderBodyAreaContainer(BodyAreaContainer area) { 264 writeStartTag("<BodyAreaContainer>"); 265 List children = area.getChildren(); 266 for (int i = 0; i < children.size(); i++) { 267 Box b = (Box)children.get(i); 268 b.render(this); 269 } 270 writeEndTag("</BodyAreaContainer>"); 271 } 272 273 278 public void renderRegionAreaContainer(AreaContainer area) { 279 renderAreaContainer(area); 280 } 281 282 287 public void renderSpanArea(SpanArea area) { 288 writeStartTag("<SpanArea>"); 289 List children = area.getChildren(); 290 for (int i = 0; i < children.size(); i++) { 291 Box b = (Box)children.get(i); 292 b.render(this); 293 } 294 writeEndTag("</SpanArea>"); 295 } 296 297 302 public void renderBlockArea(BlockArea area) { 303 StringBuffer baText = new StringBuffer (); 304 baText.append("<BlockArea start-indent=\"" + area.getStartIndent() 305 + "\""); 306 baText.append(" end-indent=\"" + area.getEndIndent() + "\""); 307 baText.append("\nis-first=\"" + area.isFirst() + "\""); 308 baText.append(" is-last=\"" + area.isLast() + "\""); 309 if (null != area.getGeneratedBy()) 310 baText.append(" generated-by=\"" 311 + area.getGeneratedBy().getName() + "//"); 312 if(consistentOutput) { 313 baText.append(area.getGeneratedBy().getClass() + "\""); 314 } else { 315 baText.append(area.getGeneratedBy() + "\""); 316 } 317 baText.append(">"); 318 writeStartTag(baText.toString()); 319 320 338 List children = area.getChildren(); 339 for (int i = 0; i < children.size(); i++) { 340 Box b = (Box)children.get(i); 341 b.render(this); 342 } 343 writeEndTag("</BlockArea>"); 344 } 345 346 public void renderInlineArea(InlineArea area) { 347 StringBuffer iaText = new StringBuffer (); 348 iaText.append("<InlineArea"); 349 iaText.append("\nis-first=\"" + area.isFirst() + "\""); 350 iaText.append(" is-last=\"" + area.isLast() + "\""); 351 if (null != area.getGeneratedBy()) 352 iaText.append(" generated-by=\"" 353 + area.getGeneratedBy().getName() + "//" 354 + area.getGeneratedBy() + "\""); 355 iaText.append(">"); 356 writeStartTag(iaText.toString()); 357 358 376 List children = area.getChildren(); 377 for (int i = 0; i < children.size(); i++) { 378 Box b = (Box)children.get(i); 379 b.render(this); 380 } 381 writeEndTag("</InlineArea>"); 382 } 383 384 389 public void renderDisplaySpace(DisplaySpace space) { 390 if (!isCoarseXml()) 391 writeEmptyElementTag("<DisplaySpace size=\"" + space.getSize() 392 + "\"/>"); 393 } 394 395 398 public void renderForeignObjectArea(ForeignObjectArea area) { 399 area.getObject().render(this); 401 } 402 403 408 public void renderSVGArea(SVGArea area) { 409 writeEmptyElementTag("<SVG/>"); 410 } 411 412 417 public void renderImageArea(ImageArea area) { 418 writeEmptyElementTag("<ImageArea/>"); 419 } 420 421 426 public void renderWordArea(WordArea area) { 427 String fontWeight = area.getFontState().getFontWeight(); 428 StringBuffer sb = new StringBuffer (); 429 String s = area.getText(); 430 int l = s.length(); 431 for (int i = 0; i < l; i++) { 432 char ch = s.charAt(i); 433 if (ch > 127) 434 sb = sb.append("&#" + (int)ch + ";"); 435 else 436 sb = sb.append(ch); 437 } 438 if (!isCoarseXml()) { 439 writeElement("<WordArea font-weight=\"" + fontWeight 440 + "\" red=\"" + area.getRed() + "\" green=\"" 441 + area.getGreen() + "\" blue=\"" + area.getBlue() 442 + "\" width=\"" + area.getContentWidth() + "\">" 443 + sb.toString() + "</WordArea>"); 444 } else { 445 this.writer.write(sb.toString()); 446 } 447 } 448 449 454 public void renderInlineSpace(InlineSpace space) { 455 if (!isCoarseXml()) 456 writeEmptyElementTag("<InlineSpace size=\"" + space.getSize() 457 + "\"/>"); 458 else 459 this.writer.write(" "); 460 } 461 462 467 public void renderLineArea(LineArea area) { 468 if (!isCoarseXml()) { 469 String fontWeight = area.getFontState().getFontWeight(); 470 writeStartTag("<LineArea font-weight=\"" + fontWeight + "\">"); 471 } 472 List children = area.getChildren(); 473 for (int i = 0; i < children.size(); i++) { 474 Box b = (Box)children.get(i); 475 b.render(this); 476 } 477 if (!isCoarseXml()) 478 writeEndTag("</LineArea>"); 479 else 480 this.writer.write("\n"); 481 } 482 483 488 public void renderPage(Page page) { 489 BodyAreaContainer body; 490 AreaContainer before, after, start, end; 491 writeStartTag("<Page number=\"" + page.getFormattedNumber() + "\">"); 492 body = page.getBody(); 493 before = page.getBefore(); 494 after = page.getAfter(); 495 start = page.getStart(); 496 end = page.getEnd(); 497 if (before != null) { 498 before.render(this); 499 } 500 if (after != null) { 501 after.render(this); 502 } 503 if (start != null) { 504 start.render(this); 505 } 506 if (end != null) { 507 end.render(this); 508 } 509 if (body != null) { 510 body.render(this); 511 } 512 writeEndTag("</Page>"); 513 } 514 515 520 public void renderLeaderArea(LeaderArea area) { 521 if (isCoarseXml()) 522 return; 523 String leaderPattern = ""; 524 switch (area.getLeaderPattern()) { 525 case LeaderPattern.SPACE: 526 leaderPattern = "space"; 527 break; 528 case LeaderPattern.RULE: 529 leaderPattern = "rule"; 530 break; 531 case LeaderPattern.DOTS: 532 leaderPattern = "dots"; 533 break; 534 case LeaderPattern.USECONTENT: 535 leaderPattern = "use-content"; 536 break; 537 } 538 539 writeEmptyElementTag("<Leader leader-pattern=\"" + leaderPattern 540 + " leader-length=\"" + area.getLeaderLength() 541 + "\" rule-thickness=\"" 542 + area.getRuleThickness() + "\" rule-style=\"" 543 + area.getRuleStyle() + "\" red=\"" 544 + area.getRed() + "\" green=\"" 545 + area.getGreen() + "\" blue=\"" 546 + area.getBlue() + "\"/>"); 547 } 548 549 private boolean isCoarseXml() { 550 if (options == null) { 551 return false; 552 } 553 Boolean b = (Boolean )options.get("fineDetail"); 554 if (b != null) { 555 return b.booleanValue(); 556 } else { 557 return false; 558 } 559 } 560 561 565 public void startRenderer(OutputStream outputStream) 566 throws IOException { 567 log.debug("rendering areas to XML"); 568 this.writer = new PrintWriter (outputStream); 569 this.writer.write( "<?xml version=\"1.0\"?>\n<!-- produced by " + 570 this.producer + " -->\n"); 571 writeStartTag("<AreaTree>"); 572 } 573 574 578 public void stopRenderer(OutputStream outputStream) 579 throws IOException { 580 writeEndTag("</AreaTree>"); 581 this.writer.flush(); 582 log.debug("written out XML"); 583 } 584 585 public IDReferences getIDReferences() { 586 return idReferences; 587 } 588 } 589 | Popular Tags |