Fixed Bug<a href="http://sourceforge.net/tracker/?func=detail&atid=484801&aid=1145683...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / php / CodeTemplateContext.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.Iterator;
14
15 import net.sourceforge.phpdt.core.ICompilationUnit;
16 import net.sourceforge.phpdt.core.IJavaProject;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.DefaultLineTracker;
21 import org.eclipse.jface.text.ILineTracker;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.templates.Template;
24 import org.eclipse.jface.text.templates.TemplateBuffer;
25 import org.eclipse.jface.text.templates.TemplateContext;
26 import org.eclipse.jface.text.templates.TemplateException;
27 import org.eclipse.jface.text.templates.TemplateTranslator;
28 import org.eclipse.jface.text.templates.TemplateVariableResolver;
29
30 public class CodeTemplateContext extends TemplateContext {
31
32         private String fLineDelimiter;
33         private IJavaProject fProject;
34
35         public CodeTemplateContext(String contextTypeName, IJavaProject project, String lineDelim) {
36                 super(PHPeclipsePlugin.getDefault().getCodeTemplateContextRegistry().getContextType(contextTypeName));
37                 fLineDelimiter= lineDelim;
38                 fProject= project;
39         }
40
41         public IJavaProject getJavaProject() {
42                 return fProject;
43         }
44
45         /*
46          * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#evaluate(net.sourceforge.phpdt.internal.corext.template.Template)
47          */
48         public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
49                 // test that all variables are defined
50                 Iterator iterator= getContextType().resolvers();
51                 while (iterator.hasNext()) {
52                         TemplateVariableResolver var= (TemplateVariableResolver) iterator.next();
53                         if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
54                         //      Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$
55                         }
56                 }
57
58                 if (!canEvaluate(template))
59                         return null;
60
61                 String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter);
62
63                 TemplateTranslator translator= new TemplateTranslator();
64                 TemplateBuffer buffer= translator.translate(pattern);
65                 getContextType().resolve(buffer, this);
66
67                 return buffer;
68         }
69
70         private static String changeLineDelimiter(String code, String lineDelim) {
71                 try {
72                         ILineTracker tracker= new DefaultLineTracker();
73                         tracker.set(code);
74                         int nLines= tracker.getNumberOfLines();
75                         if (nLines == 1) {
76                                 return code;
77                         }
78
79                         StringBuffer buf= new StringBuffer();
80                         for (int i= 0; i < nLines; i++) {
81                                 if (i != 0) {
82                                         buf.append(lineDelim);
83                                 }
84                                 IRegion region = tracker.getLineInformation(i);
85                                 String line= code.substring(region.getOffset(), region.getOffset() + region.getLength());
86                                 buf.append(line);
87                         }
88                         return buf.toString();
89                 } catch (BadLocationException e) {
90                         // can not happen
91                         return code;
92                 }
93         }
94
95         /* (non-Javadoc)
96          * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#canEvaluate(net.sourceforge.phpdt.internal.corext.template.Template)
97          */
98         public boolean canEvaluate(Template template) {
99                 return true;
100         }
101
102         public void setCompilationUnitVariables(ICompilationUnit cu) {
103                 setVariable(CodeTemplateContextType.FILENAME, cu.getElementName());
104                 setVariable(CodeTemplateContextType.PACKAGENAME, cu.getParent().getElementName());
105                 setVariable(CodeTemplateContextType.PROJECTNAME, cu.getJavaProject().getElementName());
106         }
107
108         public void setFileNameVariable(String filename, String projectname) {
109                 setVariable(CodeTemplateContextType.FILENAME, filename);
110                 setVariable(CodeTemplateContextType.PROJECTNAME, projectname);
111         }
112 }