upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / ui / CodeGeneration.java
1 /*******************************************************************************
2  *  Copyright (c) 2001, 2011 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  *     Sergey Prigogin (Google)
12  *******************************************************************************/
13 package org.eclipse.cdt.ui;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jface.text.templates.Template;
17
18 import org.eclipse.cdt.core.model.ITranslationUnit;
19
20 import org.eclipse.cdt.internal.corext.codemanipulation.StubUtility;
21
22 /**
23  * Class that offers access to the templates contained in the 'Code Templates' preference page.
24  * 
25  * @noextend This class is not intended to be subclassed by clients.
26  * @noinstantiate This class is not intended to be instantiated by clients.
27  * @since 2.1
28  */
29 public class CodeGeneration {
30
31         private CodeGeneration() {
32         }
33
34         /**
35          * Returns the content for a new header file using the default 'header file' code template.
36          * @param tu The translation unit to create the source for. The translation unit does not need
37          *              to exist.
38          * @param lineDelimiter The line delimiter to be used.
39          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
40          * @throws CoreException
41          * @since 5.3
42          */
43         public static String getHeaderFileContent(Template template, ITranslationUnit tu,
44                         String lineDelimiter) throws CoreException {    
45                 return getHeaderFileContent(template, tu, null, null, null, null, null, null, null,
46                                 lineDelimiter);
47         }
48
49         /**
50          * Returns the content for a new header file using the default 'header file' code template.
51          * @param tu The translation unit to create the source for. The translation unit does not need
52          *              to exist.
53          * @param typeComment The comment for the type to created. Used when the code template contains
54          *              a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
55          * @param typeContent The code of the type, including type declaration and body.
56          * @param lineDelimiter The line delimiter to be used.
57          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
58          * @throws CoreException
59          */
60         public static String getHeaderFileContent(ITranslationUnit tu, String typeComment,
61                         String typeContent, String lineDelimiter) throws CoreException {        
62                 return getHeaderFileContent(tu, null, null, null, null, typeComment, typeContent, null,
63                                 lineDelimiter);
64         }
65
66         /**
67          * Returns the content for a new header file using the default 'header file' code template.
68          * @param template  The file template to use or <code>null</code> for the default template
69          * @param tu The translation unit to create the source for. The translation unit does not need
70          *              to exist.
71          * @param typeComment The comment for the type to created. Used when the code template contains
72          *              a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
73          * @param typeContent The code of the type, including type declaration and body.
74          * @param lineDelimiter The line delimiter to be used.
75          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
76          * @throws CoreException
77          */
78         public static String getHeaderFileContent(Template template, ITranslationUnit tu,
79                         String typeComment, String typeContent, String lineDelimiter) throws CoreException {    
80                 return getHeaderFileContent(template, tu, null, null, null, null, typeComment, typeContent,
81                                 null, lineDelimiter);
82         }
83
84         /**
85          * Returns the content for a new header file using the default 'header file' code template.
86          * @param tu The translation unit to create the source for. The translation unit does not need
87          *              to exist.
88          * @param includes Include statements, or {@code null} if there are no includes.
89          * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
90          *              namespace.
91          * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
92          *              namespace.
93          * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
94          *              namespace.
95          * @param typeComment The comment for the type to created. Used when the code template contains
96          *              a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
97          * @param typeContent The code of the type, including type declaration and body.
98          * @param typeName The name of the type.
99          * @param lineDelimiter The line delimiter to be used.
100          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
101          * @throws CoreException
102          * @since 5.3
103          */
104         public static String getHeaderFileContent(ITranslationUnit tu, String includes,
105                         String namespaceBegin, String namespaceEnd, String namespaceName, String typeComment,
106                         String typeContent, String typeName, String lineDelimiter) throws CoreException {       
107                 return StubUtility.getHeaderFileContent(tu, typeContent,
108                                 getFileComment(tu, lineDelimiter), includes, namespaceBegin, namespaceEnd,
109                                 namespaceName, typeComment, typeName, lineDelimiter);
110         }
111
112         /**
113          * Returns the content for a new header file using the default 'header file' code template.
114          * @param template  The file template to use or <code>null</code> for the default template
115          * @param tu The translation unit to create the source for. The translation unit does not need
116          *              to exist.
117          * @param includes Include statements, or {@code null} if there are no includes.
118          * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
119          *              namespace.
120          * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
121          *              namespace.
122          * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
123          *              namespace.
124          * @param typeComment The comment for the type to created. Used when the code template contains
125          *              a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
126          * @param typeContent The code of the type, including type declaration and body.
127          * @param typeName The name of the type.
128          * @param lineDelimiter The line delimiter to be used.
129          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
130          * @throws CoreException
131          * @since 5.3
132          */
133         public static String getHeaderFileContent(Template template, ITranslationUnit tu,
134                         String includes, String namespaceBegin, String namespaceEnd, String namespaceName,
135                         String typeComment, String typeContent, String typeName, String lineDelimiter)
136                         throws CoreException {  
137                 return StubUtility.getHeaderFileContent(template, tu, typeContent,
138                                 getFileComment(tu, lineDelimiter), includes, namespaceBegin, namespaceEnd,
139                                 namespaceName, typeComment, typeName, lineDelimiter);
140         }
141
142         /**
143          * Returns the content for a new translation unit using the 'source file' code template.
144          * @param tu The translation unit to create the source for. The translation unit does not need
145          *              to exist.
146          * @param typeComment The comment for the type to created. Used when the code template contains
147          *              a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
148          * @param typeContent The code of the type, including type declaration and body.
149          * @param lineDelimiter The line delimiter to be used.
150          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
151          * @throws CoreException
152          */
153         public static String getBodyFileContent(ITranslationUnit tu, String typeComment,
154                         String typeContent, String lineDelimiter) throws CoreException {        
155                 return getBodyFileContent(tu, typeContent, null, null, null, null, typeComment, null,
156                                 lineDelimiter);
157         }
158
159         /**
160          * Returns the content for a new translation unit using the 'source file' code template.
161          * @param template  The file template to use or <code>null</code> for the default template
162          * @param tu The translation unit to create the source for. The translation unit does not need
163          *              to exist.
164          * @param typeComment The comment for the type to created. Used when the code template contains
165          *              a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
166          * @param typeContent The code of the type, including type declaration and body.
167          * @param lineDelimiter The line delimiter to be used.
168          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
169          * @throws CoreException
170          */
171         public static String getBodyFileContent(Template template, ITranslationUnit tu,
172                         String typeComment, String typeContent, String lineDelimiter) throws CoreException {    
173                 return getBodyFileContent(template, tu, typeContent, null, null, null, null, typeComment,
174                                 null, lineDelimiter);
175         }
176
177         /**
178          * Returns the content for a new source file using the default 'source file' code template.
179          * @param tu The translation unit to create the source for. The translation unit does not need
180          *              to exist.
181          * @param includes Include statements, or {@code null} if there are no includes.
182          * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
183          *              namespace.
184          * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
185          *              namespace.
186          * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
187          *              namespace.
188          * @param typeComment The comment for the type to created. Used when the code template contains
189          *              a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
190          * @param typeContent The code of the type, including type declaration and body.
191          * @param typeName The name of the type.
192          * @param lineDelimiter The line delimiter to be used.
193          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
194          * @throws CoreException
195          * @since 5.3
196          */
197         public static String getBodyFileContent(ITranslationUnit tu,
198                         String includes, String namespaceBegin, String namespaceEnd, String namespaceName,
199                         String typeComment, String typeContent, String typeName, String lineDelimiter)
200                         throws CoreException {  
201                 return StubUtility.getBodyFileContent(tu, typeContent, getFileComment(tu, lineDelimiter),
202                                 includes, namespaceBegin, namespaceEnd, namespaceName, typeComment, typeName,
203                                 lineDelimiter);
204         }
205
206         /**
207          * Returns the content for a new source file using the default 'source file' code template.
208          * @param template  The file template to use or <code>null</code> for the default template
209          * @param tu The translation unit to create the source for. The translation unit does not need
210          *              to exist.
211          * @param includes Include statements, or {@code null} if there are no includes.
212          * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
213          *              namespace.
214          * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
215          *              namespace.
216          * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
217          *              namespace.
218          * @param typeComment The comment for the type to created. Used when the code template contains
219          *              a ${typecomment} variable. Can be <code>null</code> if no comment should be added.
220          * @param typeContent The code of the type, including type declaration and body.
221          * @param typeName The name of the type.
222          * @param lineDelimiter The line delimiter to be used.
223          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
224          * @throws CoreException
225          * @since 5.3
226          */
227         public static String getBodyFileContent(Template template, ITranslationUnit tu,
228                         String includes, String namespaceBegin, String namespaceEnd, String namespaceName,
229                         String typeComment, String typeContent, String typeName, String lineDelimiter)
230                         throws CoreException {  
231                 return StubUtility.getBodyFileContent(template, tu, typeContent,
232                                 getFileComment(tu, lineDelimiter), includes, namespaceBegin, namespaceEnd,
233                                 namespaceName, typeComment, typeName, lineDelimiter);
234         }
235
236         /**
237          * Returns the content for a new translation unit using the 'test file' code template.
238          * @param tu The translation unit to create the source for. The translation unit does not need
239          *              to exist.
240          * @param includes Include statements, or {@code null} if there are no includes.
241          * @param namespaceBegin Beginning of namespace declarations, or {@code null} if there is no
242          *              namespace.
243          * @param namespaceEnd End of namespace declarations, or {@code null} if there is no
244          *              namespace.
245          * @param namespaceName Fully qualified namespace name, or {@code null} if there is no
246          *              namespace.
247          * @param testCases The test cases code.
248          * @param typeName The name of the type.
249          * @param lineDelimiter The line delimiter to be used.
250          * @return Returns the new content or <code>null</code> if the template is undefined or empty.
251          * @throws CoreException
252          * @since 5.3
253          */
254         public static String getTestFileContent(ITranslationUnit tu,
255                         String includes, String namespaceBegin, String namespaceEnd, String namespaceName,
256                         String testCases, String typeName, String lineDelimiter) throws CoreException { 
257                 return StubUtility.getTestFileContent(tu, testCases, getFileComment(tu, lineDelimiter),
258                                 includes, namespaceBegin, namespaceEnd, namespaceName, typeName, lineDelimiter);
259         }
260
261         /**
262          * Returns the content of the body for a method using the method body template.
263          * <code>null</code> is returned if the template is empty.
264          * <p>The returned string is unformatted and not indented.
265          * 
266          * @param tu The translation unit to which the method belongs. The translation unit does not need to exist.
267          * @param typeName Name of the type to which the method belongs.
268          * @param methodName Name of the method.
269          * @param bodyStatement The code to be entered at the place of the variable ${body_statement}. 
270          * @param lineDelimiter The line delimiter to be used.
271          * @return Returns the constructed body content or <code>null</code> if
272          * the comment code template is empty. The returned string is unformatted and and has no indent (formatting required).
273          * @throws CoreException Thrown when the evaluation of the code template fails.
274          */     
275         public static String getMethodBodyContent(ITranslationUnit tu, String typeName,
276                         String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
277                 return StubUtility.getMethodBodyContent(tu.getCProject(), typeName, methodName,
278                                 bodyStatement, lineDelimiter);
279         }
280
281         /**
282          * Returns the content of the body for a constructor using the constructor body template.
283          * <code>null</code> is returned if the template is empty.
284          * <p>The returned string is unformatted and not indented.
285          * 
286          * @param tu The translation unit to which the method belongs. The translation unit
287          *              does not need to exist.
288          * @param typeName Name of the type to which the constructor belongs.
289          * @param bodyStatement The code to be entered at the place of the variable ${body_statement}. 
290          * @param lineDelimiter The line delimiter to be used.
291          * @return Returns the constructed body content or <code>null</code> if the comment code
292          *              template is empty. The returned string is unformatted and and has no indent
293          *              (formatting required).
294          * @throws CoreException Thrown when the evaluation of the code template fails.
295          */     
296         public static String getConstructorBodyContent(ITranslationUnit tu, String typeName,
297                         String bodyStatement, String lineDelimiter) throws CoreException {
298                 return StubUtility.getConstructorBodyContent(tu.getCProject(), typeName, bodyStatement,
299                                 lineDelimiter);
300         }
301
302         /**
303          * Returns the content of the body for a destructor using the destructor body template.
304          * <code>null</code> is returned if the template is empty.
305          * <p>The returned string is unformatted and not indented.
306          * 
307          * @param tu The translation unit to which the method belongs. The translation unit
308          *              does not need to exist.
309          * @param typeName Name of the type to which the constructor belongs.
310          * @param bodyStatement The code to be entered at the place of the variable ${body_statement}. 
311          * @param lineDelimiter The line delimiter to be used.
312          * @return Returns the constructed body content or <code>null</code> if the comment code
313          *              template is empty. The returned string is unformatted and and has no indent
314          *              (formatting required).
315          * @throws CoreException Thrown when the evaluation of the code template fails.
316          */     
317         public static String getDestructorBodyContent(ITranslationUnit tu, String typeName,
318                         String bodyStatement, String lineDelimiter) throws CoreException {
319                 return StubUtility.getDestructorBodyContent(tu.getCProject(), typeName, bodyStatement,
320                                 lineDelimiter);
321         }
322
323         /**
324          * Returns the content of the class definition body using the class body template.
325          * <code>null</code> is returned if the template is empty.
326          * <p>The returned string is unformatted and not indented.
327          * 
328          * @param tu The translation unit to which the method belongs. The translation unit
329          *              does not need to exist.
330          * @param className The name of the class.
331          * @param classMemberDeclarations The code to be entered at the place of the variable
332          *              ${declarations}. 
333          * @param lineDelimiter The line delimiter to be used.
334          * @return Returns the constructed class body content or <code>null</code> if
335          *              the class body code template is empty. The returned string is unformatted and
336          *              has no indent (formatting required).
337          * @throws CoreException Thrown when the evaluation of the code template fails.
338          * @since 5.3
339          */     
340         public static String getClassBodyContent(ITranslationUnit tu, String className,
341                         String classMemberDeclarations, String lineDelimiter)
342                         throws CoreException {
343                 return StubUtility.getClassBodyContent(tu.getCProject(), className, classMemberDeclarations,
344                                 lineDelimiter);
345         }
346
347         /**
348          * Returns the content of the beginning of a namespace declaration using the corresponding
349          * template. <code>null</code> is returned if the template is empty.
350          * <p>The returned string is unformatted and not indented.
351          * 
352          * @param tu The translation unit to which the method belongs. The translation unit
353          *              does not need to exist.
354          * @param namespaceName The name of the namespace.
355          * @param lineDelimiter The line delimiter to be used.
356          * @return Returns the constructed beginning of a namespace declaration,or <code>null</code> if
357          *              the namespace code template is empty. The returned string is unformatted and has no
358          *              indent (formatting required).
359          * @throws CoreException Thrown when the evaluation of the code template fails.
360          * @since 5.3
361          */     
362         public static String getNamespaceBeginContent(ITranslationUnit tu, String namespaceName,
363                         String lineDelimiter) throws CoreException {
364                 return StubUtility.getNamespaceBeginContent(tu.getCProject(), namespaceName, lineDelimiter);
365         }
366
367         /**
368          * Returns the content of the end of a namespace declaration using the corresponding
369          * template. <code>null</code> is returned if the template is empty.
370          * <p>The returned string is unformatted and not indented.
371          * 
372          * @param tu The translation unit to which the method belongs. The translation unit
373          *              does not need to exist.
374          * @param namespaceName The name of the namespace.
375          * @param lineDelimiter The line delimiter to be used.
376          * @return Returns the constructed end of a namespace declaration,or <code>null</code> if
377          *              the namespace code template is empty. The returned string is unformatted and has no
378          *              indent (formatting required).
379          * @throws CoreException Thrown when the evaluation of the code template fails.
380          * @since 5.3
381          */     
382         public static String getNamespaceEndContent(ITranslationUnit tu, String namespaceName,
383                         String lineDelimiter) throws CoreException {
384                 return StubUtility.getNamespaceEndContent(tu.getCProject(), namespaceName, lineDelimiter);
385         }
386
387         /**
388          * Returns the content for a new file comment using the 'file comment' code template.
389          * The returned content is unformatted and is not indented.
390          * @param tu The translation unit to add the comment to. The translation unit does not need
391          *              to exist.
392          * @param lineDelimiter The line delimiter to be used.
393          * @return Returns the new content or <code>null</code> if the code template is undefined
394          *              or empty. The returned content is unformatted and is not indented.
395          * @throws CoreException Thrown when the evaluation of the code template fails.
396          * @since 5.0
397          */     
398         public static String getFileComment(ITranslationUnit tu, String lineDelimiter)
399                         throws CoreException {
400                 return StubUtility.getFileComment(tu, lineDelimiter);
401         }
402         
403         /**
404          * Returns the content for a new type comment using the 'typecomment' code template.
405          * The returned content is unformatted and is not indented.
406          * @param tu The translation unit where the type is contained. The translation unit
407          *              does not need to exist.
408          * @param typeQualifiedName The name of the type to which the comment is added. For inner types
409          *              the name must be qualified and include the outer types names (dot separated). 
410          * @param lineDelimiter The line delimiter to be used.
411          * @return Returns the new content or <code>null</code> if the code template is undefined
412          *              or empty. The returned content is unformatted and is not indented.
413          * @throws CoreException
414          */     
415         public static String getClassComment(ITranslationUnit tu, String typeQualifiedName,
416                         String lineDelimiter) throws CoreException {
417                 return StubUtility.getClassComment(tu, typeQualifiedName, lineDelimiter);
418         }
419
420         /**
421          * Returns the comment for a method using the method comment code template.
422          * <code>null</code> is returned if the template is empty.
423          * <p>The returned string is unformatted and not indented.
424          * 
425          * @param tu The translation unit to which the method belongs. The translation unit
426          *              does not need to exist.
427          * @param declaringTypeName Name of the type to which the method belongs.
428          * @param methodName Name of the method.
429          * @param paramNames Names of the parameters for the method.
430          * @param excTypeSig Thrown exceptions.
431          * @param retTypeSig Return type.
432          * @param lineDelimiter The line delimiter to be used.
433          * @return Returns the constructed comment or <code>null</code> if the comment code template
434          *              is empty. The returned content is unformatted and not indented (formatting required).
435          * @throws CoreException Thrown when the evaluation of the code template fails.
436          */
437         public static String getMethodComment(ITranslationUnit tu, String declaringTypeName,
438                         String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig,
439                         String lineDelimiter) throws CoreException {
440                 return StubUtility.getMethodComment(tu, declaringTypeName, methodName, paramNames,
441                                 excTypeSig, retTypeSig, lineDelimiter);
442         }
443
444         /**
445          * Returns the comment for a constructor using the constructor comment code template.
446          * <code>null</code> is returned if the template is empty.
447          * <p>The returned string is unformatted and not indented.
448          * 
449          * @param tu The translation unit to which the method belongs. The translation unit
450          *              does not need to exist.
451          * @param declaringTypeName Name of the type to which the method belongs.
452          * @param paramNames Names of the parameters for the method.
453          * @param excTypeSig Thrown exceptions.
454          * @param lineDelimiter The line delimiter to be used.
455          * @return Returns the constructed comment or <code>null</code> if the comment code template
456          *              is empty. The returned content is unformatted and not indented (formatting required).
457          * @throws CoreException Thrown when the evaluation of the code template fails.
458          */
459         public static String getConstructorComment(ITranslationUnit tu, String declaringTypeName,
460                         String[] paramNames, String[] excTypeSig, String lineDelimiter) throws CoreException {
461                 return StubUtility.getConstructorComment(tu, declaringTypeName, paramNames, excTypeSig,
462                                 lineDelimiter);
463         }
464
465         /**
466          * Returns the comment for a destructor using the destructor comment code template.
467          * <code>null</code> is returned if the template is empty.
468          * <p>The returned string is unformatted and not indented.
469          * 
470          * @param tu The translation unit to which the method belongs. The translation unit
471          *              does not need to exist.
472          * @param declaringTypeName Name of the type to which the method belongs.
473          * @param excTypeSig Thrown exceptions.
474          * @param lineDelimiter The line delimiter to be used.
475          * @return Returns the constructed comment or <code>null</code> if the comment code template
476          *              is empty. The returned content is unformatted and not indented (formatting required).
477          * @throws CoreException Thrown when the evaluation of the code template fails.
478          */
479         public static String getDestructorComment(ITranslationUnit tu, String declaringTypeName,
480                         String[] excTypeSig, String lineDelimiter) throws CoreException {
481                 return StubUtility.getDestructorComment(tu, declaringTypeName, excTypeSig, lineDelimiter);
482         }
483 }