Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd266f12b9aff3aeb0d51804cfed20c4bcc6e5a3 (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
/************************************************************************************************************************************************************
 * Copyright (c) 2009, 2020 Borland Software Corporation, CEA LIST, Artal
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/ 
 * 
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors: 
 *     Borland Software Corporation - initial API and implementation
 *    Aurélien Didier (ARTAL) - aurelien.didier51@gmail.com - Bug 569174
 *******************************************************************************/
package org.eclipse.papyrus.gmf.internal.xpand.util;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.m2m.internal.qvt.oml.common.MDAConstants;
import org.eclipse.m2m.internal.qvt.oml.compiler.BlackboxUnitResolver;
import org.eclipse.m2m.internal.qvt.oml.compiler.DelegatingUnitResolver;
import org.eclipse.m2m.internal.qvt.oml.compiler.ResolverUtils;
import org.eclipse.m2m.internal.qvt.oml.compiler.UnitContents;
import org.eclipse.m2m.internal.qvt.oml.compiler.UnitProxy;
import org.eclipse.m2m.internal.qvt.oml.compiler.UnitResolver;


public class BundleUnitResolver extends DelegatingUnitResolver {

	private final class BundleUnit extends UnitProxy {

		private final URL url;

		private BundleUnit(String namespace, String unitName, URL unitURL) {
			super(namespace, unitName, createUnitURI(unitURL));
			this.url = unitURL;
		}
		
		@Override
		public int getContentType() {
			return UnitProxy.TYPE_CST_STREAM;
		}

		@Override
		public UnitContents getContents() throws IOException {
			return new UnitContents.CSTContents() {				
				public Reader getContents() throws IOException {
					URLConnection connection = url.openConnection();
					
					String charset = connection.getContentEncoding();
					if(charset == null) {
						charset = "UTF-8"; //$NON-NLS-1$
					}

					return new InputStreamReader(connection.getInputStream(), charset);
				}
			};
		}

		@Override
		public UnitResolver getResolver() {
			return BundleUnitResolver.this;
		}
		
	}
	

	private List<URL> fBaseURLs;	
			
	public BundleUnitResolver(List<URL> baseURL) {
		if(baseURL == null || baseURL.contains(null)) {
			throw new IllegalArgumentException();
		}
		
		fBaseURLs = new ArrayList<URL>(baseURL);
	}
		
	@Override
	protected UnitProxy doResolveUnit(String qualifiedName) {
		for (URL baseURL : fBaseURLs) {
			UnitProxy unit = doResolveUnit(baseURL, qualifiedName);
			if(unit != null) {
				return unit;
			}
		}
		
		return null;
	}
	
	private UnitProxy doResolveUnit(URL baseURL, String qualifiedName) {
		String pathStr = qualifiedName.replace('.', '/');
		IPath path = new Path(pathStr).addFileExtension(MDAConstants.QVTO_FILE_EXTENSION);
		URL url;
		try {
			url = new URL(baseURL, path.toString());
		} catch (MalformedURLException e1) {
			return null;
		}
		
		InputStream is = null;
		try {
			is = url.openStream();

			String name = path.lastSegment();
			String namespace = null;
			if(path.segmentCount() > 1) {
				IPath nameSpacePath = path.removeLastSegments(1);
				namespace = ResolverUtils.getNamespace(nameSpacePath);
			}

			return new BundleUnit(namespace, name, url);
			
		} catch (IOException e) {
			return null;
		} finally {
			if(is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// do nothing
				}
			}
		}
	}
	
	private static URI createUnitURI(URL url) {
		// TODO - we might not necessarily be passed a platform URL
		// to do adjustments to produce a valid URI
		return URI.createURI(url.toString());
	}

	public static UnitResolver createResolver(List<URL> bundleBaseURLs, boolean includeBlackboxUnits) {		
		BundleUnitResolver bundleUnitResolver = new BundleUnitResolver(bundleBaseURLs);		
		if(includeBlackboxUnits) {
			bundleUnitResolver.setParent(BlackboxUnitResolver.DEFAULT);
		}
		
		return bundleUnitResolver;
	}
}

Back to the top