gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / URLUtils.java @ 2441
History | View | Annotate | Download (4.8 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.util; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.FileInputStream; |
28 |
import java.io.FileNotFoundException; |
29 |
import java.io.IOException; |
30 |
import java.io.InputStream; |
31 |
import java.net.MalformedURLException; |
32 |
import java.net.URISyntaxException; |
33 |
import java.net.URL; |
34 |
import org.apache.commons.lang3.StringUtils; |
35 |
|
36 |
/**
|
37 |
*
|
38 |
* @author gvSIG Team
|
39 |
*/
|
40 |
public class URLUtils { |
41 |
|
42 |
public static InputStream openStream(URL url) { |
43 |
if( url == null ) { |
44 |
return null; |
45 |
} |
46 |
try {
|
47 |
return url.openStream();
|
48 |
} catch (IOException ex) { |
49 |
// Do nthhing
|
50 |
} |
51 |
// En windows hay un problema con los URL de tipo fichero.
|
52 |
// A veces se construyen URL de tipo "file" que no son correctas pero
|
53 |
// que en linux funcionan y en windows no.
|
54 |
// Tiene que ver con el numero de barras que se pongan detras del "file:".
|
55 |
// Segun las que se pongan en window interpreta la letra de unidad como
|
56 |
// el host o como letra de unidad y eso hace que en ocasiones
|
57 |
// se produzcan errores en windows que no se dan a los desarrolladores
|
58 |
// en linux.
|
59 |
// Asi que si falla el openStream de la URL, y es de tipo "file",
|
60 |
// intentaremos reconstruir el File siendo mas flexibles con interpretacion
|
61 |
// de las barras del inicio de la URL.
|
62 |
//
|
63 |
String protocol = url.getProtocol();
|
64 |
String path = url.getPath();
|
65 |
if( protocol==null || path==null || path.isEmpty() || !protocol.equalsIgnoreCase("file") ) { |
66 |
return null; |
67 |
} |
68 |
File f;
|
69 |
String host = url.getHost();
|
70 |
int l = ((host==null)? 0:host.length()); |
71 |
switch(l) {
|
72 |
case 0: |
73 |
if( path.charAt(0)=='/' || path.charAt(2)==':') { |
74 |
f = new File(path.substring(1)); |
75 |
} else {
|
76 |
f = new File(path); |
77 |
} |
78 |
break;
|
79 |
case 1: |
80 |
f = new File(host+":"+path); |
81 |
break;
|
82 |
case 2: |
83 |
if( host.charAt(1)!=':' ) { // Aqui no lleha con host==null |
84 |
return null; |
85 |
} |
86 |
f = new File(host+path); |
87 |
break;
|
88 |
default:
|
89 |
return null; |
90 |
} |
91 |
FileInputStream is;
|
92 |
try {
|
93 |
is = new FileInputStream(f); |
94 |
} catch (FileNotFoundException ex) { |
95 |
return null; |
96 |
} |
97 |
return is;
|
98 |
} |
99 |
|
100 |
public static String toPath(URL url) { |
101 |
if( url == null ) { |
102 |
return null; |
103 |
} |
104 |
try {
|
105 |
String path;
|
106 |
if( StringUtils.contains(url.getPath(), "<%=") ) { |
107 |
path = url.getPath(); |
108 |
} else {
|
109 |
path = url.toURI().getPath(); |
110 |
} |
111 |
return path;
|
112 |
} catch (URISyntaxException ex) { |
113 |
throw new IllegalArgumentException(ex); |
114 |
} |
115 |
} |
116 |
|
117 |
public static File toFile(URL url) { |
118 |
if( url == null ) { |
119 |
return null; |
120 |
} |
121 |
try {
|
122 |
String path;
|
123 |
if( StringUtils.contains(url.getPath(), "<%=") ) { |
124 |
path = url.getPath(); |
125 |
} else {
|
126 |
path = url.toURI().getPath(); |
127 |
} |
128 |
return new File(path); |
129 |
} catch (URISyntaxException ex) { |
130 |
throw new IllegalArgumentException(ex); |
131 |
} |
132 |
} |
133 |
|
134 |
public static URL toURL(File f) { |
135 |
if( f == null ) { |
136 |
return null; |
137 |
} |
138 |
URL url;
|
139 |
try {
|
140 |
if( StringUtils.contains(f.getPath(), "<%=") ) { |
141 |
url = new URL("file", null, f.getPath()); |
142 |
} else {
|
143 |
url = f.toURI().toURL(); |
144 |
} |
145 |
} catch (MalformedURLException ex) { |
146 |
throw new IllegalArgumentException(ex); |
147 |
} |
148 |
return url;
|
149 |
} |
150 |
|
151 |
public static void main(String[] args) throws MalformedURLException { |
152 |
File f = new File("C:\\Users\\jolic\\gvSIG\\plugins\\org.gvsig.scripting.app.mainplugin\\2.5.1\\scripts\\addons\\Arena2Reader\\i18n\\text.properties"); |
153 |
URLUtils.openStream(f.toURI().toURL()); |
154 |
URLUtils.openStream(new URL("file:/C:/text.properties")); |
155 |
URLUtils.openStream(new URL("file://C:/text.properties")); |
156 |
URLUtils.openStream(new URL("file:///C:/text.properties")); |
157 |
} |
158 |
} |