Skip to main content
summaryrefslogtreecommitdiffstats
blob: c5c5bcbc745888ed09caf82ebe97cf43c1871302 (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) 2010, 2013 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.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.eclipse.jpt.common.core.internal.utility.ValidationMessageTools;
import org.eclipse.jpt.common.utility.collection.Bag;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.collection.HashBag;
import org.eclipse.jpt.common.utility.internal.collection.ListTools;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.jaxb.core.JaxbPreferences;
import org.eclipse.jpt.jaxb.core.JaxbProject;
import org.eclipse.jpt.jaxb.core.SchemaEntry;
import org.eclipse.jpt.jaxb.core.SchemaLibrary;
import org.eclipse.jpt.jaxb.core.validation.JptJaxbCoreValidationMessages;
import org.eclipse.jpt.jaxb.core.xsd.XsdSchema;
import org.eclipse.jpt.jaxb.core.xsd.XsdUtil;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;

public class SchemaLibraryImpl
		implements SchemaLibrary {
	
	private JaxbProject project;
	
	private final List<SchemaEntryImpl> schemaEntries;
	
	private final Map<String, SchemaEntryImpl> impliedEntries;
	
	
	SchemaLibraryImpl(JaxbProject project) {
		this.project = project;
		this.schemaEntries = new Vector<SchemaEntryImpl>();
		this.impliedEntries = new Hashtable<String, SchemaEntryImpl>();
		readProjectPreferences();
	}
	
	
	public List<SchemaEntry> getSchemaEntries() {
		return Collections.<SchemaEntry>unmodifiableList(this.schemaEntries);
	}
	
	public List<String> getSchemaLocations() {
		return Collections.unmodifiableList(
				ListTools.list(
						IterableTools.transform(this.schemaEntries, SchemaEntry.LOCATION_TRANSFORMER)));
	}
	
	public void setSchemaLocations(List<String> schemaLocations) {
		for (SchemaEntryImpl entry : this.schemaEntries) {
			entry.dispose();
		}
		this.schemaEntries.clear();
		JaxbPreferences.setSchemaLocations(this.project.getProject(), schemaLocations);
		readProjectPreferences();
	}
	
	public XsdSchema getSchema(String namespace) {
		for (SchemaEntry entry : this.schemaEntries) {
			if (ObjectTools.equals(namespace, entry.getNamespace())) {
				return entry.getXsdSchema(namespace);
			}
		}
		
		if (! this.impliedEntries.containsKey(namespace)) {
			if (! ObjectTools.equals(XsdUtil.getResolvedUri(namespace), namespace)) {
				// the namespace itself resolves to a location.  add it as an implied entry
				this.impliedEntries.put(namespace, new SchemaEntryImpl(namespace));
			}
			else {
				return null;
			}
		}
		
		return this.impliedEntries.get(namespace).getXsdSchema(namespace);
	}
	
	protected void readProjectPreferences() {
		List<String> schemas = JaxbPreferences.getSchemaLocations(this.project.getProject());
		for (String schemaLocation : schemas) {
			SchemaEntryImpl entry = new SchemaEntryImpl(schemaLocation);
			this.schemaEntries.add(entry);
		}
	}
	
	public void refreshSchema(String namespace) {
		for (SchemaEntryImpl entry : this.schemaEntries) {
			if (ObjectTools.equals(namespace, entry.getNamespace())) {
				entry.refresh();
			}
		}
	}
	
	public void refreshAllSchemas() {
		for (SchemaEntryImpl entry : this.schemaEntries) {
			entry.refresh();
		}
	}
	
	void dispose() {
		for (SchemaEntryImpl entry : this.schemaEntries) {
			entry.dispose();
		}
		for (SchemaEntryImpl entry : this.impliedEntries.values()) {
			entry.dispose();
		}
		
		this.schemaEntries.clear();
		this.impliedEntries.clear();
	}
	
	public void validate(List<IMessage> messages) {
		Bag<String> namespaces = new HashBag<String>();
		
		for (SchemaEntry entry : this.schemaEntries) {
			namespaces.add(entry.getNamespace());
			
			if (! entry.isLoaded()) {
				messages.add(
						ValidationMessageTools.buildValidationMessage(
								this.project.getProject(),
								JptJaxbCoreValidationMessages.PROJECT__UNRESOLVED_SCHEMA,
								entry.getLocation()
							));
			}
		}
		
		for (Iterator<String> stream = namespaces.uniqueIterator(); stream.hasNext(); ) {
			String namespace = stream.next();
			if (namespaces.count(namespace) > 1) {
				messages.add(
						ValidationMessageTools.buildValidationMessage(
								this.project.getProject(),
								JptJaxbCoreValidationMessages.PROJECT__DUPLICATE_NAMESPACE,
								namespace
								));
			}
		}
	}
}

Back to the top