160 lines
3 KiB
Java
160 lines
3 KiB
Java
|
package xid;
|
||
|
|
||
|
import org.xml.sax.*;
|
||
|
import java.util.*;
|
||
|
|
||
|
/**
|
||
|
* Extract from org.xml.sax Interface ErrorHandler:
|
||
|
* "If a SAX application needs to implement customized error handling,
|
||
|
* it must implement this interface and then register an instance with
|
||
|
* the XML reader using the setErrorHandler method. The parser will
|
||
|
* then report all errors and warnings through this interface."
|
||
|
*
|
||
|
*/
|
||
|
public class ParserErrorHandler implements ErrorHandler
|
||
|
{
|
||
|
Vector<String> messages;
|
||
|
int fatalErrorsCount;
|
||
|
int errorsCount;
|
||
|
int warningCount;
|
||
|
|
||
|
public ParserErrorHandler ()
|
||
|
{
|
||
|
fatalErrorsCount = 0;
|
||
|
errorsCount = 0;
|
||
|
warningCount = 0;
|
||
|
messages = new Vector<String> ();
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
public int fatalErrorsCount ()
|
||
|
{
|
||
|
int result;
|
||
|
|
||
|
result = this.fatalErrorsCount;
|
||
|
|
||
|
//
|
||
|
return (result);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
public int errorsCount ()
|
||
|
{
|
||
|
int result;
|
||
|
|
||
|
result = this.errorsCount;
|
||
|
|
||
|
//
|
||
|
return (result);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
public int warningCount ()
|
||
|
{
|
||
|
int result;
|
||
|
|
||
|
result = this.warningCount;
|
||
|
|
||
|
//
|
||
|
return (result);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
public int allErrorsCount ()
|
||
|
{
|
||
|
int result;
|
||
|
|
||
|
result = fatalErrorsCount () + errorsCount () + warningCount ();
|
||
|
|
||
|
//
|
||
|
return (result);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
public boolean hasError ()
|
||
|
{
|
||
|
boolean result;
|
||
|
|
||
|
if (allErrorsCount () == 0)
|
||
|
{
|
||
|
result = false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
result = true;
|
||
|
}
|
||
|
|
||
|
//
|
||
|
return (result);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Called by the XML parser to handle fatal errors.
|
||
|
* @param ex Parse Excpetion. Contains the warning text and the line number.
|
||
|
*/
|
||
|
public void error (SAXParseException exception)
|
||
|
{
|
||
|
String message = "Error at line " + exception.getLineNumber() + " : " + exception.getMessage();
|
||
|
|
||
|
this.errorsCount += 1;
|
||
|
this.messages.add (message);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Called by the XML parser to handle fatal errors.
|
||
|
* @param ex Parse Excpetion. Contains the error text and the line number.
|
||
|
* When a fatal parse error occurs, the parse does not return a document.
|
||
|
*/
|
||
|
public void fatalError (SAXParseException exception)
|
||
|
{
|
||
|
String message = "Fatal error at line " + exception.getLineNumber() + " : " + exception.getMessage();
|
||
|
|
||
|
this.fatalErrorsCount += 1;
|
||
|
this.messages.add (message);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Called by the XML parser to handle warnings.
|
||
|
* @param ex Parse Excpetion. Contains the warning text and the line number.
|
||
|
*/
|
||
|
public void warning(SAXParseException exception)
|
||
|
{
|
||
|
String message = "Warning at line " + exception.getLineNumber() + " : " + exception.getMessage();
|
||
|
|
||
|
this.warningCount += 1;
|
||
|
this.messages.add (message);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
public String toString ()
|
||
|
{
|
||
|
StringBuffer result;
|
||
|
|
||
|
result = new StringBuffer ();
|
||
|
|
||
|
for (String message : messages)
|
||
|
{
|
||
|
result.append (message);
|
||
|
result.append ('\n');
|
||
|
}
|
||
|
|
||
|
//
|
||
|
return (result.toString ());
|
||
|
}
|
||
|
}
|