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