import com.ebzc.common.beans.SimpleDescriber;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.xerces.xs.*;
import org.apache.xerces.xs.ElementPSVI;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SchemaAwareSAXReader;
import org.dom4j.schema.SchemaAwareElement;
import org.dom4j.schema.basic.DefaultSchemaDocumentFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author  mike skells
 */
public class Test {
    /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
    protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema";
    
    /** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */
    protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
    
    /** Dynamic validation feature id (http://apache.org/xml/features/validation/dynamic). */
    protected static final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic";
    
    /** Creates a new instance of Test */
    public Test() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{
        SAXReader reader = new SchemaAwareSAXReader();
        //        reader.setValidation(true);
        reader.setDocumentFactory(DefaultSchemaDocumentFactory.getInstance());
        reader.setEntityResolver(new MyEntityResolver());
        reader.setErrorHandler(new MyErrorHandler());
        
        //        reader.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
        //        reader.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, true);
        reader.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
        Document doc = reader.read(new File("c:\\a.xml"));
        
        Element e = doc.getRootElement();
        if (!(e instanceof SchemaAwareElement)) {
            System.out.println("failed");
            return;
        }
        SchemaAwareElement se = (SchemaAwareElement)e;
        System.out.println("psvi = "+se.getElementPsvi());
        ElementPSVI psvi = se.getElementPsvi();
        XSComplexTypeDefinition ctd = (XSComplexTypeDefinition)psvi.getElementDeclaration().getTypeDefinition();
        System.out.println("psvi details "+SimpleDescriber.beanToXMLString(psvi,50));
        System.out.println("psvi getElementDeclaration"+psvi.getElementDeclaration());
        System.out.println("ctd = "+ctd);
        System.out.println("psvi getAnnotation "+psvi.getElementDeclaration().getAnnotation());
        System.out.println("psvi getAnnotationString "+psvi.getElementDeclaration().getAnnotation().getAnnotationString());
    }
    public static class MyEntityResolver implements EntityResolver {
        
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            System.out.println("MyEntityResolver publicId = "+publicId+", systemId = "+systemId);
            if (publicId == null) {
                try {
                    URL url = new URL(systemId);
                    InputStream in = url.openStream();
                    return new InputSource(in);
                } catch (IOException e) {
                    e.printStackTrace(System.out);
                    throw e;
                } catch (Exception e) {
                    e.printStackTrace(System.out);
                    throw new SAXException(e);
                }
            }
            return null;
        }
    }
    public static class MyErrorHandler implements ErrorHandler {
        
        public void error(org.xml.sax.SAXParseException exception) throws SAXException {
            System.out.println("MyErrorHandler:error ");
            exception.printStackTrace(System.out);
        }
        
        public void fatalError(org.xml.sax.SAXParseException exception) throws SAXException {
            System.out.println("MyErrorHandler:exception ");
            exception.printStackTrace(System.out);
        }
        
        public void warning(org.xml.sax.SAXParseException exception) throws SAXException {
            System.out.println("MyErrorHandler:warning ");
            exception.printStackTrace(System.out);
        }
        
    }
    
}
