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 10:15:32 -0500 (dom, 06 mar 2011) $:
26   * Revision: $LastChangedRevision: 1913 $:
27   * URL:      $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/tools/src/main/java/name/angoca/zemucan/tools/file/FileReader.java $:
28   */
29  package name.angoca.zemucan.tools.file;
30  
31  import java.io.File;
32  import java.io.FileInputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.net.URL;
36  
37  import name.angoca.zemucan.AbstractZemucanException;
38  
39  /**
40   * Abstract exception that defines a problem while reading a file.
41   * <p>
42   * <b>Control Version</b>
43   * <p>
44   * <ul>
45   * <li>1.0.0 Class creation.</li>
46   * </ul>
47   *
48   * @author Andres Gomez Casanova <a
49   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
50   * @version 1.0.0 2010-08-22
51   */
52  public class FileReader {
53      /**
54       * Returns the stream that corresponds to the requested file.
55       *
56       * @param fileName
57       *            Name of the file.
58       * @return The stream of the requested file.
59       * @throws AbstractZemucanException
60       *             If there is a problem while retrieving the content.
61       */
62      public static InputStream/* ! */getStream(final String/* ! */fileName)
63              throws AbstractZemucanException {
64          if (fileName == null) {
65              throw new FileNotDefinedException();
66          }
67  
68          InputStream inputStream = null;
69  
70          final ClassLoader loader = ClassLoader.getSystemClassLoader();
71          if (loader != null) {
72              URL url = loader.getResource(fileName);
73              if (url == null) {
74                  url = loader.getResource("/" + fileName);
75              }
76              if (url != null) {
77                  try {
78                      inputStream = url.openStream();
79                  } catch (final IOException exception) {
80                      throw new CorruptFileException(exception);
81                  }
82              } else {
83                  final File file = new File(fileName);
84                  if (file.exists()) {
85                      try {
86                          inputStream = new FileInputStream(file);
87                      } catch (final java.io.FileNotFoundException e) {
88                          throw new name.angoca.zemucan.tools.file.FileNotFoundException(
89                                  fileName, e);
90                      }
91                  } else {
92                      throw new name.angoca.zemucan.tools.file.FileNotFoundException(
93                              fileName);
94                  }
95              }
96          }
97  
98          assert inputStream != null;
99          return inputStream;
100     }
101 }