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