67543bfc44fd2aa90e33ed699379dde5e1e88a66
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / corext / template / php / CodeTemplateContextType.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
12
13 import java.util.ArrayList;
14
15 import net.sourceforge.phpdt.core.ToolFactory;
16 import net.sourceforge.phpdt.core.compiler.IScanner;
17 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
18 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
19 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
20
21 import org.eclipse.jface.text.templates.ContextTypeRegistry;
22 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
23 import org.eclipse.jface.text.templates.TemplateContext;
24 import org.eclipse.jface.text.templates.TemplateContextType;
25 import org.eclipse.jface.text.templates.TemplateException;
26 import org.eclipse.jface.text.templates.TemplateVariable;
27 import org.eclipse.jface.text.templates.TemplateVariableResolver;
28
29 /**
30  */
31 public class CodeTemplateContextType extends TemplateContextType {
32
33         /* context types */
34         public static final String CATCHBLOCK_CONTEXTTYPE = "php_catchblock_context"; //$NON-NLS-1$
35
36         public static final String METHODBODY_CONTEXTTYPE = "php_methodbody_context"; //$NON-NLS-1$
37
38         public static final String CONSTRUCTORBODY_CONTEXTTYPE = "php_constructorbody_context"; //$NON-NLS-1$
39
40         public static final String GETTERBODY_CONTEXTTYPE = "php_getterbody_context"; //$NON-NLS-1$
41
42         public static final String SETTERBODY_CONTEXTTYPE = "php_setterbody_context"; //$NON-NLS-1$
43
44         public static final String NEWTYPE_CONTEXTTYPE = "php_newtype_context"; //$NON-NLS-1$
45
46         public static final String NEWHTML_CONTEXTTYPE = "php_newhtml_context"; //$NON-NLS-1$
47
48         public static final String TYPECOMMENT_CONTEXTTYPE = "php_typecomment_context"; //$NON-NLS-1$
49
50         public static final String FIELDCOMMENT_CONTEXTTYPE = "php_fieldcomment_context"; //$NON-NLS-1$
51
52         public static final String METHODCOMMENT_CONTEXTTYPE = "php_methodcomment_context"; //$NON-NLS-1$
53
54         public static final String CONSTRUCTORCOMMENT_CONTEXTTYPE = "php_constructorcomment_context"; //$NON-NLS-1$
55
56         public static final String OVERRIDECOMMENT_CONTEXTTYPE = "php_overridecomment_context"; //$NON-NLS-1$
57
58         public static final String GETTERCOMMENT_CONTEXTTYPE = "php_gettercomment_context"; //$NON-NLS-1$
59
60         public static final String SETTERCOMMENT_CONTEXTTYPE = "php_settercomment_context"; //$NON-NLS-1$
61
62         /* templates */
63         public static final String COMMENT_SUFFIX = "comment"; //$NON-NLS-1$
64
65         public static final String CATCHBLOCK = "catchblock"; //$NON-NLS-1$
66
67         public static final String METHODSTUB = "methodbody"; //$NON-NLS-1$     
68
69         public static final String NEWTYPE = "newtype"; //$NON-NLS-1$   
70
71         public static final String NEWHTML = "newhtml"; //$NON-NLS-1$   
72
73         public static final String CONSTRUCTORSTUB = "constructorbody"; //$NON-NLS-1$
74
75         public static final String GETTERSTUB = "getterbody"; //$NON-NLS-1$
76
77         public static final String SETTERSTUB = "setterbody"; //$NON-NLS-1$
78
79         public static final String TYPECOMMENT = "type" + COMMENT_SUFFIX; //$NON-NLS-1$
80
81         public static final String FIELDCOMMENT = "field" + COMMENT_SUFFIX; //$NON-NLS-1$
82
83         public static final String METHODCOMMENT = "method" + COMMENT_SUFFIX; //$NON-NLS-1$
84
85         public static final String CONSTRUCTORCOMMENT = "constructor" + COMMENT_SUFFIX; //$NON-NLS-1$
86
87         public static final String OVERRIDECOMMENT = "override" + COMMENT_SUFFIX; //$NON-NLS-1$
88
89         public static final String GETTERCOMMENT = "getter" + COMMENT_SUFFIX; //$NON-NLS-1$
90
91         public static final String SETTERCOMMENT = "setter" + COMMENT_SUFFIX; //$NON-NLS-1$
92
93         /* resolver types */
94         public static final String EXCEPTION_TYPE = "exception_type"; //$NON-NLS-1$
95
96         public static final String EXCEPTION_VAR = "exception_var"; //$NON-NLS-1$
97
98         public static final String ENCLOSING_METHOD = "enclosing_method"; //$NON-NLS-1$
99
100         public static final String ENCLOSING_TYPE = "enclosing_type"; //$NON-NLS-1$
101
102         public static final String BODY_STATEMENT = "body_statement"; //$NON-NLS-1$
103
104         public static final String FIELD = "field"; //$NON-NLS-1$
105
106         public static final String FIELD_TYPE = "field_type"; //$NON-NLS-1$
107
108         public static final String BARE_FIELD_NAME = "bare_field_name"; //$NON-NLS-1$
109
110         public static final String PARAM = "param"; //$NON-NLS-1$
111
112         public static final String RETURN_TYPE = "return_type"; //$NON-NLS-1$
113
114         public static final String SEE_TAG = "see_to_overridden"; //$NON-NLS-1$
115
116         public static final String TAGS = "tags"; //$NON-NLS-1$
117
118         public static final String TYPENAME = "type_name"; //$NON-NLS-1$
119
120         public static final String FILENAME = "file_name"; //$NON-NLS-1$
121
122         public static final String PACKAGENAME = "package_name"; //$NON-NLS-1$
123
124         public static final String PROJECTNAME = "project_name"; //$NON-NLS-1$
125
126         public static final String PACKAGE_DECLARATION = "package_declaration"; //$NON-NLS-1$
127
128         public static final String TYPE_DECLARATION = "type_declaration"; //$NON-NLS-1$
129
130         public static final String TYPE_COMMENT = "typecomment"; //$NON-NLS-1$
131
132         /**
133          * Resolver that resolves to the variable defined in the context.
134          */
135         public static class CodeTemplateVariableResolver extends
136                         TemplateVariableResolver {
137                 public CodeTemplateVariableResolver(String type, String description) {
138                         super(type, description);
139                 }
140
141                 protected String resolve(TemplateContext context) {
142                         return context.getVariable(getType());
143                 }
144         }
145
146         /**
147          * Resolver for javadoc tags.
148          */
149         public static class TagsVariableResolver extends TemplateVariableResolver {
150                 public TagsVariableResolver() {
151                         super(
152                                         TAGS,
153                                         JavaTemplateMessages
154                                                         .getString("CodeTemplateContextType.variable.description.tags")); //$NON-NLS-1$
155                 }
156
157                 protected String resolve(TemplateContext context) {
158                         return "@"; //$NON-NLS-1$
159                 }
160         }
161
162         /**
163          * Resolver for todo tags.
164          */
165         protected static class Todo extends TemplateVariableResolver {
166
167                 public Todo() {
168                         super(
169                                         "todo", JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.todo")); //$NON-NLS-1$ //$NON-NLS-2$
170                 }
171
172                 protected String resolve(TemplateContext context) {
173                         String todoTaskTag = StubUtility
174                                         .getTodoTaskTag(((CodeTemplateContext) context)
175                                                         .getJavaProject());
176                         if (todoTaskTag == null)
177                                 return "XXX"; //$NON-NLS-1$
178
179                         return todoTaskTag;
180                 }
181         }
182
183         private boolean fIsComment;
184
185         public CodeTemplateContextType(String contextName) {
186                 super(contextName);
187
188                 fIsComment = false;
189
190                 // global
191                 addResolver(new GlobalTemplateVariables.Dollar());
192                 addResolver(new GlobalTemplateVariables.Date());
193                 addResolver(new GlobalTemplateVariables.Year());
194                 addResolver(new GlobalTemplateVariables.Time());
195                 addResolver(new GlobalTemplateVariables.User());
196                 addResolver(new Todo());
197
198                 if (CATCHBLOCK_CONTEXTTYPE.equals(contextName)) {
199                         addResolver(new CodeTemplateVariableResolver(
200                                         EXCEPTION_TYPE,
201                                         JavaTemplateMessages
202                                                         .getString("CodeTemplateContextType.variable.description.exceptiontype"))); //$NON-NLS-1$
203                         addResolver(new CodeTemplateVariableResolver(
204                                         EXCEPTION_VAR,
205                                         JavaTemplateMessages
206                                                         .getString("CodeTemplateContextType.variable.description.exceptionvar"))); //$NON-NLS-1$
207                 } else if (METHODBODY_CONTEXTTYPE.equals(contextName)) {
208                         addResolver(new CodeTemplateVariableResolver(
209                                         ENCLOSING_TYPE,
210                                         JavaTemplateMessages
211                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
212                         addResolver(new CodeTemplateVariableResolver(
213                                         ENCLOSING_METHOD,
214                                         JavaTemplateMessages
215                                                         .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
216                         addResolver(new CodeTemplateVariableResolver(
217                                         BODY_STATEMENT,
218                                         JavaTemplateMessages
219                                                         .getString("CodeTemplateContextType.variable.description.bodystatement"))); //$NON-NLS-1$
220                 } else if (CONSTRUCTORBODY_CONTEXTTYPE.equals(contextName)) {
221                         addResolver(new CodeTemplateVariableResolver(
222                                         ENCLOSING_TYPE,
223                                         JavaTemplateMessages
224                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
225                         addResolver(new CodeTemplateVariableResolver(
226                                         BODY_STATEMENT,
227                                         JavaTemplateMessages
228                                                         .getString("CodeTemplateContextType.variable.description.bodystatement"))); //$NON-NLS-1$
229                 } else if (GETTERBODY_CONTEXTTYPE.equals(contextName)) {
230                         addResolver(new CodeTemplateVariableResolver(
231                                         ENCLOSING_TYPE,
232                                         JavaTemplateMessages
233                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
234                         addResolver(new CodeTemplateVariableResolver(
235                                         ENCLOSING_METHOD,
236                                         JavaTemplateMessages
237                                                         .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
238                         addResolver(new CodeTemplateVariableResolver(
239                                         FIELD,
240                                         JavaTemplateMessages
241                                                         .getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
242                 } else if (SETTERBODY_CONTEXTTYPE.equals(contextName)) {
243                         addResolver(new CodeTemplateVariableResolver(
244                                         ENCLOSING_TYPE,
245                                         JavaTemplateMessages
246                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
247                         addResolver(new CodeTemplateVariableResolver(
248                                         ENCLOSING_METHOD,
249                                         JavaTemplateMessages
250                                                         .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
251                         addResolver(new CodeTemplateVariableResolver(
252                                         FIELD,
253                                         JavaTemplateMessages
254                                                         .getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
255                         addResolver(new CodeTemplateVariableResolver(
256                                         PARAM,
257                                         JavaTemplateMessages
258                                                         .getString("CodeTemplateContextType.variable.description.param"))); //$NON-NLS-1$
259                 } else if (NEWTYPE_CONTEXTTYPE.equals(contextName)) {
260                         addResolver(new CodeTemplateVariableResolver(
261                                         TYPENAME,
262                                         JavaTemplateMessages
263                                                         .getString("CodeTemplateContextType.variable.description.typename"))); //$NON-NLS-1$
264                         addResolver(new CodeTemplateVariableResolver(
265                                         PACKAGE_DECLARATION,
266                                         JavaTemplateMessages
267                                                         .getString("CodeTemplateContextType.variable.description.packdeclaration"))); //$NON-NLS-1$
268                         addResolver(new CodeTemplateVariableResolver(
269                                         TYPE_DECLARATION,
270                                         JavaTemplateMessages
271                                                         .getString("CodeTemplateContextType.variable.description.typedeclaration"))); //$NON-NLS-1$
272                         addResolver(new CodeTemplateVariableResolver(
273                                         TYPE_COMMENT,
274                                         JavaTemplateMessages
275                                                         .getString("CodeTemplateContextType.variable.description.typecomment"))); //$NON-NLS-1$
276                         addCompilationUnitVariables();
277                 } else if (NEWHTML_CONTEXTTYPE.equals(contextName)) {
278                         addResolver(new CodeTemplateVariableResolver(
279                                         TYPENAME,
280                                         JavaTemplateMessages
281                                                         .getString("CodeTemplateContextType.variable.description.typename"))); //$NON-NLS-1$
282                         addResolver(new CodeTemplateVariableResolver(
283                                         PACKAGE_DECLARATION,
284                                         JavaTemplateMessages
285                                                         .getString("CodeTemplateContextType.variable.description.packdeclaration"))); //$NON-NLS-1$
286                         addResolver(new CodeTemplateVariableResolver(
287                                         TYPE_DECLARATION,
288                                         JavaTemplateMessages
289                                                         .getString("CodeTemplateContextType.variable.description.typedeclaration"))); //$NON-NLS-1$
290                         addResolver(new CodeTemplateVariableResolver(
291                                         TYPE_COMMENT,
292                                         JavaTemplateMessages
293                                                         .getString("CodeTemplateContextType.variable.description.typecomment"))); //$NON-NLS-1$
294                         addCompilationUnitVariables();
295                 } else if (TYPECOMMENT_CONTEXTTYPE.equals(contextName)) {
296                         addResolver(new CodeTemplateVariableResolver(
297                                         TYPENAME,
298                                         JavaTemplateMessages
299                                                         .getString("CodeTemplateContextType.variable.description.typename"))); //$NON-NLS-1$
300                         addResolver(new CodeTemplateVariableResolver(
301                                         ENCLOSING_TYPE,
302                                         JavaTemplateMessages
303                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
304                         addResolver(new TagsVariableResolver());
305                         addCompilationUnitVariables();
306                         fIsComment = true;
307                 } else if (FIELDCOMMENT_CONTEXTTYPE.equals(contextName)) {
308                         addResolver(new CodeTemplateVariableResolver(
309                                         FIELD_TYPE,
310                                         JavaTemplateMessages
311                                                         .getString("CodeTemplateContextType.variable.description.fieldtype"))); //$NON-NLS-1$
312                         addResolver(new CodeTemplateVariableResolver(
313                                         FIELD,
314                                         JavaTemplateMessages
315                                                         .getString("CodeTemplateContextType.variable.description.fieldname"))); //$NON-NLS-1$
316                         addCompilationUnitVariables();
317                         fIsComment = true;
318                 } else if (METHODCOMMENT_CONTEXTTYPE.equals(contextName)) {
319                         addResolver(new CodeTemplateVariableResolver(
320                                         ENCLOSING_TYPE,
321                                         JavaTemplateMessages
322                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
323                         addResolver(new CodeTemplateVariableResolver(
324                                         ENCLOSING_METHOD,
325                                         JavaTemplateMessages
326                                                         .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
327                         addResolver(new CodeTemplateVariableResolver(
328                                         RETURN_TYPE,
329                                         JavaTemplateMessages
330                                                         .getString("CodeTemplateContextType.variable.description.returntype"))); //$NON-NLS-1$
331                         addResolver(new TagsVariableResolver());
332                         addCompilationUnitVariables();
333                         fIsComment = true;
334                 } else if (OVERRIDECOMMENT_CONTEXTTYPE.equals(contextName)) {
335                         addResolver(new CodeTemplateVariableResolver(
336                                         ENCLOSING_TYPE,
337                                         JavaTemplateMessages
338                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
339                         addResolver(new CodeTemplateVariableResolver(
340                                         ENCLOSING_METHOD,
341                                         JavaTemplateMessages
342                                                         .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
343                         addResolver(new CodeTemplateVariableResolver(
344                                         SEE_TAG,
345                                         JavaTemplateMessages
346                                                         .getString("CodeTemplateContextType.variable.description.seetag"))); //$NON-NLS-1$
347                         addResolver(new TagsVariableResolver());
348                         addCompilationUnitVariables();
349                         fIsComment = true;
350                 } else if (CONSTRUCTORCOMMENT_CONTEXTTYPE.equals(contextName)) {
351                         addResolver(new CodeTemplateVariableResolver(
352                                         ENCLOSING_TYPE,
353                                         JavaTemplateMessages
354                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
355                         addResolver(new TagsVariableResolver());
356                         addCompilationUnitVariables();
357                         fIsComment = true;
358                 } else if (GETTERCOMMENT_CONTEXTTYPE.equals(contextName)) {
359                         addResolver(new CodeTemplateVariableResolver(
360                                         ENCLOSING_TYPE,
361                                         JavaTemplateMessages
362                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
363                         addResolver(new CodeTemplateVariableResolver(
364                                         FIELD_TYPE,
365                                         JavaTemplateMessages
366                                                         .getString("CodeTemplateContextType.variable.description.getterfieldtype"))); //$NON-NLS-1$
367                         addResolver(new CodeTemplateVariableResolver(
368                                         FIELD,
369                                         JavaTemplateMessages
370                                                         .getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
371                         addResolver(new CodeTemplateVariableResolver(
372                                         ENCLOSING_METHOD,
373                                         JavaTemplateMessages
374                                                         .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
375                         addResolver(new CodeTemplateVariableResolver(
376                                         BARE_FIELD_NAME,
377                                         JavaTemplateMessages
378                                                         .getString("CodeTemplateContextType.variable.description.barefieldname"))); //$NON-NLS-1$
379                         addCompilationUnitVariables();
380                         fIsComment = true;
381                 } else if (SETTERCOMMENT_CONTEXTTYPE.equals(contextName)) {
382                         addResolver(new CodeTemplateVariableResolver(
383                                         ENCLOSING_TYPE,
384                                         JavaTemplateMessages
385                                                         .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
386                         addResolver(new CodeTemplateVariableResolver(
387                                         FIELD_TYPE,
388                                         JavaTemplateMessages
389                                                         .getString("CodeTemplateContextType.variable.description.getterfieldtype"))); //$NON-NLS-1$
390                         addResolver(new CodeTemplateVariableResolver(
391                                         FIELD,
392                                         JavaTemplateMessages
393                                                         .getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
394                         addResolver(new CodeTemplateVariableResolver(
395                                         ENCLOSING_METHOD,
396                                         JavaTemplateMessages
397                                                         .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
398                         addResolver(new CodeTemplateVariableResolver(
399                                         PARAM,
400                                         JavaTemplateMessages
401                                                         .getString("CodeTemplateContextType.variable.description.param"))); //$NON-NLS-1$
402                         addResolver(new CodeTemplateVariableResolver(
403                                         BARE_FIELD_NAME,
404                                         JavaTemplateMessages
405                                                         .getString("CodeTemplateContextType.variable.description.barefieldname"))); //$NON-NLS-1$
406                         addCompilationUnitVariables();
407                         fIsComment = true;
408                 }
409         }
410
411         private void addCompilationUnitVariables() {
412                 addResolver(new CodeTemplateVariableResolver(
413                                 FILENAME,
414                                 JavaTemplateMessages
415                                                 .getString("CodeTemplateContextType.variable.description.filename"))); //$NON-NLS-1$
416                 addResolver(new CodeTemplateVariableResolver(
417                                 PACKAGENAME,
418                                 JavaTemplateMessages
419                                                 .getString("CodeTemplateContextType.variable.description.packagename"))); //$NON-NLS-1$
420                 addResolver(new CodeTemplateVariableResolver(
421                                 PROJECTNAME,
422                                 JavaTemplateMessages
423                                                 .getString("CodeTemplateContextType.variable.description.projectname"))); //$NON-NLS-1$
424         }
425
426         /*
427          * @see net.sourceforge.phpdt.internal.corext.template.ContextType#validateVariables(net.sourceforge.phpdt.internal.corext.template.TemplateVariable[])
428          */
429         protected void validateVariables(TemplateVariable[] variables)
430                         throws TemplateException {
431                 ArrayList required = new ArrayList(5);
432                 //String contextName = getId();
433                 // if (NEWTYPE_CONTEXTTYPE.equals(contextName)) {
434                 // required.add(PACKAGE_DECLARATION);
435                 // required.add(TYPE_DECLARATION);
436                 // }
437                 for (int i = 0; i < variables.length; i++) {
438                         String type = variables[i].getType();
439                         if (getResolver(type) == null) {
440                                 throw new TemplateException(
441                                                 JavaTemplateMessages
442                                                                 .getFormattedString(
443                                                                                 "CodeTemplateContextType.validate.unknownvariable", type)); //$NON-NLS-1$
444                         }
445                         required.remove(type);
446                 }
447                 if (!required.isEmpty()) {
448                         String missing = (String) required.get(0);
449                         throw new TemplateException(
450                                         JavaTemplateMessages
451                                                         .getFormattedString(
452                                                                         "CodeTemplateContextType.validate.missingvariable", missing)); //$NON-NLS-1$
453                 }
454                 super.validateVariables(variables);
455         }
456
457         /*
458          * (non-Javadoc)
459          * 
460          * @see net.sourceforge.phpdt.internal.corext.template.ContextType#createContext()
461          */
462 //      public TemplateContext createContext() {
463 //              return null;
464 //      }
465
466         public static void registerContextTypes(ContextTypeRegistry registry) {
467                 registry.addContextType(new CodeTemplateContextType(
468                                 CodeTemplateContextType.CATCHBLOCK_CONTEXTTYPE));
469                 registry.addContextType(new CodeTemplateContextType(
470                                 CodeTemplateContextType.METHODBODY_CONTEXTTYPE));
471                 registry.addContextType(new CodeTemplateContextType(
472                                 CodeTemplateContextType.CONSTRUCTORBODY_CONTEXTTYPE));
473                 registry.addContextType(new CodeTemplateContextType(
474                                 CodeTemplateContextType.GETTERBODY_CONTEXTTYPE));
475                 registry.addContextType(new CodeTemplateContextType(
476                                 CodeTemplateContextType.SETTERBODY_CONTEXTTYPE));
477                 registry.addContextType(new CodeTemplateContextType(
478                                 CodeTemplateContextType.NEWTYPE_CONTEXTTYPE));
479                 registry.addContextType(new CodeTemplateContextType(
480                                 CodeTemplateContextType.NEWHTML_CONTEXTTYPE));
481
482                 registry.addContextType(new CodeTemplateContextType(
483                                 CodeTemplateContextType.TYPECOMMENT_CONTEXTTYPE));
484                 registry.addContextType(new CodeTemplateContextType(
485                                 CodeTemplateContextType.FIELDCOMMENT_CONTEXTTYPE));
486                 registry.addContextType(new CodeTemplateContextType(
487                                 CodeTemplateContextType.METHODCOMMENT_CONTEXTTYPE));
488                 registry.addContextType(new CodeTemplateContextType(
489                                 CodeTemplateContextType.CONSTRUCTORCOMMENT_CONTEXTTYPE));
490                 registry.addContextType(new CodeTemplateContextType(
491                                 CodeTemplateContextType.OVERRIDECOMMENT_CONTEXTTYPE));
492                 registry.addContextType(new CodeTemplateContextType(
493                                 CodeTemplateContextType.GETTERCOMMENT_CONTEXTTYPE));
494                 registry.addContextType(new CodeTemplateContextType(
495                                 CodeTemplateContextType.SETTERCOMMENT_CONTEXTTYPE));
496         }
497
498         /*
499          * @see net.sourceforge.phpdt.internal.corext.template.ContextType#validate(java.lang.String)
500          */
501         public void validate(String pattern) throws TemplateException {
502                 super.validate(pattern);
503                 if (fIsComment) {
504                         if (!isValidComment(pattern)) {
505                                 throw new TemplateException(
506                                                 JavaTemplateMessages
507                                                                 .getString("CodeTemplateContextType.validate.invalidcomment")); //$NON-NLS-1$
508                         }
509                 }
510         }
511
512         private boolean isValidComment(String template) {
513                 IScanner scanner = ToolFactory.createScanner(true, false, false, true);// false);
514                 scanner.setSource(template.toCharArray());
515                 try {
516                         int next = scanner.getNextToken();
517                         while (next == ITerminalSymbols.TokenNameCOMMENT_LINE
518                                         || next == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
519                                         || next == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
520                                 next = scanner.getNextToken();
521                         }
522                         return next == ITerminalSymbols.TokenNameEOF;
523                 } catch (InvalidInputException e) {
524                 }
525                 return false;
526         }
527
528 }