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/AboutNode.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 information about the application, like the About
44   * if GUIs.
45   * <p>
46   * The license is read from a file. If the file cannot be found, it put the
47   * default one:
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>0.0.1 Class creation.</li>
60   * <li>0.1.0 First draft of this token.</li>
61   * <li>0.2.0 License token.</li>
62   * <li>1.0.0 Moved to version 1.</li>
63   * <li>1.1.0 Exception hierarchy changed.</li>
64   * <li>1.2.0 final.</li>
65   * <li>1.3.0 License from a file.</li>
66   * <li>1.3.1 GraphToken renamed by GraphNode.</li>
67   * <li>1.3.2 Extra tokens problem.</li>
68   * <li>1.4.0 Renamed.</li>
69   * <li>1.5.0 Read license public.</li>
70   * <li>1.6.0 GraphNode hierarchy.</li>
71   * </ul>
72   *
73   * @author Andres Gomez Casanova <a
74   *         href="mailto:a n g o c a at y a h o o dot c o m" >(AngocA)</a>
75   * @version 1.6.0 2010-08-29
76   */
77  public final class AboutNode extends NonReservedGraphNode {
78  
79      /**
80       * File that contains the about information to show in the application.
81       */
82      static final String ABOUT_FILE = "ABOUT.txt"; //$NON-NLS-1$
83      /**
84       * About description.
85       */
86      static final String IDENTIFICATION = "Zemucan\n" //$NON-NLS-1$
87              + "Copyright (C) 2009, 2010 Andres Gomez Casanova (AngocA)\n" //$NON-NLS-1$
88              + "This program comes with ABSOLUTELY NO WARRANTY;\n" //$NON-NLS-1$
89              + "This is free software, and you are welcome to redistribute it\n" //$NON-NLS-1$
90              + "under certain conditions."; //$NON-NLS-1$
91      /**
92       * Logger.
93       */
94      private static final Logger LOGGER = LoggerFactory
95              .getLogger(AboutNode.class);
96  
97      /**
98       * Appends the about information.
99       *
100      * @param inputStream
101      *            Stream where the about information is.
102      * @return The about information appended.
103      */
104     private static StringBuffer appendAboutInformation(
105             final BufferedReader inputStream) {
106         StringBuffer about = new StringBuffer();
107         String temp;
108         try {
109             while ((temp = inputStream.readLine()) != null) {
110                 about.append(temp);
111                 about.append('\n');
112             }
113         } catch (final IOException e) {
114             // I don't know how to test this part. It's outside my
115             // scope.
116             AboutNode.LOGGER
117                     .debug("Error while reading the about information."); //$NON-NLS-1$
118             about = new StringBuffer(AboutNode.IDENTIFICATION);
119         }
120         return about;
121     }
122 
123     /**
124      * Reads the about information from a file. If it cannot find it, it puts a
125      * default one.
126      *
127      * @return Application's about information.
128      */
129     public static String/* ! */readAboutInformation() {
130         StringBuffer about = new StringBuffer();
131         BufferedReader inputStream = null;
132         try {
133             try {
134                 inputStream = new BufferedReader(new FileReader(
135                         AboutNode.ABOUT_FILE));
136                 about = AboutNode.appendAboutInformation(inputStream);
137             } catch (final FileNotFoundException e) {
138                 AboutNode.LOGGER
139                         .debug("Error about information file not found."); //$NON-NLS-1$
140                 about = new StringBuffer(AboutNode.IDENTIFICATION);
141             }
142         } finally {
143             if (inputStream != null) {
144                 try {
145                     inputStream.close();
146                 } catch (final IOException e) {
147                     // I don't know how to test this part. It's
148                     // outside my scope.
149                     AboutNode.LOGGER
150                             .debug("Error closing about information file."); //$NON-NLS-1$
151                 }
152             }
153         }
154 
155         assert !about.toString().equals(""); //$NON-NLS-1$
156         return about.toString();
157     }
158 
159     /**
160      * Default constructor.
161      *
162      * @param graph
163      *            Associated graph.
164      * @throws InvalidGraphNodeException
165      *             The name is invalid
166      */
167     public AboutNode(final Graph/* ! */graph) throws InvalidGraphNodeException {
168         super(Constants.ABOUT_CONTENT_TOKEN_ID, AboutNode.IDENTIFICATION, graph);
169         final String about = AboutNode.readAboutInformation();
170         super.setName(about);
171     }
172 }