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;
21 * A context type is a context factory.
23 public abstract class ContextType implements ITemplateEditor {
25 /** name of the context type */
26 private final String fName;
28 /** variables used by this content type */
29 private final Map fVariables = new HashMap();
32 * Creates a context type with a name.
34 * @param name the name of the context. It has to be unique wrt to other context names.
36 public ContextType(String name) {
41 * Returns the name of the context type.
43 public String getName() {
48 * Adds a template variable to the context type.
50 public void addVariable(TemplateVariable variable) {
51 fVariables.put(variable.getName(), variable);
55 * Removes a template variable from the context type.
57 public void removeVariable(TemplateVariable variable) {
58 fVariables.remove(variable.getName());
62 * Removes all template variables from the context type.
64 public void removeAllVariables() {
69 * Returns an iterator for the variables known to the context type.
71 public Iterator variableIterator() {
72 return fVariables.values().iterator();
76 * Creates a template context.
78 public abstract TemplateContext createContext();
81 * @see ITemplateEditor#edit(TemplateBuffer)
83 public void edit(TemplateBuffer templateBuffer, TemplateContext context) throws CoreException {
84 TextBuffer textBuffer = TextBuffer.create(templateBuffer.getString());
85 TemplatePosition[] variables = templateBuffer.getVariables();
87 MultiTextEdit positions = variablesToPositions(variables);
88 MultiTextEdit multiEdit = new MultiTextEdit();
90 // iterate over all variables and try to resolve them
91 for (int i = 0; i != variables.length; i++) {
92 TemplatePosition variable = variables[i];
94 if (variable.isResolved())
97 String name = variable.getName();
98 int[] offsets = variable.getOffsets();
99 int length = variable.getLength();
101 TemplateVariable evaluator = (TemplateVariable) fVariables.get(name);
102 String value = (evaluator == null) ? null : evaluator.evaluate(context);
107 variable.setLength(value.length());
108 variable.setResolved(evaluator.isResolved(context));
110 for (int k = 0; k != offsets.length; k++)
111 multiEdit.add(SimpleTextEdit.createReplace(offsets[k], length, value));
114 TextBufferEditor editor = new TextBufferEditor(textBuffer);
115 editor.add(positions);
116 editor.add(multiEdit);
117 editor.performEdits(null);
119 positionsToVariables(positions, variables);
121 templateBuffer.setContent(textBuffer.getContent(), variables);
124 private static MultiTextEdit variablesToPositions(TemplatePosition[] variables) {
125 MultiTextEdit positions = new MultiTextEdit();
126 for (int i = 0; i != variables.length; i++) {
127 int[] offsets = variables[i].getOffsets();
128 for (int j = 0; j != offsets.length; j++)
129 positions.add(new NopTextEdit(offsets[j], 0));
135 private static void positionsToVariables(MultiTextEdit positions, TemplatePosition[] variables) {
136 Iterator iterator = positions.iterator();
138 for (int i = 0; i != variables.length; i++) {
139 TemplatePosition variable = variables[i];
141 int[] offsets = new int[variable.getOffsets().length];
142 for (int j = 0; j != offsets.length; j++)
143 offsets[j] = ((TextEdit) iterator.next()).getTextRange().getOffset();
145 variable.setOffsets(offsets);
150 * Returns the templates associated with this context type.
152 public Template[] getTemplates() {
153 Template[] templates = Templates.getInstance().getTemplates();
155 Vector vector = new Vector();
156 for (int i = 0; i != templates.length; i++)
157 if (templates[i].getContextTypeName().equals(fName))
158 vector.add(templates[i]);
160 return (Template[]) vector.toArray(new Template[vector.size()]);