001/* ========================================================================
002 * JCommon : a free general purpose class library for the Java(tm) platform
003 * ========================================================================
004 *
005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
006 * 
007 * Project Info:  http://www.jfree.org/jcommon/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it 
010 * under the terms of the GNU Lesser General Public License as published by 
011 * the Free Software Foundation; either version 2.1 of the License, or 
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but 
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022 * USA.  
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025 * in the United States and other countries.]
026 * 
027 * -------------
028 * Licences.java
029 * -------------
030 * (C) Copyright 2001-2013, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * $Id: Licences.java,v 1.4 2005/11/16 15:58:41 taqua Exp $
036 *
037 * Changes
038 * -------
039 * 26-Nov-2001 : Version 1 (DG);
040 * 29-Jan-2002 : Appended the instructions to the end of the GNU LGPL and GPL 
041 *               to comply with GNU requirements (DG);
042 * 28-Feb-2002 : Moved to package com.jrefinery.ui.about (DG);
043 * 08-Oct-2002 : Fixed errors reported by Checkstyle (DG);
044 * 17-Aug-2013 : Removed the text from GPL and LGPL static strings, which just
045 *               waste memory, and deprecated to point people to getGPL() and
046 *               getLGPL() (DG);
047 *
048 */
049
050package org.jfree.ui.about;
051
052import java.io.BufferedReader;
053import java.io.IOException;
054import java.io.InputStream;
055import java.io.InputStreamReader;
056import java.io.UnsupportedEncodingException;
057
058/**
059 * Contains the full texts of the GNU General Public Licence and the GNU Lesser
060 * General Public Licence.  These are used in the presentation of a standard 
061 * 'About' frame.
062 */
063public class Licences {
064
065    /** 
066     * The GNU General Public Licence.
067     * 
068     * @deprecated Use {@link #getGPL()}. 
069     */
070    public static final String GPL = "GNU GENERAL PUBLIC LICENSE\n";
071
072    /** 
073     * The GNU Lesser General Public Licence. 
074     * 
075     * @deprecated Use {@link #getLGPL()}. 
076     */
077    public static final String LGPL = "GNU LESSER GENERAL PUBLIC LICENSE\n";
078
079    /** The singleton instance of this class. */
080    private static Licences singleton;
081
082    /**
083     * Returns a reference to the single instance of this class.  In fact,
084     * there is little point to having an instance of this class, but we're
085     * leaving it that way for backwards compatibility until the JCommon 
086     * retirement.
087     *
088     * @return the instance reference.
089     */
090    public static Licences getInstance() {
091        if (singleton == null) {
092            singleton = new Licences();
093        }
094        return singleton;
095    }
096
097    /**
098     * Returns the GPL (v2.1) text.
099     *
100     * @return the GPL licence text.
101     */
102    public String getGPL() {
103        return readStringResource("gpl-2.0.txt");
104    }
105
106    /**
107     * Returns the LGPL (v2.1) text.
108     *
109     * @return the LGPL licence text.
110     */
111    public String getLGPL() {
112        return readStringResource("lgpl-2.1.txt");
113    }
114    
115    private String readStringResource(String name) {
116        StringBuilder sb = new StringBuilder();
117        InputStreamReader streamReader = null;
118        try {
119            InputStream inputStream = getClass().getResourceAsStream(name);
120            streamReader = new InputStreamReader(inputStream, "UTF-8");
121            BufferedReader in = new BufferedReader(streamReader);
122            for (String line; (line = in.readLine()) != null;) {
123              sb.append(line).append("\n");
124            }
125        } catch (UnsupportedEncodingException ex) {
126            throw new RuntimeException(ex);
127        } catch (IOException e) {
128            throw new RuntimeException(e);
129        } finally {
130            try {
131                streamReader.close();
132            } catch (IOException ex) {
133                throw new RuntimeException(ex);
134            }
135        }
136        return sb.toString();        
137    }
138 
139}