1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.schema.cmp;
|
11
|
|
|
12
|
|
import java.util.ArrayList;
|
13
|
|
import java.util.Collection;
|
14
|
|
import java.util.Collections;
|
15
|
|
import java.util.HashSet;
|
16
|
|
import java.util.Iterator;
|
17
|
|
import java.util.Set;
|
18
|
|
|
19
|
|
import org.jbind.xml.base.BlockType;
|
20
|
|
import org.jbind.xml.base.FinalType;
|
21
|
|
import org.jbind.xml.base.IHasLocation;
|
22
|
|
import org.jbind.xml.base.ISymbolspace;
|
23
|
|
import org.jbind.xml.base.ISymbolspaces;
|
24
|
|
import org.jbind.xml.base.WhiteSpaceProcessing;
|
25
|
|
import org.jbind.xml.core.bridge.IDataImpl;
|
26
|
|
import org.jbind.xml.core.cmp.IAttributesModel;
|
27
|
|
import org.jbind.xml.core.cmp.IContentModel;
|
28
|
|
import org.jbind.xml.core.cmp.ISourceInfo;
|
29
|
|
import org.jbind.xml.core.constraint.ICheckContext;
|
30
|
|
import org.jbind.xml.core.constraint.IConstraints;
|
31
|
|
import org.jbind.xml.core.content.IAttrDesc;
|
32
|
|
import org.jbind.xml.core.content.IElemDesc;
|
33
|
|
import org.jbind.xml.core.data.IAnyTypeData;
|
34
|
|
import org.jbind.xml.core.data.IDataCreator;
|
35
|
|
import org.jbind.xml.core.type.IAnyType;
|
36
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
37
|
|
import org.jbind.xml.msg.XmlException;
|
38
|
|
import org.jbind.xml.msg.XmlMessages;
|
39
|
|
import org.jbind.xml.schema.constraint.LocalCheckContext;
|
40
|
|
|
41
|
|
/**
|
42
|
|
* Represents a type.
|
43
|
|
*/
|
44
|
|
public abstract class AbstractType extends Component implements IAnyType {
|
45
|
|
|
46
|
|
/*
|
47
|
|
* A type is ready to create data only <b>after</b> it has been validated, i.e.
|
48
|
|
* its {@link #validate validate} method has been called.
|
49
|
|
*/
|
50
|
|
|
51
|
|
private IAnyType myBaseType = null;
|
52
|
|
private String myRole = null;
|
53
|
|
private Set myFinalTypes = null;
|
54
|
|
private Set myBlockTypes = null;
|
55
|
|
private boolean myIsAbstract;
|
56
|
|
|
57
|
|
private WhiteSpaceProcessing myWhiteSpaceProcessing = WhiteSpaceProcessing.PRESERVE;
|
58
|
|
|
59
|
|
private IContentModel myContentModel = null;
|
60
|
|
|
61
|
|
private IAttributesModel myAttributesModel = null;
|
62
|
|
|
63
|
|
private IConstraints myConstraints = null;
|
64
|
|
|
65
|
|
private IDataCreator myDataCreator = null;
|
66
|
|
|
67
|
|
private Collection myDirectSubtypes = null;
|
68
|
|
|
69
|
876
|
public AbstractType(ISourceInfo aSourceInfo, String aNamespace, String aName, String aRole, Set aFinalTypes, Set aBlockTypes, boolean anIsAbstract) {
|
70
|
876
|
super(aSourceInfo, aNamespace, aName);
|
71
|
876
|
myRole = aRole;
|
72
|
876
|
myFinalTypes = aFinalTypes;
|
73
|
876
|
myBlockTypes = aBlockTypes;
|
74
|
876
|
myIsAbstract = anIsAbstract;
|
75
|
|
}
|
76
|
|
|
77
|
1054
|
public final IDataCreator getDataCreator() {
|
78
|
1054
|
return myDataCreator;
|
79
|
|
}
|
80
|
828
|
public final void setDataCreator(IDataCreator aCreator) {
|
81
|
828
|
myDataCreator = aCreator;
|
82
|
|
}
|
83
|
|
|
84
|
466
|
public String getRoleName() {
|
85
|
466
|
return myRole;
|
86
|
|
}
|
87
|
|
|
88
|
873
|
public void setBaseType(IAnyType aType) {
|
89
|
|
assert null == myBaseType;
|
90
|
873
|
myBaseType = aType;
|
91
|
873
|
myBaseType.addDirectSubtype(this);
|
92
|
|
}
|
93
|
|
|
94
|
53324
|
public IAnyType getBaseType() {
|
95
|
53324
|
return myBaseType;
|
96
|
|
}
|
97
|
|
|
98
|
873
|
public void addDirectSubtype(IAnyType aType) {
|
99
|
873
|
if (null == myDirectSubtypes) {
|
100
|
188
|
myDirectSubtypes = new ArrayList();
|
101
|
|
}
|
102
|
|
assert aType.getBaseType() == this;
|
103
|
873
|
myDirectSubtypes.add(aType);
|
104
|
|
}
|
105
|
|
|
106
|
0
|
public Collection getDirectSubtypes() {
|
107
|
0
|
return (null == myDirectSubtypes) ? Collections.EMPTY_LIST : myDirectSubtypes;
|
108
|
|
}
|
109
|
|
|
110
|
0
|
public Collection getSubtypes() {
|
111
|
0
|
if (null == myDirectSubtypes) {
|
112
|
0
|
return Collections.EMPTY_LIST;
|
113
|
|
}
|
114
|
0
|
Collection subtypes = new ArrayList();
|
115
|
0
|
Collection directSubtypes = myDirectSubtypes;
|
116
|
0
|
while (directSubtypes.size() > 0) {
|
117
|
0
|
Collection tmp = new ArrayList();
|
118
|
0
|
for (Iterator i = directSubtypes.iterator(); i.hasNext(); ) {
|
119
|
0
|
IAnyType t = (IAnyType)i.next();
|
120
|
0
|
subtypes.add(t);
|
121
|
0
|
tmp.addAll(t.getDirectSubtypes());
|
122
|
|
}
|
123
|
0
|
directSubtypes = tmp;
|
124
|
|
}
|
125
|
0
|
return subtypes;
|
126
|
|
}
|
127
|
|
|
128
|
4714
|
public boolean isBaseType(IAnyType aType) {
|
129
|
4714
|
boolean res = false;
|
130
|
4714
|
for (IAnyType t = (IAnyType)aType; !res && (t != null);
|
131
|
|
t = t.getBaseType()) {
|
132
|
17923
|
res = this == t;
|
133
|
|
}
|
134
|
4714
|
return res;
|
135
|
|
}
|
136
|
|
|
137
|
6649
|
public boolean isAbstract() {
|
138
|
6649
|
return myIsAbstract;
|
139
|
|
}
|
140
|
|
|
141
|
2278
|
public ISymbolspace getSymbolspace() {
|
142
|
2278
|
return ISymbolspaces.TYPE;
|
143
|
|
}
|
144
|
|
|
145
|
800
|
public boolean isActive(FinalType aFinalType) {
|
146
|
800
|
return myFinalTypes.contains(aFinalType);
|
147
|
|
}
|
148
|
|
|
149
|
226
|
public boolean isActive(BlockType aBlockType) {
|
150
|
226
|
return myBlockTypes.contains(aBlockType);
|
151
|
|
}
|
152
|
|
|
153
|
10
|
protected void addFinalType(FinalType aFinalType) {
|
154
|
|
// The final types set may not support the add method. Therefore a new
|
155
|
|
// hash set is created.
|
156
|
10
|
myFinalTypes = new HashSet(myFinalTypes);
|
157
|
10
|
myFinalTypes.add(aFinalType);
|
158
|
|
}
|
159
|
|
|
160
|
4570
|
public final IAnyTypeData createData(IDataImpl anImpl) throws XmlException {
|
161
|
4570
|
IAnyTypeData res = createUncheckedData(anImpl);
|
162
|
4570
|
res.acceptImpl_();
|
163
|
4529
|
IConstraintViolations violations = XmlMessages.constraintViolations();
|
164
|
4529
|
LocalCheckContext context = new LocalCheckContext(res, violations);
|
165
|
4529
|
checkConstraints(context);
|
166
|
4529
|
if (!violations.isEmpty()) {
|
167
|
23
|
throw new XmlException(violations);
|
168
|
|
}
|
169
|
4506
|
return res;
|
170
|
|
}
|
171
|
|
|
172
|
4622
|
public final IAnyTypeData createUncheckedData(IDataImpl anImpl) throws XmlException {
|
173
|
4622
|
IAnyTypeData res = createEmptyData(anImpl);
|
174
|
4622
|
res.setImpl_(anImpl);
|
175
|
4622
|
return res;
|
176
|
|
}
|
177
|
|
|
178
|
4637
|
public final IAnyTypeData createEmptyData(IDataImpl anImpl) throws XmlException {
|
179
|
4637
|
if (isAbstract()) {
|
180
|
0
|
throw new XmlException(XmlMessages.triedToInstantiateAbstractType(getGlobalRef(), anImpl));
|
181
|
|
}
|
182
|
4637
|
return doCreateEmptyData(anImpl);
|
183
|
|
}
|
184
|
|
|
185
|
4530
|
protected IAnyTypeData doCreateEmptyData(IDataImpl anImpl) throws XmlException {
|
186
|
|
// This method is overloaded in the UnionType and RestrictedUnionType class
|
187
|
4530
|
IAnyTypeData res = myDataCreator.newData();
|
188
|
|
// If this type is a union type then the created data object has already its
|
189
|
|
// type being set.
|
190
|
4530
|
if (!isUnion()) {
|
191
|
4530
|
res.setType_(this);
|
192
|
|
} else {
|
193
|
0
|
if (null == res.getType_()) {// TODO
|
194
|
0
|
System.out.println("xxx");
|
195
|
|
}
|
196
|
|
assert null != res.getType_();
|
197
|
|
}
|
198
|
4530
|
return res;
|
199
|
|
}
|
200
|
|
|
201
|
|
protected abstract boolean isUnion();
|
202
|
|
|
203
|
6858
|
public final void checkConstraints(ICheckContext aContext) {
|
204
|
6858
|
getConstraints().checkConstraints(aContext);
|
205
|
6858
|
checkEnclosedConstraints(aContext);
|
206
|
|
}
|
207
|
|
|
208
|
311
|
public boolean canBeOverloadedBy(IAnyType aType) {
|
209
|
311
|
return isBaseType(aType);
|
210
|
|
}
|
211
|
|
|
212
|
1830
|
public boolean isInstanceType(IAnyType aType) {
|
213
|
1830
|
return aType.isBaseType(this);
|
214
|
|
}
|
215
|
|
|
216
|
14432
|
public final IContentModel getContentModel() {
|
217
|
14432
|
return myContentModel;
|
218
|
|
}
|
219
|
|
|
220
|
876
|
public final void setContentModel(IContentModel aContentModel) {
|
221
|
876
|
myContentModel = aContentModel;
|
222
|
|
}
|
223
|
|
|
224
|
16535
|
public final IAttributesModel getAttributesModel() {
|
225
|
16535
|
return myAttributesModel;
|
226
|
|
}
|
227
|
|
|
228
|
876
|
public final void setAttributesModel(IAttributesModel anAttributesModel) {
|
229
|
876
|
myAttributesModel = anAttributesModel;
|
230
|
|
}
|
231
|
|
|
232
|
18015
|
public final IConstraints getConstraints() {
|
233
|
18015
|
return myConstraints;
|
234
|
|
}
|
235
|
|
|
236
|
888
|
public final void setConstraints(IConstraints aConstraints) {
|
237
|
888
|
myConstraints = aConstraints;
|
238
|
|
}
|
239
|
|
|
240
|
723
|
public void validate(IConstraintViolations aViolations) {
|
241
|
|
assert null != myAttributesModel : "missing attributes model";
|
242
|
|
assert null != myContentModel : "missing content model";
|
243
|
723
|
myAttributesModel.validate(aViolations);
|
244
|
723
|
myContentModel.validate(aViolations);
|
245
|
|
}
|
246
|
|
|
247
|
276
|
public final boolean isExtension(IAnyType aType) {
|
248
|
276
|
boolean res = false;
|
249
|
276
|
if (aType.isBaseType(this)) {
|
250
|
276
|
for (IAnyType t = this; !res && (t != aType); t = t.getBaseType()) {
|
251
|
42
|
res = t.isExtension();
|
252
|
|
}
|
253
|
|
}
|
254
|
276
|
return res;
|
255
|
|
}
|
256
|
|
|
257
|
0
|
public final boolean isRestriction(IAnyType aType) {
|
258
|
0
|
boolean res = false;
|
259
|
0
|
if (aType.isBaseType(this)) {
|
260
|
0
|
for (IAnyType t = this; !res && (t != aType); t = t.getBaseType()) {
|
261
|
0
|
res = !t.isExtension();
|
262
|
|
}
|
263
|
|
}
|
264
|
0
|
return res;
|
265
|
|
}
|
266
|
|
|
267
|
1517
|
public IAttrDesc getAttrDesc(String aNamespace, String aName, IHasLocation aHasLocation) throws XmlException {
|
268
|
1517
|
return getAttributesModel().getAttrDesc(aNamespace, aName, aHasLocation);
|
269
|
|
}
|
270
|
|
|
271
|
6
|
public final IElemDesc getElemDesc(String aNamespace, String aName) throws XmlException {
|
272
|
6
|
return getContentModel().getElemDesc(aNamespace, aName);
|
273
|
|
}
|
274
|
|
|
275
|
0
|
public boolean isReference() {
|
276
|
0
|
return false;
|
277
|
|
}
|
278
|
|
}
|
279
|
|
|