0
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.s60tools.imaker.internal.model;
|
|
18 |
|
1
|
19 |
import java.io.File;
|
0
|
20 |
import java.io.IOException;
|
1
|
21 |
import java.io.OutputStream;
|
0
|
22 |
import java.util.ArrayList;
|
|
23 |
import java.util.Iterator;
|
|
24 |
import java.util.List;
|
|
25 |
import java.util.Properties;
|
|
26 |
|
|
27 |
import org.eclipse.core.resources.IFile;
|
|
28 |
import org.eclipse.core.resources.IProject;
|
1
|
29 |
import org.eclipse.core.resources.IResource;
|
0
|
30 |
import org.eclipse.core.runtime.CoreException;
|
|
31 |
import org.eclipse.core.runtime.IPath;
|
1
|
32 |
import org.eclipse.emf.common.util.EList;
|
|
33 |
import org.eclipse.emf.common.util.URI;
|
|
34 |
import org.eclipse.emf.ecore.EObject;
|
|
35 |
import org.eclipse.emf.ecore.resource.Resource;
|
|
36 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
|
0
|
37 |
|
|
38 |
import com.nokia.s60tools.imaker.IMakerKeyConstants;
|
|
39 |
import com.nokia.s60tools.imaker.IMakerUtils;
|
|
40 |
import com.nokia.s60tools.imaker.Messages;
|
|
41 |
import com.nokia.s60tools.imaker.UIVariable;
|
1
|
42 |
import com.nokia.s60tools.imaker.internal.impmodel.ConfigEntry;
|
|
43 |
import com.nokia.s60tools.imaker.internal.impmodel.FileListEntry;
|
|
44 |
import com.nokia.s60tools.imaker.internal.impmodel.ImpConstants;
|
|
45 |
import com.nokia.s60tools.imaker.internal.impmodel.ImpDocument;
|
|
46 |
import com.nokia.s60tools.imaker.internal.impmodel.ImpmodelFactory;
|
|
47 |
import com.nokia.s60tools.imaker.internal.impmodel.OverrideConfiguration;
|
|
48 |
import com.nokia.s60tools.imaker.internal.impmodel.OverrideFiles;
|
|
49 |
import com.nokia.s60tools.imaker.internal.impmodel.Variable;
|
|
50 |
import com.nokia.s60tools.imaker.internal.impmodel.util.BasicTokenizer;
|
|
51 |
import com.nokia.s60tools.imaker.internal.impmodel.util.ImpResourceImpl;
|
0
|
52 |
import com.nokia.s60tools.imaker.internal.managers.ProjectManager;
|
|
53 |
import com.nokia.s60tools.imaker.internal.model.iContent.IContentFactory;
|
1
|
54 |
import com.nokia.s60tools.imaker.internal.model.iContent.IMAGESECTION;
|
0
|
55 |
import com.nokia.s60tools.imaker.internal.model.iContent.IbyEntry;
|
|
56 |
import com.nokia.s60tools.imaker.internal.wrapper.IMakerWrapperPreferences;
|
|
57 |
import com.nokia.s60tools.imaker.internal.wrapper.PlatsimManager;
|
|
58 |
|
|
59 |
public class ImakerProperties extends Properties {
|
|
60 |
public static final String SEPARATOR = ":;";
|
1
|
61 |
public static final String IBYENTRY_FIELDS_SEPARATOR = ";";
|
0
|
62 |
public static final String GENERATED_FILES_FOLDER = "imakerplugin";
|
|
63 |
private static final long serialVersionUID = 1L;
|
|
64 |
private List<UIVariable> variables=null;
|
|
65 |
private String activeFile = null;
|
|
66 |
private boolean used = false;
|
|
67 |
|
|
68 |
/**
|
1
|
69 |
* Create imaker properties from the given file
|
0
|
70 |
* @param file
|
|
71 |
* @return
|
|
72 |
*/
|
|
73 |
public static ImakerProperties createFromFile(IFile file) {
|
1
|
74 |
if(file!=null) {
|
|
75 |
ImpDocument model = getModel(file);
|
|
76 |
return convertModelToProperties(model);
|
|
77 |
}
|
|
78 |
return null;
|
|
79 |
}
|
0
|
80 |
|
1
|
81 |
private static ImakerProperties convertModelToProperties(ImpDocument model) {
|
|
82 |
ImakerProperties properties = new ImakerProperties();
|
|
83 |
if(model!=null) {
|
|
84 |
//main tab basic
|
|
85 |
addProperty(properties, IMakerKeyConstants.PRODUCT, model
|
|
86 |
.getVariable(ImpConstants.TARGET_PRODUCT), model);
|
|
87 |
addProperty(properties, IMakerKeyConstants.TARGET_LIST, model
|
|
88 |
.getVariable(ImpConstants.DEFAULT_GOALS), model);
|
|
89 |
addProperty(properties, IMakerKeyConstants.TYPE, model
|
|
90 |
.getVariable(ImpConstants.TYPE), model);
|
|
91 |
addProperty(properties, IMakerKeyConstants.SYMBOLFILES, model
|
|
92 |
.getVariable(ImpConstants.USE_SYMGEN), model);
|
|
93 |
addProperty(properties, IMakerKeyConstants.VERBOSE, model
|
|
94 |
.getVariable(ImpConstants.VERBOSE), model);
|
|
95 |
|
|
96 |
//platsim variables
|
|
97 |
addProperty(properties, IMakerKeyConstants.PLATSIM_INSTANCE, model
|
|
98 |
.getVariable(ImpConstants.PLATSIM_INSTANCE), model);
|
|
99 |
addProperty(properties, IMakerKeyConstants.PLATSIM_RUN, model
|
|
100 |
.getVariable(ImpConstants.PLATSIM_RUN), model);
|
|
101 |
|
|
102 |
//additional variables
|
|
103 |
EList<Variable> vars = model.getVariables();
|
|
104 |
StringBuffer sb = new StringBuffer();
|
|
105 |
Variable variable;
|
|
106 |
for (int i = 0; i < vars.size(); i++) {
|
|
107 |
variable = vars.get(i);
|
|
108 |
sb.append(variable.getName()+"="+variable.getValue());
|
|
109 |
if(i<vars.size()-1) {
|
|
110 |
sb.append(" ");
|
|
111 |
}
|
|
112 |
}
|
|
113 |
if(!"".equals(sb.toString())) {
|
|
114 |
properties.put(IMakerKeyConstants.ADDITIONAL_PARAMETERS, sb.toString());
|
|
115 |
}
|
|
116 |
|
|
117 |
//files
|
|
118 |
EList<OverrideFiles> files = model.getOrideFiles();
|
|
119 |
List<IbyEntry> ibyEntries = new ArrayList<IbyEntry>();
|
|
120 |
if (!files.isEmpty()) {
|
|
121 |
OverrideFiles of = files.get(0);
|
|
122 |
EList<FileListEntry> entries = of.getEntries();
|
|
123 |
for (int i = 0; i < entries.size(); i++) {
|
|
124 |
FileListEntry fEntry = entries.get(i);
|
|
125 |
EList<ConfigEntry> actions = fEntry.getActions();
|
|
126 |
if (!actions.isEmpty()) {
|
|
127 |
ConfigEntry action = actions.get(0);
|
|
128 |
IbyEntry iby = IContentFactory.eINSTANCE.createIbyEntry();
|
|
129 |
iby.setEnabled(true);
|
|
130 |
iby.setFile(fEntry.getSource());
|
|
131 |
iby.setTarget(fEntry.getTarget());
|
|
132 |
iby.setLocation(IMAGESECTION.get(action.getLocation()));
|
|
133 |
ibyEntries.add(iby);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
}
|
|
137 |
if(!ibyEntries.isEmpty()) {
|
|
138 |
properties.put(IMakerKeyConstants.DEBUGFILES, ibyEntries);
|
0
|
139 |
}
|
|
140 |
}
|
1
|
141 |
return properties;
|
|
142 |
}
|
0
|
143 |
|
1
|
144 |
private static void addProperty(ImakerProperties properties,
|
|
145 |
String key, Variable var, ImpDocument model) {
|
|
146 |
if(var!=null) {
|
|
147 |
model.getVariables().remove(var);
|
|
148 |
properties.put(key, var.getValue());
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
private static ImpDocument getModel(IFile file) {
|
|
153 |
ResourceSetImpl rs = new ResourceSetImpl();
|
|
154 |
|
|
155 |
URI uri = URI.createFileURI(file.getLocation().toFile().getAbsolutePath());
|
|
156 |
Resource resource = rs.createResource(uri);
|
|
157 |
try {
|
|
158 |
resource.load(null);
|
|
159 |
EList<EObject> contents = resource.getContents();
|
|
160 |
return (ImpDocument) contents.get(0);
|
|
161 |
} catch (IOException e) {
|
|
162 |
e.printStackTrace();
|
|
163 |
return null;
|
|
164 |
}
|
0
|
165 |
}
|
|
166 |
|
|
167 |
/**
|
|
168 |
* Create imaker properties from file
|
|
169 |
* @param file
|
|
170 |
* @return
|
|
171 |
*/
|
1
|
172 |
public void saveToFile(File file) {
|
0
|
173 |
if(file!=null) {
|
1
|
174 |
ImpDocument model = getModel();
|
|
175 |
Resource resource = getResource(file);
|
|
176 |
resource.getContents().add(model);
|
0
|
177 |
try {
|
1
|
178 |
resource.save(null);
|
|
179 |
} catch (IOException e) {
|
|
180 |
// TODO log?
|
0
|
181 |
e.printStackTrace();
|
1
|
182 |
}
|
0
|
183 |
}
|
|
184 |
}
|
|
185 |
|
1
|
186 |
private Resource getResource(File file) {
|
|
187 |
ResourceSetImpl rs = new ResourceSetImpl();
|
|
188 |
URI uri = URI.createFileURI(file.getAbsolutePath());
|
|
189 |
Resource resource = rs.createResource(uri);
|
|
190 |
return resource;
|
|
191 |
}
|
|
192 |
|
|
193 |
/**
|
|
194 |
* Constract Impmodel from these properties
|
|
195 |
* @return
|
|
196 |
*/
|
|
197 |
private ImpDocument getModel() {
|
|
198 |
Properties clone = (Properties) clone();
|
|
199 |
ImpmodelFactory factory = ImpmodelFactory.eINSTANCE;
|
|
200 |
ImpDocument doc = factory.createImpDocument();
|
|
201 |
|
|
202 |
//save variables
|
|
203 |
addBasicVariables(clone, doc,factory);
|
|
204 |
addAdditionalVariables(clone, doc,factory);
|
|
205 |
|
|
206 |
//save platsim variables
|
|
207 |
addPlatsimVariables(clone, doc,factory);
|
|
208 |
|
|
209 |
//save override files
|
|
210 |
Object oEntries = clone.remove(IMakerKeyConstants.DEBUGFILES);
|
|
211 |
if(oEntries!=null) {
|
|
212 |
List<IbyEntry> entries = (List<IbyEntry>) oEntries;
|
|
213 |
OverrideFiles files = factory.createOverrideFiles();
|
|
214 |
OverrideConfiguration confs = factory.createOverrideConfiguration();
|
|
215 |
for (IbyEntry ibyEntry : entries) {
|
|
216 |
if(!ibyEntry.isEnabled()) {
|
|
217 |
continue; //Notice skipping disabled entries
|
|
218 |
}
|
|
219 |
FileListEntry fileEntry = factory.createFileListEntry();
|
|
220 |
ConfigEntry configEntry = factory.createConfigEntry();
|
|
221 |
fileEntry.setSource(ibyEntry.getFile());
|
|
222 |
fileEntry.setTarget(ibyEntry.getTarget());
|
|
223 |
files.getEntries().add(fileEntry);
|
|
224 |
|
|
225 |
//
|
|
226 |
configEntry.setTarget(ibyEntry.getTarget());
|
|
227 |
configEntry.setAction("replace-add");
|
|
228 |
configEntry.setLocation(ibyEntry.getLocation().getName().toLowerCase());
|
|
229 |
confs.getEntries().add(configEntry);
|
|
230 |
}
|
|
231 |
if(!files.getEntries().isEmpty()) {
|
|
232 |
doc.getOrideFiles().add(files);
|
|
233 |
doc.getOrideConfs().add(confs);
|
|
234 |
}
|
|
235 |
}
|
|
236 |
|
|
237 |
//save override confs
|
|
238 |
return doc;
|
|
239 |
}
|
|
240 |
|
|
241 |
private void addPlatsimVariables(Properties clone, ImpDocument doc,
|
|
242 |
ImpmodelFactory factory) {
|
|
243 |
addVariable(doc, factory, ImpConstants.PLATSIM_INSTANCE, (String) clone
|
|
244 |
.remove(IMakerKeyConstants.PLATSIM_INSTANCE));
|
|
245 |
addVariable(doc, factory, ImpConstants.PLATSIM_RUN, (String) clone
|
|
246 |
.remove(IMakerKeyConstants.PLATSIM_RUN));
|
|
247 |
}
|
|
248 |
|
|
249 |
private void addAdditionalVariables(Properties clone, ImpDocument doc,
|
|
250 |
ImpmodelFactory factory) {
|
|
251 |
String adds = (String) clone.remove(IMakerKeyConstants.ADDITIONAL_PARAMETERS);
|
|
252 |
if(adds==null) return;
|
|
253 |
BasicTokenizer bt = new BasicTokenizer(adds);
|
|
254 |
while (bt.hasMoreTokens()) {
|
|
255 |
String token = bt.nextToken();
|
|
256 |
String[] comps = token.split("=");
|
|
257 |
if(comps.length==2) {
|
|
258 |
int start = 0;
|
|
259 |
int end = comps[1].length();
|
|
260 |
if(comps[1].charAt(0)=='"') start++;
|
|
261 |
if(comps[1].charAt(comps[1].length()-1)=='"') end--;
|
|
262 |
addVariable(doc, factory, comps[0], comps[1].substring(start,end));
|
|
263 |
}
|
|
264 |
}
|
|
265 |
}
|
|
266 |
|
|
267 |
private void addBasicVariables(Properties clone, ImpDocument doc, ImpmodelFactory factory) {
|
|
268 |
addVariable(doc, factory, ImpConstants.TARGET_PRODUCT, (String) clone
|
|
269 |
.remove(IMakerKeyConstants.PRODUCT));
|
|
270 |
addVariable(doc, factory, ImpConstants.DEFAULT_GOALS, (String) clone
|
|
271 |
.remove(IMakerKeyConstants.TARGET_LIST));
|
|
272 |
addVariable(doc, factory, ImpConstants.TYPE, (String) clone
|
|
273 |
.remove(IMakerKeyConstants.TYPE));
|
|
274 |
addVariable(doc, factory, ImpConstants.USE_SYMGEN, (String) clone
|
|
275 |
.remove(IMakerKeyConstants.SYMBOLFILES));
|
|
276 |
addVariable(doc, factory, ImpConstants.VERBOSE, (String) clone
|
|
277 |
.remove(IMakerKeyConstants.VERBOSE));
|
|
278 |
// addVariable(doc, factory, ImpConstants.VERBOSE, (String) clone.remove(IMakerKeyConstants.VERBOSE));
|
|
279 |
// addVariable(doc, factory, ImpConstants.VERBOSE, (String) clone.remove(IMakerKeyConstants.VERBOSE));
|
|
280 |
}
|
|
281 |
|
|
282 |
private void addVariable(ImpDocument doc, ImpmodelFactory factory, String name, String value) {
|
|
283 |
Variable var;
|
|
284 |
if (value!=null) {
|
|
285 |
var = factory.createVariable();
|
|
286 |
var.setName(name);
|
|
287 |
var.setValue(value);
|
|
288 |
doc.getVariables().add(var);
|
|
289 |
}
|
|
290 |
}
|
|
291 |
|
0
|
292 |
public ImakerProperties() {}
|
|
293 |
|
|
294 |
|
|
295 |
/**
|
|
296 |
* Parse the contents of this properties file and create imaker command that is executable
|
|
297 |
* @param selectedProject
|
|
298 |
* @param workdir Place where the temporary iby/mk files will be placed
|
|
299 |
* @return
|
|
300 |
*/
|
|
301 |
public List<String> parseImakerCommand(IProject selectedProject, IPath workdir) {
|
|
302 |
String drive = IMakerUtils.getProjectRootLocation(selectedProject);
|
|
303 |
ArrayList<String> command = new ArrayList<String>();
|
|
304 |
|
|
305 |
String makefile = (String) get(IMakerKeyConstants.MAKEFILE);
|
|
306 |
if(makefile!=null) {
|
|
307 |
appendCommand(command,IMakerWrapperPreferences.ARG_FETCH_CONFIGURATION);
|
|
308 |
appendCommand(command,makefile);
|
|
309 |
}
|
|
310 |
addAdditionalParameters(command,(String) get(IMakerKeyConstants.TARGET_LIST)," ");
|
|
311 |
String platsim = (String) get(IMakerKeyConstants.PLATSIM_INSTANCE);
|
|
312 |
if(platsim!=null) {
|
|
313 |
command.add(IMakerKeyConstants.PLATSIM_INSTANCE +"="+parse(platsim,drive));
|
|
314 |
String run = (String)get(IMakerKeyConstants.PLATSIM_RUN);
|
|
315 |
if(run!=null&&run.equals("true")) {
|
|
316 |
command.add(IMakerKeyConstants.PLATSIM_RUN +"=1");
|
|
317 |
}
|
|
318 |
}
|
|
319 |
addAdditionalParameters(command,(String) get(IMakerKeyConstants.ADDITIONAL_PARAMETERS),SEPARATOR);
|
|
320 |
addAdditionalParameters(command, (String) get(IMakerKeyConstants.MODIFIED_SETTINGS),SEPARATOR);
|
|
321 |
String type = (String) get(IMakerKeyConstants.TYPE);
|
|
322 |
command.add(IMakerWrapperPreferences.TYPE+"="+type);
|
|
323 |
String verb = (String) get(IMakerKeyConstants.VERBOSE);
|
|
324 |
if(verb!=null) {
|
|
325 |
command.add(Messages.getString("Flashmenu.9"));
|
|
326 |
}
|
|
327 |
|
|
328 |
// parse iby files and makefile
|
|
329 |
String str = (String)get(IMakerKeyConstants.DEBUGFILES);
|
|
330 |
if(str!=null&&!str.equals("")) {
|
|
331 |
List<IbyEntry> entries = new ArrayList<IbyEntry>();
|
|
332 |
String[] entriesStr = str.split(ImakerProperties.SEPARATOR);
|
|
333 |
for (int i = 0; i < entriesStr.length; i++) {
|
|
334 |
String entryStr = entriesStr[i];
|
|
335 |
if(entryStr!=null&&!entryStr.equals("")) {
|
|
336 |
IbyEntry entry = IContentFactory.eINSTANCE.createEntryFromString(entryStr);
|
|
337 |
if(entry.isEnabled()) {
|
|
338 |
entries.add(entry);
|
|
339 |
}
|
|
340 |
}
|
|
341 |
}
|
|
342 |
ProjectManager pm = new ProjectManager(selectedProject);
|
|
343 |
IPath mkfile = IMakerUtils.createIbyFiles(entries, pm, workdir);
|
|
344 |
if(mkfile!=null) {
|
|
345 |
appendCommand(command,IMakerWrapperPreferences.ARG_FETCH_CONFIGURATION);
|
|
346 |
appendCommand(command,"\""+mkfile.toOSString()+"\"");
|
|
347 |
}
|
|
348 |
}
|
|
349 |
|
|
350 |
return command;
|
|
351 |
}
|
|
352 |
|
|
353 |
private String parse(String instance, String env) {
|
|
354 |
if(instance==null) {
|
|
355 |
instance="1";
|
|
356 |
}
|
|
357 |
if(instance.equals(Messages.getString("PlatsimTab.0"))) {
|
|
358 |
PlatsimManager pm = new PlatsimManager(env);
|
|
359 |
int max = 1;
|
|
360 |
List<String> instances = pm.getInstances();
|
|
361 |
for (String ins : instances) {
|
|
362 |
int intIns = Integer.parseInt(ins);
|
|
363 |
if(intIns>max) max = intIns;
|
|
364 |
}
|
|
365 |
return String.valueOf(max+1);
|
|
366 |
}
|
|
367 |
return instance;
|
|
368 |
}
|
|
369 |
|
|
370 |
private void addAdditionalParameters(ArrayList<String> command,
|
|
371 |
String adds, String sep) {
|
|
372 |
if(adds==null||adds.equals("")) { //$NON-NLS-1$
|
|
373 |
return;
|
|
374 |
}
|
|
375 |
String[] parts = adds.split(sep); //$NON-NLS-1$
|
|
376 |
for (int i = 0; i < parts.length; i++) {
|
|
377 |
String part = parts[i];
|
|
378 |
if(!part.equals("")) { //$NON-NLS-1$
|
|
379 |
appendCommand(command, part);
|
|
380 |
}
|
|
381 |
}
|
|
382 |
}
|
|
383 |
|
|
384 |
private void appendCommand(ArrayList<String> command, String value) {
|
|
385 |
if((value!=null)&&!(value.equals(""))) { //$NON-NLS-1$
|
|
386 |
command.add(value.trim());
|
|
387 |
}
|
|
388 |
}
|
|
389 |
|
|
390 |
private void updateVariables() {
|
|
391 |
if(variables==null) {
|
|
392 |
return;
|
|
393 |
}
|
|
394 |
List<UIVariable> includeVariables = IMakerUtils.getCommandlineIncludeVariables(variables);
|
|
395 |
List<String> variablesToStrings = IMakerUtils.convertVariablesToStrings(includeVariables);
|
|
396 |
|
|
397 |
StringBuilder settings = new StringBuilder();
|
|
398 |
|
|
399 |
for (Iterator<String> iterator = variablesToStrings.iterator(); iterator
|
|
400 |
.hasNext();) {
|
|
401 |
String setting = iterator.next();
|
|
402 |
settings.append(setting+SEPARATOR); //$NON-NLS-1$
|
|
403 |
}
|
|
404 |
|
|
405 |
String string = settings.toString();
|
|
406 |
if(!string.equals("")) { //$NON-NLS-1$
|
|
407 |
put(IMakerKeyConstants.MODIFIED_SETTINGS, string);
|
|
408 |
}
|
|
409 |
}
|
|
410 |
|
|
411 |
public void setVariables(List<UIVariable> variables) {
|
|
412 |
this.variables = variables;
|
|
413 |
updateVariables();
|
|
414 |
}
|
|
415 |
|
|
416 |
public void setActiveFile(String path) {
|
|
417 |
this.activeFile = path;
|
|
418 |
}
|
|
419 |
|
|
420 |
public String getActiveFile() {
|
|
421 |
return activeFile;
|
|
422 |
}
|
|
423 |
|
|
424 |
public void setUsed(boolean b) {
|
|
425 |
this.used = b;
|
|
426 |
}
|
|
427 |
|
|
428 |
public boolean isUsed() {
|
|
429 |
return this.used;
|
|
430 |
}
|
1
|
431 |
|
|
432 |
@Override
|
|
433 |
public synchronized void store(OutputStream out, String comments)
|
|
434 |
throws IOException {
|
|
435 |
ImpDocument model = getModel();
|
|
436 |
URI uri = URI.createFileURI("sample.imp");
|
|
437 |
ImpResourceImpl res = new ImpResourceImpl(uri);
|
|
438 |
|
|
439 |
res.getContents().add(model);
|
|
440 |
|
|
441 |
res.save(out);
|
|
442 |
}
|
|
443 |
|
|
444 |
|
0
|
445 |
}
|