001/*
002 * File   : $Source$
003 * Date   : $Date$
004 * Version: $Revision$
005 *
006 * This library is part of OpenCms -
007 * the Open Source Content Management System
008 *
009 * Copyright (C) 2002 - 2008 Alkacon Software (http://www.alkacon.com)
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Lesser General Public
013 * License as published by the Free Software Foundation; either
014 * version 2.1 of the License, or (at your option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Lesser General Public License for more details.
020 *
021 * For further information about Alkacon Software, please see the
022 * company website: http://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: http://www.opencms.org
026 *
027 * You should have received a copy of the GNU Lesser General Public
028 * License along with this library; if not, write to the Free Software
029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030 */
031
032package org.opencms.ui.apps.user;
033
034/** Helper to produce Csv files. */
035public class CmsCsvWriter {
036
037    /** The "bom" bytes as String that need to be placed at the very beginning of the produced csv. */
038    private static final String BOM = "\ufeff";
039
040    /** Internal variable holding the CSV content. */
041    StringBuffer m_csv = new StringBuffer(BOM);
042
043    /**
044     * Adds a line to the CSV.
045     * @param values the (unescaped) values to add.
046     */
047    public void addLine(String... values) {
048
049        if (null != values) {
050            if (values.length > 0) {
051                m_csv.append(esc(values[0]));
052            }
053            for (int i = 1; i < values.length; i++) {
054                m_csv.append(sep()).append(esc(values[i]));
055            }
056        }
057        m_csv.append(nl());
058    }
059
060    /**
061     * @see java.lang.Object#toString()
062     */
063    @Override
064    public String toString() {
065
066        return m_csv.toString();
067    }
068
069    /**
070     * Escapes the provided value for CSV.
071     * @param value the value to escape
072     * @return the escaped value.
073     */
074    private String esc(String value) {
075
076        value = value.replace("\"", "\"\"");
077        return '"' + value + '"';
078    }
079
080    /**
081     * Returns a line break as to use in the CSV output.
082     * @return a line break as to use in the CSV output.
083     */
084    private String nl() {
085
086        return "\n";
087    }
088
089    /**
090     * Returns the value separator to use in the CSV output.
091     * @return the value separator to use in the CSV output.
092     */
093    private String sep() {
094
095        return ";";
096    }
097
098}