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.phpdt.internal.corext.Assert;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
34 private IJavaProject fProject;
36 public CodeTemplateContext(String contextTypeName, IJavaProject project, String lineDelim) {
37 super(PHPeclipsePlugin.getDefault().getCodeTemplateContextRegistry().getContextType(contextTypeName));
38 fLineDelimiter= lineDelim;
42 public IJavaProject getJavaProject() {
47 * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#evaluate(net.sourceforge.phpdt.internal.corext.template.Template)
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$
59 if (!canEvaluate(template))
62 String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter);
64 TemplateTranslator translator= new TemplateTranslator();
65 TemplateBuffer buffer= translator.translate(pattern);
66 getContextType().resolve(buffer, this);
71 private static String changeLineDelimiter(String code, String lineDelim) {
73 ILineTracker tracker= new DefaultLineTracker();
75 int nLines= tracker.getNumberOfLines();
80 StringBuffer buf= new StringBuffer();
81 for (int i= 0; i < nLines; i++) {
83 buf.append(lineDelim);
85 IRegion region = tracker.getLineInformation(i);
86 String line= code.substring(region.getOffset(), region.getOffset() + region.getLength());
89 return buf.toString();
90 } catch (BadLocationException e) {
97 * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#canEvaluate(net.sourceforge.phpdt.internal.corext.template.Template)
99 public boolean canEvaluate(Template template) {
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());