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