cleanup / abuse system fix / prepping for a release
[mir.git] / source / mircoders / pdf / PDFGenerator.java
index 99c094c..ebfe984 100755 (executable)
@@ -37,13 +37,16 @@ import gnu.regexp.REMatchEnumeration;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.net.MalformedURLException;
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 
 import mir.config.MirPropertiesConfiguration;
 import mir.entity.EntityBrowser;
 import mir.log.LoggerWrapper;
 import mir.misc.StringUtil;
-import mir.util.DateTimeFunctions;
+import mir.util.DateTimeRoutines;
+import mir.util.HTMLRoutines;
 import mircoders.entity.EntityContent;
 import mircoders.entity.EntityImages;
 import mircoders.storage.DatabaseImages;
@@ -117,12 +120,7 @@ public class PDFGenerator{
 
   public PDFGenerator(ByteArrayOutputStream out){
     logger = new LoggerWrapper("PDFGenerator");
-    try {
-      configuration = MirPropertiesConfiguration.instance();
-    }
-    catch (MirPropertiesConfiguration.PropertiesConfigExc e) {
-      throw new RuntimeException("Can't get configuration: " + e.getMessage());
-    }
+    configuration = MirPropertiesConfiguration.instance();
     localImageDir=configuration.getString("Producer.Image.Path");
 
     try {
@@ -133,11 +131,11 @@ public class PDFGenerator{
       footerText = configuration.getString("PDF.Footer.String");
       footerFontSize   = Integer.parseInt(configuration.getString("PDF.Footer.FontSize"));
       footerFontFamily = getFontByName(configuration.getString("PDF.Footer.FontFamily"));
-      footerHeight = Integer.parseInt(configuration.getString("PDF.Footer.Height"));;
+      footerHeight = Integer.parseInt(configuration.getString("PDF.Footer.Height"));
 
       metaFontSize   = Integer.parseInt(configuration.getString("PDF.Meta.FontSize"));
       metaFontFamily = getFontByName(configuration.getString("PDF.Meta.FontFamily"));
-      metaHeight = Integer.parseInt(configuration.getString("PDF.Meta.Height"));;
+      metaHeight = Integer.parseInt(configuration.getString("PDF.Meta.Height"));
 
       descriptionFontSize   = Integer.parseInt(configuration.getString("PDF.Description.FontSize"));
       descriptionLineHeight = Integer.parseInt(configuration.getString("PDF.Description.LineHeight"));
@@ -259,8 +257,8 @@ public class PDFGenerator{
     try {
 
       ColumnText ict = new ColumnText(cb);
-      String theTitle = entityContent.getValue("title");
-      String theCreator = entityContent.getValue("creator");
+      String theTitle = entityContent.getFieldValue("title");
+      String theCreator = entityContent.getFieldValue("creator");
       Phrase titleP=new Phrase(" - " +  theTitle,new Font(indexFontFamily,indexFontSize,Font.BOLD));
       Phrase creatorP=new Phrase( " :: " + theCreator,new Font(indexFontFamily,indexFontSize));
       float toYPosition = currentYPosition - indexLineHeight;
@@ -271,7 +269,7 @@ public class PDFGenerator{
       ict.setColumns(leftIndexCols,rightIndexCols);
       ict.setYLine(currentYPosition);
       ict.setAlignment(Element.ALIGN_LEFT);
-      int status=ict.go();
+      ict.go();
       currentYPosition = toYPosition;
     }
     catch(DocumentException de) {
@@ -329,12 +327,12 @@ public class PDFGenerator{
     fct.setColumns(leftFooterCols,rightFooterCols);
     fct.setYLine(bottomEdge+footerHeight-1);
     fct.setAlignment(Element.ALIGN_JUSTIFIED);
-    int status=fct.go();
+    fct.go();
 
     Paragraph numberP=new Paragraph((new Integer(currentPage)).toString(),new Font(footerFontFamily,footerFontSize,Font.BOLD));
     fct.addText(numberP);
     fct.setAlignment(Element.ALIGN_RIGHT);
-    status=fct.go();
+    fct.go();
 
     }
     catch (DocumentException de){
@@ -439,8 +437,8 @@ public class PDFGenerator{
     while (images.hasNext()){
 
       EntityImages currentImage=(EntityImages) images.next();
-      float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();
-      float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();
+      float img_width=(new Float(currentImage.getFieldValue("img_width"))).floatValue();
+      float img_height=(new Float(currentImage.getFieldValue("img_height"))).floatValue();
       if (img_height>maxImageHeight){
   img_width=(new Float((new Float(img_width*(maxImageHeight/img_height))).intValue())).floatValue();
   img_height=maxImageHeight;
@@ -450,8 +448,8 @@ public class PDFGenerator{
   img_width=maxImageWidth;
       }
 
-      String img_title=currentImage.getValue("title");
-      String img_path=currentImage.getValue("publish_path");
+      String img_title=currentImage.getFieldValue("title");
+      String img_path=currentImage.getFieldValue("publish_path");
 
       if ((status & ColumnText.NO_MORE_TEXT) == 0){
   // there is still text, so add an image which will have text wrapped around it, then add the text which
@@ -617,8 +615,7 @@ public class PDFGenerator{
   private boolean enoughY(int heightOfBlockToAdd){
     if ((currentYPosition - heightOfBlockToAdd - footerHeight) < bottomEdge )
       return false;
-    else
-      return true;
+               return true;
   }
 
 
@@ -637,24 +634,25 @@ public class PDFGenerator{
      * source
     */
 
-   Iterator images = new EntityBrowser(
-      // TODO rewrite as relational select
-      DatabaseImages.getInstance(),
-       "exists (select * from content_x_media where content_id=" + entityContent.getId() + " and media_id=id)",
-       "id desc", 30, -1, 0);
-
-    String isHTML  = entityContent.getValue("is_html");
-    String theTitle = entityContent.getValue("title");
-    String theCreator = entityContent.getValue("creator");
-    String theDate = DateTimeFunctions.advancedDateFormat(
+    List extraTables = new ArrayList();
+    extraTables.add("content_x_media cxm");
+    Iterator images = new EntityBrowser(
+      DatabaseImages.getInstance(), "i", extraTables,
+        "cxm.content_id="+entityContent.getId()+"and cxm.media_id=i.id",
+        "i.id desc", 30, -1, 0);
+
+    String isHTML  = entityContent.getFieldValue("is_html");
+    String theTitle = entityContent.getFieldValue("title");
+    String theCreator = entityContent.getFieldValue("creator");
+    String theDate = DateTimeRoutines.advancedDateFormat(
         configuration.getString("RDF.Meta.DateFormat"),
-        StringUtil.convertMirInternalDateToDate(entityContent.getValue("webdb_create")),
+        StringUtil.convertMirInternalDateToDate(entityContent.getFieldValue("webdb_create")),
         configuration.getString("Mir.DefaultTimezone"));
 
 
-    String theDescriptionRaw = entityContent.getValue("description");
-    String theContentRaw = entityContent.getValue("content_data");
-    String theSource =  configuration.getString("Producer.ProductionHost") + "/" + configuration.getString("StandardLanguage") + entityContent.getValue("publish_path") + entityContent.getValue("id") + ".shtml";
+    String theDescriptionRaw = entityContent.getFieldValue("description");
+    String theContentRaw = entityContent.getFieldValue("content_data");
+    String theSource =  configuration.getString("Producer.PublicationHost") + "/" + configuration.getString("StandardLanguage") + entityContent.getFieldValue("publish_path") + entityContent.getFieldValue("id") + ".shtml";
 
 
 
@@ -668,27 +666,35 @@ public class PDFGenerator{
       try {
   RE nobackslashr = new RE("\r");
   theContent= nobackslashr.substituteAll(theContentRaw,"");
+  theDescription= nobackslashr.substituteAll(theDescriptionRaw,"");
 
   RE HxTag = new RE("</?h[1-6][^>]*>",RE.REG_ICASE);
   theContent = HxTag.substituteAll(theContent,"\n\n");
+  theDescription = HxTag.substituteAll(theDescription,"\n\n");
 
   RE ListItemTag = new RE("<li[^>]*>",RE.REG_ICASE);
   theContent = ListItemTag.substituteAll(theContent,"\n * ");
+  theDescription = ListItemTag.substituteAll(theDescription,"\n * ");
 
   RE ListTag = new RE("<(u|o)l[^>]*>",RE.REG_ICASE);
   theContent = ListTag.substituteAll(theContent,"\n");
+  theDescription = ListTag.substituteAll(theDescription,"\n");
 
   RE DivTag = new RE("</?div[^>]*>",RE.REG_ICASE);
   theContent= DivTag.substituteAll(theContent,"\n");
+  theDescription= DivTag.substituteAll(theDescription,"\n");
 
   RE PTag = new RE("<(p|P)([:space:]+[^>]*)?>");
   theContent= PTag.substituteAll(theContent,"\n    ");
+  theDescription= PTag.substituteAll(theDescription,"\n    ");
 
   RE PTagClose = new RE("</(p|P)([:space:]+[^>]*)?>");
   theContent= PTagClose.substituteAll(theContent,"\n");
+  theDescription= PTagClose.substituteAll(theDescription,"\n");
 
   RE BRTag = new RE("<(br|BR)([:space:]+[^>]*)?>");
   theContent= BRTag.substituteAll(theContent,"\n");
+  theDescription= BRTag.substituteAll(theDescription,"\n");
 
   RE ATagAll = new RE("<a[^>]*href=(?:\"|\')([^#\"\'][^\'\"]+)(?:\"|\')[^>]*>(.*?)</a>",RE.REG_ICASE);
   REMatchEnumeration atags= ATagAll.getMatchEnumeration(theContent);
@@ -703,13 +709,28 @@ public class PDFGenerator{
   }
   theContent=theContentCopy;
 
+  REMatchEnumeration atags2= ATagAll.getMatchEnumeration(theDescription);
+  String theDescriptionCopy=theDescription;
+  while (atags2.hasMoreMatches()){
+    REMatch atag = atags2.nextMatch();
+    String atagString=atag.toString();
+    String atagStringHref=atag.toString(1);
+    String atagStringText=atag.toString(2);
+    int begin=theDescriptionCopy.indexOf(atagString);
+    theDescriptionCopy=theDescriptionCopy.substring(0,begin) + atagStringText + " ["+ atagStringHref + "] " + theDescriptionCopy.substring(begin+atagString.length());
+  }
+  theDescription=theDescriptionCopy;
+
+
   RE noTags = new RE("<[^>]*>");
   theContent= noTags.substituteAll(theContent," ");
+  theDescription= noTags.substituteAll(theDescription," ");
 
-  theContent=mir.util.Translate.decode(theContent);
+  theContent = HTMLRoutines.resolveHTMLEntites(theContent);
+  theDescription = HTMLRoutines.resolveHTMLEntites(theDescription);
 
   RE re1 = new RE("\r?\n\r?\n");
-  String theDescription1 = re1.substituteAll(theDescriptionRaw,"BREAKHERE");
+  String theDescription1 = re1.substituteAll(theDescription,"BREAKHERE");
 
   RE re2 = new RE("\r?\n");
   String theDescription2 = re2.substituteAll(theDescription1," ");