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