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