All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.iso_relax.ant.JARVTask Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.iso_relax.ant;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.iso_relax.verifier.Verifier;
import org.iso_relax.verifier.VerifierConfigurationException;
import org.iso_relax.verifier.VerifierFactory;
import org.xml.sax.SAXException;

/**
 * Ant Task that performs XML validation through JARV.
 * 
 * @author Kohsuke Kawaguchi ([email protected])
 */
public class JARVTask extends Task {
    /** Schema file. */
    private File schemaFile;
    
    /** Schema language. */
    private String schemaLanguage;
    
    /** XML file that will be validated. */
    private File xmlFile;
    
    /** FileSets to be validated. */
    private List xmlFiles = new ArrayList();
    
    /**
     * Handles the schema attribute.
     */
    public void setSchema(String schemaFilename) {
      schemaFile = project.resolveFile(schemaFilename);
    }
    
    /**
     * Handles the schemaLanguage attribute.
     */
    public void setSchemaLanguage(String schemaLanguage) {
        this.schemaLanguage = schemaLanguage;
    }
    
    /**
     * Handles the file attribute.
     */
    public void setFile(File file) {
      this.xmlFile = file;
    }
    
    /**
     * Handles the nested file set.
     */
    public void addFileset( FileSet fs ) {
        xmlFiles.add(fs);
    }

    public void execute() throws BuildException {
        try {
            if( schemaLanguage==null )
                throw new BuildException(
                    "schema language needs to be specified through the schemaLanguage attribute",
                    location );
            
            if( schemaFile==null )
                throw new BuildException(
                    "schema file needs to be specified through the schema attribute",
                    location );
             
            // load verifier factory       
            VerifierFactory factory;
            try {
                factory = VerifierFactory.newInstance(schemaLanguage,this.getClass().getClassLoader());
            } catch( VerifierConfigurationException e ) {
                throw new BuildException(
                    "unable to load a validator for the schema language: "+schemaLanguage,
                    e, location );
            }
            
            // create a verifier by compiling a schema
            Verifier verifier;
            ErrorHandlerImpl errorHandler = new ErrorHandlerImpl(this);
            try {
                verifier = factory.newVerifier(schemaFile);
                verifier.setErrorHandler(errorHandler);
            } catch( VerifierConfigurationException e ) {
                throw new BuildException(
                    "failed to compile the schema:"+ e.getMessage(),
                    e, location );
            }
            
            boolean noError = true;
            
            // do the validation
            if( xmlFile!=null ) {
                log( "validating "+xmlFile, Project.MSG_INFO );
                noError |= verifier.verify( xmlFile );
            }
            
            for( int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy