/*
 * Copyright (c) 2014 Ngewi Fet <ngewif@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gnucash.android.importer;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Importer for Gnucash XML files and GNCA (GnuCash Android) XML files
 *
 * @author Ngewi Fet <ngewif@gmail.com>
 */
public class GncXmlImporter {

    /**
     * Parses XML into an already open database.
     * <p>This method is used mainly by the {@link org.gnucash.android.db.DatabaseHelper} for database migrations.<br>
     *     You should probably use {@link #parse(android.content.Context, java.io.InputStream)} instead</p>
     * @param db SQLite Database
     * @param gncXmlInputStream Input stream of GnuCash XML
     */
    public static void parse(SQLiteDatabase db, InputStream gncXmlInputStream) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        BufferedInputStream bos = new BufferedInputStream(gncXmlInputStream);

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */

        GncXmlHandler handler = new GncXmlHandler(db);
        xr.setContentHandler(handler);
        xr.parse(new InputSource(bos));
    }

    /**
     * Parse GnuCash XML input and populates the database
     * @param context Application context
     * @param gncXmlInputStream InputStream source of the GnuCash XML file
     */
    public static void parse(Context context, InputStream gncXmlInputStream) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        BufferedInputStream bos = new BufferedInputStream(gncXmlInputStream);

        //TODO: Set an error handler which can log errors

        GncXmlHandler handler = new GncXmlHandler(context);
        xr.setContentHandler(handler);
        xr.parse(new InputSource(bos));
    }
}

@@ -1012,36 +1012,110 @@
 .io.
-IOException;%0Aimport java.io.
+PushbackInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.zip.GZIP
 Inpu
@@ -2723,41 +2723,531 @@
  bos
- = new BufferedInputStream(gncXml
+;%0A        PushbackInputStream pb = new PushbackInputStream( gncXmlInputStream, 2 ); //we need a pushbackstream to look ahead%0A        byte %5B%5D signature = new byte%5B2%5D;%0A        pb.read( signature ); //read the signature%0A        pb.unread( signature ); //push back the signature to the stream%0A        if( signature%5B 0 %5D == (byte) 0x1f && signature%5B 1 %5D == (byte) 0x8b ) //check if matches standard gzip magic number%0A            bos = new BufferedInputStream(new GZIPInputStream(pb));%0A        else%0A            bos = new Buffered
 Inpu
@@ -3245,32 +3245,35 @@
 feredInputStream
+(pb
 );%0A%0A        //TO

Support for zipped xml file