2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 package mircoders.producer;
34 import java.io.PrintWriter;
35 import java.util.ArrayList;
36 import java.util.HashMap;
39 import mir.entity.Entity;
40 import mir.entity.EntityList;
41 import mir.entity.adapter.EntityAdapter;
42 import mir.log.LoggerToWriterAdapter;
43 import mir.log.LoggerWrapper;
44 import mir.misc.StringUtil;
45 import mir.producer.ProducerFailure;
46 import mir.producer.ProducerNode;
47 import mir.util.CachingRewindableIterator;
48 import mir.util.HTMLRoutines;
49 import mir.util.ParameterExpander;
50 import mircoders.entity.EntityContent;
51 import mircoders.entity.EntityImages;
52 import mircoders.storage.DatabaseContentToMedia;
54 //because images are returned as a template model!(maybe not needed after all!)
55 //import freemarker.template.*;
58 public class PDFPreFormattingProducerNode implements ProducerNode {
59 private String contentKey;
60 private int numLinesBetweenImages;
61 private float contentAreaWidthCM;
62 private float characterWidthCM;
63 private float pixelWidthCM;
64 private float lineHeightCM;
66 public PDFPreFormattingProducerNode(String aContentKey,String someNumLinesBetweenImages,String aContentAreaWidthCM,String aCharacterWidthCM,String aPixelWidthCM,String aLineHeightCM) {
67 contentKey = aContentKey;
68 numLinesBetweenImages=(new Integer(someNumLinesBetweenImages)).intValue();
69 contentAreaWidthCM=(new Float(aContentAreaWidthCM)).floatValue();
70 characterWidthCM=(new Float(aCharacterWidthCM)).floatValue();
71 pixelWidthCM=(new Float(aPixelWidthCM)).floatValue();
72 lineHeightCM=(new Float(aLineHeightCM)).floatValue();
74 // float characterWidthCM = 0.17F;
75 //float contentAreaWidthCM = 16;
76 //float pixelWidthCM = .03F;
77 //float lineHeightCM = .5F;
80 public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
85 data = ParameterExpander.findValueForKey( aValueMap, contentKey );
87 if (! (data instanceof EntityAdapter)) {
88 throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
91 entity = ((EntityAdapter) data).getEntity();
92 if (! (entity instanceof EntityContent)) {
93 throw new ProducerFailure("PDFPreFormattingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
96 int currentPosition = 0;
98 //int numLinesBetweenImages=3;
103 int numCharsInAnImagelessRow = (new Float(numLinesBetweenImages * (contentAreaWidthCM/characterWidthCM))).intValue();
105 boolean outOfText = false;
107 ArrayList brokenUpContent = new ArrayList();
110 EntityList images=DatabaseContentToMedia.getInstance().getImages((EntityContent)entity);
111 String theContent = ((EntityContent) entity).getValue("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);
118 HashMap row = new HashMap();
119 row.put("text",theContent);
120 row.put("hasImage","0");
121 brokenUpContent.add(row);
124 //need to add checks for out of content!
125 HashMap row0 = new HashMap();
126 if (numCharsInAnImagelessRow>(theContent).length()){
127 row0.put("text",theContent);
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;
136 row0.put("hasImage","0");
137 brokenUpContent.add(row0);
138 aLogger.debug("CP1 is "+ currentPosition);
139 while(images.hasNext()){
140 HashMap row1 = new HashMap();
141 HashMap row2 = new HashMap();
142 EntityImages currentImage=(EntityImages) images.next();
143 float img_width=(new Float(currentImage.getValue("img_width"))).floatValue();
144 float img_height=(new Float(currentImage.getValue("img_height"))).floatValue();
146 //oversize images must be shrunk
148 img_height=(new Float((new Float(img_height*(250.0F/img_width))).intValue())).floatValue();
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();
159 row1.put("text_widthCM",Float.toString(text_widthCM));
161 row1.put("img_title",currentImage.getValue("title"));
163 row1.put("img_width",Float.toString(img_width));
164 row1.put("img_height",Float.toString(img_height));
166 aLogger.debug("img_width " +Float.toString(img_width));
167 aLogger.debug("img_height "+Float.toString(img_height));
169 row1.put("img_src",currentImage.getValue("publish_path"));
170 row1.put("hasImage","1");
173 int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+text_amount);
174 row1.put("text",theContent.substring(currentPosition,lastSpaceAt));
175 currentPosition=lastSpaceAt;
177 catch (IndexOutOfBoundsException e){
178 row1.put("text",theContent.substring(currentPosition));
182 aLogger.debug("CP2 is "+ currentPosition);
183 brokenUpContent.add(row1);
187 int lastSpaceAt = theContent.lastIndexOf(" ",currentPosition+numCharsInAnImagelessRow);
188 row2.put("text",theContent.substring(currentPosition,lastSpaceAt));
189 currentPosition=lastSpaceAt;
191 catch (IndexOutOfBoundsException e){
192 row2.put("text",theContent.substring(currentPosition));
196 row2.put("hasImage","0");
197 brokenUpContent.add(row2);
199 aLogger.debug("CP3 is "+ currentPosition);
201 HashMap row3 = new HashMap();
203 row3.put("text",theContent.substring(currentPosition));
204 row3.put("hasImage","0");
205 brokenUpContent.add(row3);
214 ParameterExpander.setValueForKey(
216 "data.formatted_content",
217 new CachingRewindableIterator(brokenUpContent.iterator())
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)));