001package io.ebean.enhance.common; 002 003import java.io.File; 004import java.net.MalformedURLException; 005import java.net.URL; 006import java.util.ArrayList; 007 008/** 009 * Helper methods for URL class path conversion. 010 */ 011public class UrlPathHelper { 012 013 private static final String PROTOCAL_PREFIX = "file:"; 014 015 /** 016 * Convert string paths into URL class paths. 017 */ 018 public static URL[] convertToUrl(String[] paths) { 019 ArrayList<URL> list = new ArrayList<>(); 020 for (String path : paths) { 021 URL url = convertToUrl(path); 022 if (url != null) { 023 list.add(url); 024 } 025 } 026 return list.toArray(new URL[0]); 027 } 028 029 /** 030 * Convert string path into URL class path. 031 */ 032 public static URL convertToUrl(String path) { 033 if (isEmpty(path)) { 034 return null; 035 } 036 try { 037 return new URL(PROTOCAL_PREFIX + convertUrlString(path)); 038 } catch (MalformedURLException e) { 039 throw new RuntimeException(e); 040 } 041 } 042 043 /** 044 * Convert a string path to be used in URL class path entry. 045 */ 046 public static String convertUrlString(String classpath) { 047 if (isEmpty(classpath)) { 048 return ""; 049 } 050 classpath = classpath.trim(); 051 if (classpath.length() < 2) { 052 return ""; 053 } 054 if (classpath.charAt(0) != '/' && classpath.charAt(1) == ':') { 055 // add leading slash for windows platform 056 // assuming drive letter path 057 classpath = "/" + classpath; 058 } 059 if (!classpath.endsWith("/")) { 060 File file = new File(classpath); 061 if (file.exists() && file.isDirectory()) { 062 classpath = classpath.concat("/"); 063 } 064 } 065 return classpath; 066 } 067 068 private static boolean isEmpty(String s) { 069 return s == null || s.trim().length() == 0; 070 } 071}