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