package org.oskari.map.userlayer.input;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;

import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.kml.v22.KMLConfiguration;
import org.geotools.referencing.CRS;
import org.geotools.xml.Parser;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.xml.sax.SAXException;

import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.service.ServiceException;

/**
 * Parse Google KML
 */
public class KMLParser implements FeatureCollectionParser {

    private static final Logger LOG = LogFactory.getLogger(KMLParser.class);

    public static final String SUFFIX = "KML";

    @Override
    public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS,
            CoordinateReferenceSystem targetCRS) throws ServiceException {
        try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
            Parser parser = new Parser(new KMLConfiguration());
            DefaultFeatureCollection fc = new DefaultFeatureCollection();
            // KML always lon,lat 4326
            sourceCRS = CRS.decode("EPSG:4326", true);
            SimpleFeature f = (SimpleFeature) parser.parse(in);
            fc.add(f);
            return fc;
        } catch (FactoryException e) {
            throw new ServiceException("Failed to decode EPSG:4326");
        } catch (IOException e) {
            throw new ServiceException("IOException occured", e);
        } catch (SAXException e) {
            LOG.warn(e, "Failed to parse KML file");
            throw new ServiceException("Invalid KML file: " + e.getMessage());
        } catch (ParserConfigurationException e) {
            throw new ServiceException("KML parser failed to initialize");
        }
    }

}

@@ -397,45 +397,8 @@
 on;%0A
-import org.geotools.referencing.CRS;%0A
 impo
@@ -478,57 +478,8 @@
 re;%0A
-import org.opengis.referencing.FactoryException;%0A
 impo
@@ -1307,102 +1307,8 @@
 ();%0A
-            // KML always lon,lat 4326%0A            sourceCRS = CRS.decode(%22EPSG:4326%22, true);%0A
     
@@ -1417,117 +1417,8 @@
 fc;%0A
-        %7D catch (FactoryException e) %7B%0A            throw new ServiceException(%22Failed to decode EPSG:4326%22);%0A
     

Remove CRS from KML since its not used