Skip to main content
summaryrefslogtreecommitdiffstats
blob: cc9f954415243c375f001fa62b12b48811d3d45c (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
/*******************************************************************************
 *  Copyright (c) 2010, 2011  Oracle. 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: 
 *  	Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.jaxb.core.internal;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jpt.jaxb.core.JaxbProject;
import org.eclipse.jpt.jaxb.core.JptJaxbCorePlugin;
import org.eclipse.jpt.jaxb.core.SchemaLibrary;
import org.eclipse.jpt.jaxb.core.xsd.XsdUtil;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDResourceImpl;

public class SchemaLibraryImpl
		implements SchemaLibrary {
	
	private JaxbProject project;
	
	private final Map<String, String> schemaLocations;
	
	private final Map<String, XSDResourceImpl> schemaResources;
	
	private Adapter schemaResourceAdapter = new SchemaResourceAdapter();
	
	
	SchemaLibraryImpl(JaxbProject project) {
		this.project = project;
		this.schemaLocations = new HashMap<String, String>();
		this.schemaResources = new HashMap<String, XSDResourceImpl>();
		readProjectPreferences();
	}
	
	
	public XSDSchema getSchema(String namespace) {
		String resolvedUri = getResolvedUri(namespace);
		if (resolvedUri == null) {
			return null;
		}
		
		XSDResourceImpl schemaResource = this.schemaResources.get(namespace);
		XSDSchema schema = (schemaResource == null) ? null : schemaResource.getSchema();
		if (schemaResource != null) {
			if (schema != null && schemaResource.getURI().toString().equals(resolvedUri) && schemaResource.isLoaded()) {
				return schema;
			}
			else {
				removeSchemaResource(namespace, schemaResource);
			}
		}
		
		return addSchema(namespace, resolvedUri);
	}
	
	protected String getResolvedUri(String namespace) {
		String location = this.schemaLocations.get(namespace);
		return XsdUtil.getResolvedUri(namespace, location);
	}
	
	protected XSDSchema addSchema(String namespace, String resolvedUri) {
		XSDSchema schema = XsdUtil.buildXSDModel(resolvedUri);
		XSDResourceImpl schemaResource = (XSDResourceImpl) schema.eResource();
		if (schemaResource != null) {
			schemaResource.eAdapters().add(this.schemaResourceAdapter);
			this.schemaResources.put(namespace, schemaResource);
			return schema;
		}
		return null;
	}
	
	protected void removeSchemaResource(XSDResourceImpl schemaResource) {
		for (String namespace : this.schemaResources.keySet()) {
			if (schemaResource.equals(this.schemaResources.get(namespace))) {
				removeSchemaResource(namespace, schemaResource);
			}
		}
	}
	
	protected void removeSchemaResource(String namespace, XSDResourceImpl schemaResource) {
		schemaResource.eAdapters().remove(this.schemaResourceAdapter);
		this.schemaResources.remove(namespace);
	}
	
	protected void readProjectPreferences() {
		Map<String, String> schemaMap = JptJaxbCorePlugin.getSchemaLocationMap(this.project.getProject());
		this.schemaLocations.putAll(schemaMap);
	}
	
	public Map<String, String> getSchemaLocations() {
		return Collections.unmodifiableMap(this.schemaLocations);
	}
	
	public void setSchemaLocations(Map<String, String> schemaLocations) {
		this.schemaLocations.clear();
		JptJaxbCorePlugin.setSchemaLocationMap(this.project.getProject(), schemaLocations);
		readProjectPreferences();
	}
	
	
	private class SchemaResourceAdapter
			extends AdapterImpl {
		
		@Override
		public void notifyChanged(Notification msg) {
			if (msg.getFeatureID(Resource.class) == Resource.RESOURCE__IS_LOADED
					&& msg.getNewBooleanValue() == false) {
				removeSchemaResource((XSDResourceImpl) msg.getNotifier());
			}
		}
	}
}

Back to the top