587
|
1 |
/*
|
|
2 |
* All rights reserved.
|
|
3 |
* This component and the accompanying materials are made available
|
|
4 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
5 |
* which accompanies this distribution, and is available
|
|
6 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
*
|
|
8 |
* Initial Contributors:
|
|
9 |
* Nokia Corporation - initial contribution.
|
|
10 |
*
|
|
11 |
* Contributors:
|
|
12 |
*
|
|
13 |
* Description:
|
|
14 |
*
|
|
15 |
*/
|
|
16 |
|
|
17 |
package com.nokia.helium.ant.data;
|
|
18 |
|
628
|
19 |
import java.io.File;
|
|
20 |
import java.io.IOException;
|
|
21 |
import java.io.Writer;
|
|
22 |
import java.util.ArrayList;
|
|
23 |
import java.util.Collection;
|
|
24 |
import java.util.Collections;
|
|
25 |
import java.util.HashMap;
|
|
26 |
import java.util.Iterator;
|
|
27 |
import java.util.List;
|
|
28 |
import java.util.Map;
|
|
29 |
|
|
30 |
import org.apache.tools.ant.Project;
|
|
31 |
import org.apache.tools.ant.Target;
|
|
32 |
|
|
33 |
import com.nokia.helium.freemarker.WikiMethod;
|
|
34 |
|
587
|
35 |
import freemarker.cache.ClassTemplateLoader;
|
|
36 |
import freemarker.cache.FileTemplateLoader;
|
|
37 |
import freemarker.cache.MultiTemplateLoader;
|
|
38 |
import freemarker.cache.TemplateLoader;
|
|
39 |
import freemarker.template.Configuration;
|
|
40 |
import freemarker.template.DefaultObjectWrapper;
|
|
41 |
import freemarker.template.Template;
|
|
42 |
import freemarker.template.TemplateException;
|
|
43 |
|
|
44 |
/**
|
628
|
45 |
* Reads the current ant project and a fileset and generates a xml file with a summary of targets,
|
|
46 |
* macros and properties.
|
587
|
47 |
*/
|
|
48 |
public class Database {
|
|
49 |
/** The default scope filter if no scope filter is defined. */
|
|
50 |
public static final String DEFAULT_SCOPE = "public";
|
628
|
51 |
public static final Map<String, String> NAMESPACE_MAP;
|
587
|
52 |
|
|
53 |
private Project rootProject;
|
|
54 |
private Map<String, AntFile> antfilesMap;
|
|
55 |
private Map<String, PackageMeta> packagesMap;
|
|
56 |
private String scopeFilter;
|
|
57 |
|
628
|
58 |
static {
|
|
59 |
Map<String, String> tempMap = new HashMap<String, String>();
|
|
60 |
tempMap.put("hlm", "http://www.nokia.com/helium");
|
|
61 |
NAMESPACE_MAP = Collections.unmodifiableMap(tempMap);
|
|
62 |
}
|
587
|
63 |
|
|
64 |
public Database(Project project) throws IOException {
|
|
65 |
this(project, DEFAULT_SCOPE);
|
|
66 |
}
|
|
67 |
|
|
68 |
@SuppressWarnings("unchecked")
|
|
69 |
public Database(Project project, String scopeFilter) throws IOException {
|
|
70 |
this.rootProject = project;
|
|
71 |
this.scopeFilter = scopeFilter;
|
|
72 |
antfilesMap = new HashMap<String, AntFile>();
|
|
73 |
packagesMap = new HashMap<String, PackageMeta>();
|
|
74 |
|
|
75 |
if (project != null) {
|
|
76 |
Map<String, Target> targets = project.getTargets();
|
|
77 |
Iterator<Target> targetsIter = targets.values().iterator();
|
|
78 |
|
|
79 |
while (targetsIter.hasNext()) {
|
|
80 |
Target target = targetsIter.next();
|
|
81 |
String antFilePath = new File(target.getLocation().getFileName()).getCanonicalPath();
|
|
82 |
|
|
83 |
if (!antfilesMap.containsKey(antFilePath)) {
|
|
84 |
addAntFile(antFilePath);
|
|
85 |
}
|
|
86 |
}
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
private void log(String string, int level) {
|
|
91 |
if (rootProject != null) {
|
|
92 |
rootProject.log(string, level);
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
public void addAntFilePaths(List<String> antFilePaths) throws IOException {
|
|
97 |
for (String antFilePath : antFilePaths) {
|
|
98 |
addAntFile(antFilePath);
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
private void addAntFile(String antFilePath) throws IOException {
|
|
103 |
if (!antfilesMap.containsKey(antFilePath)) {
|
|
104 |
log("Adding project to database: " + antFilePath, Project.MSG_DEBUG);
|
|
105 |
AntFile antfile = new AntFile(this, antFilePath, scopeFilter);
|
|
106 |
antfile.setProject(rootProject);
|
|
107 |
antfilesMap.put(antFilePath, antfile);
|
|
108 |
|
|
109 |
// See if project is part of a package
|
|
110 |
checkPackageMembership(antfile);
|
|
111 |
|
|
112 |
// See if any antlibs are defined
|
|
113 |
List<AntFile> antlibFiles = antfile.getAntlibs();
|
|
114 |
for (AntFile antFile2 : antlibFiles) {
|
|
115 |
antfilesMap.put(antFile2.getFile().getCanonicalPath(), antFile2);
|
|
116 |
checkPackageMembership(antFile2);
|
|
117 |
}
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
private void checkPackageMembership(AntFile antfile) throws IOException {
|
|
122 |
RootAntObjectMeta rootObjectMeta = antfile.getRootObjectMeta();
|
|
123 |
String packageStr = rootObjectMeta.getPackage();
|
|
124 |
if (!packagesMap.containsKey(packageStr)) {
|
|
125 |
PackageMeta packageMeta = new PackageMeta(packageStr);
|
|
126 |
packageMeta.setRuntimeProject(rootProject);
|
|
127 |
packagesMap.put(packageStr, packageMeta);
|
|
128 |
}
|
|
129 |
PackageMeta packageMeta = packagesMap.get(packageStr);
|
|
130 |
packageMeta.addObject(rootObjectMeta);
|
|
131 |
}
|
|
132 |
|
|
133 |
public void setScopeFilter(String scopeFilter) {
|
|
134 |
if (!(scopeFilter.equals("public") || scopeFilter.equals("protected") || scopeFilter.equals("private"))) {
|
|
135 |
throw new IllegalArgumentException("Invalid scope value");
|
|
136 |
}
|
|
137 |
this.scopeFilter = scopeFilter;
|
|
138 |
}
|
|
139 |
|
|
140 |
public void toXML(Writer out) throws IOException {
|
|
141 |
// Setup configuration
|
|
142 |
Configuration cfg = new Configuration();
|
|
143 |
TemplateLoader[] loaders = null;
|
|
144 |
ClassTemplateLoader ctl = new ClassTemplateLoader(getClass(), "");
|
|
145 |
File testingDir = new File("src/com/nokia/helium/ant/data/taskdefs");
|
|
146 |
if (testingDir.exists()) {
|
|
147 |
FileTemplateLoader ftl1 = new FileTemplateLoader(testingDir);
|
|
148 |
loaders = new TemplateLoader[] { ftl1, ctl };
|
|
149 |
}
|
|
150 |
else {
|
|
151 |
loaders = new TemplateLoader[] { ctl };
|
|
152 |
}
|
|
153 |
MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
|
|
154 |
cfg.setTemplateLoader(mtl);
|
|
155 |
cfg.setObjectWrapper(new DefaultObjectWrapper());
|
|
156 |
|
|
157 |
// Create the data model
|
|
158 |
List<ProjectMeta> projects = new ArrayList<ProjectMeta>();
|
|
159 |
List<AntlibMeta> antlibs = new ArrayList<AntlibMeta>();
|
|
160 |
for (AntFile antfile : antfilesMap.values()) {
|
|
161 |
antfile.setScope(scopeFilter);
|
|
162 |
RootAntObjectMeta rootObject = antfile.getRootObjectMeta();
|
|
163 |
if (rootObject instanceof ProjectMeta) {
|
|
164 |
projects.add((ProjectMeta) rootObject);
|
|
165 |
}
|
|
166 |
else {
|
|
167 |
antlibs.add((AntlibMeta) rootObject);
|
|
168 |
}
|
|
169 |
}
|
|
170 |
Map<String, Object> root = new HashMap<String, Object>();
|
|
171 |
root.put("projects", projects);
|
|
172 |
root.put("antlibs", antlibs);
|
|
173 |
root.put("packages", getPackages());
|
|
174 |
|
|
175 |
// Add a custom wiki formatting method
|
|
176 |
WikiMethod wikiMethod = new WikiMethod();
|
|
177 |
root.put("wiki", wikiMethod);
|
|
178 |
|
|
179 |
// Process template
|
|
180 |
Template template = cfg.getTemplate("database.xml.ftl");
|
|
181 |
try {
|
|
182 |
template.process(root, out);
|
|
183 |
}
|
|
184 |
catch (TemplateException e) {
|
|
185 |
e.printStackTrace();
|
|
186 |
throw new IOException(e.getMessage());
|
|
187 |
}
|
|
188 |
out.flush();
|
|
189 |
}
|
|
190 |
|
|
191 |
public Collection<AntFile> getAntFiles() {
|
|
192 |
return antfilesMap.values();
|
|
193 |
}
|
|
194 |
|
628
|
195 |
public List<PropertyMeta> getProperties() {
|
587
|
196 |
List<PropertyMeta> propertiesList = new ArrayList<PropertyMeta>();
|
|
197 |
for (AntFile antfile : antfilesMap.values()) {
|
|
198 |
RootAntObjectMeta rootMeta = antfile.getRootObjectMeta();
|
|
199 |
if (rootMeta instanceof ProjectMeta) {
|
|
200 |
propertiesList.addAll(((ProjectMeta) rootMeta).getProperties());
|
|
201 |
}
|
|
202 |
}
|
|
203 |
return propertiesList;
|
|
204 |
}
|
628
|
205 |
|
|
206 |
public List<PropertyCommentMeta> getCommentProperties() {
|
|
207 |
List<PropertyCommentMeta> propertiesList = new ArrayList<PropertyCommentMeta>();
|
|
208 |
for (AntFile antfile : antfilesMap.values()) {
|
|
209 |
RootAntObjectMeta rootMeta = antfile.getRootObjectMeta();
|
|
210 |
if (rootMeta instanceof ProjectMeta) {
|
|
211 |
propertiesList.addAll(((ProjectMeta) rootMeta).getPropertyCommentBlocks());
|
|
212 |
}
|
|
213 |
}
|
|
214 |
return propertiesList;
|
|
215 |
}
|
587
|
216 |
|
|
217 |
public List<PackageMeta> getPackages() throws IOException {
|
|
218 |
List<PackageMeta> packages = new ArrayList<PackageMeta>();
|
|
219 |
for (PackageMeta packageMeta : packagesMap.values()) {
|
|
220 |
packages.add(packageMeta);
|
|
221 |
}
|
|
222 |
return packages;
|
|
223 |
}
|
|
224 |
}
|