Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 09d2f5d1511d51fd5cbfdacaa83e9f0f27f2dbcf (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 *
 * 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:
 *		
 *		CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.service.shape;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.util.DOMUtilities;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.gmf.runtime.common.core.service.AbstractProvider;
import org.eclipse.gmf.runtime.common.core.service.IOperation;
import org.eclipse.gmf.runtime.draw2d.ui.render.RenderedImage;
import org.eclipse.gmf.runtime.draw2d.ui.render.factory.RenderedImageFactory;
import org.eclipse.papyrus.infra.gmfdiag.common.handler.IRefreshHandlerPart;
import org.eclipse.papyrus.infra.gmfdiag.common.handler.RefreshHandler;
import org.eclipse.ui.IEditorPart;
import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGDocument;

/**
 * Abstract implementation of the shape provider interface.
 */
public abstract class AbstractShapeProvider extends AbstractProvider implements IShapeProvider, IRefreshHandlerPart {

	/** field for name */
	protected static final String NAME = "name";

	/** field for identifier */
	protected static final String ID = "id";

	/** field for description */
	protected static final String DESCRIPTION = "description";

	/** field for Activator ID */
	protected String bundleId;

	/** name for the factory */
	protected String name;

	/** identifier for the factory */
	protected String id;

	/** description of the factory */
	protected String description;

	/**
	 * Maps of URIs for SVG files referred to with relative paths
	 */
	private WeakHashMap<Resource, Map<String, String>> relativePaths;

	/** Cache for the loaded SVG document */
	private Map<String, SVGDocument> cache;


	/**
	 * Initializes this provider
	 */
	public AbstractShapeProvider() {
		RefreshHandler.register(this);
	}

	/**
	 * Returns the bundle identifier for this provider
	 * 
	 * @return the bundle identifier for this provider
	 */
	public String getBundleId() {
		return bundleId;
	}


	/**
	 * Returns the name of this provider
	 * 
	 * @return the name of this provider
	 */
	public String getName() {
		return name;
	}


	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getId() {
		return id;
	}


	/**
	 * Returns the description of this provider
	 * 
	 * @return the description of this provider
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @{inheritDoc
	 */
	@Override
	public boolean provides(IOperation operation) {
		return (operation instanceof GetAllShapeProvidersOperation || operation instanceof GetShapeProviderByIdentifierOperation);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setConfiguration(IConfigurationElement element) {
		name = element.getAttribute(NAME);
		id = element.getAttribute(ID);
		description = element.getAttribute(DESCRIPTION);
		bundleId = element.getContributor().getName();
	}

	/**
	 * Loads a SVG document from the given location.
	 * This method uses a cache so that any given document is only loaded once.
	 * 
	 * @param view
	 *            The view object to retrieve a svg document for
	 * @param location
	 *            The location to load the document from
	 * @return the Document SVG from its location, can return null if this is not a svg
	 */
	protected synchronized SVGDocument getSVGDocument(EObject view, String location) {
		if (relativePaths == null) {
			relativePaths = new WeakHashMap<Resource, Map<String, String>>();
		}
		String canonical = getCanonicalURI(view, location);
		return getSVGDocument(canonical);
	}

	/**
	 * Loads a SVG document from the given location.
	 * This method uses a cache so that any given document is only loaded once.
	 * 
	 * @param location
	 *            The location to load the document from
	 * @return the Document SVG from its location, can return null if this is not a svg
	 */
	protected synchronized SVGDocument getSVGDocument(String location) {
		if (cache == null) {
			cache = new HashMap<String, SVGDocument>();
		}
		if (cache.containsKey(location))
			return cache.get(location);
		SVGDocument doc = doGetSVGDocument(location);
		cache.put(location, doc);
		return doc;
	}

	/**
	 * Loads a SVG document from the given location
	 * 
	 * @param location
	 *            The location to load the document from
	 * @return the Document SVG from its location, can return null if this is not a svg
	 */
	private SVGDocument doGetSVGDocument(String location) {
		int extensionIndex = location.lastIndexOf('.');
		if(extensionIndex == 0) {
			return null;
		}
		String fileExtension = location.substring(extensionIndex);
		if(!fileExtension.equalsIgnoreCase(".svg")) {
			return null;
		}

		String parser = org.apache.batik.util.XMLResourceDescriptor.getXMLParserClassName();

		try {
			SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);

			Document doc = f.createDocument(location);
			SVGDocument svgDocument = (SVGDocument)doc;
			return svgDocument;

		} catch (Exception e) {
			org.eclipse.papyrus.infra.core.Activator.log.error(e);
		}
		return null;
	}

	/**
	 * Translates the given uri as a string to a canonical Eclipse URI
	 * The uri may be relative to the currently edited EMF resource
	 * 
	 * @param model
	 *            The model element used to retrieve the EMF resource that is currently edited
	 * @param uri
	 *            The potentially relative URI of a svg file
	 * @return The canonical URI of the resource
	 */
	private String getCanonicalURI(EObject model, String uri) {
		if (uri.startsWith("platform:/")) {
			return uri;
		}

		Map<String, String> resMap = relativePaths.get(model.eResource());
		if (resMap == null) {
			resMap = new HashMap<String, String>();
			relativePaths.put(model.eResource(), resMap);
		}
		String canonical = resMap.get(uri);
		if (canonical != null) {
			return canonical;
		}

		URI resURI = model.eResource().getURI();
		if (!resURI.isPlatform()) {
			return null;
		}
		StringBuilder builder = new StringBuilder("platform:/");
		String[] segments = resURI.segments();
		for (int i = 0; i < segments.length - 1; i++) {
			builder.append(segments[i]);
			builder.append("/");
		}
		builder.append(uri);
		canonical = builder.toString();
		resMap.put(uri, canonical);
		return canonical;
	}

	protected RenderedImage renderSVGDocument(EObject view, SVGDocument document) throws IOException {
		postProcess(view, document);
		String svgAsText = toString(document);
		byte[] buffer = svgAsText.getBytes();

		return RenderedImageFactory.getInstance(buffer);
	}

	protected void postProcess(EObject view, SVGDocument document) {
		SVGPostProcessor.instance.postProcess(view, document);
	}

	protected String toString(SVGDocument domDocument) throws IOException {
		StringWriter writer = new StringWriter();

		DOMUtilities.writeDocument(domDocument, writer);

		return writer.toString();
	}

	/**
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.handler.IRefreshHandlerPart#refresh(org.eclipse.ui.IEditorPart)
	 */
	public synchronized void refresh(IEditorPart editorPart) {
		// Clears the cache of loaded SVG documents
		// This will force their reloading
		if (cache != null) {
			cache.clear();
		}
		if (relativePaths != null) {
			relativePaths.clear();
		}
	}
}

Back to the top