You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by tr...@katamail.com on 2002/04/12 16:52:13 UTC

laxly schema valid 2

Ok for ErrorHandler, here a little piece of code that we use. The problem is that I'm not able to istruct the parser not to worry about lax tags, in other words I'm only able to strict schema validate. 

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.io.IOException;
import org.xml.sax.InputSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Hashtable;

public class Validate implements ErrorHandler {

    // awful static boolean ! 
    private static boolean status = true;

    private DOMParser parser = null;
    private FileInputStream xmlFile =null;
    
    // Constructor
    public Validate (String xmlFile) {

	parser = new DOMParser();
	try {
	    this.xmlFile=new FileInputStream(xmlFile);
	}catch (FileNotFoundException fne){
	    fne.printStackTrace();
	}
	//System.out.println("Validate()"+xmlFile);
	init();
    }

    private void init(){
	try {
	    //System.out.println("init");
	    parser.setErrorHandler(this);
	    
	    parser.setFeature("http://xml.org/sax/features/namespaces",true);
	    
	    parser.setFeature("http://xml.org/sax/features/validation",true);
	    
	    parser.setFeature("http://apache.org/xml/features/validation/dynamic",false);
	    
	    parser.setFeature("http://apache.org/xml/features/validation/schema",true);

	    parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);

	    //parser.setFeature("http://apache.org/xml/features/validation/schema/normalized-value",true);
	    
	    // => * * * * * * *  * * * * * * * * * * * * * * * * * * * * * * * * * * * *  * * * * * * * * * * *                      
	    // in my environment this  feature "throws" always an error never a warning
	    parser.setFeature("http://apache.org/xml/features/validation/warn-on-undeclared-elemdef",true);
	    //  => * * * * * * *  * * * * * * * * * * * * * * * * * * * * * * * * * * * *  * * * * * * * * * * *

	    parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion",false);
	    
	    // Validate knows only xmldsig schema 
	    parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
				"http://www.w3.org/2000/09/xmldsig# xmldsig-core-schema.xsd" );
	    // So how to deal with xsi:schemaLocation e similar ? 

	}catch (SAXNotSupportedException snse) {
            System.err.println ("de"+snse);
        }catch (SAXNotRecognizedException snre){
	    System.err.println("de"+snre);
	}
	
    }//end init()
    
    public void  Parsing() {
        //  Parse the Document
	try {
	    InputSource is = new InputSource(xmlFile);
            parser.parse(is);
	    System.out.println("parse");
        }

        catch (SAXException se) {
	    se.printStackTrace();
	    
	}
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        catch (Exception e) {
	    //e.printStackTrace();
	    System.err.println("\t"+e);
        }
    }//end Parsing()

    //  Warning Event Handler
    public void warning (SAXParseException e)
        throws SAXException {
	System.err.println ("Warning:  "+e);
    }

    //  Error Event Handler
    public void error (SAXParseException spe)
        throws SAXException {
	
	Element e = null;
	try {
	    e = (Element) parser.getProperty("http://apache.org/xml/properties/dom/current-element-node");
	}catch (SAXNotRecognizedException snre){
	    snre.printStackTrace();
	}catch (SAXNotSupportedException snre){
	    snre.printStackTrace();
	}

	String tagName = e.getTagName();
	System.out.println("Error in tag "+tagName);

	}	
    //  Fatal Error Event Handler
    public void fatalError (SAXParseException e)
        throws SAXException {
        System.err.println ("Fatal Error():  "+e);
	//filter();
    }

    
    // Main Method
    public static void main (String[] args) {
	//System.out.println("main");
	
	Validate v = new Validate(args[0]);
	v.Parsing();
	if (v.status) System.out.println("Ok for me");
	else System.out.println("Bad for me");
    }
}

__________________________________________


Fai i tuoi acquisti su www.kwshopping.it


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org