Skip to main content
summaryrefslogtreecommitdiffstats
blob: 505d4234b76d8fd9d1cf490ba35de46a7d01c20b (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
/*****************************************************************************
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.properties.catalog;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.URIHandler;
import org.eclipse.papyrus.infra.properties.internal.InfraPropertiesPlugin;
import org.eclipse.papyrus.infra.properties.spi.IPropertiesResolver;

/**
 * A URI Handler for URIs with the ppe:/ scheme
 *
 * @author Camille Letavernier
 */
public class PropertiesURIHandler implements URIHandler {

	/**
	 * The segment for environment models
	 */
	public static final String ENVIRONMENT_SEGMENT = "environment"; //$NON-NLS-1$

	/**
	 * The segment for context models
	 */
	public static final String CONTEXT_SEGMENT = "context"; //$NON-NLS-1$

	/**
	 * The handled URI scheme (ppe)
	 */
	public static final String PROPERTIES_SCHEME = "ppe"; //$NON-NLS-1$

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean canHandle(URI uri) {
		return uri != null && PROPERTIES_SCHEME.equals(uri.scheme());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException {
		URI convertedURI = getConvertedURI(uri);
		URIHandler handler = getDelegateHandler(convertedURI);
		return handler.createInputStream(convertedURI, options);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public OutputStream createOutputStream(URI uri, Map<?, ?> options) throws IOException {
		URI convertedURI = getConvertedURI(uri);
		URIHandler handler = getDelegateHandler(convertedURI);
		return handler.createOutputStream(convertedURI, options);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void delete(URI uri, Map<?, ?> options) throws IOException {
		URI convertedURI = getConvertedURI(uri);
		URIHandler handler = getDelegateHandler(convertedURI);
		handler.delete(convertedURI, options);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Map<String, ?> contentDescription(URI uri, Map<?, ?> options) throws IOException {
		URI convertedURI = getConvertedURI(uri);
		URIHandler handler = getDelegateHandler(convertedURI);
		return handler.contentDescription(convertedURI, options);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean exists(URI uri, Map<?, ?> options) {
		URI convertedURI = getConvertedURI(uri);
		URIHandler handler = getDelegateHandler(convertedURI);
		return handler.exists(convertedURI, options);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Map<String, ?> getAttributes(URI uri, Map<?, ?> options) {
		URI convertedURI = getConvertedURI(uri);
		URIHandler handler = getDelegateHandler(convertedURI);
		return handler.getAttributes(convertedURI, options);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setAttributes(URI uri, Map<String, ?> attributes, Map<?, ?> options) throws IOException {
		URI convertedURI = getConvertedURI(uri);
		URIHandler handler = getDelegateHandler(convertedURI);
		handler.setAttributes(convertedURI, attributes, options);
	}

	/**
	 * Returns the URIHandler that can handle the given URI
	 *
	 * @param convertedURI
	 *            The URI obtained after converting the ppe:/ URI to a standard URI
	 * @return
	 * 		The URIHandler corresponding to the converted URI
	 */
	protected URIHandler getDelegateHandler(URI convertedURI) {
		if (convertedURI == null) {
			return null;
		}

		for (URIHandler handler : URIHandler.DEFAULT_HANDLERS) {
			if (handler.canHandle(convertedURI)) {
				return handler;
			}
		}
		return null;
	}

	/**
	 * Converts the ppe:/ URI to a standard URI
	 *
	 * @param sourceURI
	 *            The ppe:/ URI to convert
	 * @return
	 * 		The standard URI
	 */
	public URI getConvertedURI(URI sourceURI) {
		if (sourceURI == null) {
			throw new IllegalArgumentException("sourceURI shall not be null"); //$NON-NLS-1$
		}
		String firstSegment = sourceURI.segment(0);
		URI targetURI = URI.createURI(""); //$NON-NLS-1$
		if (firstSegment.equals(ENVIRONMENT_SEGMENT)) {
			for (int i = 1; i < sourceURI.segmentsList().size(); i++) {
				String segment = sourceURI.segmentsList().get(i);
				targetURI = targetURI.appendSegment(segment);
			}
		} else if (firstSegment.equals(CONTEXT_SEGMENT)) {
			for (int i = 1; i < sourceURI.segmentsList().size(); i++) {
				String segment = sourceURI.segmentsList().get(i);
				targetURI = targetURI.appendSegment(segment);
			}
		} else {
			throw new IllegalArgumentException(sourceURI + " is not a valid URI"); //$NON-NLS-1$
		}

		URI result = targetURI.resolve(URI.createURI("platform:/plugin/")); //$NON-NLS-1$

		if (!exists(result)) {
			result = targetURI.resolve(URI.createURI("platform:/resource/")); //$NON-NLS-1$

			if (!exists(result)) {
				result = null;

				// Reach out to the resolver services
				for (IPropertiesResolver resolver : InfraPropertiesPlugin.getInstance().getPropertyResolvers()) {
					result = resolver.resolve(targetURI);
					if (!exists(result)) {
						result = null;
					} else {
						break;
					}
				}
			}
		}

		return result;
	}

	private boolean exists(URI uri) {
		for (URIHandler handler : DEFAULT_HANDLERS) {
			if (handler.canHandle(uri)) {
				return handler.exists(uri, Collections.EMPTY_MAP);
			}
		}
		return false;
	}

}

Back to the top