Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / css2 / property / BackgroundImageMeta.java
1 /*******************************************************************************\r
2  * Copyright (c) 2008 Oracle Corporation.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *    Ian Trimble - initial API and implementation\r
10  *******************************************************************************/ \r
11 package org.eclipse.jst.pagedesigner.css2.property;\r
12 \r
13 import java.net.MalformedURLException;\r
14 import java.net.URI;\r
15 import java.net.URL;\r
16 \r
17 import org.eclipse.core.resources.IContainer;\r
18 import org.eclipse.core.resources.IResource;\r
19 import org.eclipse.core.resources.IWorkspaceRoot;\r
20 import org.eclipse.core.resources.ResourcesPlugin;\r
21 import org.eclipse.jface.resource.ImageDescriptor;\r
22 import org.eclipse.jface.resource.ImageRegistry;\r
23 import org.eclipse.jst.pagedesigner.IHTMLConstants;\r
24 import org.eclipse.jst.pagedesigner.PDPlugin;\r
25 import org.eclipse.jst.pagedesigner.css2.ICSSStyle;\r
26 import org.eclipse.jst.pagedesigner.utils.DOMUtil;\r
27 import org.eclipse.swt.graphics.Image;\r
28 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;\r
29 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;\r
30 import org.w3c.dom.Element;\r
31 import org.w3c.dom.css.CSSValue;\r
32 \r
33 /**\r
34  * Provides metadata for the "background-image" CSS property.\r
35  * \r
36  * @author Ian Trimble - Oracle\r
37  */\r
38 public class BackgroundImageMeta extends CSSPropertyMeta {\r
39 \r
40         private static final String[] _keywords = {ICSSPropertyID.VAL_NONE};\r
41 \r
42         /**\r
43          * Construct an instance.\r
44          */\r
45         public BackgroundImageMeta() {\r
46                 super(false, ICSSPropertyID.VAL_NONE);\r
47         }\r
48 \r
49         /* (non-Javadoc)\r
50          * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#calculateCSSValueResult(org.w3c.dom.css.CSSValue, java.lang.String, org.eclipse.jst.pagedesigner.css2.ICSSStyle)\r
51          */\r
52         @Override\r
53         public Object calculateCSSValueResult(CSSValue value, String propertyName,\r
54                         ICSSStyle style) {\r
55                 Object ret = null;\r
56                 String valueText = value.getCssText();\r
57                 if (valueText != null && valueText.length() > 0) {\r
58                         valueText = stripURLSyntax(valueText);\r
59                         ret = getImage(valueText, null);\r
60                 }\r
61                 if (ret == null) {\r
62                         ret = getInitialValue(propertyName, style);\r
63                 }\r
64                 return ret;\r
65         }\r
66 \r
67         /* (non-Javadoc)\r
68          * @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)\r
69          */\r
70         @Override\r
71         public Object calculateHTMLAttributeOverride(Element element,\r
72                         String htmltag, String propertyName, ICSSStyle style) {\r
73                 Image image = null;\r
74                 if (\r
75                                 element != null &&\r
76                                 element.getNodeName() != null &&\r
77                                 element.getNodeName().equalsIgnoreCase(IHTMLConstants.TAG_BODY)) {\r
78                         if (ICSSPropertyID.ATTR_BACKGROUND_IMAGE.equalsIgnoreCase(propertyName)) {\r
79                                 String attrValue = DOMUtil.getAttributeIgnoreCase(element, IHTMLConstants.ATTR_BACKGROUND);\r
80                                 if (attrValue != null && attrValue.trim().length() > 0) {\r
81                                         image = getImage(attrValue.trim(), element);\r
82                                 }\r
83                         }\r
84                 }\r
85                 return image;\r
86         }\r
87 \r
88         /* (non-Javadoc)\r
89          * @see org.eclipse.jst.pagedesigner.css2.property.CSSPropertyMeta#getKeywordValues()\r
90          */\r
91         @Override\r
92         protected String[] getKeywordValues() {\r
93                 return _keywords;\r
94         }\r
95 \r
96         private String stripURLSyntax(String input) {\r
97                 String output = null;\r
98                 if (input != null) {\r
99                         //strip "url(...)"\r
100                         int startPos = input.indexOf("url(") + 4; //$NON-NLS-1$\r
101                         if (startPos > -1 && startPos < input.length() - 1) {\r
102                                 int endPos = input.indexOf(')', startPos);\r
103                                 if (endPos > startPos) {\r
104                                         String insideURL = input.substring(startPos, endPos).trim();\r
105                                         //strip double-quotes\r
106                                         if (insideURL.startsWith("\"") && insideURL.endsWith("\"")) { //$NON-NLS-1$ //$NON-NLS-2$\r
107                                                 output = insideURL.substring(1, insideURL.length() - 1);\r
108                                         //strip single-quotes\r
109                                         } else if (insideURL.startsWith("'") && insideURL.endsWith("'")) { //$NON-NLS-1$ //$NON-NLS-2$\r
110                                                 output = insideURL.substring(1, insideURL.length() - 1);\r
111                                         } else {\r
112                                                 output = insideURL;\r
113                                         }\r
114                                 }\r
115                         }\r
116                 }\r
117                 return output != null ? output : input;\r
118         }\r
119 \r
120         /* Image instances returned from this method should not be disposed because\r
121          * they are cached in the plug-in's ImageRegistry and will be disposed of\r
122          * by the registry.\r
123          */\r
124         private Image getImage(String imagePath, Element element) {\r
125                 Image image = null;\r
126                 if (imagePath != null && imagePath.length() > 0) {\r
127                         ImageRegistry registry = PDPlugin.getDefault().getImageRegistry();\r
128                         image = registry.get(imagePath);\r
129                         if (image == null) {\r
130                                 try {\r
131                                         URL imageURL = new URL(imagePath);\r
132                                         ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(imageURL);\r
133                                         image = imageDescriptor.createImage();\r
134                                         if (image != null) {\r
135                                                 registry.put(imagePath, image);\r
136                                         }\r
137                                 } catch(MalformedURLException mue) {\r
138                                         //attempt to resolve as relative to document\r
139                                         if (element instanceof IDOMNode) {\r
140                                                 IDOMModel model = ((IDOMNode)element).getModel();\r
141                                                 if (model != null) {\r
142                                                         String baseLocation = model.getBaseLocation();\r
143                                                         if (baseLocation != null && baseLocation.length() > 0) {\r
144                                                                 IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();\r
145                                                                 if (wsRoot != null) {\r
146                                                                         IResource jspRes = wsRoot.findMember(baseLocation);\r
147                                                                         if (jspRes != null) {\r
148                                                                                 IContainer jspFolder = jspRes.getParent();\r
149                                                                                 if (jspFolder != null) {\r
150                                                                                         IResource imageRes = jspFolder.findMember(imagePath);\r
151                                                                                         if (imageRes != null) {\r
152                                                                                                 URI imageURI = imageRes.getLocationURI();\r
153                                                                                                 if (imageURI != null) {\r
154                                                                                                         try {\r
155                                                                                                                 URL imageURL = imageURI.toURL();\r
156                                                                                                                 ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(imageURL);\r
157                                                                                                                 image = imageDescriptor.createImage();\r
158                                                                                                                 if (image != null) {\r
159                                                                                                                         registry.put(imagePath, image);\r
160                                                                                                                 }\r
161                                                                                                         } catch(MalformedURLException mue2) {\r
162                                                                                                                 //ignore - what else can be done?\r
163                                                                                                         }\r
164                                                                                                 }\r
165                                                                                         }\r
166                                                                                 }\r
167                                                                         }\r
168                                                                 }\r
169                                                         }\r
170                                                 }\r
171                                         }\r
172                                 }\r
173                         } else if (image.isDisposed()) {\r
174                                 //shouldn't be able to get here from there, but...just in case\r
175                                 registry.remove(imagePath);\r
176                                 image = getImage(imagePath, element);\r
177                         }\r
178                 }\r
179                 return image;\r
180         }\r
181 \r
182 }\r