001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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 3 of the License, or (at your option) any later version.
010     *
011     * Sonar 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 Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    
021    /**
022     * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
023     */
024    package net.sourceforge.pmd.cpd;
025    
026    import java.io.*;
027    
028    /**
029     * @author Philippe T'Seyen
030     */
031    public class FileReporter {
032        private File reportFile;
033        private String encoding;
034    
035        public FileReporter(String encoding) {
036            this(null, encoding);
037        }
038    
039        public FileReporter(File reportFile) {
040            this(reportFile, System.getProperty("file.encoding"));
041        }
042    
043        public FileReporter(File reportFile, String encoding) {
044            this.reportFile = reportFile;
045            this.encoding = encoding;
046        }
047    
048        public void report(String content) throws ReportException {
049            try {
050                Writer writer = null;
051                try {
052                    OutputStream outputStream;
053                    if (reportFile == null) {
054                            outputStream = System.out;
055                    } else {
056                            outputStream = new FileOutputStream(reportFile);
057                    }
058                    writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding));
059                    writer.write(content);
060                } finally {
061                    if (writer != null) writer.close();
062                }
063            } catch (IOException ioe) {
064                throw new ReportException(ioe);
065            }
066        }
067    }