Skip to main content
summaryrefslogtreecommitdiffstats
blob: 29305bf9f640f87edb861a3daeb10ac2202d8aee (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.utils;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wst.sse.core.internal.util.URIResolver;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.w3c.dom.Element;

/**
 * @author mengbo
 * @version 1.5
 */
public class ImageResolver {
	/**
	 * 
	 * @param element
	 * @param attrName
	 * @return
	 */
	static String getResolvedURL(Element element, String attrName) {
		URIResolver resolver = null;
		if (element instanceof IDOMNode) {
			resolver = ((IDOMNode) element).getModel().getResolver();
		}
		if (null == resolver) {
			return null;
		}
		String src = DOMUtil.getAttributeIgnoreCase(element, attrName);
		if (src != null && src.length() > 0) {
			return resolver.getLocationByURI(src);
		}
		return null;
	}

	/**
	 * given the element and an attribute name identifying the src of the image,
	 * create a image.
	 * 
	 * @param element
	 * @param attrName
	 * @return the new image
	 */
	public static Image initializeImage(Element element, String attrName) {
		String url = getResolvedURL(element, attrName);
		if (url == null) {
			return null;
		}
		Image img = null;
		int colonIndex = url.indexOf(":");
		int slashIndex = url.indexOf("/");
		if (colonIndex != -1 && (slashIndex != -1 && colonIndex < slashIndex)) {
			//the url seems to have a protocol, so try to load it as a URL
			try {
				URL urlObj = new URL(url);
				ImageDescriptor imgDesc = ImageDescriptor.createFromURL(urlObj);
				img = imgDesc.createImage(false);
			} catch(MalformedURLException mfe) {
				//attempt to load as a file
				try {
					img = new Image(null, url);
				} catch(SWTException se) {
					//img remains null on return
				}
			} catch(SWTException se) {
				//img remains null on return
			}
		} else {
			//no protocol, so load it as a file
			try {
				img = new Image(null, url);
			} catch(SWTException se) {
				//img remains null on return
			}
		}
		return img;
	}
}

Back to the top