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;
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;
30 public class CodeTemplateContext extends TemplateContext {
32 private String fLineDelimiter;
34 private IJavaProject fProject;
36 public CodeTemplateContext(String contextTypeName, IJavaProject project,
38 super(PHPeclipsePlugin.getDefault().getCodeTemplateContextRegistry()
39 .getContextType(contextTypeName));
40 fLineDelimiter = lineDelim;
44 public IJavaProject getJavaProject() {
49 * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#evaluate(net.sourceforge.phpdt.internal.corext.template.Template)
51 public TemplateBuffer evaluate(Template template)
52 throws BadLocationException, TemplateException {
53 // test that all variables are defined
54 Iterator iterator = getContextType().resolvers();
55 while (iterator.hasNext()) {
56 TemplateVariableResolver var = (TemplateVariableResolver) iterator
58 if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
59 // Assert.isNotNull(getVariable(var.getType()), "Variable " +
60 // var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$
64 if (!canEvaluate(template))
67 String pattern = changeLineDelimiter(template.getPattern(),
70 TemplateTranslator translator = new TemplateTranslator();
71 TemplateBuffer buffer = translator.translate(pattern);
72 getContextType().resolve(buffer, this);
77 private static String changeLineDelimiter(String code, String lineDelim) {
79 ILineTracker tracker = new DefaultLineTracker();
81 int nLines = tracker.getNumberOfLines();
86 StringBuffer buf = new StringBuffer();
87 for (int i = 0; i < nLines; i++) {
89 buf.append(lineDelim);
91 IRegion region = tracker.getLineInformation(i);
92 String line = code.substring(region.getOffset(), region
94 + region.getLength());
97 return buf.toString();
98 } catch (BadLocationException e) {
107 * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#canEvaluate(net.sourceforge.phpdt.internal.corext.template.Template)
109 public boolean canEvaluate(Template template) {
113 public void setCompilationUnitVariables(ICompilationUnit cu) {
114 setVariable(CodeTemplateContextType.FILENAME, cu.getElementName());
115 setVariable(CodeTemplateContextType.PACKAGENAME, cu.getParent()
117 setVariable(CodeTemplateContextType.PROJECTNAME, cu.getJavaProject()
121 public void setFileNameVariable(String filename, String projectname) {
122 setVariable(CodeTemplateContextType.FILENAME, filename);
123 setVariable(CodeTemplateContextType.PROJECTNAME, projectname);