2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.template;
7 import java.util.HashMap;
8 import java.util.Iterator;
10 import java.util.Vector;
12 import net.sourceforge.phpdt.internal.corext.textmanipulation.MultiTextEdit;
13 import net.sourceforge.phpdt.internal.corext.textmanipulation.NopTextEdit;
14 import net.sourceforge.phpdt.internal.corext.textmanipulation.SimpleTextEdit;
15 import net.sourceforge.phpdt.internal.corext.textmanipulation.TextBuffer;
16 import net.sourceforge.phpdt.internal.corext.textmanipulation.TextBufferEditor;
17 import net.sourceforge.phpdt.internal.corext.textmanipulation.TextEdit;
18 import org.eclipse.core.runtime.CoreException;
23 * A context type is a context factory.
25 public abstract class ContextType implements ITemplateEditor {
27 /** name of the context type */
28 private final String fName;
30 /** variables used by this content type */
31 private final Map fVariables = new HashMap();
34 * Creates a context type with a name.
36 * @param name the name of the context. It has to be unique wrt to other context names.
38 public ContextType(String name) {
43 * Returns the name of the context type.
45 public String getName() {
50 * Adds a template variable to the context type.
52 public void addVariable(TemplateVariable variable) {
53 fVariables.put(variable.getName(), variable);
57 * Removes a template variable from the context type.
59 public void removeVariable(TemplateVariable variable) {
60 fVariables.remove(variable.getName());
64 * Removes all template variables from the context type.
66 public void removeAllVariables() {
71 * Returns an iterator for the variables known to the context type.
73 public Iterator variableIterator() {
74 return fVariables.values().iterator();
78 * Creates a template context.
80 public abstract TemplateContext createContext();
83 * @see ITemplateEditor#edit(TemplateBuffer)
85 public void edit(TemplateBuffer templateBuffer, TemplateContext context) throws CoreException {
86 TextBuffer textBuffer = TextBuffer.create(templateBuffer.getString());
87 TemplatePosition[] variables = templateBuffer.getVariables();
89 MultiTextEdit positions = variablesToPositions(variables);
90 MultiTextEdit multiEdit = new MultiTextEdit();
92 // iterate over all variables and try to resolve them
93 for (int i = 0; i != variables.length; i++) {
94 TemplatePosition variable = variables[i];
96 if (variable.isResolved())
99 String name = variable.getName();
100 int[] offsets = variable.getOffsets();
101 int length = variable.getLength();
103 TemplateVariable evaluator = (TemplateVariable) fVariables.get(name);
104 String value = (evaluator == null) ? null : evaluator.evaluate(context);
109 variable.setLength(value.length());
110 variable.setResolved(evaluator.isResolved(context));
112 for (int k = 0; k != offsets.length; k++)
113 multiEdit.add(SimpleTextEdit.createReplace(offsets[k], length, value));
116 TextBufferEditor editor = new TextBufferEditor(textBuffer);
117 editor.add(positions);
118 editor.add(multiEdit);
119 editor.performEdits(null);
121 positionsToVariables(positions, variables);
123 templateBuffer.setContent(textBuffer.getContent(), variables);
126 private static MultiTextEdit variablesToPositions(TemplatePosition[] variables) {
127 MultiTextEdit positions = new MultiTextEdit();
128 for (int i = 0; i != variables.length; i++) {
129 int[] offsets = variables[i].getOffsets();
130 for (int j = 0; j != offsets.length; j++)
131 positions.add(new NopTextEdit(offsets[j], 0));
137 private static void positionsToVariables(MultiTextEdit positions, TemplatePosition[] variables) {
138 Iterator iterator = positions.iterator();
140 for (int i = 0; i != variables.length; i++) {
141 TemplatePosition variable = variables[i];
143 int[] offsets = new int[variable.getOffsets().length];
144 for (int j = 0; j != offsets.length; j++)
145 offsets[j] = ((TextEdit) iterator.next()).getTextRange().getOffset();
147 variable.setOffsets(offsets);
152 * Returns the templates associated with this context type.
154 public Template[] getTemplates() {
155 Template[] templates = Templates.getInstance().getTemplates();
157 Vector vector = new Vector();
158 for (int i = 0; i != templates.length; i++)
159 if (templates[i].getContextTypeName().equals(fName))
160 vector.add(templates[i]);
162 return (Template[]) vector.toArray(new Template[vector.size()]);