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.Iterator;
|
13
|
|
import java.util.Map;
|
14
|
|
import java.util.TreeMap;
|
15
|
|
|
16
|
|
import org.jbind.xml.base.AttributeOccurence;
|
17
|
|
import org.jbind.xml.base.IHasLocation;
|
18
|
|
import org.jbind.xml.base.ILocation;
|
19
|
|
import org.jbind.xml.base.IRef;
|
20
|
|
import org.jbind.xml.base.ISymbolspaces;
|
21
|
|
import org.jbind.xml.base.ProcessContentsType;
|
22
|
|
import org.jbind.xml.base.Ref;
|
23
|
|
import org.jbind.xml.base.RefComparator;
|
24
|
|
import org.jbind.xml.core.cmp.IAttributesModel;
|
25
|
|
import org.jbind.xml.core.cmp.ISourceInfo;
|
26
|
|
import org.jbind.xml.core.cmp.IWildcard;
|
27
|
|
import org.jbind.xml.core.content.IAttrDecl;
|
28
|
|
import org.jbind.xml.core.content.IAttrDesc;
|
29
|
|
import org.jbind.xml.core.content.IAttrGroupDecl;
|
30
|
|
import org.jbind.xml.core.content.IAttrRefOrDecl;
|
31
|
|
import org.jbind.xml.core.data.ISimpleData;
|
32
|
|
import org.jbind.xml.core.type.IComplexType;
|
33
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
34
|
|
import org.jbind.xml.msg.XmlException;
|
35
|
|
import org.jbind.xml.msg.XmlMessages;
|
36
|
|
|
37
|
|
public class AttributesModel implements IAttributesModel {
|
38
|
|
|
39
|
|
private ISourceInfo mySourceInfo;
|
40
|
|
|
41
|
|
/**
|
42
|
|
* Maps {@link org.jbind.xml.base.IGlobalRef IGlobalRef}s to {@link
|
43
|
|
* IAttrRefOrDecl IAttrRefOrDecl}. The map is sorted in order that the code
|
44
|
|
* generation yields always the same results.
|
45
|
|
*/
|
46
|
|
// TODO sort at the code generation
|
47
|
|
private Map myAttributes = new TreeMap(RefComparator.INSTANCE);
|
48
|
|
|
49
|
|
private IAttrRefOrDecl myIdAttribute = null;
|
50
|
|
|
51
|
|
private IWildcard myWildcard = null;
|
52
|
|
|
53
|
|
private boolean myIsExtension;
|
54
|
|
|
55
|
|
private IAttributesModel myParentModel = null;
|
56
|
|
private IAttrGroupDecl myAttrGroupDecl = null;
|
57
|
|
|
58
|
876
|
public AttributesModel(ISourceInfo aSourceInfo) {
|
59
|
876
|
mySourceInfo = aSourceInfo;
|
60
|
|
}
|
61
|
|
|
62
|
485
|
private AttributesModel(ISourceInfo aSourceInfo, IAttributesModel aParentModel, boolean anIsExtension) {
|
63
|
485
|
this(aSourceInfo);
|
64
|
485
|
myParentModel = aParentModel;
|
65
|
485
|
myIsExtension = anIsExtension;
|
66
|
|
}
|
67
|
|
|
68
|
108
|
public IAttributesModel extend(IComplexType aType) {
|
69
|
108
|
IAttributesModel res = new AttributesModel(aType, this, true);
|
70
|
108
|
return res;
|
71
|
|
}
|
72
|
|
|
73
|
377
|
public IAttributesModel restrict(IComplexType aType) {
|
74
|
377
|
IAttributesModel res = new AttributesModel(aType, this, false);
|
75
|
377
|
return res;
|
76
|
|
}
|
77
|
|
|
78
|
485
|
public void setAttrGroupDecl(IAttrGroupDecl anAttrGroupDecl) {
|
79
|
485
|
myAttrGroupDecl = anAttrGroupDecl;
|
80
|
|
}
|
81
|
|
|
82
|
542
|
public IWildcard getWildcard() {
|
83
|
542
|
return myWildcard;
|
84
|
|
}
|
85
|
69
|
public void setWildcard(IWildcard aWildcard) {
|
86
|
69
|
myWildcard = aWildcard;
|
87
|
|
}
|
88
|
|
|
89
|
839
|
public void addAttrRefOrDecl(IAttrRefOrDecl anAttribute, IConstraintViolations aViolations) {
|
90
|
839
|
if (null != myAttributes.put(anAttribute.getGlobalRef(), anAttribute)) {
|
91
|
3
|
aViolations.add(XmlMessages.duplicateAttribute(anAttribute.getGlobalRef(), this));
|
92
|
|
}
|
93
|
839
|
if (W3CTypes.id.isBaseType(anAttribute.getType())) {
|
94
|
6
|
if (null != myIdAttribute) {
|
95
|
0
|
aViolations.add(XmlMessages.moreThanOneIdAttribute(anAttribute.getGlobalRef(), this));
|
96
|
|
}
|
97
|
6
|
myIdAttribute = anAttribute;
|
98
|
|
}
|
99
|
|
}
|
100
|
|
|
101
|
476
|
public void completeDerivedModel(IConstraintViolations aViolations) {
|
102
|
476
|
myAttrGroupDecl.collectAttributeInfo(this, aViolations);
|
103
|
476
|
if (myIsExtension) {
|
104
|
|
// Copy the attributes from the parent model to this attributes model
|
105
|
108
|
for (Iterator i = myParentModel.iterAttributes(); i.hasNext(); ) {
|
106
|
58
|
IAttrRefOrDecl attr = (IAttrRefOrDecl)i.next();
|
107
|
58
|
addAttrRefOrDecl(attr, aViolations);
|
108
|
|
}
|
109
|
|
|
110
|
108
|
IWildcard parentWildcard = myParentModel.getWildcard();
|
111
|
108
|
if ((null != myWildcard) && (null != parentWildcard)) {
|
112
|
0
|
myWildcard = myWildcard.union(parentWildcard, myWildcard.getProcessContentsType());
|
113
|
108
|
} else if (null != parentWildcard) {
|
114
|
54
|
myWildcard = parentWildcard;
|
115
|
|
}
|
116
|
|
|
117
|
|
} else {// The attributes model is a restriction
|
118
|
368
|
IWildcard parentWildcard = myParentModel.getWildcard();
|
119
|
368
|
for (Iterator i = myAttributes.values().iterator(); i.hasNext(); ) {
|
120
|
|
// Validate if all attributes in the restricted model are contained
|
121
|
|
// in the parent model.
|
122
|
447
|
IAttrRefOrDecl attr = (IAttrRefOrDecl)i.next();
|
123
|
447
|
IRef ref = attr.getGlobalRef();
|
124
|
447
|
if (myParentModel.hasAttribute(ref)) {
|
125
|
105
|
continue;
|
126
|
|
}
|
127
|
342
|
if ((null == parentWildcard) || !parentWildcard.isAllowed(ref)) {
|
128
|
0
|
aViolations.add(XmlMessages.attributeInRestrictionNotContainedInBase(ref, attr));
|
129
|
|
}
|
130
|
|
}
|
131
|
|
|
132
|
368
|
for (Iterator i = myParentModel.iterAttributes(); i.hasNext(); ) {
|
133
|
279
|
IAttrRefOrDecl previous = (IAttrRefOrDecl)i.next();
|
134
|
279
|
IRef ref = previous.getGlobalRef();
|
135
|
279
|
IAttrRefOrDecl restricted = (IAttrRefOrDecl)myAttributes.get(ref);
|
136
|
|
|
137
|
279
|
if (null == restricted) {
|
138
|
174
|
addAttrRefOrDecl(previous, aViolations);
|
139
|
|
|
140
|
|
} else {
|
141
|
105
|
AttributeOccurence previousOccurence = previous.getOccurence();
|
142
|
105
|
AttributeOccurence restrictedOccurence = restricted.getOccurence();
|
143
|
105
|
if ((previousOccurence.equals(AttributeOccurence.PROHIBITED) && !restrictedOccurence.equals(AttributeOccurence.PROHIBITED)) || (previousOccurence.equals(AttributeOccurence.REQUIRED) && !restrictedOccurence.equals(AttributeOccurence.REQUIRED))) {
|
144
|
0
|
aViolations.add(XmlMessages.attributeOccurenceMustBeAsInBaseModel(ref, restricted));
|
145
|
|
}
|
146
|
|
|
147
|
105
|
if (!restricted.isReference()) {
|
148
|
105
|
if (restricted.hasDefaultType() && AttributeOccurence.PROHIBITED.equals(restricted.getOccurence())) {
|
149
|
54
|
((AttrDecl)restricted).setType(previous.getType());
|
150
|
|
} else {
|
151
|
51
|
if (!previous.getType().isBaseType(restricted.getType())) {
|
152
|
0
|
aViolations.add(XmlMessages.attributeMustHaveSubType(ref, restricted));
|
153
|
|
}
|
154
|
|
}
|
155
|
|
} else {
|
156
|
|
// If the restricted (and therefore also the previous decl) reference
|
157
|
|
// a declarations then they have the same type.
|
158
|
|
// -> nothing to do
|
159
|
|
}
|
160
|
|
|
161
|
105
|
ISimpleData previousFixed = previous.getFixed();
|
162
|
105
|
if ((null != previousFixed) && !previousFixed.equals(restricted.getFixed())) {
|
163
|
0
|
aViolations.add(XmlMessages.attributeRestrictionMustHaveSameFixedValue(ref, previousFixed, restricted.getFixed(), restricted));
|
164
|
|
}
|
165
|
|
|
166
|
|
}
|
167
|
|
}
|
168
|
|
|
169
|
|
// Test that the wildcard is a restriction
|
170
|
368
|
if (null != myWildcard) {
|
171
|
66
|
if ((null != parentWildcard) && !myWildcard.isSubset(parentWildcard)) {
|
172
|
0
|
aViolations.add(XmlMessages.wildcardIsNotARestriction(myWildcard, this));
|
173
|
|
}
|
174
|
|
}
|
175
|
|
|
176
|
|
}
|
177
|
|
|
178
|
|
}
|
179
|
|
|
180
|
4526
|
public Iterator iterAttributes() {
|
181
|
4526
|
return myAttributes.values().iterator();
|
182
|
|
}
|
183
|
|
|
184
|
1517
|
public IAttrDesc getAttrDesc(String aNamespace, String aLocalName, IHasLocation aHasLocation) throws XmlException {
|
185
|
1517
|
return getAttrDesc(new Ref(aNamespace, ISymbolspaces.ATTRIBUTE, aLocalName), aHasLocation);
|
186
|
|
}
|
187
|
|
|
188
|
1517
|
public IAttrDesc getAttrDesc(IRef aRef, IHasLocation aHasLocation) throws XmlException {
|
189
|
1517
|
IAttrDesc res = (IAttrRefOrDecl)myAttributes.get(aRef);
|
190
|
1517
|
if (null == res) {
|
191
|
45
|
if ((null != myWildcard) && myWildcard.isAllowed(aRef)) {
|
192
|
37
|
ProcessContentsType pct = myWildcard.getProcessContentsType();
|
193
|
37
|
if (!ProcessContentsType.SKIP.equals(pct)) {
|
194
|
27
|
res = (IAttrDecl)Schemas.getInstance().getComponent(aRef);
|
195
|
27
|
if (null == res) {
|
196
|
6
|
if (ProcessContentsType.STRICT.equals(pct)) {
|
197
|
1
|
throw new XmlException(XmlMessages.cvcUnexpectedAttribute(aRef, aHasLocation));
|
198
|
|
} else {
|
199
|
5
|
res = new AttrWildcard(mySourceInfo, myWildcard);
|
200
|
|
}
|
201
|
|
}
|
202
|
|
}
|
203
|
|
} else {
|
204
|
8
|
throw new XmlException(XmlMessages.cvcUnexpectedAttribute(aRef, aHasLocation));
|
205
|
|
}
|
206
|
|
}
|
207
|
1508
|
return res;
|
208
|
|
}
|
209
|
|
|
210
|
0
|
public IAttrRefOrDecl getIdAttribute() {
|
211
|
0
|
return myIdAttribute;
|
212
|
|
}
|
213
|
|
|
214
|
1198
|
public boolean hasAttribute(IRef aRef) {
|
215
|
1198
|
return myAttributes.containsKey(aRef);
|
216
|
|
}
|
217
|
2815
|
public IAttrRefOrDecl getAttrRefOrDecl(IRef aRef) {
|
218
|
2815
|
return (IAttrRefOrDecl)myAttributes.get(aRef);
|
219
|
|
}
|
220
|
0
|
public IAttrRefOrDecl getAttrRefOrDecl(String aNamespace, String aName) {
|
221
|
0
|
Ref ref = new Ref(aNamespace, ISymbolspaces.ATTRIBUTE, aName);
|
222
|
0
|
return (IAttrRefOrDecl)myAttributes.get(ref);
|
223
|
|
}
|
224
|
|
|
225
|
723
|
public void validate(IConstraintViolations aViolations) {}
|
226
|
|
|
227
|
8027
|
public IAttrRefOrDecl getDefaultAttr(IRef aRef) {
|
228
|
8027
|
IAttrRefOrDecl a = (IAttrRefOrDecl)myAttributes.get(aRef);
|
229
|
8027
|
if ((null != a) && (null != a.getDefault()) && AttributeOccurence.OPTIONAL.equals(a.getOccurence())) {
|
230
|
154
|
return a;
|
231
|
|
}
|
232
|
7873
|
return null;
|
233
|
|
}
|
234
|
|
|
235
|
3
|
public ILocation getLocation() {
|
236
|
3
|
return (null != mySourceInfo) ? mySourceInfo.getLocation() : null;
|
237
|
|
}
|
238
|
|
}
|
239
|
|
|