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;
38 import freemarker.template.*;
45 import mircoders.entity.*;
46 import mircoders.storage.*;
49 import org.apache.fop.apps.* ;
50 import org.xml.sax.InputSource;
51 import org.xml.sax.XMLReader;
52 import org.apache.log.*;
54 public class ProducerContent extends Producer {
56 private String contentTemplate=MirConfig.getProp("Producer.Content.Template");
57 private String contentPrintableTemplate=MirConfig.getProp("Producer.PrintableContent.Template");
58 private String html2foStyleSheetName=MirConfig.getProp("Producer.PrintableContent.html2foStyleSheetName");
59 private String generateFO=MirConfig.getProp("GenerateFO");
60 private String generatePDF=MirConfig.getProp("GeneratePDF");
61 private String producerStorageRoot=MirConfig.getProp("Producer.StorageRoot");
62 private Logger fopLog=null;
63 private String templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
65 public static void main(String argv[]){
66 //Configuration.initConfig("config");
67 System.out.println(MirConfig.getProp("Producer.DocRoot"));
70 new ProducerContent().handle(new PrintWriter(System.out), null,
72 } catch(Exception e) {
73 System.err.println(e.toString());
77 public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
79 throws StorageObjectException, ModuleException {
81 handle(htmlout,user,force,sync,null);
84 public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
85 boolean sync, String id) throws StorageObjectException,
89 long startTime = System.currentTimeMillis();
92 String whereClause = " ";
94 String htmlFileName = null;
95 String foFileName = null;
96 String pdfFileName = null;
97 EntityContent currentContent;
98 EntityList batchEntityList;
99 EntityUsers userEntity=null;
101 int contentBatchsize =
102 Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
103 // production of the content-pages
105 /** @todo this should be moved to ModuleContent */
106 orderBy="webdb_lastchange desc";
108 whereClause="is_published='1'";
109 // if true: produces a single content item
111 whereClause += " AND id="+id;
112 // I think this avoids a select count(*)...
115 batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
116 contentBatchsize, userEntity);
118 whereClause="is_produced='0' AND is_published='1'";
119 //if true produces a single contentitem
121 whereClause += " AND id="+id;
122 // this avoids a select count(*)...
125 batchEntityList = contentModule.getContent(whereClause, orderBy, 0,
126 contentBatchsize, userEntity);
129 while (batchEntityList!=null) {
130 for(int i=0;i<batchEntityList.size();i++) {
131 currentContent = (EntityContent)batchEntityList.elementAt(i);
135 SimpleHash mergeData=new SimpleHash();
136 mergeData.put("content", currentContent);
138 /** @todo this should be assembled in entity */
139 String date = currentContent.getValue("date");
140 String year = date.substring(0,4);
141 String month = date.substring(4,6);
142 htmlFileName = "/" + year + "/" + month + "/" +
143 currentContent.getValue("id") + ".shtml";
146 boolean retVal = produce(contentTemplate, htmlFileName, mergeData, htmlout);
147 if ( retVal ) currentContent.setProduced(true);
149 //produce xsl:fo and pdf version(if desired)
150 if (generateFO.toLowerCase().equals("yes")){
151 foFileName = "/" + year + "/" + month + "/"
152 + currentContent.getValue("id") + ".fo";
153 boolean foRetVal = produce(contentPrintableTemplate, foFileName,
154 mergeData, htmlout, "UTF8");
156 if (generatePDF.toLowerCase().equals("yes")){
157 pdfFileName = producerStorageRoot + "/" + year
159 + currentContent.getValue("id") + ".pdf";
160 Driver driver = new Driver();
162 Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
163 fopLog = hierarchy.getLoggerFor("fop");
164 fopLog.setPriority(Priority.WARN);
166 driver.setLogger(fopLog);
167 driver.setRenderer(Driver.RENDER_PDF);
168 File foFile=new File(producerStorageRoot + foFileName);
169 File html2foStyleSheet=new File(templateDir+"/"
170 +html2foStyleSheetName);
171 InputHandler inputHandler =
172 new XSLTInputHandler(foFile, html2foStyleSheet);
173 XMLReader parser = inputHandler.getParser();
174 driver.setOutputStream(new FileOutputStream(pdfFileName));
175 driver.render(parser, inputHandler.getInputSource());
178 } catch(Exception e) {
179 String errorText = "Producer.Content <font color=red>ERROR</font> while producing content ID:"
180 + currentContent.getId()+", skipping it :: "+e.toString();
181 logHTML(htmlout, errorText);
182 theLog.printError(errorText);
187 // if next batch get it...
188 if (batchEntityList.hasNextBatch()){
189 batchEntityList = contentModule.getContent(whereClause, orderBy,
190 batchEntityList.getNextBatch(),
191 contentBatchsize, userEntity);
193 batchEntityList=null;
196 logHTMLFinish(htmlout, "Content", pageCount, startTime, System.currentTimeMillis());
197 /** @todo why no syncing here? */