Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorteicher2004-04-14 14:21:04 +0000
committerteicher2004-04-14 14:21:04 +0000
commitfeec0ceb0b6a70b3211729f5051a965caf07b8c3 (patch)
tree6e5f89547065d32c4dacd18756c9432ea103acfd
parentf76337eb2bc5da86cf9a5fee0d62a4c4421068a4 (diff)
downloadeclipse.platform.text-feec0ceb0b6a70b3211729f5051a965caf07b8c3.tar.gz
eclipse.platform.text-feec0ceb0b6a70b3211729f5051a965caf07b8c3.tar.xz
eclipse.platform.text-feec0ceb0b6a70b3211729f5051a965caf07b8c3.zip
template validation throws now an exception
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateCompletionProcessor.java5
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateProposal.java14
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateSet.java12
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateStore.java12
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/ContextType.java97
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/DocumentTemplateContext.java5
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateContext.java5
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateException.java56
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateTranslator.java14
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/templates/EditTemplateDialog.java28
10 files changed, 157 insertions, 91 deletions
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateCompletionProcessor.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateCompletionProcessor.java
index da1a99d31..e55ec472d 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateCompletionProcessor.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateCompletionProcessor.java
@@ -69,8 +69,11 @@ public abstract class TemplateCompletionProcessor implements IContentAssistProce
List matches= new ArrayList();
for (int i= 0; i < templates.length; i++) {
Template template= templates[i];
- if (context.getContextType().validate(template.getPattern()) != null)
+ try {
+ context.getContextType().validate(template.getPattern());
+ } catch (TemplateException e) {
continue;
+ }
if (template.matches(prefix, context.getContextType().getId()))
matches.add(new TemplateProposal(template, context, region, getImage(template), getRelevance(template, prefix)));
}
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateProposal.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateProposal.java
index b464e5f8b..cf063b5a6 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateProposal.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/TemplateProposal.java
@@ -106,8 +106,10 @@ public class TemplateProposal implements ICompletionProposal, ICompletionProposa
try {
fContext.setReadOnly(false);
- TemplateBuffer templateBuffer= fContext.evaluate(fTemplate);
- if (templateBuffer == null) {
+ TemplateBuffer templateBuffer;
+ try {
+ templateBuffer= fContext.evaluate(fTemplate);
+ } catch (TemplateException e1) {
fSelectedRegion= fRegion;
return;
}
@@ -266,10 +268,12 @@ public class TemplateProposal implements ICompletionProposal, ICompletionProposa
public String getAdditionalProposalInfo() {
try {
fContext.setReadOnly(true);
- TemplateBuffer templateBuffer= fContext.evaluate(fTemplate);
-
- if (templateBuffer == null)
+ TemplateBuffer templateBuffer;
+ try {
+ templateBuffer= fContext.evaluate(fTemplate);
+ } catch (TemplateException e1) {
return null;
+ }
return templateBuffer.getString();
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateSet.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateSet.java
index 64c702a02..6d085cef6 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateSet.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateSet.java
@@ -49,6 +49,7 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.templates.ContextType;
import org.eclipse.jface.text.templates.ContextTypeRegistry;
import org.eclipse.jface.text.templates.Template;
+import org.eclipse.jface.text.templates.TemplateException;
import org.eclipse.jface.text.templates.TemplateMessages;
/**
@@ -80,7 +81,7 @@ public class TemplateSet {
/**
* Convenience method for reading templates from a file.
*
- * @see #addFromStream(InputStream, boolean, boolean)
+ * @see #addFromStream(InputStream, boolean, boolean, ResourceBundle)
*/
public void addFromFile(File file, boolean allowDuplicates, ResourceBundle bundle) throws CoreException {
InputStream stream= null;
@@ -204,12 +205,17 @@ public class TemplateSet {
return TemplateMessages.getString(key); // default messages
}
- protected String validateTemplate(Template template) throws CoreException {
+ protected String validateTemplate(Template template) {
ContextType type= fRegistry.getContextType(template.getContextTypeId());
if (type == null) {
return "Unknown context type: " + template.getContextTypeId(); //$NON-NLS-1$
}
- return type.validate(template.getPattern());
+ try {
+ type.validate(template.getPattern());
+ return null;
+ } catch (TemplateException e) {
+ return e.getMessage();
+ }
}
private String getAttributeValue(NamedNodeMap attributes, String name) {
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateStore.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateStore.java
index b0a4778ad..71965eea1 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateStore.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/templates/persistence/TemplateStore.java
@@ -36,6 +36,7 @@ import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.Assert;
import org.eclipse.jface.text.templates.ContextTypeRegistry;
import org.eclipse.jface.text.templates.Template;
+import org.eclipse.jface.text.templates.TemplateException;
/**
* Manages templates. Handles reading default templates contributed via XML and
@@ -365,7 +366,16 @@ public class TemplateStore {
*/
private boolean validateTemplate(Template template) {
String contextTypeId= template.getContextTypeId();
- return contextExists(contextTypeId) && (fRegistry == null || fRegistry.getContextType(contextTypeId).validate(template.getPattern()) == null);
+ if (contextExists(contextTypeId)) {
+ if (fRegistry != null)
+ try {
+ fRegistry.getContextType(contextTypeId).validate(template.getPattern());
+ } catch (TemplateException e) {
+ return false;
+ }
+ return true;
+ } else
+ return false;
}
/**
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/templates/ContextType.java b/org.eclipse.text/src/org/eclipse/jface/text/templates/ContextType.java
index ecca59e6c..aec9c4fa8 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/templates/ContextType.java
+++ b/org.eclipse.text/src/org/eclipse/jface/text/templates/ContextType.java
@@ -50,7 +50,8 @@ public class ContextType {
private String fName= null;
/**
- * Creates a context type with an identifier. The identifier must be unique, a qualified name is suggested. The id is also used as name.
+ * Creates a context type with an identifier. The identifier must be unique,
+ * a qualified name is suggested. The id is also used as name.
*
* @param id the unique identifier of the context type
*/
@@ -72,6 +73,25 @@ public class ContextType {
}
/**
+ * Returns the name of the context type.
+ *
+ * @return the name of the receiver
+ */
+ public String getId() {
+ return fId;
+ }
+
+
+ /**
+ * Returns the name of the context type.
+ *
+ * @return the name of the context type
+ */
+ public String getName() {
+ return fName;
+ }
+
+ /**
* Creates a context type with a <code>null</code> identifier.
* <p>
* This is a framework-only constructor that exists only so that context
@@ -87,26 +107,34 @@ public class ContextType {
* Sets the id of this context.
* <p>
* This is a framework-only method that exists solely so that context types
- * can be contributed via an extension point and that should not be called in
- * client code; use {@link #ContextType(String)} instead.
+ * can be contributed via an extension point and that should not be called
+ * in client code; use {@link #ContextType(String)} instead.
* </p>
*
- * @param id
- * @throws RuntimeException an unspecified exception if the id has already been set on this context type
+ * @param id the identifier of this context
+ * @throws RuntimeException an unspecified exception if the id has already
+ * been set on this context type
*/
- public void setId(String id) throws RuntimeException {
+ final void setId(String id) throws RuntimeException {
Assert.isNotNull(id);
Assert.isTrue(fId == null); // may only be called once when the context is instantiated
fId= id;
}
/**
- * Returns the name of the context type.
+ * Sets the name of the context type.
*
- * @return the name of the receiver
+ * <p>
+ * This is a framework-only method that exists solely so that context types
+ * can be contributed via an extension point and that should not be called
+ * in client code; use {@link #ContextType(String, String)} instead.
+ * </p>
+ *
+ * @param name the name of the context type
*/
- public String getId() {
- return fId;
+ final void setName(String name) {
+ Assert.isTrue(fName == null); // only initialized by extension code
+ fName= name;
}
/**
@@ -116,6 +144,7 @@ public class ContextType {
* @param resolver the resolver to be added under its name
*/
public void addResolver(TemplateVariableResolver resolver) {
+ Assert.isNotNull(resolver);
fResolvers.put(resolver.getType(), resolver);
}
@@ -125,6 +154,7 @@ public class ContextType {
* @param resolver the varibable to be removed
*/
public void removeResolver(TemplateVariableResolver resolver) {
+ Assert.isNotNull(resolver);
fResolvers.remove(resolver.getType());
}
@@ -158,22 +188,23 @@ public class ContextType {
* Validates a pattern and returnes <code>null</code> if the validation was
* a success or an error message if not.
*
- * XXX subject to change - will throw an exception
- *
* @param pattern the template pattern to validate
- * @return the translated pattern if successful, or an error message if not TODO what do we return there? throw an exception
+ * @throws TemplateException if the pattern is invalid
*/
- public String validate(String pattern) {
+ public void validate(String pattern) throws TemplateException {
TemplateTranslator translator= new TemplateTranslator();
TemplateBuffer buffer= translator.translate(pattern);
- if (buffer != null) {
- return validateVariables(buffer.getVariables());
- }
- return translator.getErrorMessage();
+ validateVariables(buffer.getVariables());
}
- protected String validateVariables(TemplateVariable[] variables) {
- return null;
+ /**
+ * Validates the variables in this context type. If a variable is not valid, e.g. if its type is not known
+ * in this context type, a <code>TemplateException</code> is thrown.
+ *
+ * @param variables the variables to validate
+ * @throws TemplateException if one of the variables is not valid in this context type
+ */
+ protected void validateVariables(TemplateVariable[] variables) throws TemplateException {
}
/**
@@ -186,6 +217,7 @@ public class ContextType {
* @throws BadLocationException if the buffer cannot be successfully modified
*/
public void resolve(TemplateBuffer buffer, TemplateContext context) throws MalformedTreeException, BadLocationException {
+ Assert.isNotNull(context);
TemplateVariable[] variables= buffer.getVariables();
List positions= variablesToPositions(variables);
@@ -253,29 +285,4 @@ public class ContextType {
variable.setOffsets(offsets);
}
}
-
- /**
- * Returns the name of the context type.
- *
- * @return the name of the context type
- */
- public String getName() {
- return fName;
- }
-
- /**
- * Sets the name of the context type.
- *
- * <p>
- * This is a framework-only method that exists solely so that context types
- * can be contributed via an extension point and that should not be called in
- * client code; use {@link #ContextType(String, String)} instead.
- * </p>
-
- * @param name the name of the context type
- */
- public void setName(String name) {
- Assert.isTrue(fName == null); // only initialized by extension code
- fName= name;
- }
}
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/templates/DocumentTemplateContext.java b/org.eclipse.text/src/org/eclipse/jface/text/templates/DocumentTemplateContext.java
index b921d50c7..1c5bf82ca 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/templates/DocumentTemplateContext.java
+++ b/org.eclipse.text/src/org/eclipse/jface/text/templates/DocumentTemplateContext.java
@@ -137,16 +137,13 @@ public class DocumentTemplateContext extends TemplateContext {
/*
* @see org.eclipse.jface.text.templates.TemplateContext#evaluate(org.eclipse.jface.text.templates.Template)
*/
- public TemplateBuffer evaluate(Template template) throws BadLocationException {
+ public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
if (!canEvaluate(template))
return null;
TemplateTranslator translator= new TemplateTranslator();
TemplateBuffer buffer= translator.translate(template);
- if (buffer == null)
- return buffer;
-
getContextType().resolve(buffer, this);
return buffer;
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateContext.java b/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateContext.java
index 0b67ea9a6..61145acd1 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateContext.java
+++ b/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateContext.java
@@ -96,9 +96,10 @@ public abstract class TemplateContext {
*
* @param template the template to evaluate
* @return returns the buffer with the evaluated template or <code>null</code> if the buffer could not be created
- * @throws BadLocationException if evaluation fails
+ * @throws BadLocationException if evaluation fails due to concurrently changed documents etc.
+ * @throws TemplateException if the template specification is not valid
*/
- public abstract TemplateBuffer evaluate(Template template) throws BadLocationException;
+ public abstract TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException;
/**
* Tests if the specified template can be evaluated in this context.
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateException.java b/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateException.java
new file mode 100644
index 000000000..ccd05b977
--- /dev/null
+++ b/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateException.java
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.jface.text.templates;
+
+
+/**
+ * Thrown when a template cannot be validated.
+ *
+ * @since 3.0
+ */
+public class TemplateException extends Exception {
+
+ /**
+ * Creates a new template exception.
+ */
+ public TemplateException() {
+ super();
+ }
+
+ /**
+ * Creates a new template exception.
+ *
+ * @param message the message describing the problem that arised
+ */
+ public TemplateException(String message) {
+ super(message);
+ }
+
+ /**
+ * Creates a new template exception.
+ *
+ * @param message the message describing the problem that arised
+ * @param cause the original exception
+ */
+ public TemplateException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ /**
+ * Creates a new template exception.
+ *
+ * @param cause the original exception
+ */
+ public TemplateException(Throwable cause) {
+ super(cause);
+ }
+}
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateTranslator.java b/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateTranslator.java
index 6ae821f24..7fc67316e 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateTranslator.java
+++ b/org.eclipse.text/src/org/eclipse/jface/text/templates/TemplateTranslator.java
@@ -75,7 +75,7 @@ public class TemplateTranslator {
* if there was an error.
* @see #getErrorMessage()
*/
- public TemplateBuffer translate(Template template) {
+ public TemplateBuffer translate(Template template) throws TemplateException {
return translate(template.getPattern());
}
@@ -89,7 +89,7 @@ public class TemplateTranslator {
* if there was an error.
* @see #getErrorMessage()
*/
- public TemplateBuffer translate(String string) {
+ public TemplateBuffer translate(String string) throws TemplateException {
fBuffer.setLength(0);
fOffsets.clear();
@@ -98,7 +98,7 @@ public class TemplateTranslator {
fErrorMessage= null;
if (!parse(string))
- return null;
+ throw new TemplateException(fErrorMessage);
switch (fState) {
case TEXT:
@@ -106,15 +106,11 @@ public class TemplateTranslator {
// illegal
case ESCAPE:
- fErrorMessage= TemplateMessages.getString("TemplateTranslator.error.incomplete.variable"); //$NON-NLS-1$
- fBuffer.append(ESCAPE_CHARACTER);
- return null;
+ throw new TemplateException(TemplateMessages.getString("TemplateTranslator.error.incomplete.variable")); //$NON-NLS-1$
// illegal
case IDENTIFIER:
- fErrorMessage= TemplateMessages.getString("TemplateTranslator.error.incomplete.variable"); //$NON-NLS-1$
- fBuffer.append(ESCAPE_CHARACTER);
- return null;
+ throw new TemplateException(TemplateMessages.getString("TemplateTranslator.error.incomplete.variable")); //$NON-NLS-1$
}
int[] offsets= new int[fOffsets.size()];
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/templates/EditTemplateDialog.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/templates/EditTemplateDialog.java
index 6181553e7..1ec53da15 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/templates/EditTemplateDialog.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/templates/EditTemplateDialog.java
@@ -18,7 +18,6 @@ import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.VerifyKeyListener;
-import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
@@ -58,6 +57,8 @@ import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.jface.text.templates.ContextType;
import org.eclipse.jface.text.templates.ContextTypeRegistry;
import org.eclipse.jface.text.templates.Template;
+import org.eclipse.jface.text.templates.TemplateException;
+
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.IUpdate;
@@ -293,10 +294,11 @@ public class EditTemplateDialog extends StatusDialog {
fValidationStatus.setOK();
ContextType contextType= fContextTypeRegistry.getContextType(fTemplate.getContextTypeId());
if (contextType != null) {
- String errorMessage= contextType.validate(text);
- if (errorMessage != null) {
- fValidationStatus.setError(errorMessage);
- }
+ try {
+ contextType.validate(text);
+ } catch (TemplateException e) {
+ fValidationStatus.setError(e.getLocalizedMessage());
+ }
}
updateUndoAction();
@@ -391,22 +393,6 @@ public class EditTemplateDialog extends StatusDialog {
return viewer;
}
- private void handleKeyPressed(KeyEvent event) {
- if (event.stateMask != SWT.MOD1)
- return;
-
- switch (event.character) {
- case ' ':
- fPatternEditor.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
- break;
-
- // CTRL-Z
- case 'z' - 'a' + 1:
- fPatternEditor.doOperation(ITextOperationTarget.UNDO);
- break;
- }
- }
-
private void handleVerifyKeyPressed(VerifyEvent event) {
if (!event.doit)
return;

Back to the top