major cleanup:
[mir.git] / source / mircoders / producer / PDFPreFormattingProducerNode.java
index 86ad9a9..77fd4c6 100755 (executable)
-/*
- * Copyright (C) 2001, 2002 The Mir-coders group
- *
- * This file is part of Mir.
- *
- * Mir is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * Mir is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Mir; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with  any library licensed under the Apache Software License, 
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library 
- * (or with modified versions of the above that use the same license as the above), 
- * and distribute linked combinations including the two.  You must obey the 
- * GNU General Public License in all respects for all of the code used other than 
- * the above mentioned libraries.  If you modify this file, you may extend this 
- * exception to your version of the file, but you are not obligated to do so.  
- * If you do not wish to do so, delete this exception statement from your version.
- */
-
-package mircoders.producer;
-
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Map;
-
-import mir.entity.Entity;
-import mir.entity.EntityList;
-import mir.entity.adapter.EntityAdapter;
-import mir.log.LoggerToWriterAdapter;
-import mir.log.LoggerWrapper;
-import mir.misc.StringUtil;
-import mir.producer.ProducerFailure;
-import mir.producer.ProducerNode;
-import mir.util.CachingRewindableIterator;
-import mir.util.HTMLRoutines;
-import mir.util.ParameterExpander;
-import mircoders.entity.EntityContent;
-import mircoders.entity.EntityImages;
-import mircoders.storage.DatabaseContentToMedia;
-
-//because images are returned as a template model!(maybe not needed after all!)
-//import freemarker.template.*;
-
-
-public class PDFPreFormattingProducerNode implements ProducerNode {
-  private String contentKey;
-  private int numLinesBetweenImages;
-  private float contentAreaWidthCM;
-  private float characterWidthCM;
-  private float pixelWidthCM;
-  private float lineHeightCM;
-
-  public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {
-    contentKey = aContentKey;
-    numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();
-    contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();
-    characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();
-    pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();
-    lineHeightCM=(new Float(aLineHeightCM)).floatValue();
-
-    //    float characterWidthCM = 0.17F;
-    //float contentAreaWidthCM = 16;
-    //float pixelWidthCM = .03F;
-    //float lineHeightCM = .5F;
-  }
-
-  public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
-    Object data;
-    Entity entity;
-
-    try {
-      data = ParameterExpander.findValueForKey( aValueMap, contentKey );
-
-      if (! (data instanceof EntityAdapter)) {
-        throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
-      }
-
-      entity = ((EntityAdapter) data).getEntity();
-      if (! (entity instanceof EntityContent)) {
-        throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
-      }
-
-      int currentPosition = 0;
-
-      //int numLinesBetweenImages=3;
-
-
-
-
-      int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();
-
-      boolean outOfText = false;
-
-      ArrayList brokenUpContent = new ArrayList();
-
-
-      EntityList images=DatabaseContentToMedia.getInstance().getImages((EntityContent)entity);
-      String theContent = ((EntityContent) entity).getValue("content_data");
-      //remove pesky characters
-      theContent = HTMLRoutines.encodeXML(theContent);
-      //put in the <BR> tags so we can turn them to empty blocks
-      theContent = StringUtil.convertNewline2Break(theContent);
-
-      if (images == null){
-          Map row = new HashMap();
-          row.put("text",theContent);
-          row.put("hasImage","0");
-          brokenUpContent.add(row);
-      }
-      if (images != null){
-          //need to add checks for out of content!
-          Map row0 = new HashMap();
-          if (numCharsInAnImagelessRow>(theContent).length()){
-              row0.put("text",theContent);
-              outOfText = true;
-          }
-          else {
-              //break on words so we don't split html entities
-              int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);
-              row0.put("text",theContent.substring(0,lastSpaceAt));
-              currentPosition=lastSpaceAt;
-          }
-          row0.put("hasImage","0");
-          brokenUpContent.add(row0);
-          aLogger.debug("CP1 is "+ currentPosition);
-          while(images.hasNext()){
-              Map row1 = new HashMap();
-              Map row2 = new HashMap();
-              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();
-
-              //oversize images must be shrunk
-              if (img_width>250){
-                  img_height=(new Float((new Float(img_height*(250.0F/img_width))).intValue())).floatValue();
-                  img_width=250.0F;
-              }
-
-
-              //calculate how much text goes in the column(use 8 pixels to pad the column)
-              float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);
-              float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int
-              //add one line for image description
-              int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();
-
-              row1.put("text_widthCM",Float.toString(text_widthCM));
-
-              row1.put("img_title",currentImage.getValue("title"));
-
-              row1.put("img_width",Float.toString(img_width));
-              row1.put("img_height",Float.toString(img_height));
-
-              aLogger.debug("img_width " +Float.toString(img_width));
-              aLogger.debug("img_height "+Float.toString(img_height));
-
-              row1.put("img_src",currentImage.getValue("publish_path"));
-              row1.put("hasImage","1");
-              if (! outOfText){
-                  try {
-                      int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
-                      row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
-                      currentPosition=lastSpaceAt;
-                  }
-                  catch (IndexOutOfBoundsException e){
-                      row1.put("text",theContent.substring(currentPosition));
-                      outOfText = true;
-                          }
-              }
-              aLogger.debug("CP2 is "+ currentPosition);
-              brokenUpContent.add(row1);
-
-              if (! outOfText){
-                  try {
-                      int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
-                      row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
-                      currentPosition=lastSpaceAt;
-                  }
-                  catch (IndexOutOfBoundsException e){
-                      row2.put("text",theContent.substring(currentPosition));
-                      outOfText = true;
-                          }
-              }
-              row2.put("hasImage","0");
-              brokenUpContent.add(row2);
-
-              aLogger.debug("CP3 is "+ currentPosition);
-          }
-          Map row3 = new HashMap();
-          if (! outOfText){
-              row3.put("text",theContent.substring(currentPosition));
-              row3.put("hasImage","0");
-              brokenUpContent.add(row3);
-          }
-
-      }
-
-
-
-
-
-      ParameterExpander.setValueForKey(
-                                       aValueMap,
-                                       "data.formatted_content",
-                                       new CachingRewindableIterator(brokenUpContent.iterator())
-                                       );
-
-
-    }
-    catch (Throwable t) {
-      aLogger.error("Error while formatting content for PDF: " + t.getMessage());
-      t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));
-    }
-  }
-}
-
-
-
-
+/*\r
+ * Copyright (C) 2001, 2002 The Mir-coders group\r
+ *\r
+ * This file is part of Mir.\r
+ *\r
+ * Mir is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Mir is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with Mir; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * In addition, as a special exception, The Mir-coders gives permission to link\r
+ * the code of this program with  any library licensed under the Apache Software License,\r
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
+ * (or with modified versions of the above that use the same license as the above),\r
+ * and distribute linked combinations including the two.  You must obey the\r
+ * GNU General Public License in all respects for all of the code used other than\r
+ * the above mentioned libraries.  If you modify this file, you may extend this\r
+ * exception to your version of the file, but you are not obligated to do so.\r
+ * If you do not wish to do so, delete this exception statement from your version.\r
+ */\r
+\r
+package mircoders.producer;\r
+\r
+import java.io.PrintWriter;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.*;\r
+\r
+import mir.entity.*;\r
+import mir.entity.EntityList;\r
+import mir.entity.adapter.EntityAdapter;\r
+import mir.log.LoggerToWriterAdapter;\r
+import mir.log.LoggerWrapper;\r
+import mir.misc.StringUtil;\r
+import mir.producer.ProducerFailure;\r
+import mir.producer.ProducerNode;\r
+import mir.util.CachingRewindableIterator;\r
+import mir.util.HTMLRoutines;\r
+import mir.util.ParameterExpander;\r
+import mircoders.entity.EntityContent;\r
+import mircoders.entity.EntityImages;\r
+import mircoders.storage.*;\r
+\r
+\r
+public class PDFPreFormattingProducerNode implements ProducerNode {\r
+  private String contentKey;\r
+  private int numLinesBetweenImages;\r
+  private float contentAreaWidthCM;\r
+  private float characterWidthCM;\r
+  private float pixelWidthCM;\r
+  private float lineHeightCM;\r
+\r
+  public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {\r
+    contentKey = aContentKey;\r
+    numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();\r
+    contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();\r
+    characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();\r
+    pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();\r
+    lineHeightCM=(new Float(aLineHeightCM)).floatValue();\r
+\r
+    //    float characterWidthCM = 0.17F;\r
+    //float contentAreaWidthCM = 16;\r
+    //float pixelWidthCM = .03F;\r
+    //float lineHeightCM = .5F;\r
+  }\r
+\r
+  public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {\r
+    Object data;\r
+    Entity entity;\r
+\r
+    try {\r
+      data = ParameterExpander.findValueForKey( aValueMap, contentKey );\r
+\r
+      if (! (data instanceof EntityAdapter)) {\r
+        throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);\r
+      }\r
+\r
+      entity = ((EntityAdapter) data).getEntity();\r
+      if (! (entity instanceof EntityContent)) {\r
+        throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);\r
+      }\r
+\r
+      int currentPosition = 0;\r
+\r
+      //int numLinesBetweenImages=3;\r
+\r
+\r
+\r
+\r
+      int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();\r
+\r
+      boolean outOfText = false;\r
+\r
+      ArrayList brokenUpContent = new ArrayList();\r
+\r
+      Iterator images = new EntityBrowser(\r
+         DatabaseImages.getInstance(),\r
+          "exists (select * from content_x_media where content_id=" + entity.getId() + " and media_id=id)",\r
+          "id desc", 30, -1, 0);\r
+\r
+      String theContent = ((EntityContent) entity).getValue("content_data");\r
+      //remove pesky characters\r
+      theContent = HTMLRoutines.encodeXML(theContent);\r
+      //put in the <BR> tags so we can turn them to empty blocks\r
+      theContent = StringUtil.convertNewline2Break(theContent);\r
+\r
+      if (images == null){\r
+          Map row = new HashMap();\r
+          row.put("text",theContent);\r
+          row.put("hasImage","0");\r
+          brokenUpContent.add(row);\r
+      }\r
+      if (images != null){\r
+          //need to add checks for out of content!\r
+          Map row0 = new HashMap();\r
+          if (numCharsInAnImagelessRow>(theContent).length()){\r
+              row0.put("text",theContent);\r
+              outOfText = true;\r
+          }\r
+          else {\r
+              //break on words so we don't split html entities\r
+              int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);\r
+              row0.put("text",theContent.substring(0,lastSpaceAt));\r
+              currentPosition=lastSpaceAt;\r
+          }\r
+          row0.put("hasImage","0");\r
+          brokenUpContent.add(row0);\r
+          aLogger.debug("CP1 is "+ currentPosition);\r
+          while(images.hasNext()){\r
+              Map row1 = new HashMap();\r
+              Map row2 = new HashMap();\r
+              EntityImages currentImage=(EntityImages) images.next();\r
+              float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();\r
+              float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();\r
+\r
+              //oversize images must be shrunk\r
+              if (img_width>250){\r
+                  img_height=(new Float((new Float(img_height*(250.0F/img_width))).intValue())).floatValue();\r
+                  img_width=250.0F;\r
+              }\r
+\r
+\r
+              //calculate how much text goes in the column(use 8 pixels to pad the column)\r
+              float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);\r
+              float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int\r
+              //add one line for image description\r
+              int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();\r
+\r
+              row1.put("text_widthCM",Float.toString(text_widthCM));\r
+\r
+              row1.put("img_title",currentImage.getValue("title"));\r
+\r
+              row1.put("img_width",Float.toString(img_width));\r
+              row1.put("img_height",Float.toString(img_height));\r
+\r
+              aLogger.debug("img_width " +Float.toString(img_width));\r
+              aLogger.debug("img_height "+Float.toString(img_height));\r
+\r
+              row1.put("img_src",currentImage.getValue("publish_path"));\r
+              row1.put("hasImage","1");\r
+              if (! outOfText){\r
+                  try {\r
+                      int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);\r
+                      row1.put("text",theContent.substring(currentPosition,lastSpaceAt));\r
+                      currentPosition=lastSpaceAt;\r
+                  }\r
+                  catch (IndexOutOfBoundsException e){\r
+                      row1.put("text",theContent.substring(currentPosition));\r
+                      outOfText = true;\r
+                          }\r
+              }\r
+              aLogger.debug("CP2 is "+ currentPosition);\r
+              brokenUpContent.add(row1);\r
+\r
+              if (! outOfText){\r
+                  try {\r
+                      int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);\r
+                      row2.put("text",theContent.substring(currentPosition,lastSpaceAt));\r
+                      currentPosition=lastSpaceAt;\r
+                  }\r
+                  catch (IndexOutOfBoundsException e){\r
+                      row2.put("text",theContent.substring(currentPosition));\r
+                      outOfText = true;\r
+                          }\r
+              }\r
+              row2.put("hasImage","0");\r
+              brokenUpContent.add(row2);\r
+\r
+              aLogger.debug("CP3 is "+ currentPosition);\r
+          }\r
+          Map row3 = new HashMap();\r
+          if (! outOfText){\r
+              row3.put("text",theContent.substring(currentPosition));\r
+              row3.put("hasImage","0");\r
+              brokenUpContent.add(row3);\r
+          }\r
+\r
+      }\r
+\r
+\r
+\r
+\r
+\r
+      ParameterExpander.setValueForKey(\r
+                                       aValueMap,\r
+                                       "data.formatted_content",\r
+                                       new CachingRewindableIterator(brokenUpContent.iterator())\r
+                                       );\r
+\r
+\r
+    }\r
+    catch (Throwable t) {\r
+      aLogger.error("Error while formatting content for PDF: " + t.getMessage());\r
+      t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));\r
+    }\r
+  }\r
+}\r
+\r
+\r
+\r
+\r