Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: db4f13b5edf757c6112c4c5dbb91d63c401bbe7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*******************************************************************************
 * Copyright (c) 2007, 2009 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/

package org.eclipse.jpt.jpa.gen.internal.util;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;

/**
 * Collections of utility methods handling URLs.
 * 
 */
public class UrlUtil
{
	/**
	 * The <code>file</code> string indicating a url file protocol.
	 */
	public static final String FILE_PROTOCOL = "file";
	/**
	 * The <code>file</code> string indicating a url http protocol.
	 */
	public static final String HTTP_PROTOCOL = "http";
	/**
	 * The <code>file</code> string indicating a url http protocol.
	 */
	public static final String HTTPS_PROTOCOL = "https";
	/**
	 * The <code>file</code> string indicating a url file protocol.
	 */
	public static final String JAR_PROTOCOL = "jar";

	
	/**
	 * Returns true if the specified url is to a file, i.e its protocol is <code>file</code>.
	 */
	public static boolean isFileUrl(URL url) {
		return url != null && FILE_PROTOCOL.equals(url.getProtocol());
	}
	/**
	 * Returns true if the specified url is to a jar, i.e its protocol is <code>jar</code>.
	 * For example <code>jar:file:/C:/testapps/example/WEB-INF/lib/struts.jar!/META-INF/tlds/struts-bean.tld</code>.
	 */
	public static boolean isJarUrl(URL url) {
		return url != null && JAR_PROTOCOL.equals(url.getProtocol());
	}
	/**
	 * Returns true if the specified url protocol is http.
	 */
	public static boolean isHttpUrl(URL url) {
		String protocol =  url.getProtocol();
		return url != null && (HTTP_PROTOCOL.equals(protocol) || HTTPS_PROTOCOL.equals(protocol));
	}
	/**
	 * Returns the <code>File</code> corresponding to a url, or null if the url 
	 * protocol is not file.
	 */
	public static java.io.File getUrlFile(URL url) {
		if (isFileUrl(url) && !isJarUrl( url ) ){
			File ret = new java.io.File(url.getFile());
			return ret ;
		}
		return null;
 	}

	
	/**
	 * Returns the url to a jar file given a url to a file inside 
	 * the jar.
	 * For example if given 
	 * <code>jar:file:/C:/testapps/example/WEB-INF/lib/struts.jar!/META-INF/tlds/struts-bean.tld</code>
	 * this method returns <code>file:/C:/testapps/example/WEB-INF/lib/struts.jar</code>.
	 * 
	 * <p>Returns null if the given url is not recognized as a url to a file 
	 * inside a jar.
	 */
	public static URL getJarFileUrl(URL url) {
		if (!isJarUrl(url)) {
			return null;
		}
		String file = url.getFile(); //file:/C:/testapps/example/WEB-INF/lib/struts.jar!/META-INF/tlds/struts-bean.tld
		int index = file.indexOf('!');
		if (index < 0) {
			return null;
		}
		String jarFileUrlStr = file.substring(0, index);
		try {
			return new URL(jarFileUrlStr);
		} catch (MalformedURLException e) {
			return null;
		}
	}
	
	public static boolean isRemote(String url){
		return url.startsWith("http:")||url.startsWith("https:")||url.startsWith("www.");
	}
	
	public static File getTemplateFolder(String plugId , String strPath ){
		Bundle bundle = Platform.getBundle( plugId );
		Path path = new Path( strPath );
		URL url = FileLocator.find(bundle, path, null);
		try {
			URL templUrl;
			templUrl = FileLocator.resolve(url);
			File templDir = UrlUtil.getUrlFile(templUrl);
			return templDir ;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

Back to the top