1 /**************************************************************************
2 Copyright 2005 Webstersmalley
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 *************************************************************************/
16
17
18 package com.webstersmalley.picweb.offline;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import javax.xml.parsers.ParserConfigurationException;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.xml.sax.SAXException;
31
32 import com.webstersmalley.picweb.offline.config.ConfigurationModel;
33 import com.webstersmalley.picweb.utils.FolderContents;
34 import com.webstersmalley.picweb.utils.IOUtils;
35 import com.webstersmalley.picweb.utils.PictureFilenameFilter;
36 import com.webstersmalley.picweb.utils.StringMapper;
37
38 /***
39 * @author Matthew Smalley
40 */
41 public class WebsiteGenerator
42 {
43 /*** Logger for the class. */
44 private static Log log = LogFactory.getLog(WebsiteGenerator.class);
45
46 private ConfigurationModel model;
47 private File[] pictures;
48 private TemplateModel template;
49 private StringMapper mapper;
50
51
52 public WebsiteGenerator(ConfigurationModel model) throws ParserConfigurationException, SAXException, IOException
53 {
54 this.model = model;
55 this.template = new TemplateModel(model.getTemplateFilename());
56 this.mapper = new StringMapper();
57 mapper.addMapping("title", model.getTitle());
58 mapper.addMapping("cssfilename", "stylesheet.css");
59 }
60
61 public void go() throws IOException
62 {
63 populateList();
64 FolderContents folder = new FolderContents(model.getRootFolder());
65 copyArtifacts();
66 HomepageGenerator hg = new HomepageGenerator(model, mapper, template, folder);
67 hg.generate();
68
69 generateThumbs(folder);
70 }
71
72 private void generateThumbs(FolderContents folder) throws FileNotFoundException
73 {
74 ThumbPageGenerator tpg = new ThumbPageGenerator(model, mapper, template, folder);
75 tpg.generate();
76
77 Iterator it = folder.getChildren().iterator();
78 while (it.hasNext())
79 {
80 FolderContents child = (FolderContents)it.next();
81 generateThumbs(child);
82 }
83 List pictures = folder.getPictures();
84 ImageProcessor processor = new ImageProcessor(pictures, model, folder.getRelativePath().replace("'", ""));
85 processor.execute();
86 }
87
88 private void populateList()
89 {
90 File root = new File(model.getRootFolder());
91 pictures = root.listFiles(new PictureFilenameFilter());
92 }
93
94 private void copyArtifacts() throws IOException
95 {
96 IOUtils.copyFolderContents(model.getArtifactsFolder(), model.getOutputFolder());
97 }
98 }