b3d5f62cc618c4bdc3189c8b9876c29df2c97e93
[mir.git] / source / mircoders / pdf / PDFGenerator.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.pdf;
31
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.net.MalformedURLException;
35 import java.util.*;
36
37 import com.lowagie.text.BadElementException;
38 import com.lowagie.text.Document;
39 import com.lowagie.text.DocumentException;
40 import com.lowagie.text.Element;
41 import com.lowagie.text.Font;
42 import com.lowagie.text.Image;
43 import com.lowagie.text.PageSize;
44 import com.lowagie.text.Paragraph;
45 import com.lowagie.text.Phrase;
46 import com.lowagie.text.pdf.BaseFont;
47 import com.lowagie.text.pdf.ColumnText;
48 import com.lowagie.text.pdf.PdfContentByte;
49 import com.lowagie.text.pdf.PdfTemplate;
50 import com.lowagie.text.pdf.PdfWriter;
51 import gnu.regexp.RE;
52 import gnu.regexp.REException;
53 import gnu.regexp.REMatch;
54 import gnu.regexp.REMatchEnumeration;
55
56 import mir.config.MirPropertiesConfiguration;
57 import mir.entity.*;
58 import mir.log.LoggerWrapper;
59 import mir.misc.StringUtil;
60 import mir.util.DateTimeFunctions;
61 import mircoders.entity.EntityContent;
62 import mircoders.entity.EntityImages;
63 import mircoders.storage.*;
64
65 public class PDFGenerator{
66
67   public Document document;
68   public PdfWriter writer;
69   public PdfContentByte cb;
70   public String localImageDir;
71   public float currentYPosition;
72   public int currentPage;
73   public float pageWidth;
74   public float pageHeight;
75   public float verticalMargin;
76   public float horizontalMargin;
77   public float topEdge;
78   public float bottomEdge;
79   public float rightEdge;
80   public float leftEdge;
81
82   public int    maxImageHeight;
83   public int    maxImageWidth;
84   protected LoggerWrapper logger;
85
86   public int indexFontSize;
87   public int indexLineHeight;
88   public int indexFontFamily;
89
90   public float footerHeight;
91   public String footerText;
92   public int footerFontSize;
93   public int footerFontFamily;
94
95   public int metaHeight;
96   public int metaFontSize;
97   public int metaFontFamily;
98
99   public int descriptionLineHeight;
100   public int descriptionFontSize;
101   public int descriptionFontFamily;
102
103   public int contentLineHeight;
104   public int contentFontSize;
105   public int contentFontFamily;
106
107   public int sourceLineHeight;
108   public int sourceFontSize;
109   public int sourceFontFamily;
110
111   public int bigImageCaptionFontSize;
112   public int bigImageCaptionFontFamily;
113
114   protected MirPropertiesConfiguration configuration;
115
116
117   public PDFGenerator(ByteArrayOutputStream out){
118     logger = new LoggerWrapper("PDFGenerator");
119     try {
120       configuration = MirPropertiesConfiguration.instance();
121     }
122     catch (MirPropertiesConfiguration.PropertiesConfigExc e) {
123       throw new RuntimeException("Can't get configuration: " + e.getMessage());
124     }
125     localImageDir=configuration.getString("Producer.Image.Path");
126
127     try {
128       indexFontSize   = Integer.parseInt(configuration.getString("PDF.Index.FontSize"));
129       indexLineHeight = Integer.parseInt(configuration.getString("PDF.Index.LineHeight"));
130       indexFontFamily = getFontByName(configuration.getString("PDF.Index.FontFamily"));
131
132       footerText = configuration.getString("PDF.Footer.String");
133       footerFontSize   = Integer.parseInt(configuration.getString("PDF.Footer.FontSize"));
134       footerFontFamily = getFontByName(configuration.getString("PDF.Footer.FontFamily"));
135       footerHeight = Integer.parseInt(configuration.getString("PDF.Footer.Height"));;
136
137       metaFontSize   = Integer.parseInt(configuration.getString("PDF.Meta.FontSize"));
138       metaFontFamily = getFontByName(configuration.getString("PDF.Meta.FontFamily"));
139       metaHeight = Integer.parseInt(configuration.getString("PDF.Meta.Height"));;
140
141       descriptionFontSize   = Integer.parseInt(configuration.getString("PDF.Description.FontSize"));
142       descriptionLineHeight = Integer.parseInt(configuration.getString("PDF.Description.LineHeight"));
143       descriptionFontFamily = getFontByName(configuration.getString("PDF.Description.FontFamily"));
144
145       contentFontSize   = Integer.parseInt(configuration.getString("PDF.Content.FontSize"));
146       contentLineHeight = Integer.parseInt(configuration.getString("PDF.Content.LineHeight"));
147       contentFontFamily = getFontByName(configuration.getString("PDF.Content.FontFamily"));
148
149       sourceFontSize   = Integer.parseInt(configuration.getString("PDF.Source.FontSize"));
150       sourceLineHeight = Integer.parseInt(configuration.getString("PDF.Source.LineHeight"));
151       sourceFontFamily = getFontByName(configuration.getString("PDF.Source.FontFamily"));
152
153       bigImageCaptionFontSize   = Integer.parseInt(configuration.getString("PDF.BigImageCaption.FontSize"));
154       bigImageCaptionFontFamily = getFontByName(configuration.getString("PDF.BigImageCaption.FontFamily"));
155
156     }
157     catch (NumberFormatException e){
158       e.printStackTrace();
159     }
160
161     // step 1: make a document
162
163     String pageSize = configuration.getString("PDF.Pagesize");
164
165     if (pageSize.equals("LETTER")){
166       document = new Document(PageSize.LETTER);
167       pageWidth = 612;
168       pageHeight = 792;
169     }
170     else {
171        document = new Document(PageSize.A4);
172        pageWidth=595;
173        pageHeight=842;
174     }
175
176     maxImageHeight    = 250;
177     maxImageWidth     = 250;
178
179
180     verticalMargin = 20;
181     horizontalMargin = 20;
182
183
184
185     topEdge=pageHeight-verticalMargin;
186     bottomEdge=verticalMargin;
187     rightEdge=pageWidth-horizontalMargin;
188     leftEdge=horizontalMargin;
189
190     currentYPosition=topEdge;
191     currentPage = 1;
192
193     String headerText = configuration.getString("PDF.Title.String");
194
195     try{
196       writer = PdfWriter.getInstance(document, out);
197       cb = writer.getDirectContent();
198
199       document.open();
200       addHeader(headerText);
201     }
202     catch(DocumentException de) {
203       logger.error(de.getMessage());
204     }
205   }
206
207   public void stop(){
208     addFooter();
209     document.close();
210   }
211
212   public void addHeader(String headerText){
213     int titleFontSize=Integer.parseInt(configuration.getString("PDF.Title.FontSize"));
214     int titleLineHeight=Integer.parseInt(configuration.getString("PDF.Title.LineHeight"));
215     String titleFontFamily=configuration.getString("PDF.Title.FontFamily");
216     String headerImage=configuration.getString("PDF.Header.Image");
217     int headerImageHeight = Integer.parseInt(configuration.getString("PDF.Header.ImageHeight"));
218
219     try {
220       if ((! headerImage.equals("")) && headerImageHeight != 0){
221   PdfTemplate template = cb.createTemplate(rightEdge-horizontalMargin,headerImageHeight);
222
223   float toYPosition=currentYPosition - headerImageHeight;
224   Image theImage = Image.getInstance(headerImage);
225   theImage.setAbsolutePosition(0,0);
226   //    theImage.scaleAbsolute(img_width,img_height);
227   template.addImage(theImage);
228
229
230   cb.addTemplate(template,leftEdge,toYPosition);
231   currentYPosition = toYPosition;
232       }
233       if (! headerText.equals("")){
234   ColumnText ct = new ColumnText(cb);
235   //add a basic header
236   ct.addText(new Phrase(headerText, new Font(getFontByName(titleFontFamily), titleFontSize,Font.BOLD)));
237   float[] rightCol = {rightEdge,currentYPosition,rightEdge,currentYPosition-titleLineHeight};
238   float[] leftCol = {leftEdge,currentYPosition,leftEdge,currentYPosition-titleLineHeight};
239   ct.setColumns(leftCol,rightCol);
240   ct.setYLine(currentYPosition);
241   ct.setAlignment(Element.ALIGN_CENTER);
242   ct.go();
243
244   currentYPosition = currentYPosition - titleLineHeight;
245       }
246     }
247     catch(DocumentException de) {
248       logger.error(de.getMessage());
249     }
250     catch(MalformedURLException de) {
251       logger.error(de.getMessage());
252     }
253     catch(IOException de) {
254       logger.error(de.getMessage());
255     }
256   }
257   public void addIndexItem(EntityContent entityContent){
258     try {
259
260       ColumnText ict = new ColumnText(cb);
261       String theTitle = entityContent.getValue("title");
262       String theCreator = entityContent.getValue("creator");
263       Phrase titleP=new Phrase(" - " +  theTitle,new Font(indexFontFamily,indexFontSize,Font.BOLD));
264       Phrase creatorP=new Phrase( " :: " + theCreator,new Font(indexFontFamily,indexFontSize));
265       float toYPosition = currentYPosition - indexLineHeight;
266       float[] leftIndexCols = {leftEdge,currentYPosition,leftEdge,toYPosition};
267       float[] rightIndexCols = {rightEdge,currentYPosition,rightEdge,toYPosition};
268       ict.addText(titleP);
269       ict.addText(creatorP);
270       ict.setColumns(leftIndexCols,rightIndexCols);
271       ict.setYLine(currentYPosition);
272       ict.setAlignment(Element.ALIGN_LEFT);
273       int status=ict.go();
274       currentYPosition = toYPosition;
275     }
276     catch(DocumentException de) {
277       logger.error(de.getMessage());
278     }
279
280
281
282   }
283
284   private int addTextLine(ColumnText ct,int lineHeight,int alignment,float left, float right){
285     logger.debug("adding a line of text");
286     if (! enoughY(lineHeight)){
287       newPage();
288     }
289     float toYPosition = currentYPosition - lineHeight;
290     float[] leftContentCols = {left,currentYPosition,left,toYPosition};
291     float[] rightContentCols = {right,currentYPosition,right,toYPosition};
292     ct.setColumns(leftContentCols,rightContentCols);
293     ct.setYLine(currentYPosition);
294     ct.setAlignment(alignment);
295     try{
296       int status=ct.go();
297       currentYPosition = toYPosition;
298       return status;
299     }
300     catch(DocumentException de) {
301       logger.error(de.getMessage());
302     }
303     return 0;
304   }
305
306   public void addLine(){
307     cb.setLineWidth(1f);
308     cb.moveTo(rightEdge, currentYPosition-5);
309     cb.lineTo(leftEdge, currentYPosition-5);
310     cb.stroke();
311     currentYPosition = currentYPosition - 10;
312
313   }
314
315   public void addFooter(){
316     try{
317     ColumnText fct = new ColumnText(cb);
318     cb.setLineWidth(1f);
319     cb.moveTo(rightEdge, bottomEdge+footerHeight-5);
320     cb.lineTo(leftEdge, bottomEdge+footerHeight-5);
321     cb.stroke();
322     float[] leftFooterCols = {leftEdge,bottomEdge+footerHeight-1,leftEdge,bottomEdge};
323     float[] rightFooterCols = {rightEdge,bottomEdge+footerHeight-1,rightEdge,bottomEdge};
324
325     Paragraph footerP=new Paragraph(footerText,new Font(footerFontFamily,footerFontSize));
326     fct.addText(footerP);
327
328     fct.setColumns(leftFooterCols,rightFooterCols);
329     fct.setYLine(bottomEdge+footerHeight-1);
330     fct.setAlignment(Element.ALIGN_JUSTIFIED);
331     int status=fct.go();
332
333     Paragraph numberP=new Paragraph((new Integer(currentPage)).toString(),new Font(footerFontFamily,footerFontSize,Font.BOLD));
334     fct.addText(numberP);
335     fct.setAlignment(Element.ALIGN_RIGHT);
336     status=fct.go();
337
338     }
339     catch (DocumentException de){
340       logger.error(de.getMessage());
341     }
342
343
344   }
345
346   public void newPage(){
347     try{
348       //add a footer
349       addFooter();
350       document.newPage();
351       currentPage++;
352       currentYPosition=topEdge;
353     }
354     catch(DocumentException de) {
355       logger.error(de.getMessage());
356     }
357   }
358
359   public void addArticleSeparator(){
360     // make a line
361     if (! enoughY(10)){
362       newPage();
363     }
364     cb.setLineWidth(1f);
365     cb.moveTo(rightEdge, currentYPosition-5);
366     cb.lineTo(leftEdge, currentYPosition-5);
367     cb.stroke();
368     currentYPosition = currentYPosition - 10;
369   }
370
371   public void addArticleMetaInfo(ColumnText ct,String theTitle,String theCreator,String theDate){
372       //see if we have room for the author and title
373
374     if (! enoughY(metaHeight)){
375   newPage();
376       }
377
378     Paragraph titleP=new Paragraph(theTitle+"\n",new Font(metaFontFamily,metaFontSize,Font.BOLD));
379     Paragraph whowhenP=new Paragraph(theCreator + "  " + theDate ,new Font(metaFontFamily,metaFontSize));
380     ct.addText(titleP);
381     ct.addText(whowhenP);
382
383     ct.setYLine(currentYPosition);
384     ct.setAlignment(Element.ALIGN_LEFT);
385
386     float toYPosition = currentYPosition - metaHeight;
387     float[] leftHeadCols = {leftEdge,currentYPosition,leftEdge,toYPosition};
388     float[] rightHeadCols = {rightEdge,currentYPosition,rightEdge,toYPosition};
389
390     ct.setColumns(leftHeadCols,rightHeadCols);
391     try {
392       ct.go();
393       currentYPosition = toYPosition;
394     }
395     catch(DocumentException de) {
396       logger.error(de.getMessage());
397     }
398
399   }
400
401   public void addArticleDescription(ColumnText ct,String theDescription){
402     // Now we add the description, one line at a time, the ct should be empty at this point
403
404
405     Paragraph descP=new Paragraph(theDescription,new Font(descriptionFontFamily,descriptionFontSize,Font.BOLD));
406     ct.addText(descP);
407
408     // every article has a description, so we can assume that:
409     int status = ColumnText.NO_MORE_COLUMN;
410
411     int brake=1000;
412     while ((status & ColumnText.NO_MORE_TEXT) == 0 && brake >0){
413       //there is still text left in the description.
414       status = addTextLine(ct,descriptionLineHeight,Element.ALIGN_JUSTIFIED,leftEdge,rightEdge);
415       brake--;
416     }
417     if (brake == 0)
418       logger.error("runaway description...try increasing the line height or decreasing the font size");
419   }
420
421   public void addArticleContent(ColumnText ct,String theContent,Iterator images){
422     //let's go ahead and add in all the body text
423     Paragraph contentP=new Paragraph(theContent,new Font(contentFontFamily,contentFontSize));
424     ct.addText(contentP);
425
426     int contentLinesBeforeImages=3;
427     //and assume we have at least one line of text in the content
428     int status = ColumnText.NO_MORE_COLUMN;
429
430     // let's add a little bit of text, like a couple of lines
431     int x = 0;
432     while (((status & ColumnText.NO_MORE_TEXT) == 0) && x<contentLinesBeforeImages){
433       status=addTextLine(ct,contentLineHeight,Element.ALIGN_JUSTIFIED,leftEdge,rightEdge);
434       x++;
435     }
436
437     boolean addImageOnLeft = true; //we will alternate within articles
438     while (images.hasNext()){
439
440       EntityImages currentImage=(EntityImages) images.next();
441       float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();
442       float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();
443       if (img_height>maxImageHeight){
444   img_width=(new Float((new Float(img_width*(maxImageHeight/img_height))).intValue())).floatValue();
445   img_height=maxImageHeight;
446       }
447       if (img_width>maxImageWidth){
448   img_height=(new Float((new Float(img_height*(maxImageWidth/img_width))).intValue())).floatValue();
449   img_width=maxImageWidth;
450       }
451
452       String img_title=currentImage.getValue("title");
453       String img_path=currentImage.getValue("publish_path");
454
455       if ((status & ColumnText.NO_MORE_TEXT) == 0){
456   // there is still text, so add an image which will have text wrapped around it, then add the text which
457   // will be on the left or the right of the image
458
459   float templateMinimumHeight = img_height+20;
460   float templateWidth = img_width+10;
461   float templateHeight = templateMinimumHeight+contentLineHeight-(templateMinimumHeight % contentLineHeight);
462
463   PdfTemplate template = cb.createTemplate(templateWidth,templateHeight);
464
465
466   //here we need a page check
467   if (! enoughY((new Float(templateHeight)).intValue())){
468     //ok, well just fill text to the bottom then
469     float toYPosition = bottomEdge+footerHeight;
470     float[] leftBottomCols = {leftEdge,currentYPosition,leftEdge,toYPosition};
471     float[] rightBottomCols = {rightEdge,currentYPosition,rightEdge,toYPosition};
472     ct.setColumns(leftBottomCols,rightBottomCols);
473     ct.setYLine(currentYPosition);
474     ct.setAlignment(Element.ALIGN_JUSTIFIED);
475     try{
476       status=ct.go();
477     }
478     catch(DocumentException de) {
479       logger.error(de.getMessage());
480     }
481     newPage();
482   }
483
484   float toYPosition=currentYPosition - templateHeight;
485
486   try {
487     Image theImage = Image.getInstance(localImageDir+img_path);
488     theImage.scaleAbsolute(img_width,img_height);
489     theImage.setAbsolutePosition(5,13);
490
491     template.addImage(theImage);
492     template.beginText();
493     BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
494     template.setFontAndSize(bf, 8);
495     template.setTextMatrix(5, 3);
496     template.showText(img_title);
497     template.endText();
498   }
499   catch(BadElementException de) {
500     logger.error(de.getMessage());
501   }
502   catch(DocumentException de) {
503     logger.error(de.getMessage());
504   }
505   catch (MalformedURLException de){
506    logger.error(de.getMessage());
507   }
508   catch (IOException de){
509     logger.error(de.getMessage());
510   }
511
512
513   float leftImageTextEdge=leftEdge;
514   float rightImageTextEdge=rightEdge;
515
516
517   if (addImageOnLeft){
518     cb.addTemplate(template,leftEdge,toYPosition);
519     leftImageTextEdge=leftEdge+templateWidth+5;
520     addImageOnLeft = false;
521   }
522   else {
523     cb.addTemplate(template,rightEdge-templateWidth,toYPosition);
524     rightImageTextEdge = rightEdge-templateWidth-5;
525     addImageOnLeft = true;
526   }
527
528   logger.debug("adding template at " + leftEdge + "," + toYPosition);
529
530   //and fill some text while we are at it
531
532   float[] leftBottomCols = {leftImageTextEdge,currentYPosition,leftImageTextEdge,toYPosition};
533   float[] rightBottomCols = {rightImageTextEdge,currentYPosition,rightImageTextEdge,toYPosition};
534   ct.setColumns(leftBottomCols,rightBottomCols);
535   ct.setYLine(currentYPosition);
536   ct.setAlignment(Element.ALIGN_JUSTIFIED);
537   try{
538   status=ct.go();
539   currentYPosition=toYPosition;
540   }
541   catch(DocumentException de) {
542     logger.error(de.getMessage());
543   }
544
545
546       }
547       else {
548   //add an image on the left with a big caption to the right
549   currentYPosition = currentYPosition - 10;
550   float templateMinimumHeight = img_height;
551   float templateWidth = img_width;
552   float templateHeight = templateMinimumHeight+contentLineHeight-(templateMinimumHeight % contentLineHeight);
553   PdfTemplate template = cb.createTemplate(templateWidth,templateHeight);
554   if (! enoughY((new Float(templateHeight)).intValue())){
555     newPage();
556   }
557   float toYPosition=currentYPosition - templateHeight;
558   try{
559     Image theImage = Image.getInstance(localImageDir+img_path);
560     theImage.setAbsolutePosition(0,13);
561     theImage.scaleAbsolute(img_width,img_height);
562     template.addImage(theImage);
563   }
564   catch(BadElementException de) {
565     logger.error(de.getMessage());
566   }
567   catch(DocumentException de) {
568     logger.error(de.getMessage());
569   }
570   catch(MalformedURLException de) {
571     logger.error(de.getMessage());
572   }
573   catch(IOException de) {
574     logger.error(de.getMessage());
575   }
576
577   cb.addTemplate(template,leftEdge,toYPosition);
578
579   // add a big caption
580   ColumnText cct = new ColumnText(cb);
581   float[] leftCaptionCols = {leftEdge+templateWidth+5,currentYPosition-5,leftEdge+templateWidth+5,toYPosition};
582   float[] rightCaptionCols = {rightEdge,currentYPosition-5,rightEdge,toYPosition};
583
584   Paragraph captionP=new Paragraph(img_title,new Font(bigImageCaptionFontFamily,bigImageCaptionFontSize,Font.BOLD));
585   cct.addText(captionP);
586   cct.setColumns(leftCaptionCols,rightCaptionCols);
587   cct.setYLine(currentYPosition-5);
588   cct.setAlignment(Element.ALIGN_LEFT);
589   try{
590     cct.go();
591     currentYPosition=toYPosition;
592   }
593   catch(DocumentException de) {
594     logger.error(de.getMessage());
595   }
596       }
597     }
598
599     //add the rest of the text which might be left
600     int brake = 10000;
601     while ((status & ColumnText.NO_MORE_TEXT) == 0  && brake > 0){
602       status=addTextLine(ct,contentLineHeight,Element.ALIGN_JUSTIFIED,leftEdge,rightEdge);
603       brake --;
604     }
605     if (brake == 0)
606       logger.error("runaway content....try decreasing font size or increasing line height");
607   }
608
609   private void addArticleSource(ColumnText ct,String theSource){
610     Paragraph sourceP = new Paragraph(theSource,new Font(sourceFontFamily,sourceFontSize,Font.BOLD));
611     ct.addText(sourceP);
612     addTextLine(ct,sourceLineHeight,Element.ALIGN_RIGHT,leftEdge,rightEdge);
613   }
614
615
616   private boolean enoughY(int heightOfBlockToAdd){
617     if ((currentYPosition - heightOfBlockToAdd - footerHeight) < bottomEdge )
618       return false;
619     else
620       return true;
621   }
622
623
624   public void add(EntityContent entityContent){
625     logger.error("adding a content Entity");
626
627     /*
628      * initialize
629      * separate
630      * meta info
631      * description
632      * content
633      * --image with text
634      * --normal text
635      * --image without text
636      * source
637     */
638
639    Iterator images = new EntityBrowser(
640       DatabaseImages.getInstance(),
641        "exists (select * from content_x_media where content_id=" + entityContent.getId() + " and media_id=id)",
642        "id desc", 30, -1, 0);
643
644     String isHTML  = entityContent.getValue("is_html");
645     String theTitle = entityContent.getValue("title");
646     String theCreator = entityContent.getValue("creator");
647     String theDate = DateTimeFunctions.advancedDateFormat(
648         configuration.getString("RDF.Meta.DateFormat"),
649         StringUtil.convertMirInternalDateToDate(entityContent.getValue("webdb_create")),
650         configuration.getString("Mir.DefaultTimezone"));
651
652
653     String theDescriptionRaw = entityContent.getValue("description");
654     String theContentRaw = entityContent.getValue("content_data");
655     String theSource =  configuration.getString("Producer.ProductionHost") + "/" + configuration.getString("StandardLanguage") + entityContent.getValue("publish_path") + entityContent.getValue("id") + ".shtml";
656
657
658
659     String theContent = "";
660     String theDescription = "";
661
662     if (isHTML.equals("1")){
663
664
665
666       try {
667   RE nobackslashr = new RE("\r");
668   theContent= nobackslashr.substituteAll(theContentRaw,"");
669
670   RE HxTag = new RE("</?h[1-6][^>]*>",RE.REG_ICASE);
671   theContent = HxTag.substituteAll(theContent,"\n\n");
672
673   RE ListItemTag = new RE("<li[^>]*>",RE.REG_ICASE);
674   theContent = ListItemTag.substituteAll(theContent,"\n * ");
675
676   RE ListTag = new RE("<(u|o)l[^>]*>",RE.REG_ICASE);
677   theContent = ListTag.substituteAll(theContent,"\n");
678
679   RE DivTag = new RE("</?div[^>]*>",RE.REG_ICASE);
680   theContent= DivTag.substituteAll(theContent,"\n");
681
682   RE PTag = new RE("<(p|P)([:space:]+[^>]*)?>");
683   theContent= PTag.substituteAll(theContent,"\n    ");
684
685   RE PTagClose = new RE("</(p|P)([:space:]+[^>]*)?>");
686   theContent= PTagClose.substituteAll(theContent,"\n");
687
688   RE BRTag = new RE("<(br|BR)([:space:]+[^>]*)?>");
689   theContent= BRTag.substituteAll(theContent,"\n");
690
691   RE ATagAll = new RE("<a[^>]*href=(?:\"|\')([^#\"\'][^\'\"]+)(?:\"|\')[^>]*>(.*?)</a>",RE.REG_ICASE);
692   REMatchEnumeration atags= ATagAll.getMatchEnumeration(theContent);
693   String theContentCopy=theContent;
694   while (atags.hasMoreMatches()){
695     REMatch atag = atags.nextMatch();
696     String atagString=atag.toString();
697     String atagStringHref=atag.toString(1);
698     String atagStringText=atag.toString(2);
699     int begin=theContentCopy.indexOf(atagString);
700     theContentCopy=theContentCopy.substring(0,begin) + atagStringText + " ["+ atagStringHref + "] " + theContentCopy.substring(begin+atagString.length());
701   }
702   theContent=theContentCopy;
703
704   RE noTags = new RE("<[^>]*>");
705   theContent= noTags.substituteAll(theContent," ");
706
707   theContent=mir.util.Translate.decode(theContent);
708
709   RE re1 = new RE("\r?\n\r?\n");
710   String theDescription1 = re1.substituteAll(theDescriptionRaw,"BREAKHERE");
711
712   RE re2 = new RE("\r?\n");
713   String theDescription2 = re2.substituteAll(theDescription1," ");
714
715   RE re3 = new RE("BREAKHERE");
716   theDescription = re3.substituteAll(theDescription2,"\n    ");
717
718
719       }
720       catch(REException ree){
721   logger.error(ree.getMessage());
722       }
723     }
724     else {
725       try {
726   RE re1 = new RE("\r?\n\r?\n");
727   String theContent1 = re1.substituteAll(theContentRaw,"BREAKHERE");
728   String theDescription1 = re1.substituteAll(theDescriptionRaw,"BREAKHERE");
729
730   RE re2 = new RE("\r?\n");
731   String theContent2 = re2.substituteAll(theContent1," ");
732   String theDescription2 = re2.substituteAll(theDescription1," ");
733
734   RE re3 = new RE("BREAKHERE");
735   theContent = "    " + re3.substituteAll(theContent2,"\n    ");
736   theDescription = re3.substituteAll(theDescription2,"\n    ");
737
738       }
739       catch(REException ree){
740   logger.error(ree.getMessage());
741       }
742     }
743
744     addArticleSeparator();
745
746     ColumnText ct = new ColumnText(cb);
747
748     addArticleMetaInfo(ct,theTitle,theCreator,theDate);
749     addArticleDescription(ct,theDescription);
750     addArticleContent(ct,theContent,images);
751     addArticleSource(ct,theSource);
752
753   }
754
755   public int getFontByName(String fontName) {
756     int theFont = 0;
757     if (fontName.equalsIgnoreCase("helvetica")){
758       theFont = Font.HELVETICA;
759     }
760     else {
761       if (fontName.equalsIgnoreCase("courier")) {
762   theFont = Font.COURIER;
763       }
764       else {
765   if (fontName.equalsIgnoreCase("times")) {
766     theFont = Font.TIMES_ROMAN;
767   }
768   else {
769     logger.error("using helvetica because I can't get font for name: "+fontName);
770     theFont = Font.HELVETICA;
771   }
772       }
773     }
774
775     return theFont;
776
777   }
778 }
779
780