* anti-abuse upgrade: filters now stored in the database (experimental)
[mir.git] / source / mircoders / producer / PDFPreFormattingProducerNode.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
31 package mircoders.producer;
32
33 import java.io.PrintWriter;
34 import java.util.*;
35
36 import mir.entity.Entity;
37 import mir.entity.EntityBrowser;
38 import mir.entity.adapter.EntityAdapter;
39 import mir.log.LoggerToWriterAdapter;
40 import mir.log.LoggerWrapper;
41 import mir.misc.StringUtil;
42 import mir.producer.ProducerFailure;
43 import mir.producer.ProducerNode;
44 import mir.util.CachingRewindableIterator;
45 import mir.util.HTMLRoutines;
46 import mir.util.ParameterExpander;
47 import mircoders.entity.EntityContent;
48 import mircoders.entity.EntityImages;
49 import mircoders.storage.DatabaseImages;
50
51
52 public class PDFPreFormattingProducerNode implements ProducerNode {
53   private String contentKey;
54   private int numLinesBetweenImages;
55   private float contentAreaWidthCM;
56   private float characterWidthCM;
57   private float pixelWidthCM;
58   private float lineHeightCM;
59
60   public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {
61     contentKey = aContentKey;
62     numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();
63     contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();
64     characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();
65     pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();
66     lineHeightCM=(new Float(aLineHeightCM)).floatValue();
67
68     //    float characterWidthCM = 0.17F;
69     //float contentAreaWidthCM = 16;
70     //float pixelWidthCM = .03F;
71     //float lineHeightCM = .5F;
72   }
73
74   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
75     Object data;
76     Entity entity;
77
78     try {
79       data = ParameterExpander.findValueForKey( aValueMap, contentKey );
80
81       if (! (data instanceof EntityAdapter)) {
82         throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
83       }
84
85       entity = ((EntityAdapter) data).getEntity();
86       if (! (entity instanceof EntityContent)) {
87         throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
88       }
89
90       int currentPosition = 0;
91
92       //int numLinesBetweenImages=3;
93
94
95
96
97       int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();
98
99       boolean outOfText = false;
100
101       ArrayList brokenUpContent = new ArrayList();
102
103
104       List extraTables = new ArrayList();
105       extraTables.add("content_x_media cxm");
106       Iterator images = new EntityBrowser(
107         DatabaseImages.getInstance(), "i", extraTables,
108           "cxm.content_id="+entity.getId()+"and cxm.media_id=i.id",
109           "i.id desc", 30, -1, 0);
110
111       String theContent = ((EntityContent) entity).getFieldValue("content_data");
112       //remove pesky characters
113       theContent = HTMLRoutines.encodeXML(theContent);
114       //put in the <BR> tags so we can turn them to empty blocks
115       theContent = StringUtil.convertNewline2Break(theContent);
116
117       if (images == null){
118           Map row = new HashMap();
119           row.put("text",theContent);
120           row.put("hasImage","0");
121           brokenUpContent.add(row);
122       }
123       if (images != null){
124           //need to add checks for out of content!
125           Map row0 = new HashMap();
126           if (numCharsInAnImagelessRow>(theContent).length()){
127               row0.put("text",theContent);
128               outOfText = true;
129           }
130           else {
131               //break on words so we don't split html entities
132               int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);
133               row0.put("text",theContent.substring(0,lastSpaceAt));
134               currentPosition=lastSpaceAt;
135           }
136           row0.put("hasImage","0");
137           brokenUpContent.add(row0);
138           aLogger.debug("CP1 is "+ currentPosition);
139           while(images.hasNext()){
140               Map row1 = new HashMap();
141               Map row2 = new HashMap();
142               EntityImages currentImage=(EntityImages) images.next();
143               float img_width=(new Float(currentImage.getFieldValue("img_width"))).floatValue();
144               float img_height=(new Float(currentImage.getFieldValue("img_height"))).floatValue();
145
146               //oversize images must be shrunk
147               if (img_width>250){
148                   img_height=(new Float((new Float(img_height*(250.0F/img_width))).intValue())).floatValue();
149                   img_width=250.0F;
150               }
151
152
153               //calculate how much text goes in the column(use 8 pixels to pad the column)
154               float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);
155               float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int
156               //add one line for image description
157               int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();
158
159               row1.put("text_widthCM",Float.toString(text_widthCM));
160
161               row1.put("img_title",currentImage.getFieldValue("title"));
162
163               row1.put("img_width",Float.toString(img_width));
164               row1.put("img_height",Float.toString(img_height));
165
166               aLogger.debug("img_width " +Float.toString(img_width));
167               aLogger.debug("img_height "+Float.toString(img_height));
168
169               row1.put("img_src",currentImage.getFieldValue("publish_path"));
170               row1.put("hasImage","1");
171               if (! outOfText){
172                   try {
173                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
174                       row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
175                       currentPosition=lastSpaceAt;
176                   }
177                   catch (IndexOutOfBoundsException e){
178                       row1.put("text",theContent.substring(currentPosition));
179                       outOfText = true;
180                           }
181               }
182               aLogger.debug("CP2 is "+ currentPosition);
183               brokenUpContent.add(row1);
184
185               if (! outOfText){
186                   try {
187                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
188                       row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
189                       currentPosition=lastSpaceAt;
190                   }
191                   catch (IndexOutOfBoundsException e){
192                       row2.put("text",theContent.substring(currentPosition));
193                       outOfText = true;
194                           }
195               }
196               row2.put("hasImage","0");
197               brokenUpContent.add(row2);
198
199               aLogger.debug("CP3 is "+ currentPosition);
200           }
201           Map row3 = new HashMap();
202           if (! outOfText){
203               row3.put("text",theContent.substring(currentPosition));
204               row3.put("hasImage","0");
205               brokenUpContent.add(row3);
206           }
207
208       }
209
210
211
212
213
214       ParameterExpander.setValueForKey(
215                                        aValueMap,
216                                        "data.formatted_content",
217                                        new CachingRewindableIterator(brokenUpContent.iterator())
218                                        );
219
220
221     }
222     catch (Throwable t) {
223       aLogger.error("Error while formatting content for PDF: " + t.getMessage());
224       t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));
225     }
226   }
227 }
228
229
230
231