forked from gwt-plugins/gwt-eclipse-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectTemplate.java
More file actions
187 lines (172 loc) · 5.44 KB
/
ProjectTemplate.java
File metadata and controls
187 lines (172 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*******************************************************************************
* Copyright 2024 GWT Eclipse Plugin. All Rights Reserved.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.google.gdt.eclipse.suite.wizards;
import org.apache.commons.io.FileUtils;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.JavaCore;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.osgi.framework.Bundle;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* This class loads and parses the template files.
*/
public class ProjectTemplate implements Comparable<ProjectTemplate>{
private static final String GWT_TEMPLATE_DIR = "GWT_TEMPLATE_DIR";
private Document config;
private File dir;
/**
* @throws IOException
* @throws JDOMException
*/
public ProjectTemplate(File config) throws JDOMException, IOException {
this.config = parseTemplate(config);
dir = config.getParentFile();
}
/**
* Get a List of all templates.
* @return
* @throws IOException
*/
public static final List<ProjectTemplate> getTemplates() throws IOException {
List<ProjectTemplate> templates = new ArrayList<>();
try {
Bundle bundle = Platform.getBundle("com.gwtplugins.gdt.eclipse.suite");
URL url = bundle.getResource("/project_templates");
url = FileLocator.toFileURL(url);
File templatesDir = new File(new URI(url.toString()));
List<ProjectTemplate> list = readTemplates(templatesDir);
templates.addAll(list);
//Load custom templates
String tmp = System.getenv(GWT_TEMPLATE_DIR);
if(tmp != null)
{
list = readTemplates(new File(tmp));
templates.addAll(list);
}
}
catch(Exception ex) {
throw new IOException("Unable to read templates" , ex);
}
return templates;
}
private static List<ProjectTemplate> readTemplates(File templatesDir) throws JDOMException, IOException
{
List<ProjectTemplate> templates = new ArrayList<>();
if(templatesDir.exists())
{
File[] dirs = templatesDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
for(File dir : dirs)
{
File config = new File(dir, "config.xml");
if(config.exists())
{
ProjectTemplate temp = new ProjectTemplate(config);
templates.add(temp);
}
}
}
return templates;
}
/**
* @param config
* @return
* @throws IOException
* @throws JDOMException
*/
private static Document parseTemplate(File config) throws JDOMException, IOException {
SAXBuilder parser = new SAXBuilder();
Document doc = parser.build(config);
doc.getRootElement().getChildren("project");
return doc;
}
/**
* Returns a list of the project names that are build by this template.
* @return
*/
public List<String> getProjectNames(String baseName) {
List<String> names = new ArrayList<>();
List<Element> list = config.getRootElement().getChildren("project");
for(Element el : list)
{
String name = el.getAttributeValue("name");
name = name.replace("_PROJECTNAME_", baseName);
names.add(name);
}
return names;
}
/**
* Creates a copy of the template project into the destination dir.
* @param project Number of the project to copy.
* @param dir
* @throws IOException
*/
public void copyProject(int projectIndex, File dir) throws IOException {
Element project = getProjectElement(projectIndex);
File source = new File(this.dir, project.getAttributeValue("dir"));
FileUtils.copyDirectory(source, dir);
}
private Element getProjectElement(int index)
{
return config.getRootElement().getChildren("project").get(index);
}
public String getName()
{
return config.getRootElement().getChild("name").getTextNormalize();
}
public String getDescription()
{
return config.getRootElement().getChild("description").getTextNormalize();
}
/**
* @param i
* @return
*/
public List<String> getNatureIds(int index) {
Element project = getProjectElement(index);
List<String> natureIds = new ArrayList<>();
natureIds.add(JavaCore.NATURE_ID);
Element natures = project.getChild("natures");
if(natures != null)
{
for(Element nature : project.getChildren("nature"))
{
natureIds.add(nature.getAttributeValue("id"));
}
}
return natureIds;
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(ProjectTemplate o) {
return getName().compareTo(o.getName());
}
}