Coverage Report - org.seasar.uruma.rcp.util.RcpResourceUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
RcpResourceUtil
11%
5/46
0%
0/14
0
 
 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.rcp.util;
 17  
 
 18  
 import java.io.File;
 19  
 import java.io.FileFilter;
 20  
 import java.io.IOException;
 21  
 import java.net.URL;
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 
 25  
 import org.eclipse.core.runtime.FileLocator;
 26  
 import org.eclipse.core.runtime.Platform;
 27  
 import org.eclipse.core.runtime.Plugin;
 28  
 import org.eclipse.osgi.service.datalocation.Location;
 29  
 import org.osgi.framework.Bundle;
 30  
 import org.seasar.framework.exception.IORuntimeException;
 31  
 import org.seasar.framework.util.AssertionUtil;
 32  
 import org.seasar.framework.util.ResourceUtil;
 33  
 import org.seasar.uruma.util.PathUtil;
 34  
 
 35  
 /**
 36  
  * Eclipse / RCP 環境でのリソースを扱うためのユーティリティクラスです。<br />
 37  
  * 
 38  
  * @author y-komori
 39  
  */
 40  0
 public class RcpResourceUtil {
 41  
     private static final String SLASH = "/";
 42  
 
 43  
     private static final String PROTCOL_JAR = "jar";
 44  
 
 45  
     private static final String PROTCOL_FILE = "file";
 46  
 
 47  
     /**
 48  
      * 指定されたプラグインの実行時パスを返します。<br />
 49  
      * 
 50  
      * @param uiPlugin
 51  
      *            実行時パスを調べる {@link Plugin}
 52  
      * @return 実行時パスを表す {@link URL} オブジェクト
 53  
      * @throws IOException
 54  
      *             パスの変換に失敗した場合
 55  
      * @see FileLocator#resolve(URL)
 56  
      */
 57  
     public static URL getNativePluginPath(final Plugin uiPlugin)
 58  
             throws IOException {
 59  0
         URL pluginUrl = uiPlugin.getBundle().getEntry(SLASH);
 60  0
         return FileLocator.resolve(pluginUrl);
 61  
     }
 62  
 
 63  
     /**
 64  
      * {@link Bundle}の絶対パスを返します。<br />
 65  
      * 
 66  
      * @param bundle
 67  
      *            {@link Bundle}
 68  
      * @return {@link Bundle}の絶対パス
 69  
      */
 70  
     public static String getBunldePath(final Bundle bundle) {
 71  0
         URL entry = bundle.getEntry(SLASH);
 72  0
         String pluginDirPath = null;
 73  
         try {
 74  0
             pluginDirPath = FileLocator.resolve(entry).getPath();
 75  0
         } catch (IOException e) {
 76  0
             throw new IORuntimeException(e);
 77  0
         }
 78  0
         return removeHeadChar(pluginDirPath);
 79  
     }
 80  
 
 81  
     /**
 82  
      * 指定されたパスにおけるリソースのローカルシステム上の {@link URL} を返します。<br />
 83  
      * 
 84  
      * @param path
 85  
      *            リソースのパス
 86  
      * @return ローカルシステム上の {@link URL}。パスが存在しない場合は <code>null</code>。
 87  
      * @throws IOException
 88  
      *             パスの変換に失敗した場合
 89  
      * @see FileLocator#resolve(URL)
 90  
      */
 91  
     public static URL getLocalResourceUrl(final String path) throws IOException {
 92  0
         URL url = ResourceUtil.getClassLoader().getResource(path);
 93  0
         if (url != null) {
 94  0
             return FileLocator.resolve(url);
 95  
         } else {
 96  0
             return null;
 97  
         }
 98  
     }
 99  
 
 100  
     /**
 101  
      * 指定されたパスにおけるリソースのローカルシステム上の {@link URL} を返します。<br />
 102  
      * 
 103  
      * @param path
 104  
      *            リソースのパス
 105  
      * @return ローカルシステム上の {@link URL} <br />
 106  
      *         リソースが存在しない場合は、NULLを返却。
 107  
      * @see FileLocator#resolve(URL)
 108  
      */
 109  
     public static URL getLocalResourceUrlNoException(final String path) {
 110  16
         URL url = ResourceUtil.getClassLoader().getResource(path);
 111  16
         URL fileLocatorUrl = null;
 112  
         try {
 113  16
             fileLocatorUrl = FileLocator.resolve(url);
 114  16
         } catch (Exception ignore) {
 115  0
         }
 116  16
         return fileLocatorUrl;
 117  
     }
 118  
 
 119  
     /**
 120  
      * 指定されたパスのバンドルルートパスからの相対パスを返します。<br />
 121  
      * 
 122  
      * @param bundle
 123  
      *            バンドル
 124  
      * @param path
 125  
      *            パス
 126  
      * @return 相対パス。相対パスが得られなかった場合、 <code>null</code>。
 127  
      */
 128  
     public static String getBundleRelativePath(final Bundle bundle,
 129  
             final String path) {
 130  0
         AssertionUtil.assertNotNull("bundle", bundle);
 131  0
         AssertionUtil.assertNotNull("path", path);
 132  
         try {
 133  0
             URL url = getLocalResourceUrl(path);
 134  0
             if (url != null) {
 135  0
                 URL bundlePath = FileLocator.resolve(bundle.getEntry(SLASH));
 136  0
                 return PathUtil.getRelativePath(bundlePath.getPath(), url
 137  
                         .getPath());
 138  
             } else {
 139  0
                 return null;
 140  
             }
 141  0
         } catch (IOException ignore) {
 142  0
             return null;
 143  
         }
 144  
 
 145  
     }
 146  
 
 147  
     /**
 148  
      * 指定されたパスを起点として、ファイルシステムからリソースを再帰的に検索して返します。<br />
 149  
      * 
 150  
      * @param baseDir
 151  
      *            起点となるパス
 152  
      * @param filter
 153  
      *            条件を指定する {@link FileFilter}
 154  
      * @return 検索結果
 155  
      */
 156  
     public static List<File> findFileResources(final File baseDir,
 157  
             final FileFilter filter) {
 158  0
         List<File> results = new ArrayList<File>();
 159  
 
 160  0
         if (baseDir.isDirectory()) {
 161  0
             findFileResources(baseDir, filter, results);
 162  
         }
 163  
 
 164  0
         return results;
 165  
     }
 166  
 
 167  
     private static void findFileResources(final File parentDir,
 168  
             final FileFilter filter, final List<File> results) {
 169  0
         File[] files = parentDir.listFiles();
 170  0
         for (int i = 0; i < files.length; i++) {
 171  0
             File file = files[i];
 172  
 
 173  0
             if (file.isDirectory()) {
 174  0
                 findFileResources(file, filter, results);
 175  0
             } else if (file.isFile() && filter.accept(file)) {
 176  0
                 results.add(file);
 177  
             }
 178  
         }
 179  0
     }
 180  
 
 181  
     /**
 182  
      * アプリケーションの絶対パスを返します。 <br />
 183  
      * 
 184  
      * @return アプリケーションの絶対パス
 185  
      */
 186  
     public static String getInstallPath() {
 187  0
         Location installLocation = Platform.getInstallLocation();
 188  0
         String path = installLocation.getURL().getFile();
 189  0
         return removeHeadChar(path);
 190  
     }
 191  
 
 192  
     private static String removeHeadChar(final String path) {
 193  0
         AssertionUtil.assertNotEmpty("path", path);
 194  0
         return path.substring(1, path.length() - 1);
 195  
     }
 196  
 }