1
/*******************************************************************************
2
* Copyright (c) 2001, 2008 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Rational Software - initial implementation
10
* Anton Leherbauer (Wind River Systems)
11
* Jens Elmenthaler (Verigy) - http://bugs.eclipse.org/235586
12
*******************************************************************************/
13
package org.eclipse.cdt.internal.corext.codemanipulation;
15
import java.io.IOException;
16
import java.util.ArrayList;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.UUID;
21
import org.eclipse.core.resources.IFile;
22
import org.eclipse.core.resources.IProject;
23
import org.eclipse.core.resources.ProjectScope;
24
import org.eclipse.core.runtime.CoreException;
25
import org.eclipse.core.runtime.Path;
26
import org.eclipse.core.runtime.Platform;
27
import org.eclipse.core.runtime.Status;
28
import org.eclipse.core.runtime.preferences.IScopeContext;
29
import org.eclipse.core.runtime.preferences.InstanceScope;
30
import org.eclipse.jface.text.BadLocationException;
31
import org.eclipse.jface.text.Document;
32
import org.eclipse.jface.text.IDocument;
33
import org.eclipse.jface.text.IRegion;
34
import org.eclipse.jface.text.templates.Template;
35
import org.eclipse.jface.text.templates.TemplateBuffer;
36
import org.eclipse.jface.text.templates.TemplateContext;
37
import org.eclipse.jface.text.templates.TemplateException;
38
import org.eclipse.jface.text.templates.TemplateVariable;
39
import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
40
import org.eclipse.swt.SWT;
41
import org.eclipse.text.edits.DeleteEdit;
42
import org.eclipse.text.edits.InsertEdit;
43
import org.eclipse.text.edits.MalformedTreeException;
44
import org.eclipse.text.edits.MultiTextEdit;
46
import org.eclipse.cdt.core.CCorePlugin;
47
import org.eclipse.cdt.core.CCorePreferenceConstants;
48
import org.eclipse.cdt.core.model.CModelException;
49
import org.eclipse.cdt.core.model.CoreModel;
50
import org.eclipse.cdt.core.model.IBuffer;
51
import org.eclipse.cdt.core.model.ICElement;
52
import org.eclipse.cdt.core.model.ICProject;
53
import org.eclipse.cdt.core.model.ITranslationUnit;
54
import org.eclipse.cdt.ui.CUIPlugin;
55
import org.eclipse.cdt.ui.PreferenceConstants;
57
import org.eclipse.cdt.internal.corext.template.c.CodeTemplateContext;
58
import org.eclipse.cdt.internal.corext.template.c.CodeTemplateContextType;
59
import org.eclipse.cdt.internal.corext.template.c.FileTemplateContext;
60
import org.eclipse.cdt.internal.corext.template.c.FileTemplateContextType;
61
import org.eclipse.cdt.internal.corext.util.Strings;
63
import org.eclipse.cdt.internal.ui.viewsupport.ProjectTemplateStore;
65
public class StubUtility {
67
private static final String[] EMPTY= new String[0];
69
private StubUtility() {
73
* Don't use this method directly, use CodeGeneration.
74
* @see org.eclipse.cdt.ui.CodeGeneration#getHeaderFileContent(ITranslationUnit, String, String, String)
76
public static String getHeaderFileContent(ITranslationUnit tu, String fileComment, String typeComment, String declarations, String lineDelimiter) throws CoreException {
77
return getHeaderFileContent(getDefaultFileTemplate(tu), tu, fileComment, typeComment, declarations, lineDelimiter);
81
* Don't use this method directly, use CodeGeneration.
82
* @see org.eclipse.cdt.ui.CodeGeneration#getHeaderFileContent(Template, ITranslationUnit, String, String, String)
84
public static String getHeaderFileContent(Template template, ITranslationUnit tu, String fileComment, String typeComment, String declarations, String lineDelimiter) throws CoreException {
85
if (template == null) {
88
ICProject project= tu.getCProject();
89
CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
90
context.setTranslationUnitVariables(tu);
91
context.setVariable(CodeTemplateContextType.TYPE_COMMENT, typeComment != null ? typeComment : ""); //$NON-NLS-1$
92
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
93
context.setVariable(CodeTemplateContextType.DECLARATIONS, declarations != null ? declarations : ""); //$NON-NLS-1$
94
context.setVariable(CodeTemplateContextType.TYPENAME, new Path(tu.getElementName()).removeFileExtension().toString());
95
String includeGuardSymbol= generateIncludeGuardSymbol(tu.getElementName(), project);
96
context.setVariable(CodeTemplateContextType.INCLUDE_GUARD_SYMBOL, includeGuardSymbol != null ? includeGuardSymbol : ""); //$NON-NLS-1$
98
String[] fullLine= { CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT, CodeTemplateContextType.DECLARATIONS };
99
return evaluateTemplate(context, template, fullLine);
103
* Don't use this method directly, use CodeGeneration.
104
* @see org.eclipse.cdt.ui.CodeGeneration#getBodyFileContent(ITranslationUnit, String, String, String)
106
public static String getBodyFileContent(ITranslationUnit tu, String fileComment, String typeComment, String declarations, String lineDelimiter) throws CoreException {
107
return getBodyFileContent(getDefaultFileTemplate(tu), tu, fileComment, typeComment, declarations, lineDelimiter);
111
* Don't use this method directly, use CodeGeneration.
112
* @see org.eclipse.cdt.ui.CodeGeneration#getBodyFileContent(Template, ITranslationUnit, String, String, String)
114
public static String getBodyFileContent(Template template, ITranslationUnit tu, String fileComment, String typeComment, String declarations, String lineDelimiter) throws CoreException {
115
if (template == null) {
118
ICProject project= tu.getCProject();
119
CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
120
context.setTranslationUnitVariables(tu);
121
context.setVariable(CodeTemplateContextType.TYPE_COMMENT, typeComment != null ? typeComment : ""); //$NON-NLS-1$
122
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
123
context.setVariable(CodeTemplateContextType.DECLARATIONS, declarations != null ? declarations : ""); //$NON-NLS-1$
125
String[] fullLine= { CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT, CodeTemplateContextType.DECLARATIONS };
126
return evaluateTemplate(context, template, fullLine);
129
public static String getFileContent(Template template, IFile file, String lineDelimiter) throws CoreException {
130
FileTemplateContext context= new FileTemplateContext(template.getContextTypeId(), lineDelimiter);
131
String fileComment= getFileComment(file, lineDelimiter);
132
context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
133
ICProject cproject = CoreModel.getDefault().create(file.getProject());
134
String includeGuardSymbol= generateIncludeGuardSymbol(file.getName(), cproject);
135
context.setVariable(CodeTemplateContextType.INCLUDE_GUARD_SYMBOL, includeGuardSymbol != null ? includeGuardSymbol : ""); //$NON-NLS-1$
136
context.setResourceVariables(file);
137
String[] fullLine= { CodeTemplateContextType.FILE_COMMENT };
138
return evaluateTemplate(context, template, fullLine);
142
* Don't use this method directly, use CodeGeneration.
144
public static String getMethodBodyContent(ICProject project, String typeName, String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
145
String templateId= CodeTemplateContextType.METHODSTUB_ID;
146
return getMethodBodyContent(templateId, project, typeName, methodName, bodyStatement, lineDelimiter);
150
* Don't use this method directly, use CodeGeneration.
152
public static String getConstructorBodyContent(ICProject project, String typeName, String bodyStatement, String lineDelimiter) throws CoreException {
153
String templateId= CodeTemplateContextType.CONSTRUCTORSTUB_ID;
154
return getMethodBodyContent(templateId, project, typeName, typeName, bodyStatement, lineDelimiter);
158
* Don't use this method directly, use CodeGeneration.
160
public static String getDestructorBodyContent(ICProject project, String typeName, String bodyStatement, String lineDelimiter) throws CoreException {
161
String templateId= CodeTemplateContextType.DESTRUCTORSTUB_ID;
162
return getMethodBodyContent(templateId, project, typeName, "~"+typeName, bodyStatement, lineDelimiter); //$NON-NLS-1$
166
* Don't use this method directly, use CodeGeneration.
168
public static String getMethodBodyContent(String templateId, ICProject project, String typeName, String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
169
Template template= getCodeTemplate(templateId, project);
170
if (template == null) {
171
return bodyStatement;
173
CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
174
context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
175
context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
176
context.setVariable(CodeTemplateContextType.BODY_STATEMENT, bodyStatement != null ? bodyStatement : ""); //$NON-NLS-1$
177
String str= evaluateTemplate(context, template, new String[] { CodeTemplateContextType.BODY_STATEMENT });
178
if (str == null && !Strings.containsOnlyWhitespaces(bodyStatement)) {
179
return bodyStatement;
185
* Don't use this method directly, use CodeGeneration.
186
* @see org.eclipse.cdt.ui.CodeGeneration#getFileComment(ITranslationUnit, String)
188
public static String getFileComment(ITranslationUnit tu, String lineDelimiter) throws CoreException {
189
Template template= getCodeTemplate(CodeTemplateContextType.FILECOMMENT_ID, tu.getCProject());
190
if (template == null) {
194
ICProject project= tu.getCProject();
195
CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
196
context.setTranslationUnitVariables(tu);
197
return evaluateTemplate(context, template);
200
private static String getFileComment(IFile file, String lineDelimiter) throws CoreException {
201
Template template= getCodeTemplate(CodeTemplateContextType.FILECOMMENT_ID, file.getProject());
202
if (template == null) {
206
FileTemplateContext context= new FileTemplateContext(template.getContextTypeId(), lineDelimiter);
207
context.setResourceVariables(file);
208
return evaluateTemplate(context, template);
212
* Don't use this method directly, use CodeGeneration.
213
* @see org.eclipse.cdt.ui.CodeGeneration#getClassComment(ITranslationUnit, String, String)
215
public static String getClassComment(ITranslationUnit tu, String typeQualifiedName, String lineDelimiter) throws CoreException {
216
Template template= getCodeTemplate(CodeTemplateContextType.TYPECOMMENT_ID, tu.getCProject());
217
if (template == null) {
221
ICProject project= tu.getCProject();
222
CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
223
context.setTranslationUnitVariables(tu);
224
context.setVariable(CodeTemplateContextType.TYPENAME, typeQualifiedName);
225
return evaluateTemplate(context, template);
229
* Don't use this method directly, use CodeGeneration.
230
* @see org.eclipse.cdt.ui.CodeGeneration#getMethodComment(ITranslationUnit, String, String, String[], String[], String, String)
232
public static String getMethodComment(ITranslationUnit tu, String typeName, String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig, String lineDelimiter) throws CoreException {
233
String templateId= CodeTemplateContextType.METHODCOMMENT_ID;
234
return getMethodComment(templateId, tu, typeName, methodName, paramNames, excTypeSig, retTypeSig, lineDelimiter);
238
* Don't use this method directly, use CodeGeneration.
239
* @see org.eclipse.cdt.ui.CodeGeneration#getConstructorComment(ITranslationUnit, String, String[], String[], String)
241
public static String getConstructorComment(ITranslationUnit tu, String typeName, String[] paramNames, String[] excTypeSig, String lineDelimiter) throws CoreException {
242
String templateId= CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
243
return getMethodComment(templateId, tu, typeName, typeName, paramNames, excTypeSig, null, lineDelimiter);
247
* Don't use this method directly, use CodeGeneration.
248
* @see org.eclipse.cdt.ui.CodeGeneration#getDestructorComment(ITranslationUnit, String, String[], String)
250
public static String getDestructorComment(ITranslationUnit tu, String typeName, String[] excTypeSig, String lineDelimiter) throws CoreException {
251
String templateId= CodeTemplateContextType.DESTRUCTORCOMMENT_ID;
252
return getMethodComment(templateId, tu, typeName, "~"+typeName, EMPTY, excTypeSig, null, lineDelimiter); //$NON-NLS-1$
256
* Don't use this method directly, use CodeGeneration.
257
* @see org.eclipse.cdt.ui.CodeGeneration#getMethodComment(ITranslationUnit, String, String, String[], String[], String, String)
259
public static String getMethodComment(String templateId, ITranslationUnit tu, String typeName, String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig, String lineDelimiter) throws CoreException {
260
Template template= getCodeTemplate(templateId, tu.getCProject());
261
if (template == null) {
264
CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), tu.getCProject(), lineDelimiter);
265
context.setTranslationUnitVariables(tu);
266
context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
267
context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
269
if (retTypeSig != null) {
270
context.setVariable(CodeTemplateContextType.RETURN_TYPE, retTypeSig);
272
context.setTranslationUnitVariables(tu);
273
TemplateBuffer buffer;
275
buffer= context.evaluate(template);
276
} catch (BadLocationException e) {
277
throw new CoreException(Status.CANCEL_STATUS);
278
} catch (TemplateException e) {
279
throw new CoreException(Status.CANCEL_STATUS);
281
if (buffer == null) {
285
// TODO doc comment tags
287
String str= buffer.getString();
288
if (Strings.containsOnlyWhitespaces(str)) {
295
// remove lines for empty variables, prefix multi-line variables
296
private static String fixFullLineVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException {
297
IDocument doc= new Document(buffer.getString());
298
int nLines= doc.getNumberOfLines();
299
MultiTextEdit edit= new MultiTextEdit();
300
HashSet<Integer> removedLines= new HashSet<Integer>();
301
for (int i= 0; i < variables.length; i++) {
302
TemplateVariable position= findVariable(buffer, variables[i]);
303
if (position == null) {
306
if (position.getLength() > 0) {
307
int[] offsets= position.getOffsets();
308
for (int j= 0; j < offsets.length; j++) {
309
final int offset = offsets[j] ;
311
int startLine= doc.getLineOfOffset(offset);
312
int startOffset= doc.getLineOffset(startLine);
313
int endLine= doc.getLineOfOffset(offset + position.getLength());
314
String prefix= doc.get(startOffset, offset - startOffset);
315
if (prefix.length() > 0 && startLine < endLine) {
316
for (int line= startLine + 1; line <= endLine; ++line) {
317
int lineOffset= doc.getLineOffset(line);
318
edit.addChild(new InsertEdit(lineOffset, prefix));
321
} catch (BadLocationException exc) {
326
int[] offsets= position.getOffsets();
327
for (int k= 0; k < offsets.length; k++) {
328
int line= doc.getLineOfOffset(offsets[k]);
329
IRegion lineInfo= doc.getLineInformation(line);
330
int offset= lineInfo.getOffset();
331
String str= doc.get(offset, lineInfo.getLength());
332
if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) {
333
int nextStart= doc.getLineOffset(line + 1);
334
int length= nextStart - offset;
335
edit.addChild(new DeleteEdit(offset, length));
345
private static TemplateVariable findVariable(TemplateBuffer buffer, String variable) {
346
TemplateVariable[] positions= buffer.getVariables();
347
for (int i= 0; i < positions.length; i++) {
348
TemplateVariable curr= positions[i];
349
if (variable.equals(curr.getType())) {
356
private static String evaluateTemplate(TemplateContext context, Template template) throws CoreException {
357
TemplateBuffer buffer;
359
buffer= context.evaluate(template);
360
} catch (BadLocationException e) {
361
throw new CoreException(Status.CANCEL_STATUS);
362
} catch (TemplateException e) {
363
throw new CoreException(Status.CANCEL_STATUS);
367
String str= buffer.getString();
368
if (Strings.containsOnlyWhitespaces(str)) {
374
private static String evaluateTemplate(TemplateContext context, Template template, String[] fullLineVariables) throws CoreException {
375
TemplateBuffer buffer;
377
buffer= context.evaluate(template);
380
String str= fixFullLineVariables(buffer, fullLineVariables);
381
if (Strings.containsOnlyWhitespaces(str)) {
385
} catch (BadLocationException e) {
386
throw new CoreException(Status.CANCEL_STATUS);
387
} catch (TemplateException e) {
388
throw new CoreException(Status.CANCEL_STATUS);
393
* Returns the line delimiter which is used in the specified project.
395
* @param project the C project, or <code>null</code>
396
* @return the used line delimiter
398
public static String getLineDelimiterUsed(ICProject project) {
399
return getProjectLineDelimiter(project);
402
private static String getProjectLineDelimiter(ICProject cProject) {
403
IProject project= null;
404
if (cProject != null)
405
project= cProject.getProject();
407
String lineDelimiter= getLineDelimiterPreference(project);
408
if (lineDelimiter != null)
409
return lineDelimiter;
411
return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
414
public static String getLineDelimiterPreference(IProject project) {
415
IScopeContext[] scopeContext;
416
if (project != null) {
417
// project preference
418
scopeContext= new IScopeContext[] { new ProjectScope(project) };
419
String lineDelimiter= Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext);
420
if (lineDelimiter != null)
421
return lineDelimiter;
423
// workspace preference
424
scopeContext= new IScopeContext[] { new InstanceScope() };
425
String platformDefault= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
426
return Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, platformDefault, scopeContext);
430
* Examines a string and returns the first line delimiter found.
432
public static String getLineDelimiterUsed(ICElement elem) throws CModelException {
433
if (elem == null) return ""; //$NON-NLS-1$
435
ITranslationUnit cu= (ITranslationUnit) elem.getAncestor(ICElement.C_UNIT);
436
if (cu != null && cu.exists()) {
437
IBuffer buf= cu.getBuffer();
438
int length= buf.getLength();
439
for (int i= 0; i < length; i++) {
440
char ch= buf.getChar(i);
442
if (i + 1 < length) {
443
if (buf.getChar(i + 1) == SWT.LF) {
444
return "\r\n"; //$NON-NLS-1$
447
return "\r"; //$NON-NLS-1$
448
} else if (ch == SWT.LF) {
449
return "\n"; //$NON-NLS-1$
453
return getProjectLineDelimiter(elem.getCProject());
458
* Get the default task tag for the given project.
461
* @return the default task tag
463
public static String getTodoTaskTag(ICProject project) {
464
String markers= null;
465
if (project == null) {
466
markers= CCorePlugin.getOption(CCorePreferenceConstants.TODO_TASK_TAGS);
468
markers= project.getOption(CCorePreferenceConstants.TODO_TASK_TAGS, true);
471
if (markers != null && markers.length() > 0) {
472
int idx= markers.indexOf(',');
476
return markers.substring(0, idx);
478
return CCorePreferenceConstants.DEFAULT_TASK_TAG;
481
public static boolean doAddComments(ICProject project) {
482
return PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_ADD_COMMENTS, project, false);
485
private static Template getDefaultFileTemplate(ITranslationUnit tu) {
486
String templateId= null;
487
if (tu.isASMLanguage()) {
488
templateId= CodeTemplateContextType.ASM_SOURCEFILE_ID;
489
} else if (tu.isCXXLanguage()) {
490
if (tu.isHeaderUnit()) {
491
templateId= CodeTemplateContextType.CPP_HEADERFILE_ID;
493
templateId= CodeTemplateContextType.CPP_SOURCEFILE_ID;
495
} else if (tu.isCLanguage()) {
496
if (tu.isHeaderUnit()) {
497
templateId= CodeTemplateContextType.C_HEADERFILE_ID;
499
templateId= CodeTemplateContextType.C_SOURCEFILE_ID;
502
return getCodeTemplate(templateId, tu.getCProject());
505
private static Template getCodeTemplate(String id, ICProject cProject) {
506
return getCodeTemplate(id, cProject != null ? cProject.getProject() : null);
508
private static Template getCodeTemplate(String id, IProject project) {
510
return CUIPlugin.getDefault().getCodeTemplateStore().findTemplateById(id);
511
ProjectTemplateStore projectStore= new ProjectTemplateStore(project);
514
} catch (IOException e) {
517
return projectStore.findTemplateById(id);
520
private static int getIncludeGuardScheme(ICProject cproject) {
521
return PreferenceConstants.getPreference(
522
PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME,
524
PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_NAME);
527
private static String generateIncludeGuardSymbol(String fileName, ICProject cproject) {
529
int preferenceValue = getIncludeGuardScheme(cproject);
531
switch (preferenceValue) {
532
case PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_NAME:
533
if (fileName != null) {
534
return generateIncludeGuardSymbolFromFileName(fileName);
539
case PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_UUID:
540
return generateIncludeGuardSymbolFromUUID();
543
CUIPlugin.log("Unknown preference value " + preferenceValue + "for include guard scheme", null); //$NON-NLS-1$ //$NON-NLS-2$
548
private static String generateIncludeGuardSymbolFromFileName(String fileName) {
549
String name = fileName;
550
//convert to upper case and remove invalid characters
551
//eg convert foo.h --> FOO_H_
552
StringBuffer buf = new StringBuffer();
553
// Do not do this, leading underscores are discouraged by the std.
555
for (int i = 0; i < name.length(); ++i) {
556
char ch = name.charAt(i);
557
if (Character.isLetterOrDigit(ch)) {
558
buf.append(Character.toUpperCase(ch));
559
} else if (ch == '.' || ch == '_') {
564
return buf.toString();
567
private static String generateIncludeGuardSymbolFromUUID() {
568
String uuid = UUID.randomUUID().toString();
570
// 1) Make sure the guard always starts with a letter.
571
// 2) Convert to upper case and remove invalid characters
574
// 067e6162-3b6f-4ae2-a171-2470b63dff00 to
575
// H067E6162-3b6F-4AE2-A171-2470B63DFF00
576
StringBuffer buf = new StringBuffer();
580
for (int i = 0; i < uuid.length(); ++i) {
581
char ch = uuid.charAt(i);
582
if (Character.isLetterOrDigit(ch)) {
583
buf.append(Character.toUpperCase(ch));
589
return buf.toString();
593
* Get a set of file templates for the given content types.
595
* @param contentTypes the list of content types
596
* @param project the project or <code>null</code>
597
* @return an array of templates
599
public static Template[] getFileTemplatesForContentTypes(String[] contentTypes, IProject project) {
600
if (contentTypes == null || contentTypes.length == 0) {
601
return new Template[0];
603
TemplatePersistenceData[] templateDatas;
604
if (project == null) {
605
templateDatas= CUIPlugin.getDefault().getCodeTemplateStore().getTemplateData(true);
607
ProjectTemplateStore projectStore= new ProjectTemplateStore(project.getProject());
610
} catch (IOException e) {
613
templateDatas= projectStore.getTemplateData();
615
List<Template> result= new ArrayList<Template>();
616
for (int j = 0; j < contentTypes.length; j++) {
617
for (int i = 0; i < templateDatas.length; i++) {
618
Template template = templateDatas[i].getTemplate();
619
final String contextTypeId = template.getContextTypeId();
620
if (FileTemplateContextType.isContextTypeForContentType(contextTypeId, contentTypes[j])) {
621
result.add(template);
625
return result.toArray(new Template[result.size()]);