Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ac1acacfb555ed36e99905873a95aed479f8283a (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
 * Copyright (c) 2008 Oracle Corporation.
 * 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:
 *    Ian Trimble - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.jst.pagedesigner.css2.property;

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

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jst.pagedesigner.IHTMLConstants;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.css2.ICSSStyle;
import org.eclipse.jst.pagedesigner.utils.DOMUtil;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.w3c.dom.Element;
import org.w3c.dom.css.CSSValue;

/**
 * Provides metadata for the "background-image" CSS property.
 * 
 * @author Ian Trimble - Oracle
 */
public class BackgroundImageMeta extends CSSPropertyMeta {

	private static final String[] _keywords = {ICSSPropertyID.VAL_NONE};

	/**
	 * Construct an instance.
	 */
	public BackgroundImageMeta() {
		super(false, ICSSPropertyID.VAL_NONE);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#calculateCSSValueResult(org.w3c.dom.css.CSSValue, java.lang.String, org.eclipse.jst.pagedesigner.css2.ICSSStyle)
	 */
	@Override
	public Object calculateCSSValueResult(CSSValue value, String propertyName,
			ICSSStyle style) {
		Object ret = null;
		String valueText = value.getCssText();
		if (valueText != null && valueText.length() > 0) {
			valueText = stripURLSyntax(valueText);
			ret = getImage(valueText, null);
		}
		if (ret == null) {
			ret = getInitialValue(propertyName, style);
		}
		return ret;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#calculateHTMLAttributeOverride(org.w3c.dom.Element, java.lang.String, java.lang.String, org.eclipse.jst.pagedesigner.css2.ICSSStyle)
	 */
	@Override
	public Object calculateHTMLAttributeOverride(Element element,
			String htmltag, String propertyName, ICSSStyle style) {
		Image image = null;
		if (
				element != null &&
				element.getNodeName() != null &&
				element.getNodeName().equalsIgnoreCase(IHTMLConstants.TAG_BODY)) {
			if (ICSSPropertyID.ATTR_BACKGROUND_IMAGE.equalsIgnoreCase(propertyName)) {
				String attrValue = DOMUtil.getAttributeIgnoreCase(element, IHTMLConstants.ATTR_BACKGROUND);
				if (attrValue != null && attrValue.trim().length() > 0) {
					image = getImage(attrValue.trim(), element);
				}
			}
		}
		return image;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#getKeywordValues()
	 */
	@Override
	protected String[] getKeywordValues() {
		return _keywords;
	}

	private String stripURLSyntax(String input) {
		String output = null;
		if (input != null) {
			//strip "url(...)"
			int startPos = input.indexOf("url(") + 4; //$NON-NLS-1$
			if (startPos > -1 && startPos < input.length() - 1) {
				int endPos = input.indexOf(')', startPos);
				if (endPos > startPos) {
					String insideURL = input.substring(startPos, endPos).trim();
					//strip double-quotes
					if (insideURL.startsWith("\"") && insideURL.endsWith("\"")) { //$NON-NLS-1$ //$NON-NLS-2$
						output = insideURL.substring(1, insideURL.length() - 1);
					//strip single-quotes
					} else if (insideURL.startsWith("'") && insideURL.endsWith("'")) { //$NON-NLS-1$ //$NON-NLS-2$
						output = insideURL.substring(1, insideURL.length() - 1);
					} else {
						output = insideURL;
					}
				}
			}
		}
		return output != null ? output : input;
	}

	/* Image instances returned from this method should not be disposed because
	 * they are cached in the plug-in's ImageRegistry and will be disposed of
	 * by the registry.
	 */
	private Image getImage(String imagePath, Element element) {
		Image image = null;
		if (imagePath != null && imagePath.length() > 0) {
			ImageRegistry registry = PDPlugin.getDefault().getImageRegistry();
			image = registry.get(imagePath);
			if (image == null) {
				try {
					URL imageURL = new URL(imagePath);
					ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(imageURL);
					image = imageDescriptor.createImage();
					if (image != null) {
						registry.put(imagePath, image);
					}
				} catch(MalformedURLException mue) {
					//attempt to resolve as relative to document
					if (element instanceof IDOMNode) {
						IDOMModel model = ((IDOMNode)element).getModel();
						if (model != null) {
							String baseLocation = model.getBaseLocation();
							if (baseLocation != null && baseLocation.length() > 0) {
								IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
								if (wsRoot != null) {
									IResource jspRes = wsRoot.findMember(baseLocation);
									if (jspRes != null) {
										IContainer jspFolder = jspRes.getParent();
										if (jspFolder != null) {
											IResource imageRes = jspFolder.findMember(imagePath);
											if (imageRes != null) {
												URI imageURI = imageRes.getLocationURI();
												if (imageURI != null) {
													try {
														URL imageURL = imageURI.toURL();
														ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(imageURL);
														image = imageDescriptor.createImage();
														if (image != null) {
															registry.put(imagePath, image);
														}
													} catch(MalformedURLException mue2) {
														//ignore - what else can be done?
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			} else if (image.isDisposed()) {
				//shouldn't be able to get here from there, but...just in case
				registry.remove(imagePath);
				image = getImage(imagePath, element);
			}
		}
		return image;
	}

}

Back to the top