View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
23   */
24  
25  package mk.fsgrep.util;
26  
27  
28  import java.io.*;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  
33  /***
34   * Read a resource from the jar (classpath actually) and return it as a string.
35   * 
36   * @author  Murali Krishnan
37   *
38   */
39  public class TextResource extends PackageResource {     
40  
41      //------------------------------------------------------------  
42      //- Class Variables     
43  
44      private static final Map<String,String> sCache = new HashMap<String,String>();
45  
46  
47  
48      //------------------------------------------------------------  
49      //- Class Functions     
50  
51      public static String get(String pUri) {
52          String result = sCache.get(pUri);
53  
54          if (result == null) {
55              TextResource resource = new TextResource(pUri);
56              result = resource.toString();
57              sCache.put(pUri, result);
58          }
59  
60          return result;
61      }
62  
63  
64      //------------------------------------------------------------  
65      //- Instance Variables  
66  
67  
68  
69      //------------------------------------------------------------  
70      //- Constructors        
71  
72      protected TextResource(String pUri) {
73          super(pUri);
74      }
75  
76  
77      //------------------------------------------------------------  
78      //- Accessors   
79  
80  
81  
82      //------------------------------------------------------------  
83      //- Settors     
84  
85  
86  
87      //------------------------------------------------------------  
88      //- Private/Protected Utility Functions 
89  
90      protected synchronized Object readContent() {
91          String result = "";  // Error case default.
92  
93          StringBuffer buffer = new StringBuffer();
94          InputStream is = getClass().getResourceAsStream(getUri());
95          if (is != null) {
96              try {
97                  InputStreamReader isr = new InputStreamReader(is);
98                  BufferedReader br = new BufferedReader(isr);
99  
100                 for (String line=br.readLine(); line!=null; line=br.readLine()) {
101                     buffer.append(line).append("\n");
102                 }
103 
104                 br.close();
105                 isr.close();
106                 is.close();
107 
108                 result = buffer.toString().trim();
109             } catch (Exception ex) {
110                 // Could be IOException, but would be NullPointerException
111                 // if uri points to a non-existent resource.
112                 System.err.println("Could not fetch the text resource [" +
113                                    getUri() + "].");
114                 ex.printStackTrace();
115             }
116         } else {
117             System.err.println("Invalid resource [" + getUri() + "]");
118         }
119 
120         return result;
121     }
122 
123 
124 
125     //------------------------------------------------------------  
126     //- Public Interface Functions  
127 
128     public String toString() {
129         String result = (String) getContent();
130 
131         return result;
132     }
133 
134 
135 
136     //------------------------------------------------------------  
137     //- Class Interface Functions   
138 
139 
140 
141     //------------------------------------------------------------  
142     //- Inner Classes       
143 
144 
145 
146     //------------------------------------------------------------  
147     //- Main        
148 
149 
150 
151 }