Eclipse Validators
by Lawrence Mandel


Example 1:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
   id="WSDLValidator"
   name="WSDL Validator Plug-in"
   version="1.0.0">

  <runtime>
    <library name="wsdlvalidator.jar"/>
  </runtime>

  <requires>
    <import plugin="org.eclipse.ui"/>
    <import plugin="org.eclipse.core.runtime"/>
    <import plugin="org.eclipse.core.resources"/>
  </requires>
</plugin>


Example 2:

<extension point="org.eclipse.ui.popupMenus">
  <objectContribution
      objectClass="org.eclipse.core.resources.IFile"
      nameFilter="*.wsdl"
      id="org.eclipse.wsdl.validate.wsdlaction">
     <action
        id="org.eclipse.wsdl.validate.ValidateWSDLActionDelegate"
        label="Validate WSDL File"
        class="org.eclipse.wsdl.validate.ValidateWSDLActionDelegate"
        enablesFor="1"/>
  </objectContribution>
</extension>


Listing One
package org.eclipse.wsdl.validate;

import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class WSDLValidator
{
  private List errorList = new ArrayList();
  public void validate(String fileLocation)
  {
    errorList.clear();
    try
    {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setValidating(false);
      DocumentBuilder docBuilder = factory.newDocumentBuilder();
      docBuilder.setErrorHandler(new WSDLErrorHandler());
      Document doc = docBuilder.parse(fileLocation);
      Element rootElement = doc.getDocumentElement();
      if (!(rootElement.getLocalName().equals("definitions") 
            && rootElement.getNamespaceURI().equals(
              "http://schemas.xmlsoap.org/wsdl/")))
      {
        errorList
            .add(new ErrorMessage("The root element of the WSDL document "
              + "is not definitions from the namespace "
              + "http://schemas.xmlsoap.org/wsdl/.", 1));
      }
    }
    catch (Exception e)
    {
    }
  }
  public List getErrors()
  {
    return errorList;
  }
  private class WSDLErrorHandler implements ErrorHandler
  {
    public void error(SAXParseException exception)
        throws SAXException
    {
      errorList.add(new ErrorMessage(exception.getMessage(),
                exception.getLineNumber()));
    }
    public void fatalError(SAXParseException exception)
        throws SAXException
    {
      error(exception);
    }
    public void warning(SAXParseException exception)
        throws SAXException
    {
      error(exception);
    }
  }
}



Listing Two

package org.eclipse.wsdl.validate;
public class ErrorMessage 
{
  private String message;
  private int lineNumber;
  
  public ErrorMessage(String message, int lineNumber)
  {
    this.message = message;
    this.lineNumber = lineNumber;
  }
  public String getMessage()
  {
    return message;
  }
  public int getLineNumber()
  {
    return lineNumber;
  }
}


Listing Thre

package org.eclipse.wsdl.validate;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionDelegate;

public class ValidateWSDLActionDelegate implements IActionDelegate
{
  private ISelection selection;
  public void selectionChanged(IAction action, ISelection selection)
  {
    this.selection = selection;
  }
  public void run(IAction action)
  {
    if (!selection.isEmpty()
        && selection instanceof IStructuredSelection)
    {
      IStructuredSelection structuredSelection = 
        (IStructuredSelection) selection;
      Object element = structuredSelection.getFirstElement();
      if (element instanceof IFile)
      {
        ValidateAction validateAction = new ValidateAction((IFile) element);
        validateAction.run();
      }
    }
  }
}


Listing Four

package org.eclipse.wsdl.validate;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;

public class ValidateAction extends Action
{
  private IFile iFile;
  public ValidateAction(IFile iFile)
  {
    this.iFile = iFile;
  }
  public void run()
  {
    validate(iFile);
  }
  protected void validate(final IFile file)
  {
    final WSDLValidator validator = new WSDLValidator();
    IWorkspaceRunnable op = new IWorkspaceRunnable()
    {
      public void run(IProgressMonitor progressMonitor)
          throws CoreException
      {
        clearMarkers(file);
        try
        {
          String location = file.getLocation().toFile()
                            .getCanonicalFile().getAbsolutePath();
          validator.validate(location);
        }
        catch (IOException e)
        {
        }
        createMarkers(file, validator.getErrors());
      }
    };
    try
    {
      ResourcesPlugin.getWorkspace().run(op, null);
      if (validator.getErrors().isEmpty())
      {
        String title = "Validation Succeeded";
        String message = "The WSDL file is valid.";
        MessageDialog.openInformation(Display.getDefault()
            .getActiveShell(), title, message);
      }
      else
      {
        String title = "Validation Failed";
        String message = "The WSDL file is not valid. "
                       + "Please see the problems view for errors.";
        MessageDialog.openError(Display.getDefault()
            .getActiveShell(), title, message);
      }
    }
    catch (Exception e)
    {
    }
  }
  private void createMarkers(IFile iFile, List errors)
  {
    Iterator errorIter = errors.iterator();
    while (errorIter.hasNext())
    {
      ErrorMessage errorMessage = (ErrorMessage) errorIter.next();
      int line = errorMessage.getLineNumber();
      String message = errorMessage.getMessage();
      try
      {
        IMarker marker = iFile.createMarker(IMarker.PROBLEM);
        String[] attNames = new String[4];
        Object[] attValues = new Object[4];
        attNames[0] = "owner";
        attValues[0] = "org.eclipse.wsdl.validate";
        attNames[1] = IMarker.LINE_NUMBER;
        attValues[1] = new Integer(line);
        attNames[2] = IMarker.SEVERITY;
        attValues[2] = new Integer(IMarker.SEVERITY_ERROR);
        attNames[3] = IMarker.MESSAGE;
        attValues[3] = message;
        marker.setAttributes(attNames, attValues);
      }
      catch (CoreException e)
      {
      }
    }
  }
  private void clearMarkers(IFile iFile)
  {
    try
    {
      IMarker[] markers = iFile.findMarkers(null, true, IResource.DEPTH_ZERO);
      IMarker[] deleteMarkers = new IMarker[markers.length];
      int deleteindex = 0;
      Object owner;
      for (int i = markers.length - 1; i >= 0; i--)
      {
        IMarker marker = markers[i];
        owner = marker.getAttribute("owner");

        if (owner != null && owner instanceof String) if (owner
            .equals("org.eclipse.wsdl.validate"))
        {
          deleteMarkers[deleteindex++] = markers[i];
        }
      }
      if (deleteindex > 0)
      {
        IMarker[] todelete = new IMarker[deleteindex];
        System.arraycopy(deleteMarkers, 0, todelete, 0, deleteindex);
        iFile.getWorkspace().deleteMarkers(todelete);
      }
    }
    catch (CoreException e)
    {
    }
  }
}





6


