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.config;
19  
20  import java.io.File;
21  import java.io.IOException;
22  
23  import javax.xml.parsers.DocumentBuilder;
24  import javax.xml.parsers.DocumentBuilderFactory;
25  import javax.xml.parsers.ParserConfigurationException;
26  
27  import org.apache.commons.digester.Digester;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  import org.xml.sax.SAXException;
31  
32  /***
33   * Holds the model for the configuration.
34   * This is expected to be instantiated with the default
35   * constructor and the fields set by either the
36   * configuration GUI or a saved configuration.
37   */
38  public final class ConfigurationModel
39  {
40  	/***
41  	 * Returns the image size
42  	 * @return the imageSize.
43  	 */
44  	public int getImageSize()
45  	{
46  		return imageSize;
47  	}
48  	/***
49  	 * Sets the maximum size of the image.
50  	 *
51  	 * Irrespective of whether the image is portrait (height > width)
52  	 * or landscape (width > height), no dimension will be greater
53  	 * than the set imageSize.
54  	 *
55  	 * @param imageSize The imageSize to set.
56  	 */
57  	public void setImageSize(final int imageSize)
58  	{
59  		this.imageSize = imageSize;
60  	}
61  	/***
62  	 * Returns the maximum number of rows in the table on a given
63  	 * thumbnail page.
64  	 * @return Returns the maxRows.
65  	 */
66  	public int getMaxRows()
67  	{
68  		return maxRows;
69  	}
70  	/***
71  	 * Sets the maximum number of rows in the table on a given thumbnail page.
72  	 *
73  	 * If the number of rows would exceed this value, the generated
74  	 * web page should be split into sub pages.
75  	 *
76  	 * @param maxRows The maxRows to set.
77  	 */
78  	public void setMaxRows(final int maxRows)
79  	{
80  		this.maxRows = maxRows;
81  	}
82  	/***
83  	 * Returns the folder in which all output is to be saved.
84  	 *
85  	 * @return Returns the outputFolder.
86  	 */
87  	public String getOutputFolder()
88  	{
89  		return outputFolder;
90  	}
91  	/***
92  	 * Sets the folder in which all output is to be saved.
93  	 *
94  	 * @param outputFolder The outputFolder to set.
95  	 */
96  	public void setOutputFolder(final String outputFolder)
97  	{
98  		this.outputFolder = outputFolder;
99  	}
100 	/***
101 	 * Returns the folder in which the images are to be found.
102 	 *
103 	 * @return Returns the rootFolder.
104 	 */
105 	public String getRootFolder()
106 	{
107 		return rootFolder;
108 	}
109 	/***
110 	 * Sets the folder in which the images are to be found.
111 	 *
112 	 * @param rootFolder The rootFolder to set.
113 	 */
114 	public void setRootFolder(final String rootFolder)
115 	{
116 		this.rootFolder = rootFolder;
117 	}
118 	/***
119 	 * Returns the maximum number of images in a given row in the
120 	 * thumbnails page.
121 	 *
122 	 * @return Returns the rowSize.
123 	 */
124 	public int getRowSize()
125 	{
126 		return rowSize;
127 	}
128 	/***
129 	 * Sets the maximum number of images in a given row in the thumbnails
130 	 * page.
131 	 *
132 	 * If the number of images exceeds this value, the table is wrapped
133 	 * onto a new row.
134 	 *
135 	 * @param rowSize The rowSize to set.
136 	 */
137 	public void setRowSize(final int rowSize)
138 	{
139 		this.rowSize = rowSize;
140 	}
141 	/***
142 	 * Returns the maximum size of the thumbnails, irrespective of
143 	 * picture orientation.
144 	 * @return Returns the thumbSize.
145 	 */
146 	public int getThumbSize()
147 	{
148 		return thumbSize;
149 	}
150 	/***
151 	 * Sets the maximum size of the thumbnail.
152 	 *
153 	 * Irrespective of whether the image is portrait (height > width)
154 	 * or landscape (width > height), no dimension will be greater
155 	 * than the set thumbSize.
156 	 *
157 	 * @param thumbSize The thumbSize to set.
158 	 */
159 	public void setThumbSize(final int thumbSize)
160 	{
161 		this.thumbSize = thumbSize;
162 	}
163 	/***
164 	 * Returns the title of the generated web site.
165 	 *
166 	 * @return Returns the title.
167 	 */
168 	public String getTitle()
169 	{
170 		return title;
171 	}
172 	/***
173 	 * Sets the title of the generated web site.
174 	 *
175 	 * @param title The title to set.
176 	 */
177 	public void setTitle(final String title)
178 	{
179 		this.title = title;
180 	}
181 
182 	/***
183 	 * The base output folder (eg /html/picweb-generated).
184 	 */
185 	private String outputFolder;
186 
187 	/***
188 	 * The base input folder (eg /pics/holiday).
189 	 */
190 	private String rootFolder;
191 	/***
192 	 * The title of the webpage (appears as the html title).
193 	 */
194 	private String title;
195 	/***
196 	 * Maximum number of images per row in the table of thumbnails
197 	 */
198 	private int rowSize;
199 	/***
200 	 * Maximum number of rows before a new page of thumbs is created.
201 	 */
202 	private int maxRows;
203 	/***
204 	 * Maximum size of thumbnail (height or width).
205 	 */
206 	private int thumbSize;
207 	/***
208 	 * Maximum size of image (height or width).
209 	 */
210 	private int imageSize;
211 	/***
212 	 * Name of the template file.
213 	 */
214 	private String templateFilename;
215 	/***
216 	 * Google Ad ID
217 	 */
218 	private String googleAdId; 
219 	/***
220 	 * Artifacts folder. All files in here will be copied to the
221 	 * base output folder. Put things like icons (for navigation)
222 	 * and stylesheets here.
223 	 */
224 	private String artifactsFolder;
225 	
226 	/***
227 	 * Returns the filename of the template.
228 	 *
229 	 * @return Returns the templateFilename.
230 	 */
231 	public String getTemplateFilename()
232 	{
233 		return templateFilename;
234 	}
235 	/***
236 	 * Filename of the template file to set.
237 	 * @param templateFilename The templateFilename to set.
238 	 */
239 	public void setTemplateFilename(final String templateFilename)
240 	{
241 		this.templateFilename = templateFilename;
242 	}
243 
244 	/***
245 	 * @return Returns the artifactsFolder.
246 	 */
247 	public String getArtifactsFolder()
248 	{
249 		return artifactsFolder;
250 	}
251 	/***
252 	 * @param artifactsFolder The artifactsFolder to set.
253 	 */
254 	public void setArtifactsFolder(String artifactsFolder)
255 	{
256 		this.artifactsFolder = artifactsFolder;
257 	}
258 	/***
259 	 * @return Returns the googleAdId.
260 	 */
261 	public String getGoogleAdId()
262 	{
263 		return googleAdId;
264 	}
265 	/***
266 	 * @param googleAdId The googleAdId to set.
267 	 */
268 	public void setGoogleAdId(String googleAdId)
269 	{
270 		this.googleAdId = googleAdId;
271 	}
272 	/***
273 	 * Create a ConfigurationModel object out of an xml filename
274 	 * @param filename the filename of the XML file to parse
275 	 * @return ConfigurationModel the model
276 	 * @throws SAXException
277 	 * @throws IOException
278 	 */
279 	public static ConfigurationModel fromXML(String filename) throws IOException, SAXException
280 	{
281 		Digester digester = new Digester();
282 		digester.setValidating(false);
283 		digester.addObjectCreate("picweb/offline/config", ConfigurationModel.class);
284 		digester.addSetProperties("picweb/offline/config");
285 		ConfigurationModel model = (ConfigurationModel) digester.parse(new File(filename));
286 		return model;
287 	}
288 	/***
289 	 * Returns an XML representation of the configuration model
290 	 * @return
291 	 * @throws ParserConfigurationException
292 	 */
293 	public Document toXML() throws ParserConfigurationException
294 	{
295 		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
296 		DocumentBuilder builder = builderFactory.newDocumentBuilder();
297 		Document doc = builder.newDocument();
298 		Element picweb = doc.createElement("picweb");
299 		doc.appendChild(picweb);
300 		Element offline = doc.createElement("offline");
301 		picweb.appendChild(offline);
302 		Element config = doc.createElement("config");
303 		config.setAttribute("rootFolder", rootFolder);
304 		config.setAttribute("outputFolder", outputFolder);
305 		config.setAttribute("title", title);
306 		config.setAttribute("rowSize", Integer.toString(rowSize));
307 		config.setAttribute("maxRows", Integer.toString(maxRows));
308 		config.setAttribute("thumbSize", Integer.toString(thumbSize));
309 		config.setAttribute("imageSize", Integer.toString(imageSize));
310 		config.setAttribute("templateFilename", templateFilename);
311 		config.setAttribute("googleAdId", googleAdId);
312 		config.setAttribute("artifactsFolder", artifactsFolder);
313 		offline.appendChild(config);
314 
315 		return doc;
316 	}
317 }