Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

XMLSchema Validation With XMLProcessor

Since EMF 2.2, you can use XMLProcessor to instantiate an in-memory model from an XML Schema, then use Diagnostician.validate() to validate an XML instance document to ensure it complies with that schema.

For example, here's some code derived from the RSSFeedValidatorDynamicTask. You can also generate code using EMF, then use that generated code to validate using something like RSSFeedValidatorGeneratedTask

/**
 * <copyright>
 *
 * Copyright (c) 2005 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 - Initial API and implementation
 *
 * </copyright>
 *
 * $Id$
 */

import java.io.File;
import java.util.Iterator;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.emf.ecore.xmi.util.XMLProcessor;

public class Validator
{

  public static void main(String[] args)
  {
      String xsdFile = "schema/atom10_build.xsd";
      String xmlFile = "data/builds-eclipse.xml";
      org.eclipse.emf.common.util.URI xsdURI = null;
      XMLProcessor processor = null;
      Resource resource = null;
      try
      {
        xsdURI = URI.createFileURI((new File(xsdFile)).getCanonicalPath());
        processor = new XMLProcessor(xsdURI);
        resource = processor.load(new File(xmlFile).getAbsolutePath(), null);
      }
      catch (Exception e)
      {
        e.printStackTrace();
      } 
      
      EObject document = (EObject)resource.getContents().get(0);
      EObject feed = (EObject)document.eGet(document.eClass().getEStructuralFeature("feed")); 

      Diagnostician validator = Diagnostician.INSTANCE;

      // Validate the feed and inspect the resulting diagnostic.
      Diagnostic diagnostic = validator.validate(feed);
      printDiagnostic(diagnostic, 0);
  }

  private static void printDiagnostic(Diagnostic diagnostic, int depth)
  {
    String message = null;
    switch (diagnostic.getSeverity())
    {
      case Diagnostic.OK:
      message = "OK";
        break;
      case Diagnostic.ERROR:
      message = "ERROR: " + diagnostic.getMessage();
        break;
      case Diagnostic.INFO:
      message = "INFO: " + diagnostic.getMessage();
        break;
    }

    for (int i = 0; i < depth; i++)
    {
      System.out.print(i == depth - 1 ? "-> " : "   ");
    }
    System.out.println(message);

    for (Iterator i = diagnostic.getChildren().iterator(); i.hasNext();)
    {
      printDiagnostic((Diagnostic)i.next(), depth + 1);
    }
  }

}

Back to the top