/*  Copyright (C) 2003-2011 JabRef contributors.
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package net.sf.jabref.imports;

import java.io.BufferedReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;

/**
 * 
 * Warning -- it is not a generic filter, only read is implemented!
 *
 * Note: this is just a quick port of the original SPIRESBibtexFilterReader.
 * 
 * @author Fedor Bezrukov
 * @author Sheer El-Showk
 * 
 * @version $Id$
 * 
 * TODO: Fix grammar in bibtex entries -- it ma return invalid bibkeys (with space)
 * 
 */
class INSPIREBibtexFilterReader extends FilterReader {

    private final BufferedReader in;

    private String line;
    private int pos;
    private boolean pre;


    INSPIREBibtexFilterReader(Reader _in) {
        super(_in);
        in = new BufferedReader(_in);
        pos = -1;
        pre = false;
    }

    private String readpreLine() throws IOException {
        String l;
        do {
            l = in.readLine();
            if (l == null) {
                return null;
            }
            if (l.indexOf("<pre>") != -1) {
                pre = true;
                l = in.readLine();
            }
            if (l.indexOf("</pre>") != -1) {
                pre = false;
            }
        } while (!pre);
        return l;
    }

    private String fixBibkey(String in) {
        if (in == null) {
            return null;
        }
        //System.out.println(in);
        if (in.matches("@Article\\{.*,")) {
            //System.out.println(in.replace(' ','_'));
            return in.replace(' ', '_');
        } else {
            return in;
        }
    }

    @Override
    public int read() throws IOException {
        if (pos < 0) {
            line = fixBibkey(readpreLine());
            pos = 0;
            if (line == null) {
                return -1;
            }
        }
        if (pos >= line.length()) {
            pos = -1;
            return '\n';
        }
        return line.charAt(pos++);
    }

}

@@ -1763,23 +1763,24 @@
   if (l.
-indexOf
+contains
 (%22%3Cpre%3E%22
@@ -1776,30 +1776,24 @@
 ins(%22%3Cpre%3E%22)
- != -1
 ) %7B%0A        
@@ -1883,15 +1883,16 @@
  (l.
-indexOf
+contains
 (%22%3C/
@@ -1901,14 +1901,8 @@
 e%3E%22)
- != -1
 ) %7B%0A

Update INSPIREBibtexFilterReader.java