validation - Implement validator in Eclipse -


i trying implement validation in eclipse. work has many little projects serve customize our product various customers. since there ton of developers trying find way enforce various standards , best practices.

for example, have many xml configurations want validate. built-in validators ensure files well-formed , follow schema, add validations such checking java class referenced in xml exists on classpath. example validating java class implementing interface not have object variables (i.e. code needs operate on parameters , not maintain state).

it appears there 2 ways add validation. first through builder adds markers. second through stand-alone validation. however, not building anything, , have not found useful tutorials or examples on validation (does not help.eclipse.org being moved , unavailable).

when right-click test project , select "validate" message stating there error during validation, , test message not show in problem view. however, there no errors in eclipse log. host eclipse shows nothing in console. no exceptions logged anywhere, , no message. project have required custom nature.

i following these instructions there no code or functioning example, , google has not been kind enough fill in blanks. combined eclipse site being down right now, @ loss how proceed.

plugin.xml:

<plugin>   <extension name="my validator" point="org.eclipse.wst.validation.validator"              id="com.mycompany.pluginname.validator.myvalidator">     <validator>       <projectnature id="com.mycompany.pluginname.nature.mynature"/>       <helper class="org.eclipse.wst.validation.internal.operations.workbenchcontext"/>       <markerid markeridvalue="com.mycompany.pluginname.validator.defaultmarker"/>       <run class="com.mycompany.pluginname.validation.validator.myvalidator"/>       <runstrategy project="true"/>     </validator>   </extension>   <extension point="org.eclipse.core.resources.markers" name="my validator"              id="com.mycompany.pluginname.validator.defaultmarker">     <super type="org.eclipse.core.resources.problemmarker"/>     <persistent value="true"/>     <attribute name="owner"/>     <attribute name="validationseverity"/>     <attribute name="targetobject"/>     <attribute name="groupname"/>     <attribute name="messageid"/>   </extension> </plugin> 

validator code:

package com.mycompany.pluginname.validation.validator;  import org.eclipse.core.resources.iproject; import org.eclipse.wst.validation.internal.core.message; import org.eclipse.wst.validation.internal.core.validationexception; import org.eclipse.wst.validation.internal.operations.iworkbenchcontext; import org.eclipse.wst.validation.internal.provisional.core.*;  import com.mycompany.pluginname.validation.plugin.validationplugin;  @suppresswarnings("restriction") public class myvalidator     implements ivalidator {    @override   public void cleanup(ireporter argreporter) {     argreporter.removeallmessages(this);   }    @override   public void validate(ivalidationcontext argcontext, ireporter argreporter)       throws validationexception {     string bundle = validationplugin.getdefault().gettranslationsbundlename();     iproject prj = ((iworkbenchcontext) argcontext).getproject();     string[] attributes =         new string[] {"owner", "validationseverity", "targetobject", "groupname", "messageid"};     imessage msg = new message(bundle, imessage.high_severity, "test", attributes, prj);     argreporter.addmessage(this, msg);   }  } 

i find odd adding validation require using restricted packages , interfaces. seems odd wants imessage rather imarker.

i did @ eclipse plugin custom validation seems oriented around creating new editor, want validate files regardless of editor used (in fact not want create editor).

edit: updated use v2 framework, nothing appears in problem view. doing wrong? there tutorial somewhere explains how works? able figure out following, not correct:

  public validationresult validate(validationevent argevent, validationstate argstate,       iprogressmonitor argmonitor) {     final iresource resource = argevent.getresource();     final validationresult result = new validationresult();     try {       list<string> contents = resources.readfile((ifile) resource);       (int = 0; < contents.size(); ++i) {         int offset = contents.get(i).indexof("bad_string");         if (offset >= 0) {           result.add(validatormessage.create("found bad string", resource));           result.incrementerror(1);         }       }     }     catch (exception ex) {       result.add(validatormessage.create(ex.getmessage(), resource));     }     return result;   } 

i admit stab in dark: documentation not descriptive , have not found tutorials on v2 validator. oh, have filter on validator receives specific xml files, why there no input validation.

also, since pedant myself, using old-style loop there because expect show line number error user. not quite there yet.

another edit: here working code. issue squiggly not on correct line because offset start of file, not line. work:

  public validationresult validate(validationevent argevent, validationstate argstate,       iprogressmonitor argmonitor) {     final iresource resource = argevent.getresource();     final validationresult result = new validationresult();     try {       list<string> contents = resources.readfile((ifile) resource);       int location = 0;       (int = 0; < contents.size(); ++i) {         int offset = contents.get(i).indexof(constant);         if (offset >= 0) {           validatormessage vm = validatormessage.create("message", resource);           vm.setattribute(imarker.severity, imarker.severity_error);           vm.setattribute(imarker.source_id, imarker.problem);           vm.setattribute(imarker.line_number, + 1);           vm.setattribute(imarker.char_start, location + offset);           vm.setattribute(imarker.char_end, location + offset + constant.length());           result.add(vm);         }         // todo: account different line endings.         location += (line.length() + 2);       }     }     catch (exception ex) {       validationplugin.getdefault().warn(ex);       result.add(validatormessage.create(ex.tostring(), resource));     }     return result;   } 

plugin.xml:

  <extension name="my validator" point="org.eclipse.wst.validation.validatorv2"              id="com.company.plugin.validation.validator.myvalidator">     <validator class="com.company.plugin.validation.validator.myvalidator">       <include>         <rules>           <file type="file" name="filename.xml"/>         </rules>       </include>     </validator>   </extension> 

i found question along these lines corroborates found: setting imarker.char_start , imarker.char_end attributes imarkers annotations

sigh document out of date. should use org.eclipse.wst.validation.validatorv2 extension point, extending newer org.eclipse.wst.validation.abstractvalidator class.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -