/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  Santhosh Kumar T <santhosh.tekuri@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package jlibs.xml.xsd;

import org.apache.xerces.impl.Constants;
import org.apache.xerces.impl.xs.XSImplementationImpl;
import org.apache.xerces.impl.xs.SchemaGrammar;
import org.apache.xerces.impl.xs.XSModelImpl;
import org.apache.xerces.impl.xs.util.StringListImpl;
import org.apache.xerces.xs.XSLoader;
import org.apache.xerces.xs.XSModel;
import org.apache.xerces.dom.DOMXSImplementationSourceImpl;
import org.apache.xerces.dom.DOMInputImpl;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMErrorHandler;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.LSResourceResolver;
import jlibs.core.lang.ImpossibleException;

/**
 * @author Santhosh Kumar T
 */
public class XSParser{
    private XSLoader xsLoader;

    public XSParser(){
        this(null, null);
    }

    public XSParser(LSResourceResolver entityResolver, DOMErrorHandler errorHandler){
        System.setProperty(DOMImplementationRegistry.PROPERTY, DOMXSImplementationSourceImpl.class.getName());
        DOMImplementationRegistry registry;
        try{
            registry = DOMImplementationRegistry.newInstance();
        }catch(Exception ex){
            throw new ImpossibleException(ex);
        }
        XSImplementationImpl xsImpl = (XSImplementationImpl)registry.getDOMImplementation("XS-Loader");

        xsLoader = xsImpl.createXSLoader(null);
        DOMConfiguration config = xsLoader.getConfig();
        config.setParameter(Constants.DOM_VALIDATE, Boolean.TRUE);

        if(entityResolver!=null)
            config.setParameter(Constants.DOM_RESOURCE_RESOLVER, entityResolver);

        if(errorHandler!=null)
            config.setParameter(Constants.DOM_ERROR_HANDLER, errorHandler);
    }

    public XSModel parse(String uri){
        return xsLoader.loadURI(uri);
    }

    public XSModel parse(String... uris){
        return xsLoader.loadURIList(new StringListImpl(uris, uris.length));
    }

    /**
     * Parse an XML Schema document from String specified
     * 
     * @param schema    String data to parse. If provided, this will always be treated as a
     *                  sequence of 16-bit units (UTF-16 encoded characters). If an XML
     *                  declaration is present, the value of the encoding attribute
     *                  will be ignored.
     * @param baseURI   The base URI to be used for resolving relative
     *                  URIs to absolute URIs.
     */
    public XSModel parseString(String schema, String baseURI){
        return xsLoader.load(new DOMInputImpl(null, null, baseURI, schema, null));
    }

    public static XSModel getBuiltInSchema(){
        return new XSModelImpl(new SchemaGrammar[0]);
    }
}

@@ -650,16 +650,60 @@
 l.xsd;%0A%0A
+import jlibs.core.lang.ImpossibleException;%0A
 import o
@@ -723,22 +723,24 @@
 ces.
-impl.Constants
+dom.DOMInputImpl
 ;%0Aim
@@ -762,24 +762,23 @@
 .xerces.
-impl.xs.
+dom.DOM
 XSImplem
@@ -781,24 +781,30 @@
 plementation
+Source
 Impl;%0Aimport
@@ -831,24 +831,120 @@
 mpl.
-xs.SchemaGrammar
+Constants;%0Aimport org.apache.xerces.impl.xs.SchemaGrammar;%0Aimport org.apache.xerces.impl.xs.XSImplementationImpl
 ;%0Aim
@@ -1120,111 +1120,8 @@
 el;%0A
-import org.apache.xerces.dom.DOMXSImplementationSourceImpl;%0Aimport org.apache.xerces.dom.DOMInputImpl;%0A
 impo
@@ -1287,24 +1287,25 @@
 solver;%0A
+%0A
 import j
 libs.cor
@@ -1300,42 +1300,23 @@
 rt j
-libs.core.lang.ImpossibleException
+ava.util.Arrays
 ;%0A%0A/
@@ -2411,38 +2411,49 @@
 g uri)%7B%0A        
-return
+XSModel xsModel =
  xsLoader.loadUR
@@ -2460,16 +2460,143 @@
 I(uri);%0A
+        if(xsModel==null)%0A            throw new RuntimeException(%22Couldn't load XMLSchema from %22+uri);%0A        return xsModel;%0A
     %7D%0A%0A 
@@ -2636,38 +2636,49 @@
  uris)%7B%0A        
-return
+XSModel xsModel =
  xsLoader.loadUR
@@ -2719,24 +2719,168 @@
 s.length));%0A
+        if(xsModel==null)%0A            throw new RuntimeException(%22Couldn't load XMLSchema from %22+ Arrays.asList(uris));%0A        return xsModel;%0A
     %7D%0A%0A    /

throw exception if xsModel is null