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

msv.msv.examples.errorinfo.ErrorReporter Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
/*
 * @(#)$Id: ErrorReporter.java 1598 2003-09-04 20:45:05Z kohsuke $
 *
 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the proprietary information of Sun Microsystems, Inc.  
 * Use is subject to license terms.
 * 
 */
package errorinfo;

import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParserFactory;
import com.sun.msv.verifier.ErrorInfo;
import com.sun.msv.verifier.ValidityViolation;
import org.iso_relax.verifier.*;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLFilterImpl;

/**
 * Uses {@link ErrorInfo} information to get detailed information about errors
 * and its source.
 * 
 * 

* This example takes a schema and a document, then dumps the document. * When an error is found, the error is highlighted. * *

* The purpose of this example is to illustrate how you can access * {@link ErrorInfo}, which will give you detailed information of * the error. * *

* {@link ValidityViolation} is derived from SAXParseException, so you can * always access line/column number information at least. * However, since this information is about the textual representation of * XML, it is sometimes not much useful. * *

* ErrorInfo provides high-level error information, * and therefore it is often useful to provide an * application-specific error messages to the user. * * @author Kohsuke KAWAGUCHI */ public class ErrorReporter { public static void main( String args[] ) throws Exception { if( args.length!=2 ) { System.out.println("ErrorReporter "); return; }; // control MSV through JARV. // For more information about JARV, see the jarv examples. VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl(); // compile a schema and gets a verifier. final Verifier verifier = factory.newVerifier(new File(args[0])); // create a SAX parser SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setNamespaceAware(true); XMLReader reader = parserFactory.newSAXParser().getXMLReader(); // then setup the SAX pipe line as follows: // // parser ==> interceptor ==> verifier // // "interceptor" works as a SAX filter. Interceptor interceptor = new Interceptor(); interceptor.setParent(reader); interceptor.setContentHandler(verifier.getVerifierHandler()); // set an error handler that throws any error. verifier.setErrorHandler( com.sun.msv.verifier.util.ErrorHandlerImpl.theInstance); // parse the file. interceptor.parse( new InputSource( new FileInputStream(args[1]) ) ); } /** * Interceptor works in this way: * *

  • * First, it receives a SAX event from the parser. * *
  • * It then passes a SAX event to the verifier, to see if it's correct. * *
    • * If there is no error, then the verifier will return without * reporting any error. So the interceptor will print it. * *
    • * If there is an error, the verfieier will report an error to ErrorHandler. * The error handler we set throws it, so Interceptor will catch it. * If the error is catched, the interceptor will print it accordingly. * */ private static class Interceptor extends XMLFilterImpl { /** * characters has to be buffered because of the way MSV works. * For more information, see the javadoc of * {@link ErrorInfo.BadText}. */ private StringBuffer buffer = new StringBuffer(); public void startElement( String ns, String local, String qname, Attributes atts ) throws SAXException { ErrorInfo ei = null; boolean unknownError = false; try { super.startElement(ns,local,qname,atts); // there is no error. } catch( ValidityViolation vv ) { // there was an error. ei = vv.getErrorInfo(); if(!(ei instanceof ErrorInfo.BadText) && !(ei instanceof ErrorInfo.BadTagName) && !(ei instanceof ErrorInfo.BadAttribute) && !(ei instanceof ErrorInfo.MissingAttribute) ) // if the type of information is unknown to us, // or ei equals to null unknownError = true; } // print the text printText( ei instanceof ErrorInfo.BadText ); printIndent(); print("<"); print( qname, unknownError || // if the error is unknown, highlight the tag name (ei instanceof ErrorInfo.BadTagName) ); for( int i=0; i\n"); indent++; } public void endElement( String ns, String local, String qname ) throws SAXException { ErrorInfo ei = null; try { super.endElement(ns,local,qname); // there is no error. } catch( ValidityViolation vv ) { // there was an error. ei = vv.getErrorInfo(); } // print the text printText(ei instanceof ErrorInfo.BadText); indent--; printIndent(); print("\n"); } public void characters( char[] buf, int start, int len ) throws SAXException { super.characters(buf,start,len); // just collect characters in this callback buffer.append(buf,start,len); } private void printText( boolean fError ) { String str = buffer.toString().trim(); if(str.length()!=0 || fError) { printIndent(); print(str, fError ); print("\n"); } // update the text buffer buffer = new StringBuffer(); } // // pretty print engine // /** indent depth. */ private int indent = 0; /** Prints whitespaces. */ private void printIndent() { for( int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy