View Javadoc

1   /*
2    * Copyright 2004-2008 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 org.seasar.uruma.util;
17  
18  import org.seasar.framework.util.StringUtil;
19  
20  /**
21   * パスに関するユーティリティメソッドを提供するクラスです。<br />
22   * 
23   * @author y-komori
24   */
25  public class PathUtil {
26      private PathUtil() {
27  
28      }
29  
30      private static final String YEN_SIGN = "\\";
31  
32      private static final String SLASH = "/";
33  
34      private static final char SLASH_CHAR = '/';
35  
36      private static final String PERIOD = ".";
37  
38      private static final String NULL_STRING = "";
39  
40      /**
41       * 与えられた基準パスと相対パスから絶対パスを生成します。</br>
42       * <ul>
43       * <li>パス中の <code>\</code> はすべて <code>/</code> に変換します。
44       * <li>基本動作として <code>basePath</code> と <code>relPath</code>
45       * を連結した文字列を返します。
46       * <li>この際、<code>basePath</code> が <code>/</code> で終了していない場合、<code>/</code>
47       * を付加します。
48       * <li><code>relPath</code> が <code>/</code> から始まる場合、<code>relPath</code>
49       * が絶対パスを表していると見なして、<code>basePath</code> は無視されます。
50       * <li><code>basePath</code> が <code>relPath</code> の先頭に含まれる場合、<code>basePath</code>
51       * は無視されます。
52       * </ul>
53       * 
54       * @param basePath
55       *            基準パス
56       * @param relPath
57       *            相対パス
58       * @return 生成したパス
59       */
60      public static String createPath(String basePath, String relPath) {
61          basePath = replaceSeparator(basePath);
62          relPath = replaceSeparator(relPath);
63          String path = "";
64          if (relPath.charAt(0) == SLASH_CHAR) {
65              basePath = NULL_STRING;
66          }
67          if (!StringUtil.isEmpty(basePath)) {
68              if (!relPath.startsWith(basePath)) {
69                  path += basePath;
70                  if (!path.endsWith(SLASH)) {
71                      path += SLASH;
72                  }
73              }
74          }
75          path += relPath;
76          return path;
77      }
78  
79      /**
80       * セパレータを「\」から「/」へ変換します。<br />
81       * 
82       * @param path
83       *            パス
84       * @return 変換後のパス
85       */
86      public static String replaceSeparator(final String path) {
87          if (path != null) {
88              return StringUtil.replace(path, YEN_SIGN, SLASH);
89          } else {
90              return NULL_STRING;
91          }
92      }
93  
94      /**
95       * 与えられたパスのベースパスを基準とした相対パスを返します。<br />
96       * 
97       * @param basePath
98       *            ベースパス
99       * @param path
100      *            パス
101      */
102     public static String getRelativePath(final String basePath,
103             final String path) {
104         AssertionUtil.assertNotNull("basePath", basePath);
105         AssertionUtil.assertNotNull("path", path);
106 
107         if (path.startsWith(basePath)) {
108             return path.substring(basePath.length(), path.length());
109         } else {
110             return path;
111         }
112     }
113 
114     /**
115      * 指定されたパスの親ディレクトリ部分を返します。<br />
116      * セパレータは \ と / の両方を認識します。<br />
117      * 混在している場合は、より後ろの方を区切りとします。<br />
118      * 
119      * @param path
120      *            パス
121      * @return 親ディレクトリ部分
122      */
123     public static String getParent(final String path) {
124         if (path != null) {
125             int slashIndex = path.lastIndexOf(SLASH);
126             int yenIndex = path.lastIndexOf(YEN_SIGN);
127             int pos = (slashIndex > yenIndex) ? slashIndex : yenIndex;
128             if (pos >= 0) {
129                 return path.substring(0, pos);
130             } else {
131                 return path;
132             }
133         } else {
134             return null;
135         }
136     }
137 
138     /**
139      * 指定されたパスのファイル名部分を返します。<br />
140      * セパレータは \ と / の両方を認識します。<br />
141      * 混在している場合は、より後ろの方を区切りとします。<br />
142      * 
143      * @param path
144      *            パス
145      * @return ファイル名部分
146      */
147     public static String getFileName(final String path) {
148         if (path != null) {
149             int slashIndex = path.lastIndexOf(SLASH);
150             int yenIndex = path.lastIndexOf(YEN_SIGN);
151             int pos = (slashIndex > yenIndex) ? slashIndex : yenIndex;
152             return path.substring(pos + 1, path.length());
153         } else {
154             return null;
155         }
156     }
157 
158     /**
159      * ファイル名から拡張子を除いた部分を返します。<br />
160      * 
161      * @param fileName
162      *            フィル名
163      * @return 拡張子を除いた部分
164      */
165     public static String getBaseName(final String fileName) {
166         if (fileName != null) {
167             return StringUtil.substringFromLast(fileName, PERIOD);
168         } else {
169             return null;
170         }
171     }
172 
173     /**
174      * パスの拡張子の部分(最後に登場するピリオド以降)を返します。<br />
175      * 
176      * @param path
177      *            パス
178      * @return 拡張子
179      */
180     public static String getExt(final String path) {
181         if (path != null) {
182             if (path.indexOf(PERIOD) > 0) {
183                 return StringUtil.substringToLast(path, PERIOD);
184             } else {
185                 return NULL_STRING;
186             }
187         } else {
188             return null;
189         }
190     }
191 }