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  import java.io.*;
28  import java.util.regex.*;
29  
30  /***
31   * Read a resource from the jar (classpath actually).
32   * 
33   * @author  Murali Krishnan
34   *
35   */
36  public abstract class PackageResource {     
37  
38      //------------------------------------------------------------  
39      //- Class Variables     
40  
41      public static final String INCLUDE_PATH = "/include";
42  
43      public static final Pattern RE_MULTI_SLASH = Pattern.compile("[/////]+");
44  
45  
46      //------------------------------------------------------------  
47      //- Class Functions     
48  
49  
50  
51      //------------------------------------------------------------  
52      //- Instance Variables  
53  
54      private String _uri = null;
55      private Object _content = null;
56  
57  
58  
59      //------------------------------------------------------------  
60      //- Constructors        
61  
62      public PackageResource(String pUri) {
63          String path = RE_MULTI_SLASH.matcher(pUri).replaceAll("/");
64  
65          if (!path.startsWith(INCLUDE_PATH)) {
66              path = INCLUDE_PATH + "/" + path;
67          }
68  
69          _uri = RE_MULTI_SLASH.matcher(path).replaceAll("/");
70      }
71  
72  
73      //------------------------------------------------------------  
74      //- Accessors   
75  
76      public String getUri() {return _uri;}
77  
78  
79  
80      //------------------------------------------------------------  
81      //- Settors     
82  
83  
84  
85      //------------------------------------------------------------  
86      //- Private/Protected Utility Functions 
87  
88      protected Object getContent() {
89          if (_content == null) {
90              _content = readContent();
91          }
92  
93          return _content;
94      }
95  
96  
97  
98      //------------------------------------------------------------  
99      //- Public Interface Functions  
100 
101     public String toString() {
102         String result = "Resource[" + getUri() + "]";
103 
104         return result;
105     }
106 
107 
108 
109     //------------------------------------------------------------  
110     //- Class Interface Functions   
111 
112     protected abstract Object readContent();
113 
114 
115     //------------------------------------------------------------  
116     //- Inner Classes       
117 
118 
119 
120     //------------------------------------------------------------  
121     //- Main        
122 
123 
124 
125 }