| 1 | |
package org.jbehave.core.io; |
| 2 | |
|
| 3 | |
import static org.apache.commons.lang.StringUtils.removeEnd; |
| 4 | |
|
| 5 | |
import java.io.File; |
| 6 | |
import java.net.URL; |
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | 1 | public class CodeLocations { |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
public static URL codeLocationFromClass(Class<?> codeLocationClass) { |
| 21 | 27 | String pathOfClass = codeLocationClass.getName().replace(".", "/") + ".class"; |
| 22 | 27 | URL classResource = codeLocationClass.getClassLoader().getResource(pathOfClass); |
| 23 | 27 | String codeLocationPath = removeEnd(classResource.getFile(), pathOfClass); |
| 24 | 27 | return codeLocationFromPath(codeLocationPath); |
| 25 | |
} |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public static URL codeLocationFromPath(String filePath) { |
| 35 | |
try { |
| 36 | 277 | return new File(filePath).toURI().toURL(); |
| 37 | 1 | } catch (Exception e) { |
| 38 | 1 | throw new InvalidCodeLocation(filePath); |
| 39 | |
} |
| 40 | |
} |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public static URL codeLocationFromURL(String url) { |
| 50 | |
try { |
| 51 | 3 | return new URL(url); |
| 52 | 1 | } catch (Exception e) { |
| 53 | 1 | throw new InvalidCodeLocation(url); |
| 54 | |
} |
| 55 | |
} |
| 56 | |
|
| 57 | 1 | @SuppressWarnings("serial") |
| 58 | |
public static class InvalidCodeLocation extends RuntimeException { |
| 59 | |
|
| 60 | |
public InvalidCodeLocation(String path) { |
| 61 | 2 | super(path); |
| 62 | 2 | } |
| 63 | |
|
| 64 | |
} |
| 65 | |
|
| 66 | |
} |