KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > FileLocator


1 /*
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */

16 package com.jdon.util;
17
18 import java.io.InputStream JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.io.File JavaDoc;
21
22 /**
23  * the locator for configure file
24  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
25  *
26  */

27 public class FileLocator {
28
29  /**
30   * com.jdon.sample.xxx.xml ==>
31   * com/jdon/sample/xxx.xml
32   * @param filePathName
33   * @return filename's string
34   */

35   public String JavaDoc getConfPathXmlFile(String JavaDoc filePathName){
36      int i = filePathName.lastIndexOf(".xml");
37      String JavaDoc name = filePathName.substring(0, i);
38      name = name.replace('.', '/');
39      name += ".xml";
40      return getConfFile(name);
41   }
42
43   /**
44    * same as getConfPathXmlFile
45    * @param filePathName
46    * @return the InputStream intance
47    */

48   public InputStream JavaDoc getConfPathXmlStream(String JavaDoc filePathName){
49      int i = filePathName.lastIndexOf(".xml");
50      String JavaDoc name = filePathName.substring(0, i);
51      name = name.replace('.', '/');
52      name += ".xml";
53      return getConfStream(name);
54   }
55
56
57   public String JavaDoc getConfFile(String JavaDoc fileName) {
58     ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
59     if (classLoader == null) {
60       classLoader = getClass().getClassLoader();
61     }
62     URL JavaDoc confURL = classLoader.getResource(fileName);
63     if (confURL == null)
64       confURL = classLoader.getResource("META-INF/" + fileName);
65     if (confURL == null) {
66       return null;
67     } else {
68       File JavaDoc file1 = new File JavaDoc(confURL.getFile());
69       if (file1.isFile()) {
70         System.out.println(" locate file: " + confURL.getFile());
71         return confURL.getFile();
72       } else {
73         System.err.println(" it is not a file: " + confURL.getFile());
74         return null;
75       }
76     }
77   }
78
79   public InputStream JavaDoc getConfStream(String JavaDoc fileName) {
80     ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
81     if (classLoader == null) {
82       classLoader = this.getClass().getClassLoader();
83     }
84     InputStream JavaDoc stream = classLoader.getResourceAsStream(fileName);
85     if (stream == null)
86       stream = classLoader.getResourceAsStream("META-INF/" + fileName);
87    
88     return stream;
89   }
90
91 }
92
Popular Tags