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