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