View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: TemplateUtil.java
13   * Created: 2007-00-00
14   * Author: bmilos, entrop
15   * $Id: AbstractTemplateUtil.java 3266 2009-08-27 15:28:45Z tmilos $
16   */
17  
18  package pl.edu.agh.cast.data.template;
19  
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.HashSet;
28  import java.util.List;
29  
30  import javax.xml.parsers.ParserConfigurationException;
31  
32  import org.apache.log4j.Logger;
33  import org.eclipse.core.resources.IFile;
34  import org.eclipse.core.resources.IProject;
35  import org.eclipse.core.resources.IResource;
36  import org.eclipse.core.runtime.CoreException;
37  import org.eclipse.core.runtime.IPath;
38  import org.xml.sax.SAXException;
39  
40  import pl.edu.agh.cast.data.Activator;
41  import pl.edu.agh.cast.data.template.xml.TemplateXmlException;
42  import pl.edu.agh.cast.data.template.xml.TemplateXmlExporter;
43  import pl.edu.agh.cast.data.template.xml.TemplateXmlImporter;
44  import pl.edu.agh.cast.data.util.FileIOUtil;
45  import pl.edu.agh.cast.project.ProjectUtil;
46  import pl.edu.agh.cast.util.Configuration;
47  
48  /**
49   * Utility methods for the import templates.
50   *
51   * @author AGH CAST Team
52   */
53  public abstract class AbstractTemplateUtil {
54  
55  	private static final String DOT = "."; //$NON-NLS-1$
56  
57  	private static Logger log = Activator.getLogger();
58  
59  	/**
60  	 * Returns template's project.
61  	 *
62  	 * @return the template's project.
63  	 */
64  	public static IProject getTemplatesProject() throws CoreException {
65  		IPath templatesPath = null;
66  		return ProjectUtil.getInstance().createProject(Configuration.TEMPLATES_PROJECT_NAME, templatesPath);
67  	}
68  
69  	/**
70  	 * Returns templates names converted to lower case.
71  	 *
72  	 * @return lower case template names.
73  	 */
74  	public static HashSet<String> getTemplateLowerCaseNames() {
75  		HashSet<String> names = new HashSet<String>();
76  		try {
77  			IProject project = AbstractTemplateUtil.getTemplatesProject();
78  			String templateExtension = Configuration.TEMPLATE_FILE_EXTENSION;
79  			for (IResource resource : project.members()) {
80  				String fileExtension = resource.getFileExtension();
81  				if (fileExtension != null && fileExtension.equals(templateExtension)) {
82  					String templFileName = resource.getName();
83  					names.add(templFileName.substring(0, templFileName.lastIndexOf('.')).toLowerCase());
84  				}
85  			}
86  			return names;
87  		} catch (Exception e) {
88  			log.error(e.getMessage(), e);
89  			return new HashSet<String>();
90  		}
91  	}
92  
93  	/**
94  	 * Retrieves a collection of valid templates.
95  	 *
96  	 * @return collection of valid templates
97  	 */
98  	public static Collection<Template> getTemplates() {
99  		try {
100 			IProject project = AbstractTemplateUtil.getTemplatesProject();
101 			final List<Template> validTemplates = new ArrayList<Template>();
102 
103 			String templateExtension = Configuration.TEMPLATE_FILE_EXTENSION;
104 			for (IResource resource : project.members()) {
105 				String fileExtension = resource.getFileExtension();
106 				if (fileExtension != null && fileExtension.equals(templateExtension)) {
107 					Template newTemplate = loadFromXmlFile(resource.getName());
108 					validTemplates.add(newTemplate);
109 				}
110 			}
111 			return validTemplates;
112 		} catch (Exception e) {
113 			log.error(e.getMessage(), e);
114 			return Collections.emptyList();
115 		}
116 	}
117 
118 	private static Template loadFromXmlFile(String fileName) throws IOException, CoreException,
119 	        ParserConfigurationException, SAXException {
120 
121 		IProject project = AbstractTemplateUtil.getTemplatesProject();
122 		IFile ifile = project.getFile(fileName);
123 		IPath location = ifile.getLocation();
124 
125 		if (location != null) {
126 			File file = location.toFile();
127 			TemplateXmlImporter importer = new TemplateXmlImporter();
128 			Template readTempl = importer.importTemplate(file);
129 			log.info("Template " + readTempl.getName() //$NON-NLS-1$
130 			        + " successfully read from file: " //$NON-NLS-1$
131 			        + ifile.getLocation().toString());
132 			return readTempl;
133 		}
134 		return null;
135 	}
136 
137 	/**
138 	 * Exports and saves a given template to an XML file.
139 	 *
140 	 * @param template
141 	 *            the template to be saved
142 	 * @throws CoreException
143 	 * @throws IOException
144 	 */
145 	public static void saveToXmlFile(Template template) throws CoreException, IOException {
146 
147 		IProject project = AbstractTemplateUtil.getTemplatesProject();
148 		String templateFileName = template.getName() + DOT + Configuration.TEMPLATE_FILE_EXTENSION;
149 		IFile ifile = project.getFile(templateFileName);
150 		IPath location = ifile.getLocation();
151 
152 		if (location != null) {
153 			File file = location.toFile();
154 			TemplateXmlExporter exporter = new TemplateXmlExporter();
155 			exporter.export(template, file);
156 			ifile.createLink(location, IResource.NONE, null);
157 
158 			log.info("Template " + template.getName() //$NON-NLS-1$
159 			        + " successfully saved to file: " //$NON-NLS-1$
160 			        + ifile.getLocation().toString());
161 		}
162 	}
163 
164 	/**
165 	 * Registers a new template from a given template file.
166 	 *
167 	 * @param path
168 	 *            the path to file containing a template to register
169 	 * @return the registered template
170 	 * @throws IOException
171 	 * @throws CoreException
172 	 * @throws TemplateXmlException
173 	 * @throws ParserConfigurationException
174 	 * @throws SAXException
175 	 */
176 	public static Template registerTemplate(String path) throws IOException, CoreException, TemplateXmlException,
177 	        ParserConfigurationException, SAXException {
178 
179 		Template template = loadTemplateFromFile(path);
180 		return registerTemplate(template);
181 	}
182 
183 	/**
184 	 * Registers a new given template.
185 	 *
186 	 * @param template
187 	 *            the template to register
188 	 * @return the registered template
189 	 * @throws CoreException
190 	 * @throws IOException
191 	 */
192 	public static Template registerTemplate(Template template) throws CoreException, IOException {
193 		IFile templateFile = getTemplatesProject().getFile(getTemplateFileName(template));
194 		if (templateFile.exists()) {
195 			return null;
196 		}
197 
198 		saveToXmlFile(template);
199 		return template;
200 	}
201 
202 	/**
203 	 * Imports and loads a new template from an XML file.
204 	 *
205 	 * @param path
206 	 *            the path to file containing a template to be loaded
207 	 * @return the loaded template
208 	 * @throws SAXException
209 	 * @throws ParserConfigurationException
210 	 * @throws IOException
211 	 */
212 	public static Template loadTemplateFromFile(String path) throws SAXException, ParserConfigurationException,
213 	        IOException {
214 		TemplateXmlImporter importer = new TemplateXmlImporter();
215 		Template template = importer.importTemplate(new File(path));
216 		return template;
217 	}
218 
219 	/**
220 	 * Exports specified template to target file. It overrides target file by default.
221 	 *
222 	 * @param template
223 	 *            the template to export
224 	 * @param target
225 	 *            the file where new template will be exported
226 	 * @throws IOException
227 	 * @throws CoreException
228 	 */
229 	public static void exportTemplate(Template template, File target) throws IOException, CoreException {
230 		IProject project = AbstractTemplateUtil.getTemplatesProject();
231 		String templateFileName = getTemplateFileName(template);
232 		IFile source = project.getFile(templateFileName);
233 		if (source == null) {
234 			throw new FileNotFoundException("Cannot find specified template: " //$NON-NLS-1$
235 			        + template.getName());
236 		}
237 		FileIOUtil.copyFileContent(source.getContents(), new FileOutputStream(target));
238 	}
239 
240 	private static String getTemplateFileName(Template template) {
241 		return template.getName() + DOT + Configuration.TEMPLATE_FILE_EXTENSION;
242 	}
243 
244 }