View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package com.isenshi.util;
17  
18  public class PathUtils {
19  	
20  	public static final String PATH_SEPARATOR = "/";
21  	
22  
23  	public static final String join(String parent, String child) {
24  		if(parent == null || parent.length() == 0){
25  			return child;
26  		}
27  		return CharUtil.chartrim(parent, '/') + "/" + child;
28  	}
29  	
30  	/***
31  	 * 
32  	 * @param path     ex. /folderA/fileName.ext
33  	 * @return        ex. /folderA
34  	 */
35  	public static final String getParentPath(String path) {
36  		if (path == null) {
37  			return null;
38  		}
39  		path = CharUtil.chartrim(path, '/');
40  		int index = path.lastIndexOf("/");
41  		if (index < 1) {
42  			return null;
43  		}
44  		return path.substring(0, index);
45  	}
46  	
47  	/***
48  	 * 
49  	 * @param path    ex. /folderA/fileName.suf
50  	 * @return        ex. fileName.suf
51  	 */
52  	public static String getResourceName(String path) {
53  		if(path == null){
54  			return null;
55  		}
56  		path = CharUtil.chartrim(path, '/');
57  		int index = path.lastIndexOf('/');
58  		if (index == -1) {
59  			return path;
60  		} else {
61  			return path.substring(index + 1);
62  		}
63  	}
64  	
65  	
66  	/***
67  	 * 
68  	 * @param path    ex. /folderA/fileName.suf
69  	 * @return        ex. suf
70  	 */
71  	public static String getExtension(String path) {
72  		int index = path.lastIndexOf('.');
73  		if(index != -1 && index != path.length() - 1){
74  			return path.substring(index + 1);
75  		}else{
76  			return "";
77  		}
78  	}
79  
80  	/***
81  	 * 
82  	 * @param path    ex. /folderA/fileName.suf
83  	 * @return        ex. fileName
84  	 */
85  	public static String getFileName(String path) {
86  		if(path == null){
87  			return path;
88  		}
89  		String resourceName = getResourceName(path);
90  		int index = resourceName.lastIndexOf('.');
91  		if(index >= 0){
92  			return resourceName.substring(0, index);
93  		}else{
94  			return path;
95  		}
96  	}
97  	
98  	/***
99  	 * 
100 	 * @param rootPath ex. /foo
101 	 * @param path     ex. /foo/bar
102 	 * @return         ex. bar
103 	 */
104 	public static String getRelativePath(String rootPath, String path){
105 		rootPath = CharUtil.charpop(rootPath, '/');
106 		path = CharUtil.charpop(path, '/');
107 		if(path.length() > rootPath.length()){
108 			path = path.substring(rootPath.length());
109 			path = CharUtil.charpop(path, '/');
110 			return path;	
111 		}else{
112 			return path;
113 		}
114 	}
115 	
116 	/***
117 	 * @param path ex. /foo/bar/path.suf
118 	 * @return     ex. /foo
119 	 */
120 	public static String getRootPath(String path){		
121 		path = CharUtil.charpop(path,'/');
122 		String[] buf = path.split("/");		
123 		return buf[0];
124 	}
125 	
126 	
127 	
128 }