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;
33 private IJavaProject fProject;
35 public CodeTemplateContext(String contextTypeName, IJavaProject project, String lineDelim) {
36 super(PHPeclipsePlugin.getDefault().getCodeTemplateContextRegistry().getContextType(contextTypeName));
37 fLineDelimiter= lineDelim;
41 public IJavaProject getJavaProject() {
46 * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#evaluate(net.sourceforge.phpdt.internal.corext.template.Template)
48 public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
49 // test that all variables are defined
50 Iterator iterator= getContextType().resolvers();
51 while (iterator.hasNext()) {
52 TemplateVariableResolver var= (TemplateVariableResolver) iterator.next();
53 if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
54 // Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$
58 if (!canEvaluate(template))
61 String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter);
63 TemplateTranslator translator= new TemplateTranslator();
64 TemplateBuffer buffer= translator.translate(pattern);
65 getContextType().resolve(buffer, this);
70 private static String changeLineDelimiter(String code, String lineDelim) {
72 ILineTracker tracker= new DefaultLineTracker();
74 int nLines= tracker.getNumberOfLines();
79 StringBuffer buf= new StringBuffer();
80 for (int i= 0; i < nLines; i++) {
82 buf.append(lineDelim);
84 IRegion region = tracker.getLineInformation(i);
85 String line= code.substring(region.getOffset(), region.getOffset() + region.getLength());
88 return buf.toString();
89 } catch (BadLocationException e) {
96 * @see net.sourceforge.phpdt.internal.corext.template.TemplateContext#canEvaluate(net.sourceforge.phpdt.internal.corext.template.Template)
98 public boolean canEvaluate(Template template) {
102 public void setCompilationUnitVariables(ICompilationUnit cu) {
103 setVariable(CodeTemplateContextType.FILENAME, cu.getElementName());
104 setVariable(CodeTemplateContextType.PACKAGENAME, cu.getParent().getElementName());
105 setVariable(CodeTemplateContextType.PROJECTNAME, cu.getJavaProject().getElementName());
108 public void setFileNameVariable(String filename) {
109 setVariable(CodeTemplateContextType.FILENAME, filename);