You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Mikael St�ldal <mi...@ingen.reklam.staldal.nu> on 2000/10/15 19:12:30 UTC

Small application to check XML files for errors

I have wrote a small application to check XML files for errors. It's
designed to be invoked as a "compiler" from an editor such as Emacs
which can point out the error in the XML file automatically.

It might be useful to include in the Xerces dist, perhaps with the
samples.

import java.io.*;

import org.xml.sax.*;

/**
 * Small application to check for errors in XML files.
 * Can be invoked as a "compiler" from an editor such as Emacs
 * to automatically point out error locations in the XML file.
 *
 * Accepts either a local file name or an URL on the command line.
 *
 * Written by Mikael Ståldal. Donated to the Public Domain.
 */
public class XMLCheck
{
	public static void main(String[] args)
	{
		boolean validate = false;
		String input = null;

		for (int i = 0; i<args.length; i++)
		{
			if (args[i].equals("-v"))
				validate = true;
			else
				input = args[i];
		}

		if (input == null)
		{
			System.out.println("Syntax: XMLCheck [-v] <filename or URL>");
			return;
		}

		MyParser parser = new MyParser();

		if (parser.parse(input, validate))
			System.exit(0);
		else
			System.exit(1);
	}
}

class MyParser implements ErrorHandler
{
	private String filename;
	private boolean ioError;

	MyParser() {}

	boolean parse(String input, boolean validate)
	{
		XMLReader parser = new org.apache.xerces.parsers.SAXParser();

        if (validate)
        {
    		try {
				parser.setFeature("http://xml.org/sax/features/validation", 
					true);
		    }
    		catch (SAXException e)
	    	{
		    	System.err.println("Unable to turn on validation: " + 
			    	e.getMessage());
    		}
		}

		parser.setErrorHandler(this);

		try {
			InputSource is;

			if (input.indexOf(':') > 1) // looks like an URL
			{
				filename = null;
				is = new InputSource(input);
			}
			else // a local filename
			{
				filename = input;
				is = new InputSource(new FileInputStream(filename));
			}

			ioError = false;
			parser.parse(is);
		}
		catch (java.io.FileNotFoundException e)
		{
			System.err.println("File not found: " + e.getMessage());
			return false;
		}
		catch (java.io.IOException e)
		{
			System.err.println(e.getClass().getName() + ": " + e.getMessage());
			return false;
		}
		catch (SAXException e)
		{
			if (!ioError) System.err.println("Document not well-formed");
			return false;
		}
		return true;
	}

	public void warning(SAXParseException e)
	{
		String name = (filename == null) ? e.getSystemId() : filename;
		System.err.println(name + ":" + e.getLineNumber() + ":"
			+ e.getColumnNumber() + ": Warning: " + e.getMessage());
	}

	public void error(SAXParseException e)
	{
		String name = (filename == null) ? e.getSystemId() : filename;
		System.err.println(name + ":" + e.getLineNumber() + ":"
			+ e.getColumnNumber() + ": Error: " + e.getMessage());
	}

	public void fatalError(SAXParseException e)
	{
		String name = (filename == null) ? e.getSystemId() : filename;
		if (name == null)
		{
			System.err.println(e.getMessage());
			ioError = true;
		}
		else
		{
			System.err.println(name + ":" + e.getLineNumber() + ":"
				+ e.getColumnNumber() +  ": Fatal: " + e.getMessage());
		}
	}
}

-- 
/****************************************************************\
* You have just read a message from Mikael Ståldal.              *
*                                                                *
* Remove "ingen.reklam." from the address before mail replying.  *
\****************************************************************/

Re: Small application to check XML files for errors

Posted by Mikael St�ldal <mi...@ingen.reklam.staldal.nu>.
In article <JP...@palacio-cristal.com>,
"Tako Schotanus" <qu...@palacio-cristal.com> wrote:

>> I have wrote a small application to check XML files for errors. It's
>> designed to be invoked as a "compiler" from an editor such as Emacs
>> which can point out the error in the XML file automatically.
>>
><snip>
>> 			if (input.indexOf(':') > 1) // looks like an URL
>> 			else // a local filename
>
>Hehehe, I guess you're not using Windows if you use a check like that :-)

What's wrong with this check?

-- 
/****************************************************************\
* You have just read a message from Mikael Ståldal.              *
*                                                                *
* Remove "ingen.reklam." from the address before mail replying.  *
\****************************************************************/

RE: Small application to check XML files for errors

Posted by Tako Schotanus <qu...@palacio-cristal.com>.

> -----Original Message-----
> From: Mikael Ståldal [mailto:mikael@ingen.reklam.staldal.nu]
> Sent: zondag 15 oktober 2000 19:13
> To: xerces-j-dev@xml.apache.org
> Subject: Small application to check XML files for errors
>
>
> I have wrote a small application to check XML files for errors. It's
> designed to be invoked as a "compiler" from an editor such as Emacs
> which can point out the error in the XML file automatically.
>
<snip>
> 			if (input.indexOf(':') > 1) // looks like an URL
> 			else // a local filename

Hehehe, I guess you're not using Windows if you use a check like that :-)

-Tako