1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
50
51
52
53 public abstract class AbstractTemplateUtil {
54
55 private static final String DOT = ".";
56
57 private static Logger log = Activator.getLogger();
58
59
60
61
62
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
71
72
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
95
96
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()
130 + " successfully read from file: "
131 + ifile.getLocation().toString());
132 return readTempl;
133 }
134 return null;
135 }
136
137
138
139
140
141
142
143
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()
159 + " successfully saved to file: "
160 + ifile.getLocation().toString());
161 }
162 }
163
164
165
166
167
168
169
170
171
172
173
174
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
185
186
187
188
189
190
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
204
205
206
207
208
209
210
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
221
222
223
224
225
226
227
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: "
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 }