comparison core/src/luan/modules/Utils.java @ 203:99eef1d0e706

IO security git-svn-id: https://luan-java.googlecode.com/svn/trunk@204 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 06 Jul 2014 03:54:08 +0000
parents 1cb298d918b2
children a1b142f9c5c0
comparison
equal deleted inserted replaced
202:75750ceb45ee 203:99eef1d0e706
58 } catch(IOException e) { 58 } catch(IOException e) {
59 throw new RuntimeException(e); 59 throw new RuntimeException(e);
60 } 60 }
61 } 61 }
62 62
63 public static boolean isFile(String path) { 63 public static File toFile(String path) {
64 return !path.contains("//") && exists(new File(path)); 64 if( path.contains("//") )
65 return null;
66 File file = new File(path);
67 return exists(file) ? file : null;
65 } 68 }
66 69
67 public static String toUrl(String path) { 70 public static URL toUrl(String path) {
68 if( path.indexOf(':') == -1 ) 71 if( path.indexOf(':') == -1 )
69 return null; 72 return null;
70 if( path.startsWith("java:") ) { 73 if( path.startsWith("java:") ) {
71 path = path.substring(5); 74 path = path.substring(5);
72 if( path.contains("//") ) 75 if( path.contains("//") )
73 return null; 76 return null;
74 URL url = ClassLoader.getSystemResource(path); 77 URL url = ClassLoader.getSystemResource(path);
75 return url==null ? null : url.toString(); 78 return url==null ? null : url;
76 } 79 }
77 try { 80 try {
78 new URL(path); 81 return new URL(path);
79 return path;
80 } catch(MalformedURLException e) {} 82 } catch(MalformedURLException e) {}
81 return null; 83 return null;
82 } 84 }
83 85
84 public static boolean exists(String path) { 86 public static boolean exists(String path) {
85 return isFile(path) || toUrl(path)!=null; 87 return toFile(path)!=null || toUrl(path)!=null;
86 } 88 }
87 } 89 }