001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.migrator;
026
027import java.io.BufferedReader;
028import java.io.ByteArrayInputStream;
029import java.io.ByteArrayOutputStream;
030import java.io.File;
031import java.io.FileInputStream;
032import java.io.FileOutputStream;
033import java.io.IOException;
034import java.io.InputStreamReader;
035import java.io.OutputStream;
036import java.io.Reader;
037
038import org.slf4j.migrator.internal.ProgressListener;
039import org.slf4j.migrator.line.LineConverter;
040import org.slf4j.migrator.line.RuleSet;
041
042public class InplaceFileConverter {
043
044    final static int BUFFER_LEN = 8 * 1024;
045    final LineConverter lineConverter;
046    final String lineTerminator;
047    final ProgressListener pl;
048
049    InplaceFileConverter(RuleSet ruleSet, ProgressListener pl) {
050        this.lineConverter = new LineConverter(ruleSet);
051        lineTerminator = System.getProperty("line.separator");
052        this.pl = pl;
053    }
054
055    private byte[] readIntoByteArray(File file) throws IOException {
056        FileInputStream fis = new FileInputStream(file);
057        ByteArrayOutputStream baos = new ByteArrayOutputStream();
058        int n = 0;
059        byte[] buffer = new byte[BUFFER_LEN];
060        while ((n = fis.read(buffer)) != -1) {
061            // System.out.println("ba="+new String(buffer, "UTF-8"));
062            baos.write(buffer, 0, n);
063        }
064        fis.close();
065        return baos.toByteArray();
066    }
067
068    void convert(File file) throws IOException {
069        byte[] originalBytes = readIntoByteArray(file);
070        byte[] convertedBytes = convertIntoTempByteArray(originalBytes);
071        if (lineConverter.atLeastOneMatchOccured()) {
072            // System.out.println("Converting ["+file+"]");
073            writeConvertedBytesIntoFile(file, convertedBytes);
074            pl.onInplaceConversion(file);
075        } else {
076            // System.out.println("Not touching ["+file+"]");
077        }
078    }
079
080    private void writeConvertedBytesIntoFile(File file, byte[] convertedBytes) throws IOException {
081        FileOutputStream fos = new FileOutputStream(file);
082        fos.write(convertedBytes);
083        fos.flush();
084        fos.close();
085    }
086
087    private byte[] convertIntoTempByteArray(byte[] input) throws IOException {
088        ByteArrayInputStream bais = new ByteArrayInputStream(input);
089        Reader reader = new InputStreamReader(bais);
090        BufferedReader breader = new BufferedReader(reader);
091        ByteArrayOutputStream baos = new ByteArrayOutputStream();
092        while (true) {
093            String line = breader.readLine();
094            if (line != null) {
095                String[] replacement = lineConverter.getReplacement(line);
096                writeReplacement(baos, replacement);
097            } else {
098                break;
099            }
100        }
101        return baos.toByteArray();
102    }
103
104    private void writeReplacement(OutputStream os, String[] replacement) throws IOException {
105        for (String s : replacement) {
106            os.write(s.getBytes());
107            os.write(lineTerminator.getBytes());
108        }
109    }
110}