/
SchemaDescriptionImpl.java
242 lines (201 loc) · 6.16 KB
/
SchemaDescriptionImpl.java
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (c) 2010-2018 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.prism.impl.schema;
import com.evolveum.midpoint.prism.AbstractFreezable;
import com.evolveum.midpoint.prism.schema.PrismSchema;
import com.evolveum.midpoint.prism.schema.SchemaDescription;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.DebugUtil;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import java.io.InputStream;
/**
* Schema (prism or non-prism) with additional information.
*
* TODO Make this class "initializable at once" i.e. that it would not need to be in semi-finished state e.g. during parsing.
*/
public final class SchemaDescriptionImpl extends AbstractFreezable implements SchemaDescription {
private final String path;
private final String sourceDescription;
private String usualPrefix;
private String namespace;
private SchemaSource source;
private InputStreamable streamable;
private Node node;
private boolean isPrismSchema = false;
private boolean isDefault = false;
private boolean isDeclaredByDefault = false;
private PrismSchema schema;
private Package compileTimeClassesPackage;
private String defaultPrefix;
SchemaDescriptionImpl(String sourceDescription, String path) {
this.sourceDescription = sourceDescription;
this.path = path;
}
@Override
public String getPath() {
return path;
}
@Override
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
checkMutable();
this.namespace = namespace;
}
void setStreamable(InputStreamable streamable) {
checkMutable();
this.streamable = streamable;
}
public void setNode(Node node) {
checkMutable();
this.node = node;
this.source = SchemaSource.from(element(node), streamable);
}
@Override
public String getUsualPrefix() {
return usualPrefix;
}
void setUsualPrefix(String usualPrefix) {
checkMutable();
this.usualPrefix = usualPrefix;
}
@Override
public String getSourceDescription() {
return sourceDescription;
}
@Override
public boolean isPrismSchema() {
return isPrismSchema;
}
@SuppressWarnings("SameParameterValue")
void setPrismSchema(boolean value) {
checkMutable();
this.isPrismSchema = value;
}
@Override
public boolean isDefault() {
return isDefault;
}
public void setDefault(boolean isDefault) {
checkMutable();
this.isDefault = isDefault;
}
@Override
public boolean isDeclaredByDefault() {
return isDeclaredByDefault;
}
void setDeclaredByDefault(boolean isDeclaredByDefault) {
checkMutable();
this.isDeclaredByDefault = isDeclaredByDefault;
}
@Override
public PrismSchema getSchema() {
return schema;
}
public void setSchema(PrismSchema schema) {
checkMutable();
this.schema = schema;
}
@Override
public Package getCompileTimeClassesPackage() {
return compileTimeClassesPackage;
}
void setCompileTimeClassesPackage(Package compileTimeClassesPackage) {
checkMutable();
this.compileTimeClassesPackage = compileTimeClassesPackage;
}
@Override
public boolean canInputStream() {
return streamable != null;
}
@Override
public InputStream openInputStream() {
if (!canInputStream()) {
throw new IllegalStateException("Schema "+sourceDescription+" cannot provide input stream");
}
return streamable.openInputStream();
}
/**
* Thread-unsafe if no stream is available, but method is called only
* during SchemaRegistryImpl.initialize, which is invoked from single thread.
*/
Source transformSource() {
Source source;
if (canInputStream()) {
InputStream inputStream = openInputStream();
// Return stream source as a first option. It is less efficient,
// but it provides information about line numbers
source = new StreamSource(inputStream);
} else {
source = new DOMSource(node);
}
source.setSystemId(path);
return source;
}
/**
* Thread-unsafe, but used only during construction of SchemaDescriptionImpl
* in single threaded context
*
*/
Element getDomElement() {
return source.element();
}
private static Element element(Node node) {
if (node instanceof Element) {
return (Element)node;
} else {
return DOMUtil.getFirstChildElement(node);
}
}
@FunctionalInterface
interface InputStreamable {
InputStream openInputStream();
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append(path);
if (schema != null) {
sb.append(" ");
sb.append(schema.toString());
}
return sb.toString();
}
@Override
protected void performFreeze() {
if (schema != null) {
schema.freeze();
}
}
@Override public String toString() {
return "SchemaDescriptionImpl{" +
"sourceDescription='" + sourceDescription + '\'' +
", usualPrefix='" + usualPrefix + '\'' +
", namespace='" + namespace + '\'' +
", schema=" + schema +
'}';
}
public void setDefaultPrefix(String prefix) {
this.defaultPrefix = prefix;
}
public String getDefaultPrefix() {
return defaultPrefix;
}
public InputSource saxInputSource() {
return source.saxInputSource();
}
public SchemaSource schemaSource() {
return source;
}
}