added some stuff to make sure xml is encoded properly...
[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 the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.producer;
33
34 import java.util.*;
35 import java.io.*;
36
37 import mir.util.*;
38 import mir.producer.*;
39 import mir.entity.*;
40 import mir.entity.adapter.*;
41 import mir.log.*;
42 import mir.misc.StringUtil;
43 import mir.util.HTMLRoutines;
44
45
46 import mircoders.entity.*;
47 import mircoders.storage.*;
48
49 //because images are returned as a template model!(maybe not needed after all!)
50 //import freemarker.template.*;
51
52
53 public class PDFPreFormattingProducerNode implements ProducerNode {
54   private String contentKey;
55   private int numLinesBetweenImages;
56   private float contentAreaWidthCM;
57   private float characterWidthCM;
58   private float pixelWidthCM;
59   private float lineHeightCM;
60
61   public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {
62     contentKey = aContentKey;
63     numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();
64     contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();
65     characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();
66     pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();
67     lineHeightCM=(new Float(aLineHeightCM)).floatValue();
68
69     //    float characterWidthCM = 0.17F;
70     //float contentAreaWidthCM = 16;
71     //float pixelWidthCM = .03F;
72     //float lineHeightCM = .5F;
73   }
74
75   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
76     Object data;
77     Entity entity;
78
79     try {
80       data = ParameterExpander.findValueForKey( aValueMap, contentKey );
81
82       if (! (data instanceof EntityAdapter)) {
83         throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
84       }
85
86       entity = ((EntityAdapter) data).getEntity();
87       if (! (entity instanceof EntityContent)) {
88         throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
89       }
90
91       int currentPosition = 0;
92
93       //int numLinesBetweenImages=3;
94
95
96
97
98       int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();
99
100       boolean outOfText = false;
101
102       ArrayList brokenUpContent = new ArrayList();
103
104
105       EntityList images=DatabaseContentToMedia.getInstance().getImages((EntityContent)entity);
106       String theContent = ((EntityContent) entity).getValue("content_data");
107       //remove pesky characters
108       theContent = HTMLRoutines.encodeXML(theContent);
109       //put in the <BR> tags so we can turn them to empty blocks
110       theContent = StringUtil.convertNewline2Break(theContent);
111
112       if (images == null){
113           HashMap row = new HashMap();
114           row.put("text",theContent);
115           row.put("hasImage","0");
116           brokenUpContent.add(row);
117       }
118       if (images != null){
119           //need to add checks for out of content!
120           HashMap row0 = new HashMap();
121           if (numCharsInAnImagelessRow>(theContent).length()){
122               row0.put("text",theContent);
123               outOfText = true;
124           }
125           else {
126               //break on words so we don't split html entities
127               int lastSpaceAt = theContent.lastIndexOf(" ",numCharsInAnImagelessRow);
128               row0.put("text",theContent.substring(0,lastSpaceAt));
129               currentPosition=lastSpaceAt;
130           }
131           row0.put("hasImage","0");
132           brokenUpContent.add(row0);
133           aLogger.debug("CP1 is "+ currentPosition);
134           while(images.hasNext()){
135               HashMap row1 = new HashMap();
136               HashMap row2 = new HashMap();
137               EntityImages currentImage=(EntityImages) images.next();
138               float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();
139               float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();
140
141               //oversize images must be shrunk
142               if (img_width>250){
143                   img_height=(new Float((new Float(img_height*(250.0F/img_width))).intValue())).floatValue();
144                   img_width=250.0F;
145               }
146
147
148               //calculate how much text goes in the column(use 8 pixels to pad the column)
149               float text_widthCM = contentAreaWidthCM-((img_width+8)*pixelWidthCM);
150               float number_of_lines = img_height*pixelWidthCM/lineHeightCM; //don't worry we will make it an int
151               //add one line for image description
152               int text_amount= (new Float((text_widthCM/characterWidthCM)*(number_of_lines+1))).intValue();
153
154               row1.put("text_widthCM",Float.toString(text_widthCM));
155
156               row1.put("img_title",currentImage.getValue("title"));
157
158               row1.put("img_width",Float.toString(img_width));
159               row1.put("img_height",Float.toString(img_height));
160
161               aLogger.debug("img_width " +Float.toString(img_width));
162               aLogger.debug("img_height "+Float.toString(img_height));
163
164               row1.put("img_src",currentImage.getValue("publish_path"));
165               row1.put("hasImage","1");
166               if (! outOfText){
167                   try {
168                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
169                       row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
170                       currentPosition=lastSpaceAt;
171                   }
172                   catch (IndexOutOfBoundsException e){
173                       row1.put("text",theContent.substring(currentPosition));
174                       outOfText = true;
175                           }
176               }
177               aLogger.debug("CP2 is "+ currentPosition);
178               brokenUpContent.add(row1);
179
180               if (! outOfText){
181                   try {
182                       int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
183                       row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
184                       currentPosition=lastSpaceAt;
185                   }
186                   catch (IndexOutOfBoundsException e){
187                       row2.put("text",theContent.substring(currentPosition));
188                       outOfText = true;
189                           }
190               }
191               row2.put("hasImage","0");
192               brokenUpContent.add(row2);
193
194               aLogger.debug("CP3 is "+ currentPosition);
195           }
196           HashMap row3 = new HashMap();
197           if (! outOfText){
198               row3.put("text",theContent.substring(currentPosition));
199               row3.put("hasImage","0");
200               brokenUpContent.add(row3);
201           }
202
203       }
204
205
206
207
208
209       ParameterExpander.setValueForKey(
210                                        aValueMap,
211                                        "data.formatted_content",
212                                        new CachingRewindableIterator(brokenUpContent.iterator())
213                                        );
214
215
216     }
217     catch (Throwable t) {
218       aLogger.error("Error while formatting content for PDF: " + t.getMessage());
219       t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));
220     }
221   }
222 }
223
224
225
226