merged with 1.1
[mir.git] / source / mircoders / producer / PDFPreFormattingProducerNode.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with  any library licensed under the Apache Software License,\r
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
23  * (or with modified versions of the above that use the same license as the above),\r
24  * and distribute linked combinations including the two.  You must obey the\r
25  * GNU General Public License in all respects for all of the code used other than\r
26  * the above mentioned libraries.  If you modify this file, you may extend this\r
27  * exception to your version of the file, but you are not obligated to do so.\r
28  * If you do not wish to do so, delete this exception statement from your version.\r
29  */\r
30 \r
31 package mircoders.producer;\r
32 \r
33 import java.io.PrintWriter;\r
34 import java.util.ArrayList;\r
35 import java.util.HashMap;\r
36 import java.util.*;\r
37 \r
38 import mir.entity.*;\r
39 import mir.entity.EntityList;\r
40 import mir.entity.adapter.EntityAdapter;\r
41 import mir.log.LoggerToWriterAdapter;\r
42 import mir.log.LoggerWrapper;\r
43 import mir.misc.StringUtil;\r
44 import mir.producer.ProducerFailure;\r
45 import mir.producer.ProducerNode;\r
46 import mir.util.CachingRewindableIterator;\r
47 import mir.util.HTMLRoutines;\r
48 import mir.util.ParameterExpander;\r
49 import mircoders.entity.EntityContent;\r
50 import mircoders.entity.EntityImages;\r
51 import mircoders.storage.*;\r
52 \r
53 \r
54 public class PDFPreFormattingProducerNode implements ProducerNode {\r
55   private String contentKey;\r
56   private int numLinesBetweenImages;\r
57   private float contentAreaWidthCM;\r
58   private float characterWidthCM;\r
59   private float pixelWidthCM;\r
60   private float lineHeightCM;\r
61 \r
62   public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {\r
63     contentKey = aContentKey;\r
64     numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();\r
65     contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();\r
66     characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();\r
67     pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();\r
68     lineHeightCM=(new Float(aLineHeightCM)).floatValue();\r
69 \r
70     //    float characterWidthCM = 0.17F;\r
71     //float contentAreaWidthCM = 16;\r
72     //float pixelWidthCM = .03F;\r
73     //float lineHeightCM = .5F;\r
74   }\r
75 \r
76   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {\r
77     Object data;\r
78     Entity entity;\r
79 \r
80     try {\r
81       data = ParameterExpander.findValueForKey( aValueMap, contentKey );\r
82 \r
83       if (! (data instanceof EntityAdapter)) {\r
84         throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);\r
85       }\r
86 \r
87       entity = ((EntityAdapter) data).getEntity();\r
88       if (! (entity instanceof EntityContent)) {\r
89         throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);\r
90       }\r
91 \r
92       int currentPosition = 0;\r
93 \r
94       //int numLinesBetweenImages=3;\r
95 \r
96 \r
97 \r
98 \r
99       int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();\r
100 \r
101       boolean outOfText = false;\r
102 \r
103       ArrayList brokenUpContent = new ArrayList();\r
104 \r
105       Iterator images = new EntityBrowser(\r
106          DatabaseImages.getInstance(),\r
107           "exists (select * from content_x_media where content_id=" + entity.getId() + " and media_id=id)",\r
108           "id desc", 30, -1, 0);\r
109 \r
110       String theContent = ((EntityContent) entity).getValue("content_data");\r
111       //remove pesky characters\r
112       theContent = HTMLRoutines.encodeXML(theContent);\r
113       //put in the <BR> tags so we can turn them to empty blocks\r
114       theContent = StringUtil.convertNewline2Break(theContent);\r
115 \r
116       if (images == null){\r
117           Map row = new HashMap();\r
118           row.put("text",theContent);\r
119           row.put("hasImage","0");\r
120           brokenUpContent.add(row);\r
121       }\r
122       if (images != null){\r
123           //need to add checks for out of content!\r
124           Map row0 = new HashMap();\r
125           if (numCharsInAnImagelessRow>(theContent).length()){\r
126               row0.put("text",theContent);\r
127               outOfText = true;\r
128           }\r
129           else {\r
130               //break on words so we don't split html entities\r
131               int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);\r
132               row0.put("text",theContent.substring(0,lastSpaceAt));\r
133               currentPosition=lastSpaceAt;\r
134           }\r
135           row0.put("hasImage","0");\r
136           brokenUpContent.add(row0);\r
137           aLogger.debug("CP1 is "+ currentPosition);\r
138           while(images.hasNext()){\r
139               Map row1 = new HashMap();\r
140               Map row2 = new HashMap();\r
141               EntityImages currentImage=(EntityImages) images.next();\r
142               float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();\r
143               float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();\r
144 \r
145               //oversize images must be shrunk\r
146               if (img_width>250){\r
147                   img_height=(new Float((new Float(img_height*(250.0F/img_width))).intValue())).floatValue();\r
148                   img_width=250.0F;\r
149               }\r
150 \r
151 \r
152               //calculate how much text goes in the column(use 8 pixels to pad the column)\r
153               float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);\r
154               float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int\r
155               //add one line for image description\r
156               int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();\r
157 \r
158               row1.put("text_widthCM",Float.toString(text_widthCM));\r
159 \r
160               row1.put("img_title",currentImage.getValue("title"));\r
161 \r
162               row1.put("img_width",Float.toString(img_width));\r
163               row1.put("img_height",Float.toString(img_height));\r
164 \r
165               aLogger.debug("img_width " +Float.toString(img_width));\r
166               aLogger.debug("img_height "+Float.toString(img_height));\r
167 \r
168               row1.put("img_src",currentImage.getValue("publish_path"));\r
169               row1.put("hasImage","1");\r
170               if (! outOfText){\r
171                   try {\r
172                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);\r
173                       row1.put("text",theContent.substring(currentPosition,lastSpaceAt));\r
174                       currentPosition=lastSpaceAt;\r
175                   }\r
176                   catch (IndexOutOfBoundsException e){\r
177                       row1.put("text",theContent.substring(currentPosition));\r
178                       outOfText = true;\r
179                           }\r
180               }\r
181               aLogger.debug("CP2 is "+ currentPosition);\r
182               brokenUpContent.add(row1);\r
183 \r
184               if (! outOfText){\r
185                   try {\r
186                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);\r
187                       row2.put("text",theContent.substring(currentPosition,lastSpaceAt));\r
188                       currentPosition=lastSpaceAt;\r
189                   }\r
190                   catch (IndexOutOfBoundsException e){\r
191                       row2.put("text",theContent.substring(currentPosition));\r
192                       outOfText = true;\r
193                           }\r
194               }\r
195               row2.put("hasImage","0");\r
196               brokenUpContent.add(row2);\r
197 \r
198               aLogger.debug("CP3 is "+ currentPosition);\r
199           }\r
200           Map row3 = new HashMap();\r
201           if (! outOfText){\r
202               row3.put("text",theContent.substring(currentPosition));\r
203               row3.put("hasImage","0");\r
204               brokenUpContent.add(row3);\r
205           }\r
206 \r
207       }\r
208 \r
209 \r
210 \r
211 \r
212 \r
213       ParameterExpander.setValueForKey(\r
214                                        aValueMap,\r
215                                        "data.formatted_content",\r
216                                        new CachingRewindableIterator(brokenUpContent.iterator())\r
217                                        );\r
218 \r
219 \r
220     }\r
221     catch (Throwable t) {\r
222       aLogger.error("Error while formatting content for PDF: " + t.getMessage());\r
223       t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));\r
224     }\r
225   }\r
226 }\r
227 \r
228 \r
229 \r
230 \r