alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.nbbuild;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import com.sun.resolver.tools.ResolvingXMLReader;
import com.sun.resolver.Catalog;

/**
 * Validation class for XML files.
 * 

Note: unnecessary in Ant 1.5 and later, as <validate> * can read XML entity catalogs. * @author Michal Zlamal */ public class XMLValidate extends MatchingTask { protected File baseDir = null; protected ValidatorErrorHandler errorHandler = null; protected File catalogFile = null; protected boolean warnings = false; /** basedir for scanning files */ public void setBasedir( File baseDir ) { if (baseDir.isDirectory()) this.baseDir = baseDir; } /** catalog file to resolve entities */ public void setCatalog( File catalog ) { this.catalogFile = catalog; } /** Warning level - default - no warings */ public void setWarnings( boolean warnings ) { this.warnings = warnings; } public void execute() throws BuildException { File file = null; try { ResolvingXMLReader reader = new ResolvingXMLReader(); Catalog catalog = reader.getCatalog(); errorHandler = new XMLValidate.ValidatorErrorHandler( warnings ); if ( catalogFile != null) catalog.parseCatalog( catalogFile.getAbsolutePath() ); if (baseDir == null) baseDir = this.getProject().getBaseDir(); DirectoryScanner ds = this.getDirectoryScanner( baseDir ); ds.scan(); String included[] = ds.getIncludedFiles(); log( (new Integer( included.length )).toString() + " file(s) to validate/parse" ); for (int i=0; i < included.length; i++) { file = new File( baseDir, included[i] ); if (!file.canRead() || !file.exists() || !file.isFile()) { throw new BuildException( "Couldn't read file " + file ); } InputSource is = new InputSource(file.getPath()); errorHandler.init( file.getPath() ); reader.setErrorHandler( errorHandler ); reader.parse(is); log(file + " file have been successfully validated."); } } catch (SAXException ex) { if (file != null) throw new BuildException( ex.getMessage() + ": Can't parse " + file.getPath() ); else throw new BuildException( ex ); } catch (IOException ex1) { ex1.printStackTrace(); } } protected class ValidatorErrorHandler extends DefaultHandler { String fileName = null; private boolean warn = true; public ValidatorErrorHandler( boolean warnings ) { warn = warnings; } public void init( String fileName ) { this.fileName = fileName; } public void fatalError(SAXParseException exception) { log( "[FatalError] " + fileName + ": " + exception.getMessage() ); } public void error(SAXParseException exception) { log( "[Error] " + fileName + ": " + exception.getMessage() ); } public void warning(SAXParseException exception) { if (warn) log( "[Warning] " + fileName + ": " + exception.getMessage() ); } } }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.