001    /*
002      * mesopotamia-java @mesopotamia.version@
003     * Multilingual parser and repository. 
004     * Copyright (C) 2005  Hammurapi Group
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with this library; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019     *
020     * URL: http://http://www.hammurapi.biz
021     * e-Mail: support@hammurapi.biz
022     */
023    
024    package org.mesopotamia.lang.java;
025    
026    import java.io.FilterReader;
027    import java.io.IOException;
028    import java.io.Reader;
029    
030    /**
031     * There is a bug/problem in java.g - it hangs on SL_COMMENT at the end of file.
032     * This class virtually adds '\n' characted at the end of file.
033     * 
034     * @author Pavel Vlasov
035     * @version $Revision: 1.1 $
036     */
037    public class CheatingFilterReader extends FilterReader {
038            private boolean cheated = false;
039    
040            /** Creates a new instance of CheatingFilterReader */
041            public CheatingFilterReader(Reader in) {
042                    super(in);
043            }
044    
045            public int read() throws IOException {
046                    int ret = super.read();
047                    if (ret == -1 && !cheated) {
048                            cheated = true;
049                            ret = '\n';
050                    }
051    
052                    return ret;
053            }
054    
055            // TODO Analyze whether is SL_COMMENT and cheat only in this case.
056            public int read(char[] cbuf, int off, int len) throws IOException {
057                    int ret = super.read(cbuf, off, len);
058                    if (ret != len && !cheated) {
059                            cheated = true;
060                            cbuf[ret] = '\n';
061                            ret++;
062                    }
063                    return ret;
064            }
065    
066            public boolean ready() throws IOException {
067                    return super.ready() || !cheated;
068            }
069    
070            public void reset() throws IOException {
071                    super.reset();
072                    cheated = false;
073            }
074    }