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;
19 import org.eclipse.core.runtime.CoreException;
24 * A context type is a context factory.
26 public abstract class ContextType implements ITemplateEditor {
28 /** name of the context type */
29 private final String fName;
31 /** variables used by this content type */
32 private final Map fVariables = new HashMap();
35 * Creates a context type with a name.
37 * @param name the name of the context. It has to be unique wrt to other context names.
39 public ContextType(String name) {
44 * Returns the name of the context type.
46 public String getName() {
51 * Adds a template variable to the context type.
53 public void addVariable(TemplateVariable variable) {
54 fVariables.put(variable.getName(), variable);
58 * Removes a template variable from the context type.
60 public void removeVariable(TemplateVariable variable) {
61 fVariables.remove(variable.getName());
65 * Removes all template variables from the context type.
67 public void removeAllVariables() {
72 * Returns an iterator for the variables known to the context type.
74 public Iterator variableIterator() {
75 return fVariables.values().iterator();
79 * Creates a template context.
81 public abstract TemplateContext createContext();
84 * @see ITemplateEditor#edit(TemplateBuffer)
86 public void edit(TemplateBuffer templateBuffer, TemplateContext context) throws CoreException {
87 TextBuffer textBuffer = TextBuffer.create(templateBuffer.getString());
88 TemplatePosition[] variables = templateBuffer.getVariables();
90 MultiTextEdit positions = variablesToPositions(variables);
91 MultiTextEdit multiEdit = new MultiTextEdit();
93 // iterate over all variables and try to resolve them
94 for (int i = 0; i != variables.length; i++) {
95 TemplatePosition variable = variables[i];
97 if (variable.isResolved())
100 String name = variable.getName();
101 int[] offsets = variable.getOffsets();
102 int length = variable.getLength();
104 TemplateVariable evaluator = (TemplateVariable) fVariables.get(name);
105 String value = (evaluator == null) ? null : evaluator.evaluate(context);
110 variable.setLength(value.length());
111 variable.setResolved(evaluator.isResolved(context));
113 for (int k = 0; k != offsets.length; k++)
114 multiEdit.add(SimpleTextEdit.createReplace(offsets[k], length, value));
117 TextBufferEditor editor = new TextBufferEditor(textBuffer);
118 editor.add(positions);
119 editor.add(multiEdit);
120 editor.performEdits(null);
122 positionsToVariables(positions, variables);
124 templateBuffer.setContent(textBuffer.getContent(), variables);
127 private static MultiTextEdit variablesToPositions(TemplatePosition[] variables) {
128 MultiTextEdit positions = new MultiTextEdit();
129 for (int i = 0; i != variables.length; i++) {
130 int[] offsets = variables[i].getOffsets();
131 for (int j = 0; j != offsets.length; j++)
132 positions.add(new NopTextEdit(offsets[j], 0));
138 private static void positionsToVariables(MultiTextEdit positions, TemplatePosition[] variables) {
139 Iterator iterator = positions.iterator();
141 for (int i = 0; i != variables.length; i++) {
142 TemplatePosition variable = variables[i];
144 int[] offsets = new int[variable.getOffsets().length];
145 for (int j = 0; j != offsets.length; j++)
146 offsets[j] = ((TextEdit) iterator.next()).getTextRange().getOffset();
148 variable.setOffsets(offsets);
153 * Returns the templates associated with this context type.
155 public Template[] getTemplates() {
156 Template[] templates = Templates.getInstance().getTemplates();
158 Vector vector = new Vector();
159 for (int i = 0; i != templates.length; i++)
160 if (templates[i].getContextTypeName().equals(fName))
161 vector.add(templates[i]);
163 return (Template[]) vector.toArray(new Template[vector.size()]);