/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2004-2006, Geotools Project Managment Committee (PMC)
 *
 *    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 org.geotools.xml;

import java.io.File;
import java.net.URI;
import java.util.logging.Level;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.geotools.feature.Feature;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.TestData;

import junit.framework.TestCase;


/**
 *
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/test/java/org/geotools/xml/GMLInheritanceTest.java $
 */
public class GMLInheritanceTest extends TestCase {

    
    public void testNestedFeature() throws Throwable {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(false);

        SAXParser parser = spf.newSAXParser();

        String path = "xml/sample/nestedFeatures.xml";
        File f = TestData.copy(this,path);
        TestData.copy(this,"xml/sample/nestedFeatures.xsd");
        URI u = f.toURI();

        XMLSAXHandler xmlContentHandler = new XMLSAXHandler(u,null);
        XMLSAXHandler.setLogLevel(Level.FINEST);
        XSISAXHandler.setLogLevel(Level.FINEST);
        XMLElementHandler.setLogLevel(Level.FINEST);
        XSIElementHandler.setLogLevel(Level.FINEST);

        parser.parse(f, xmlContentHandler);

        Object doc = xmlContentHandler.getDocument();
        assertNotNull("Document missing", doc);
//            System.out.println(doc);
        
        checkFeatureCollection((FeatureCollection)doc);
    }
    public void testMultiInheritance() throws Throwable {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(false);

        SAXParser parser = spf.newSAXParser();

        String path = "xml/sample/multiInheritance.xml";
        File f = TestData.copy(this,path);
        TestData.copy(this,"xml/sample/multiInheritance.xsd");
        URI u = f.toURI();

        XMLSAXHandler xmlContentHandler = new XMLSAXHandler(u,null);
        XMLSAXHandler.setLogLevel(Level.FINEST);
        XSISAXHandler.setLogLevel(Level.FINEST);
        XMLElementHandler.setLogLevel(Level.FINEST);
        XSIElementHandler.setLogLevel(Level.FINEST);

        parser.parse(f, xmlContentHandler);

        Object doc = xmlContentHandler.getDocument();
        assertNotNull("Document missing", doc);
//            System.out.println(doc);
        
        checkFeatureCollection((FeatureCollection)doc);
    }
    
    private void checkFeatureCollection(FeatureCollection doc){
               
        //remaining slot (s) should be feature(s)
        assertTrue("Requires atleast one feature",doc.size()>0);  //bbox + feature
        FeatureIterator i = doc.features();
        int j = 1;
        while(i.hasNext()){
            Feature ft = i.next();
            assertNotNull("Feature #"+j+" is null",ft);
//            assertNotNull("Feature #"+j+" missing crs ",ft.getFeatureType().getDefaultGeometry().getCoordinateSystem());
//            System.out.println("Feature "+j+" : "+ft);
            j++;
        }
        assertEquals( 2, j );
        // System.out.println("Found "+j+" Features");
    }
}
