~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
7
 
 *
8
 
 * Contributors:
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;
14
 
 
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;
20
 
 
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;
45
 
 
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;
56
 
 
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;
62
 
 
63
 
import org.eclipse.cdt.internal.ui.viewsupport.ProjectTemplateStore;
64
 
 
65
 
public class StubUtility {
66
 
 
67
 
        private static final String[] EMPTY= new String[0];
68
 
        
69
 
        private StubUtility() {
70
 
        }
71
 
 
72
 
        /*
73
 
         * Don't use this method directly, use CodeGeneration.
74
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getHeaderFileContent(ITranslationUnit, String, String, String)
75
 
         */     
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);
78
 
        }
79
 
 
80
 
        /*
81
 
         * Don't use this method directly, use CodeGeneration.
82
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getHeaderFileContent(Template, ITranslationUnit, String, String, String)
83
 
         */     
84
 
        public static String getHeaderFileContent(Template template, ITranslationUnit tu, String fileComment, String typeComment, String declarations, String lineDelimiter) throws CoreException {
85
 
                if (template == null) {
86
 
                        return null;
87
 
                }
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$
97
 
                
98
 
                String[] fullLine= { CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT, CodeTemplateContextType.DECLARATIONS };
99
 
                return evaluateTemplate(context, template, fullLine);
100
 
        }
101
 
 
102
 
        /*
103
 
         * Don't use this method directly, use CodeGeneration.
104
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getBodyFileContent(ITranslationUnit, String, String, String)
105
 
         */     
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);
108
 
        }
109
 
 
110
 
        /*
111
 
         * Don't use this method directly, use CodeGeneration.
112
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getBodyFileContent(Template, ITranslationUnit, String, String, String)
113
 
         */     
114
 
        public static String getBodyFileContent(Template template, ITranslationUnit tu, String fileComment, String typeComment, String declarations, String lineDelimiter) throws CoreException {
115
 
                if (template == null) {
116
 
                        return null;
117
 
                }
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$
124
 
                
125
 
                String[] fullLine= { CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT, CodeTemplateContextType.DECLARATIONS };
126
 
                return evaluateTemplate(context, template, fullLine);
127
 
        }
128
 
 
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);
139
 
        }
140
 
 
141
 
        /*
142
 
         * Don't use this method directly, use CodeGeneration.
143
 
         */
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);
147
 
        }
148
 
 
149
 
        /*
150
 
         * Don't use this method directly, use CodeGeneration.
151
 
         */
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);
155
 
        }
156
 
 
157
 
        /*
158
 
         * Don't use this method directly, use CodeGeneration.
159
 
         */
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$
163
 
        }
164
 
 
165
 
        /*
166
 
         * Don't use this method directly, use CodeGeneration.
167
 
         */
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;
172
 
                }
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;
180
 
                }
181
 
                return str;
182
 
        }
183
 
 
184
 
        /*
185
 
         * Don't use this method directly, use CodeGeneration.
186
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getFileComment(ITranslationUnit, String)
187
 
         */     
188
 
        public static String getFileComment(ITranslationUnit tu, String lineDelimiter) throws CoreException {
189
 
                Template template= getCodeTemplate(CodeTemplateContextType.FILECOMMENT_ID, tu.getCProject());
190
 
                if (template == null) {
191
 
                        return null;
192
 
                }
193
 
                
194
 
                ICProject project= tu.getCProject();
195
 
                CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
196
 
                context.setTranslationUnitVariables(tu);
197
 
                return evaluateTemplate(context, template);
198
 
        }       
199
 
 
200
 
        private static String getFileComment(IFile file, String lineDelimiter) throws CoreException {
201
 
                Template template= getCodeTemplate(CodeTemplateContextType.FILECOMMENT_ID, file.getProject());
202
 
                if (template == null) {
203
 
                        return null;
204
 
                }
205
 
                
206
 
                FileTemplateContext context= new FileTemplateContext(template.getContextTypeId(), lineDelimiter);
207
 
                context.setResourceVariables(file);
208
 
                return evaluateTemplate(context, template);
209
 
        }       
210
 
 
211
 
        /*
212
 
         * Don't use this method directly, use CodeGeneration.
213
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getClassComment(ITranslationUnit, String, String)
214
 
         */     
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) {
218
 
                        return null;
219
 
                }
220
 
                
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);
226
 
        }
227
 
 
228
 
        /*
229
 
         * Don't use this method directly, use CodeGeneration.
230
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getMethodComment(ITranslationUnit, String, String, String[], String[], String, String)
231
 
         */
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);
235
 
        }
236
 
        
237
 
        /*
238
 
         * Don't use this method directly, use CodeGeneration.
239
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getConstructorComment(ITranslationUnit, String, String[], String[], String)
240
 
         */
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);
244
 
        }
245
 
        
246
 
        /*
247
 
         * Don't use this method directly, use CodeGeneration.
248
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getDestructorComment(ITranslationUnit, String, String[], String)
249
 
         */
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$
253
 
        }
254
 
 
255
 
        /*
256
 
         * Don't use this method directly, use CodeGeneration.
257
 
         * @see org.eclipse.cdt.ui.CodeGeneration#getMethodComment(ITranslationUnit, String, String, String[], String[], String, String)
258
 
         */
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) {
262
 
                        return null;
263
 
                }
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);
268
 
                
269
 
                if (retTypeSig != null) {
270
 
                        context.setVariable(CodeTemplateContextType.RETURN_TYPE, retTypeSig);
271
 
                }
272
 
                context.setTranslationUnitVariables(tu);
273
 
                TemplateBuffer buffer;
274
 
                try {
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);
280
 
                }
281
 
                if (buffer == null) {
282
 
                        return null;
283
 
                }
284
 
                
285
 
                // TODO doc comment tags
286
 
                
287
 
                String str= buffer.getString();
288
 
                if (Strings.containsOnlyWhitespaces(str)) {
289
 
                        return null;
290
 
                }
291
 
 
292
 
                return str;
293
 
        }
294
 
        
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) {
304
 
                                continue;
305
 
                        }
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] ;
310
 
                                        try {
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));
319
 
                                                        }
320
 
                                                }
321
 
                                        } catch (BadLocationException exc) {
322
 
                                                break;
323
 
                                        }
324
 
                                }
325
 
                        } else {
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));
336
 
                                        }
337
 
                                }
338
 
                        }
339
 
                }
340
 
                edit.apply(doc, 0);
341
 
                return doc.get();
342
 
        }
343
 
 
344
 
 
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())) {
350
 
                                return curr;
351
 
                        }
352
 
                }
353
 
                return null;
354
 
        }
355
 
        
356
 
        private static String evaluateTemplate(TemplateContext context, Template template) throws CoreException {
357
 
                TemplateBuffer buffer;
358
 
                try {
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);
364
 
                }
365
 
                if (buffer == null)
366
 
                        return null;
367
 
                String str= buffer.getString();
368
 
                if (Strings.containsOnlyWhitespaces(str)) {
369
 
                        return null;
370
 
                }
371
 
                return str;
372
 
        }
373
 
        
374
 
        private static String evaluateTemplate(TemplateContext context, Template template, String[] fullLineVariables) throws CoreException {
375
 
                TemplateBuffer buffer;
376
 
                try {
377
 
                        buffer= context.evaluate(template);
378
 
                        if (buffer == null)
379
 
                                return null;
380
 
                        String str= fixFullLineVariables(buffer, fullLineVariables);
381
 
                        if (Strings.containsOnlyWhitespaces(str)) {
382
 
                                return null;
383
 
                        }
384
 
                        return str;
385
 
                } catch (BadLocationException e) {
386
 
                        throw new CoreException(Status.CANCEL_STATUS);
387
 
                } catch (TemplateException e) {
388
 
                        throw new CoreException(Status.CANCEL_STATUS);
389
 
                }
390
 
        }
391
 
 
392
 
        /**
393
 
         * Returns the line delimiter which is used in the specified project.
394
 
         * 
395
 
         * @param project the C project, or <code>null</code>
396
 
         * @return the used line delimiter
397
 
         */
398
 
        public static String getLineDelimiterUsed(ICProject project) {
399
 
                return getProjectLineDelimiter(project);
400
 
        }
401
 
 
402
 
        private static String getProjectLineDelimiter(ICProject cProject) {
403
 
                IProject project= null;
404
 
                if (cProject != null)
405
 
                        project= cProject.getProject();
406
 
                
407
 
                String lineDelimiter= getLineDelimiterPreference(project);
408
 
                if (lineDelimiter != null)
409
 
                        return lineDelimiter;
410
 
                
411
 
                return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
412
 
        }
413
 
        
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;
422
 
                }
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);
427
 
        }
428
 
        
429
 
        /**
430
 
         * Examines a string and returns the first line delimiter found.
431
 
         */
432
 
        public static String getLineDelimiterUsed(ICElement elem) throws CModelException {
433
 
        if (elem == null) return ""; //$NON-NLS-1$
434
 
        
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);
441
 
                                if (ch == SWT.CR) {
442
 
                                        if (i + 1 < length) {
443
 
                                                if (buf.getChar(i + 1) == SWT.LF) {
444
 
                                                        return "\r\n"; //$NON-NLS-1$
445
 
                                                }
446
 
                                        }
447
 
                                        return "\r"; //$NON-NLS-1$
448
 
                                } else if (ch == SWT.LF) {
449
 
                                        return "\n"; //$NON-NLS-1$
450
 
                                }
451
 
                        }
452
 
                }
453
 
                return getProjectLineDelimiter(elem.getCProject());
454
 
        }
455
 
 
456
 
 
457
 
        /**
458
 
         * Get the default task tag for the given project.
459
 
         * 
460
 
         * @param project
461
 
         * @return the default task tag
462
 
         */
463
 
        public static String getTodoTaskTag(ICProject project) {
464
 
                String markers= null;
465
 
                if (project == null) {
466
 
                        markers= CCorePlugin.getOption(CCorePreferenceConstants.TODO_TASK_TAGS);
467
 
                } else {
468
 
                        markers= project.getOption(CCorePreferenceConstants.TODO_TASK_TAGS, true);
469
 
                }
470
 
                
471
 
                if (markers != null && markers.length() > 0) {
472
 
                        int idx= markers.indexOf(',');
473
 
                        if (idx == -1) {
474
 
                                return markers;
475
 
                        }
476
 
                        return markers.substring(0, idx);
477
 
                }
478
 
                return CCorePreferenceConstants.DEFAULT_TASK_TAG;
479
 
        }
480
 
        
481
 
        public static boolean doAddComments(ICProject project) {
482
 
                return PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_ADD_COMMENTS, project, false); 
483
 
        }
484
 
        
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;
492
 
                        } else {
493
 
                                templateId= CodeTemplateContextType.CPP_SOURCEFILE_ID;
494
 
                        }
495
 
                } else if (tu.isCLanguage()) {
496
 
                        if (tu.isHeaderUnit()) {
497
 
                                templateId= CodeTemplateContextType.C_HEADERFILE_ID;
498
 
                        } else {
499
 
                                templateId= CodeTemplateContextType.C_SOURCEFILE_ID;
500
 
                        }
501
 
                }
502
 
                return getCodeTemplate(templateId, tu.getCProject());
503
 
        }
504
 
 
505
 
        private static Template getCodeTemplate(String id, ICProject cProject) {
506
 
                return getCodeTemplate(id, cProject != null ? cProject.getProject() : null);
507
 
        }
508
 
        private static Template getCodeTemplate(String id, IProject project) {
509
 
                if (project == null)
510
 
                        return CUIPlugin.getDefault().getCodeTemplateStore().findTemplateById(id);
511
 
                ProjectTemplateStore projectStore= new ProjectTemplateStore(project);
512
 
                try {
513
 
                        projectStore.load();
514
 
                } catch (IOException e) {
515
 
                        CUIPlugin.log(e);
516
 
                }
517
 
                return projectStore.findTemplateById(id);
518
 
        }
519
 
 
520
 
        private static int getIncludeGuardScheme(ICProject cproject) {
521
 
                return PreferenceConstants.getPreference(
522
 
                                PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME,
523
 
                                cproject,
524
 
                                PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_NAME);
525
 
        }
526
 
 
527
 
        private static String generateIncludeGuardSymbol(String fileName, ICProject cproject) {
528
 
 
529
 
                int preferenceValue = getIncludeGuardScheme(cproject);
530
 
                
531
 
                switch (preferenceValue) {
532
 
                case PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_FILE_NAME:
533
 
                        if (fileName != null) {
534
 
                                return generateIncludeGuardSymbolFromFileName(fileName);
535
 
                        } else {
536
 
                                return null;
537
 
                        }
538
 
                        
539
 
                case PreferenceConstants.CODE_TEMPLATES_INCLUDE_GUARD_SCHEME_UUID:
540
 
                return generateIncludeGuardSymbolFromUUID();
541
 
                        
542
 
                default:
543
 
                        CUIPlugin.log("Unknown preference value " + preferenceValue + "for include guard scheme", null); //$NON-NLS-1$ //$NON-NLS-2$
544
 
                        return null;
545
 
                }
546
 
    }
547
 
 
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.
554
 
                //buf.append('_');
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 == '_') {
560
 
                                buf.append('_');
561
 
                        }
562
 
                }
563
 
                buf.append('_');
564
 
                return buf.toString();
565
 
        }
566
 
 
567
 
        private static String generateIncludeGuardSymbolFromUUID() {
568
 
                String uuid = UUID.randomUUID().toString();
569
 
                
570
 
                // 1) Make sure the guard always starts with a letter.
571
 
                // 2) Convert to upper case and remove invalid characters
572
 
                // 
573
 
                // e.g. convert
574
 
                //         067e6162-3b6f-4ae2-a171-2470b63dff00 to
575
 
                //        H067E6162-3b6F-4AE2-A171-2470B63DFF00
576
 
                StringBuffer buf = new StringBuffer();
577
 
                
578
 
                buf.append('H');
579
 
                
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));
584
 
                        } else {
585
 
                                buf.append('_');
586
 
                        }
587
 
                }
588
 
 
589
 
                return buf.toString();
590
 
        }
591
 
 
592
 
        /**
593
 
         * Get a set of file templates for the given content types.
594
 
         * 
595
 
         * @param contentTypes  the list of content types
596
 
         * @param project  the project or <code>null</code>
597
 
         * @return an array of templates
598
 
         */
599
 
        public static Template[] getFileTemplatesForContentTypes(String[] contentTypes, IProject project) {
600
 
                if (contentTypes == null || contentTypes.length == 0) {
601
 
                        return new Template[0];
602
 
                }
603
 
                TemplatePersistenceData[] templateDatas;
604
 
                if (project == null) {
605
 
                        templateDatas= CUIPlugin.getDefault().getCodeTemplateStore().getTemplateData(true);
606
 
                } else {
607
 
                        ProjectTemplateStore projectStore= new ProjectTemplateStore(project.getProject());
608
 
                        try {
609
 
                                projectStore.load();
610
 
                        } catch (IOException e) {
611
 
                                CUIPlugin.log(e);
612
 
                        }
613
 
                        templateDatas= projectStore.getTemplateData();
614
 
                }
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);
622
 
                                }
623
 
                        }
624
 
                }
625
 
                return result.toArray(new Template[result.size()]);
626
 
        }
627
 
        
628
 
}