blob: f437087e903ae581109e315d04cf397656c361ef (
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
|
/**
* Copyright (c) 2013, 2016 Angelo ZERR.
* 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:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package org.eclipse.wst.json.core.internal.schema;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.json.schema.IJSONSchemaDocument;
import org.eclipse.json.schema.IJSONSchemaProcessor;
import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverPlugin;
import org.eclipse.wst.common.uriresolver.internal.util.URIHelper;
import org.eclipse.wst.json.core.JSONCorePlugin;
import org.eclipse.wst.json.core.document.IJSONDocument;
import org.eclipse.wst.json.core.document.IJSONModel;
import org.eclipse.wst.json.core.document.IJSONNode;
import org.eclipse.wst.json.core.document.IJSONPair;
import org.eclipse.wst.json.core.document.IJSONValue;
import org.eclipse.wst.json.core.internal.Logger;
import org.eclipse.wst.json.core.util.JSONUtil;
public class SchemaProcessorRegistryReader {
protected static final String EXTENSION_POINT_ID = "schemaProcessors"; //$NON-NLS-1$
protected static final String TAG_CONTRIBUTION = "schemaProcessor"; //$NON-NLS-1$
public static SchemaProcessorRegistryReader INSTANCE = null;
private IJSONSchemaProcessor defaultProcessor;
public static SchemaProcessorRegistryReader getInstance() {
if (INSTANCE == null) {
INSTANCE = new SchemaProcessorRegistryReader();
INSTANCE.readRegistry();
}
return INSTANCE;
}
public IJSONSchemaProcessor getDefaultProcessor() {
return defaultProcessor;
}
public IJSONSchemaDocument getSchemaDocument(IJSONNode node)
throws IOException {
return getSchemaDocument(node.getModel());
}
public IJSONSchemaDocument getSchemaDocument(IJSONModel model)
throws IOException {
IJSONSchemaProcessor processor = getDefaultProcessor();
if (processor == null) {
return null;
}
IJSONDocument document = model.getDocument();
IJSONNode jsonObject = document.getFirstChild();
if (jsonObject != null) {
IJSONNode child = jsonObject.getFirstChild();
while (child != null) {
if (child instanceof IJSONPair) {
IJSONPair pair = (IJSONPair) child;
String name = pair.getName();
IJSONValue valueNode = pair.getValue();
if (valueNode != null && "$schema".equals(name)) { //$NON-NLS-1$
String schema = JSONUtil.getString(valueNode);
try {
if (schema != null) {
schema = URIHelper.addImpliedFileProtocol(schema);
new URL(schema);
return processor.getSchema(schema);
}
} catch (MalformedURLException e) {
}
}
}
child = child.getNextSibling();
}
}
String base = model == null || model.getResolver() == null ?
null : model.getResolver().getFileBaseLocation();
/**
* We shouldn't assert a failure because the catalog does not require a
* base location to operate and it will be called from non-file-based
* scenarios.
*
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=206176
*/
// Assert.isNotNull(base, "Base location is expected to be non null."); //$NON-NLS-1$
if (base != null) {
base = URIHelper.addImpliedFileProtocol(base);
}
String schemaURL = resolve(base, null, null);
if (schemaURL != null) {
return processor.getSchema(schemaURL);
}
return null;
}
private String getFileMatch(String location) {
if (location == null) {
return null;
}
int index = location.lastIndexOf('/');
if (index == -1) {
index = location.lastIndexOf('\\');
}
if (index != -1) {
return location.substring(index, location.length());
}
return location;
}
private String resolve(String base, String publicId, String systemId) {
String result = systemId;
result = URIResolverPlugin.createResolver().resolve(base, publicId,
systemId);
return result;
}
/**
* read from plugin registry and parse it.
*/
protected void readRegistry() {
IExtensionRegistry pluginRegistry = Platform.getExtensionRegistry();
IExtensionPoint point = pluginRegistry
.getExtensionPoint(JSONCorePlugin.getDefault().getBundle()
.getSymbolicName(), EXTENSION_POINT_ID);
if (point != null) {
IConfigurationElement[] elements = point.getConfigurationElements();
for (int i = 0; i < elements.length; i++) {
readElement(elements[i]);
}
}
}
protected void readElement(IConfigurationElement element) {
if (TAG_CONTRIBUTION.equals(element.getName())) {
String id = element.getAttribute("id");
String name = element.getAttribute("name");
try {
IJSONSchemaProcessor schemaProcessor = (IJSONSchemaProcessor) element
.createExecutableExtension("class");
this.defaultProcessor = schemaProcessor;
} catch (CoreException e) {
Logger.logException(e);
}
}
}
}
|