Adding custom validator
From Aptana Development
Adding Content Validator for an Aptana Editor
This page describes how to add custom validator for an existing Aptana Editor.
We register a validator for a particular content type (MIME type) by extending extension-point “com.aptana.ide.editors.validator”. Aptana editors look up validators registered via the extension point for the content type they edit. Whenever there are modifications to editor text, all the registered validators for that content type are invoked. Errors/warning list supplied by the validators is then displayed in the Problems View.
Instructions
As a simple, hypothetical example, let us write an html validator that will complain whenever inline css style is used. The validator will detect code position where a <style> tag is found and will provide a warning message suggesting use of external stylesheets.
1. Add <validator> entry to “com.aptana.ide.editors.validator” extension point for editor in question, in this case, html editor ( com.aptana.ide.editor.html). Since html editor already has Tidy Html Validator registered, ours will be second validator.
<extension
point="com.aptana.ide.editors.validator">
<validator
class="com.aptana.ide.editor.html.validator.TidyHtmlValidator"
mimeType="text/html"
name="Tidy Html Validator" />
<validator
class="com.aptana.ide.editor.html.validator.NoInlineStyleHtmlValidator"
mimeType="text/html"
name="No Inline Style Validator" />
</extension>
- class
- Fully qualified Java class name implementing com.aptana.ide.editors.IValidator interface.
- mimeType
- MIME type of content that this validator validates
- name
- Descriptive name of the validator, will be used by preference UI to let users enable/disable the validator.
2. Write Java class specified in step 1 above, implementing IValidator interface.
The com.aptana.ide.editors.IValidator interface has just one function:
IFileError[] parseForErrors(
String path,
String source,
IFileSourceProvider sourceProvider,
boolean collectErrors,
boolean collectWarnings,
boolean collectInfos);
- path
- The path to the file on disk or elsewhere. Use to indicate to the problems pane where the file is on disk
- souce
- The actual text of the file. This has all other languages stripped out, replaced with whitespace, so line number positions are preserved.
- sourceProvider
- The eclipse source provider. Generally used in a constructor to UnifiedErrorReporter
- collectErrors
- Are we showing errors?
- collectWarnings
- Are we showing warnings?
- collectInfos
- Are we showing information messages?
Instead of implementing IValidator interface directly, it is recommended to extend from abstract base class com.aptana.ide.editors.ValidatorBase which implements the interface, and then override parseForErrors() method of the base class.
For the example scenario at hand, we can have a simple implementation as below. There is lot of scope for further improvement - e.g. while looking for style tag, this validator does not ignore HTML comments. Also, this simple example does case sensitive search, so it is not able to find an HTML style tag if it is not in lower case. Also if the tag beginning marker (“<”) and the keyword “style” are not in the same line, it will not be detected.
public IFileError[] parseForErrors(String path, String source, IFileSourceProvider sourceProvider, boolean collectErrors, boolean collectWarnings)
{
if(!collectWarnings)
{
return new IFileError[0];
}
int lineNo = 0, charCount = 0;
BufferedReader reader = new BufferedReader(new StringReader(source));
UnifiedErrorReporter reporter = new UnifiedErrorReporter(sourceProvider);
try
{
String line = reader.readLine();
while(line != null)
{
lineNo++;
Matcher matcher = pattern.matcher(line);
if(matcher.matches())
{
reporter.error("Instead of style tag, use external stylesheet", path, lineNo, line, matcher.start(1) );
}
charCount += line.length();
line = reader.readLine();
}
}
catch(IOException ex)
{
IdeLog.logError(HTMLPlugin.getDefault(), "I/O error", ex);
}
return reporter.getErrors();
}
3. Verify that the validator thus registered is detected in Aptana. Build everything and launch IDE. Go to Window > Preferences > Aptana > Editors > (The editor for which you registered validator e.g. Html Editor) > Problems View. You should see the validator just created listed as "No Inline Style Validator".
Building on this example, one can devise a generalized scheme to read “prohibited” tags from configuration/preference and provide error list accordingly.
The IValidator interface can be used to integrate some existing third party validator with Aptana editors e.g. Aptana HTML validator (Tidy HTML Validator) relies on W3C Tidy to do the actual validation.. If you are interested, you can take a look at the default validators supplied with Aptana:
- com.aptana.ide.editor.html.validator.TidyHtmlValidator
- com.aptana.ide.editor.css.validator.StylesheetValidator
- com.aptana.ide.editor.js.validator.MozillaJsValidator


