View Javadoc

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.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import com.webstersmalley.picweb.offline.config.ConfigurationModel;
29  import com.webstersmalley.picweb.utils.FolderContents;
30  import com.webstersmalley.picweb.utils.IOUtils;
31  import com.webstersmalley.picweb.utils.StringMapper;
32  
33  /***
34   * @author Matthew Smalley
35   */
36  public class ThumbPageGenerator
37  {
38  	/*** Logger for the class. */
39  	private static Log log = LogFactory.getLog(ThumbPageGenerator.class);
40  	
41  	private ConfigurationModel model;
42  	private StringMapper mapper;
43  	private TemplateModel template;
44  	private FolderContents folder;
45  	private String filename;
46  	private List pictures;
47  	private String stylesheet = "";
48  	
49  	public ThumbPageGenerator(ConfigurationModel model, StringMapper mapper, TemplateModel template, FolderContents folder)
50  	{
51  		this.model = model;
52  		this.mapper = mapper;
53  		this.template = template;
54  		this.folder = folder;
55  		
56  		this.filename = model.getOutputFolder() + File.separator + folder.getRelativePath().replaceAll("'", "") + File.separator + "thumbs.html";
57  		this.pictures = folder.getPictures();
58  		for (int i = 0; i < folder.getFolderDepth(); i++)
59  		{
60  			this.stylesheet += "../";
61  		}
62  		stylesheet += "stylesheet.css";
63  		mapper.addMapping("cssfilename", stylesheet);
64  		mapper.addMapping("title", folder.getTopName());
65  		mapper.addMapping("google_ad_client", model.getGoogleAdId());
66  	}
67  
68  	public void generate() throws FileNotFoundException
69  	{
70  		StringBuffer output = new StringBuffer();
71  		output.append(mapper.map(template.getThumbPageHeader()));
72  		
73  		int colCount = 0;
74  		Iterator it = pictures.iterator();
75  		while(it.hasNext())
76  		{
77  			File picture = (File)it.next(); 
78  			if (colCount == 0)
79  			{
80  				output.append(mapper.map(template.getThumbPageRowHeader()));
81  			}
82  			
83  			output.append(processFile(picture));
84  			if (colCount == (model.getRowSize()-1))
85  			{
86  				output.append(mapper.map(template.getThumbPageRowFooter()));
87  				colCount = 0;
88  			}
89  			else
90  			{
91  				colCount++;
92  			}
93  		}
94  
95  		output.append(mapper.map(template.getThumbPageFooter()));
96  		
97  		IOUtils.writeFile(filename, output.toString());
98  	}
99  	
100 	private String processFile(File picture)
101 	{
102 		String imageref = "IMAGE-" + picture.getName();
103 		String thumbref = "THUMB-" + picture.getName();
104 		String imagename = picture.getName();
105 		mapper.addMapping("imageref", imageref);
106 		mapper.addMapping("thumbref", thumbref);
107 		mapper.addMapping("imagename", imagename);
108 
109 		return mapper.map(template.getThumbPageCell());
110 	}
111 }