View Javadoc

1   /*
2    * Zemucan: A Syntax Assistant for DB2
3    * Copyright (C) 2009, 2010 Andres Gomez Casanova
4    *
5    * This file is part of Zemucan.
6    *
7    * Zemucan is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation; either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * Zemucan is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public License
18   * along with this library; if not, see <http://www.gnu.org/licenses/>.
19   *
20   * Contact:
21   * a n g o c a  at  y a h o o  dot  c o m
22   * Cra. 45 No 61 - 31, Bogota, Colombia.
23   *
24   * Author:   $LastChangedBy: angoca $:
25   * Date:     $LastChangedDate: 2011-03-06 22:24:44 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1915 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/graph/src/main/java/name/angoca/zemucan/core/graph/model/LicenseNode.java $:
28   */
29  package name.angoca.zemucan.core.graph.model;
30  
31  import java.io.BufferedReader;
32  import java.io.FileNotFoundException;
33  import java.io.FileReader;
34  import java.io.IOException;
35  
36  import name.angoca.zemucan.core.graph.api.InvalidGraphNodeException;
37  import name.angoca.zemucan.tools.Constants;
38  
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * This token represents the license of the application, that descrives all the
44   * license of the components used.
45   * <p>
46   * The license is read from a file. If the file cannot be found, it will show a
47   * message indicatin:
48   * <p>
49   * Zemucan\nCopyright (C) 2009, 2010 Andres Gomez Casanova (AngocA)\nThis
50   * program comes with ABSOLUTELY NO WARRANTY;\nThis is free software, and you
51   * are welcome to redistribute it\nunder certain conditions.
52   * <p>
53   * This object is immutable, once it is created, their properties cannot be
54   * changed. This is the reason because there are not setter methods.
55   * <p>
56   * <b>Control Version</b>
57   * <p>
58   * <ul>
59   * <li>1.0.0 Class creation.</li>
60   * <li>1.1.0 GraphNode hierarchy.</li>
61   * </ul>
62   *
63   * @author Andres Gomez Casanova <a
64   *         href="mailto:a n g o c a at y a h o o dot c o m" >(AngocA)</a>
65   * @version 1.1.0 2010-08-29
66   */
67  public final class LicenseNode extends NonReservedGraphNode {
68  
69      /**
70       * License description.
71       */
72      static final String IDENTIFICATION = "Please see the LICENSE.txt file."; //$NON-NLS-1$
73      /**
74       * File that contains the license to show in the application.
75       */
76      static final String LICENSE_FILE = "LICENSE.txt"; //$NON-NLS-1$
77      /**
78       * Logger.
79       */
80      private static final Logger LOGGER = LoggerFactory
81              .getLogger(LicenseNode.class);
82  
83      /**
84       * Appends the license.
85       *
86       * @param inputStream
87       *            Stream where the license is.
88       * @return The license appended.
89       */
90      private static StringBuffer appendLicense(final BufferedReader inputStream) {
91          StringBuffer license = new StringBuffer();
92          String temp;
93          try {
94              while ((temp = inputStream.readLine()) != null) {
95                  license.append(temp);
96                  license.append('\n');
97              }
98          } catch (final IOException e) {
99              // I don't know how to test this part. It's outside my
100             // scope.
101             LicenseNode.LOGGER.debug("Error while reading license."); //$NON-NLS-1$
102             license = new StringBuffer(LicenseNode.IDENTIFICATION);
103         }
104         return license;
105     }
106 
107     /**
108      * Reads the license from a file. If it cannot find it, it puts a default
109      * one.
110      *
111      * @return Application's license.
112      */
113     static String/* ! */readLicense() {
114         StringBuffer license = new StringBuffer();
115         BufferedReader inputStream = null;
116         try {
117             try {
118                 inputStream = new BufferedReader(new FileReader(
119                         LicenseNode.LICENSE_FILE));
120                 license = LicenseNode.appendLicense(inputStream);
121             } catch (final FileNotFoundException e) {
122                 LicenseNode.LOGGER.debug("Error license file not found."); //$NON-NLS-1$
123                 license = new StringBuffer(LicenseNode.IDENTIFICATION);
124             }
125         } finally {
126             if (inputStream != null) {
127                 try {
128                     inputStream.close();
129                 } catch (final IOException e) {
130                     // I don't know how to test this part. It's outside my
131                     // scope.
132                     LicenseNode.LOGGER.debug("Error closing license file."); //$NON-NLS-1$
133                 }
134             }
135         }
136 
137         assert !license.toString().equals(""); //$NON-NLS-1$
138         return license.toString();
139     }
140 
141     /**
142      * Default constructor.
143      *
144      *@param graph
145      *            Associated graph.
146      * @throws InvalidGraphNodeException
147      *             The name is invalid
148      */
149     public LicenseNode(final Graph/* ! */graph) throws InvalidGraphNodeException {
150         super(Constants.LICENSE_CONTENT_TOKEN_ID, LicenseNode.IDENTIFICATION,
151                 graph);
152         final String license = LicenseNode.readLicense();
153         super.setName(license);
154     }
155 }