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.element;
|
11
|
|
|
12
|
|
import java.util.Iterator;
|
13
|
|
import java.util.Map;
|
14
|
|
import java.util.Set;
|
15
|
|
|
16
|
|
import org.jbind.xml.base.FinalType;
|
17
|
|
import org.jbind.xml.base.IAttribute;
|
18
|
|
import org.jbind.xml.base.IQName;
|
19
|
|
import org.jbind.xml.base.IRef;
|
20
|
|
import org.jbind.xml.base.ISymbolspaces;
|
21
|
|
import org.jbind.xml.base.Ref;
|
22
|
|
import org.jbind.xml.core.cmp.IComponent;
|
23
|
|
import org.jbind.xml.core.cmp.IComponentStore;
|
24
|
|
import org.jbind.xml.core.cmp.ISourceInfo;
|
25
|
|
import org.jbind.xml.core.constraint.ConstraintType;
|
26
|
|
import org.jbind.xml.core.constraint.IConstraints;
|
27
|
|
import org.jbind.xml.core.constraint.IEnumerationConstraint;
|
28
|
|
import org.jbind.xml.core.data.IAnyTypeData;
|
29
|
|
import org.jbind.xml.core.data.INOTATIONData;
|
30
|
|
import org.jbind.xml.core.type.IAnyType;
|
31
|
|
import org.jbind.xml.core.type.ISimpleType;
|
32
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
33
|
|
import org.jbind.xml.msg.XmlException;
|
34
|
|
import org.jbind.xml.msg.XmlMessages;
|
35
|
|
import org.jbind.xml.schema.cmp.Schemas;
|
36
|
|
import org.jbind.xml.schema.cmp.W3CTypes;
|
37
|
|
import org.jbind.xml.schema.compiler.CartridgeMgr;
|
38
|
|
import org.jbind.xml.schema.constraint.Constraints;
|
39
|
|
import org.jbind.xml.schema.instantiation.IElemValHelper;
|
40
|
|
import org.jbind.xml.schema.instantiation.IElementHelper;
|
41
|
|
import org.jbind.xml.schema.instantiation.IJobRefs;
|
42
|
|
|
43
|
|
public abstract class SimpleRestriction extends Annotated {
|
44
|
|
|
45
|
|
private ISimpleTypeDef mySimpleTypeDef = null;
|
46
|
|
|
47
|
|
private IRef myBase = null;
|
48
|
|
|
49
|
242
|
public SimpleRestriction(CreationParams aCreationParams) {
|
50
|
242
|
super(aCreationParams);
|
51
|
|
}
|
52
|
|
|
53
|
356
|
protected IElement doCreateChild(CreationParams aCreationParams) throws XmlException {
|
54
|
356
|
IElement res = null;
|
55
|
356
|
String componentName = NameUtil.getSchemaComponentName(aCreationParams);
|
56
|
356
|
if ("simpleType".equals(componentName)) {
|
57
|
16
|
res = new SimpleTypeDef(aCreationParams, "Base");
|
58
|
340
|
} else if ("minInclusive".equals(componentName)) {
|
59
|
12
|
res = new OrderFacet(aCreationParams, ConstraintType.MIN_INCLUSIVE);
|
60
|
328
|
} else if ("minExclusive".equals(componentName)) {
|
61
|
0
|
res = new OrderFacet(aCreationParams, ConstraintType.MIN_EXCLUSIVE);
|
62
|
328
|
} else if ("maxExclusive".equals(componentName)) {
|
63
|
1
|
res = new OrderFacet(aCreationParams, ConstraintType.MAX_EXCLUSIVE);
|
64
|
327
|
} else if ("maxInclusive".equals(componentName)) {
|
65
|
20
|
res = new OrderFacet(aCreationParams, ConstraintType.MAX_INCLUSIVE);
|
66
|
307
|
} else if ("enumeration".equals(componentName)) {
|
67
|
179
|
res = new EnumerationFacet(aCreationParams);
|
68
|
128
|
} else if ("pattern".equals(componentName)) {
|
69
|
30
|
res = new PatternFacet(aCreationParams);
|
70
|
98
|
} else if ("length".equals(componentName)) {
|
71
|
20
|
res = new LengthFacet(aCreationParams, ConstraintType.LENGTH);
|
72
|
78
|
} else if ("minLength".equals(componentName)) {
|
73
|
18
|
res = new LengthFacet(aCreationParams, ConstraintType.MIN_LENGTH);
|
74
|
60
|
} else if ("maxLength".equals(componentName)) {
|
75
|
6
|
res = new LengthFacet(aCreationParams, ConstraintType.MAX_LENGTH);
|
76
|
54
|
} else if ("whiteSpace".equals(componentName)) {
|
77
|
48
|
res = new WhiteSpaceFacet(aCreationParams);
|
78
|
6
|
} else if ("totalDigits".equals(componentName)) {
|
79
|
0
|
res = new DigitsFacet(aCreationParams, ConstraintType.TOTAL_DIGITS);
|
80
|
6
|
} else if ("fractionDigits".equals(componentName)) {
|
81
|
2
|
res = new DigitsFacet(aCreationParams, ConstraintType.FRACTION_DIGITS);
|
82
|
|
} else {
|
83
|
4
|
res = super.doCreateChild(aCreationParams);
|
84
|
|
}
|
85
|
356
|
return res;
|
86
|
|
}
|
87
|
|
|
88
|
226
|
protected IAttribute doCreateAttribute(ACParams anACParams) throws XmlException {
|
89
|
226
|
IAttribute res = null;
|
90
|
226
|
String an = NameUtil.getSchemaAttributeName(anACParams);
|
91
|
226
|
if ("base".equals(an)) {
|
92
|
226
|
res = new RefAttribute(anACParams, ISymbolspaces.TYPE);
|
93
|
226
|
myBase = res.getRef();
|
94
|
|
} else {
|
95
|
0
|
res = super.doCreateAttribute(anACParams);
|
96
|
|
}
|
97
|
226
|
return res;
|
98
|
|
}
|
99
|
|
|
100
|
|
|
101
|
219
|
public Iterator iterFacets() {
|
102
|
219
|
return iterChildrenByClass(IFacet.class);
|
103
|
|
}
|
104
|
|
|
105
|
|
|
106
|
918
|
public ISimpleTypeDef getSimpleTypeDef() {
|
107
|
918
|
return mySimpleTypeDef;
|
108
|
|
}
|
109
|
|
|
110
|
|
|
111
|
1145
|
public IRef getBaseAttribute() {
|
112
|
1145
|
return myBase;
|
113
|
|
}
|
114
|
|
|
115
|
|
|
116
|
242
|
public void validateElement(IElemValHelper aHelper, IConstraintViolations aViolations) {
|
117
|
242
|
super.validateElement(aHelper, aViolations);
|
118
|
242
|
mySimpleTypeDef = (ISimpleTypeDef)getSubElement(ISimpleTypeDef.class, "inlined type", aViolations);
|
119
|
242
|
if ((getBaseAttribute() == null) && (getSimpleTypeDef() == null)) {
|
120
|
0
|
aViolations.add(XmlMessages.missingType(this));
|
121
|
|
}
|
122
|
|
}
|
123
|
|
|
124
|
212
|
public void collectRefsForCreation(IJobRefs aJobRefs) {
|
125
|
212
|
IRef r = null;
|
126
|
?
|
if (null != (r = getBaseAttribute())) {
|
127
|
197
|
aJobRefs.add(r, true, this);
|
128
|
|
}
|
129
|
212
|
if (null != getSimpleTypeDef()) {
|
130
|
|
// There is an inlined simple type definition.
|
131
|
|
// Recursively collect its references.
|
132
|
15
|
getSimpleTypeDef().collectRefsForCreation(aJobRefs);
|
133
|
|
}
|
134
|
|
}
|
135
|
|
|
136
|
0
|
public void collectRefsForCompletion(IElementHelper aHelper, IJobRefs aJobRefs) {}
|
137
|
|
|
138
|
0
|
public void collectRefsForValidation(IElementHelper aHelper, IJobRefs aJobRefs) {}
|
139
|
|
|
140
|
204
|
public IComponent createComponent(IElementHelper anElementHelper, ISourceInfo aSourceInfo, String aNamespace, String aName, String aRole, Set aFinalTypes, IConstraintViolations aViolations) {
|
141
|
|
// The the base type determines the variety of the restricted type.
|
142
|
|
// The base type can either be referenced by the base attribute of the simple
|
143
|
|
// restriction or by the base attribute of an inlined type definition.
|
144
|
|
//
|
145
|
|
// The base attribute may either reference a simple type or a complex type with
|
146
|
|
// simple content. (The simple restriction may either occur inside a simpleType-
|
147
|
|
// or a simpleContent-element.)
|
148
|
|
|
149
|
204
|
ISimpleType baseType = null;
|
150
|
|
|
151
|
204
|
if (null != getBaseAttribute()) {
|
152
|
190
|
IAnyType referencedBaseType = (IAnyType)anElementHelper.getComponent(getBaseAttribute(), true);
|
153
|
190
|
if (referencedBaseTypeMustNotBeComplex() && referencedBaseType.isComplex()) {
|
154
|
0
|
aViolations.add(XmlMessages.baseTypeMustNotBeComplex(this));
|
155
|
|
}
|
156
|
190
|
if (!referencedBaseType.isSimple()) {
|
157
|
0
|
aViolations.add(XmlMessages.baseTypeMustBeSimpleOrHaveSc(this));
|
158
|
|
}
|
159
|
190
|
if (null != getSimpleTypeDef()) {
|
160
|
0
|
baseType = (ISimpleType)anElementHelper.getComponent(getSimpleTypeDef().getBaseRef(), true);
|
161
|
0
|
if (!referencedBaseType.isBaseType(baseType)) {
|
162
|
0
|
aViolations.add(XmlMessages.inlinedTypeMustBeSubTypeOfBaseType(this));
|
163
|
|
}
|
164
|
|
} else {
|
165
|
190
|
baseType = (ISimpleType)referencedBaseType;
|
166
|
|
}
|
167
|
|
} else {
|
168
|
14
|
baseType = (ISimpleType)anElementHelper.getComponent(getSimpleTypeDef().getBaseRef(), true);
|
169
|
|
}
|
170
|
|
|
171
|
204
|
if (baseType.isActive(FinalType.RESTRICTION)) {
|
172
|
0
|
aViolations.add(XmlMessages.baseTypeIsFinalizedForRestriction(this));
|
173
|
|
}
|
174
|
204
|
ISimpleType res = baseType.createRestriction(aSourceInfo, aNamespace, aName, aRole, aFinalTypes, aViolations);
|
175
|
204
|
res.setBaseType(baseType);
|
176
|
|
|
177
|
204
|
return res;
|
178
|
|
}
|
179
|
|
|
180
|
219
|
public void collectConstraints(IAnyType aType, IConstraintViolations aViolations) {
|
181
|
219
|
if (null != getSimpleTypeDef()) {
|
182
|
14
|
getSimpleTypeDef().collectConstraints(aType, aViolations);
|
183
|
|
}
|
184
|
|
|
185
|
219
|
IConstraints constraints = aType.getConstraints();
|
186
|
|
|
187
|
219
|
IConstraints additionalConstraints = new Constraints();
|
188
|
|
|
189
|
219
|
for (Iterator i = iterFacets(); i.hasNext(); ) {
|
190
|
323
|
IFacet facet = (IFacet)i.next();
|
191
|
|
// The type aType can be a complex type with simple content. Its simple
|
192
|
|
// content type is used to create the facet data.
|
193
|
323
|
ISimpleType simpleType = aType.getSimpleContentType();
|
194
|
323
|
facet.apply(simpleType, additionalConstraints, aViolations);
|
195
|
|
}
|
196
|
|
|
197
|
219
|
constraints.add(additionalConstraints, aViolations, this);
|
198
|
|
}
|
199
|
|
|
200
|
214
|
public void addSchemaData(IComponentStore aComponentStore, IComponent aComponent, IConstraintViolations aViolations) {
|
201
|
214
|
ISimpleType type = (ISimpleType)aComponent;
|
202
|
214
|
collectConstraints(type, aViolations);
|
203
|
214
|
IEnumerationConstraint ec = (IEnumerationConstraint)type.getConstraints().getConstraint(ConstraintType.ENUMERATION);
|
204
|
214
|
if (null != ec) {
|
205
|
68
|
String typeName = CartridgeMgr.instance.getDataInterfaceCartridge().getUsableFqName(type);
|
206
|
68
|
for (Iterator i = ec.iterOptions(); i.hasNext(); ) {
|
207
|
171
|
IAnyTypeData option = (IAnyTypeData)((Map.Entry)i.next()).getKey();
|
208
|
171
|
Schemas.getInstance().addEnumerationOption(typeName, option.toString(), option);
|
209
|
|
}
|
210
|
|
}
|
211
|
214
|
if (W3CTypes.notation.isBaseType(type)) {
|
212
|
0
|
if (null == ec) {
|
213
|
0
|
aViolations.add(XmlMessages.typeDerivedFromNotationMustHaveEnumerationFacet(type));
|
214
|
|
} else {
|
215
|
0
|
for (Iterator i = ec.iterOptions(); i.hasNext(); ) {
|
216
|
0
|
INOTATIONData notationData = (INOTATIONData)((Map.Entry)i.next()).getKey();
|
217
|
0
|
IQName qName = notationData.getQName();
|
218
|
0
|
Ref ref = new Ref(qName.getNamespace(), ISymbolspaces.NOTATION, qName.getLocalPart());
|
219
|
0
|
IComponent c = aComponentStore.getComponent(ref);
|
220
|
0
|
if (null == c) {
|
221
|
0
|
aViolations.add(XmlMessages.unknownNotation(ref, notationData.getImpl_()));
|
222
|
|
}
|
223
|
|
}
|
224
|
|
}
|
225
|
|
}
|
226
|
|
}
|
227
|
|
|
228
|
|
protected abstract boolean referencedBaseTypeMustNotBeComplex();
|
229
|
|
|
230
|
3
|
public void setBaseAttribute(IRef aRef) {
|
231
|
3
|
myBase = aRef;
|
232
|
|
}
|
233
|
|
|
234
|
|
}
|
235
|
|
|