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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
13 import java.util.Iterator;
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;
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;
31 public class CodeTemplateContext extends TemplateContext {
33 private String fLineDelimiter;
35 private IJavaProject fProject;
37 public CodeTemplateContext(String contextTypeName, IJavaProject project,
39 super(WebUI.getDefault().getCodeTemplateContextRegistry()
40 .getContextType(contextTypeName));
41 fLineDelimiter = lineDelim;
45 public IJavaProject getJavaProject() {
50 * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#evaluate(net.sourceforge.phpdt.internal.corext.template.Template)
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
59 if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
60 // Assert.isNotNull(getVariable(var.getType()), "Variable " +
61 // var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$
65 if (!canEvaluate(template))
68 String pattern = changeLineDelimiter(template.getPattern(),
71 TemplateTranslator translator = new TemplateTranslator();
72 TemplateBuffer buffer = translator.translate(pattern);
73 getContextType().resolve(buffer, this);
78 private static String changeLineDelimiter(String code, String lineDelim) {
80 ILineTracker tracker = new DefaultLineTracker();
82 int nLines = tracker.getNumberOfLines();
87 StringBuffer buf = new StringBuffer();
88 for (int i = 0; i < nLines; i++) {
90 buf.append(lineDelim);
92 IRegion region = tracker.getLineInformation(i);
93 String line = code.substring(region.getOffset(), region
95 + region.getLength());
98 return buf.toString();
99 } catch (BadLocationException e) {
108 * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#canEvaluate(net.sourceforge.phpdt.internal.corext.template.Template)
110 public boolean canEvaluate(Template template) {
114 public void setCompilationUnitVariables(ICompilationUnit cu) {
115 setVariable(CodeTemplateContextType.FILENAME, cu.getElementName());
116 setVariable(CodeTemplateContextType.PACKAGENAME, cu.getParent()
118 setVariable(CodeTemplateContextType.PROJECTNAME, cu.getJavaProject()
122 public void setFileNameVariable(String filename, String projectname) {
123 setVariable(CodeTemplateContextType.FILENAME, filename);
124 setVariable(CodeTemplateContextType.PROJECTNAME, projectname);