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.reader;
|
11
|
|
|
12
|
|
import java.util.Iterator;
|
13
|
|
|
14
|
|
import org.jbind.util.collection.UnsynchronizedStack;
|
15
|
|
import org.jbind.xml.core.cmp.IBinding;
|
16
|
|
import org.jbind.xml.core.cmp.IComponent;
|
17
|
|
import org.jbind.xml.core.content.IAttrDecl;
|
18
|
|
import org.jbind.xml.core.content.IAttrGroupDesc;
|
19
|
|
import org.jbind.xml.core.content.IAttrRef;
|
20
|
|
import org.jbind.xml.core.content.IAttrWildcard;
|
21
|
|
import org.jbind.xml.core.content.IElemDecl;
|
22
|
|
import org.jbind.xml.core.content.IElemGroupDesc;
|
23
|
|
import org.jbind.xml.core.content.IElemRef;
|
24
|
|
import org.jbind.xml.core.content.IElemWildcard;
|
25
|
|
import org.jbind.xml.core.type.IAnyType;
|
26
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
27
|
|
import org.jbind.xml.msg.XmlException;
|
28
|
|
import org.jbind.xml.schema.cmp.ComponentVisitor;
|
29
|
|
import org.jbind.xml.schema.compiler.CartridgeMgr;
|
30
|
|
import org.jbind.xml.schema.compiler.IBinder;
|
31
|
|
import org.jbind.xml.schema.compiler.ICartridge;
|
32
|
|
|
33
|
|
public class BindingVisitor extends ComponentVisitor {
|
34
|
|
|
35
|
|
private static interface IBind {
|
36
|
|
IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) throws XmlException;
|
37
|
|
IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException;
|
38
|
|
}
|
39
|
|
|
40
|
|
private static final IBind BIND_ANY_TYPE = new IBind() {
|
41
|
1864
|
public IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) throws XmlException {
|
42
|
1864
|
return aBinder.createGlobalAnyTypeBinding(aComponent, aRootPackage, aUseBuiltInClassesOnly);
|
43
|
|
}
|
44
|
940
|
public IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException {
|
45
|
940
|
return aBinder.createInnerAnyTypeBinding(aComponent, aParentBinding, aUseBuiltInClassesOnly);
|
46
|
|
}
|
47
|
|
};
|
48
|
|
|
49
|
|
private static final IBind BIND_ATTR_REF = new IBind() {
|
50
|
0
|
public IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) {
|
51
|
|
assert false;
|
52
|
0
|
return null;
|
53
|
|
}
|
54
|
120
|
public IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException {
|
55
|
120
|
return aBinder.createInnerAttrRefBinding(aComponent, aParentBinding, aUseBuiltInClassesOnly);
|
56
|
|
}
|
57
|
|
};
|
58
|
|
|
59
|
|
private static final IBind BIND_ATTR_DECL = new IBind() {
|
60
|
236
|
public IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) throws XmlException {
|
61
|
236
|
return aBinder.createGlobalAttrDeclBinding(aComponent, aRootPackage, aUseBuiltInClassesOnly);
|
62
|
|
}
|
63
|
2240
|
public IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException {
|
64
|
2240
|
return aBinder.createInnerAttrDeclBinding(aComponent, aParentBinding, aUseBuiltInClassesOnly);
|
65
|
|
}
|
66
|
|
};
|
67
|
|
|
68
|
|
private static final IBind BIND_ATTR_WILDCARD = new IBind() {
|
69
|
0
|
public IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) {
|
70
|
|
assert false;
|
71
|
0
|
return null;
|
72
|
|
}
|
73
|
264
|
public IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException {
|
74
|
264
|
return aBinder.createInnerAttrWildcardBinding(aComponent, aParentBinding, aUseBuiltInClassesOnly);
|
75
|
|
}
|
76
|
|
};
|
77
|
|
|
78
|
|
private static final IBind BIND_GROUP_REF_OR_DECL = new IBind() {
|
79
|
140
|
public IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) throws XmlException {
|
80
|
140
|
return aBinder.createGlobalGroupDeclBinding(aComponent, aRootPackage, aUseBuiltInClassesOnly);
|
81
|
|
}
|
82
|
1544
|
public IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException {
|
83
|
1544
|
return aBinder.createInnerGroupRefOrDeclBinding(aComponent, aParentBinding, aUseBuiltInClassesOnly);
|
84
|
|
}
|
85
|
|
};
|
86
|
|
|
87
|
|
private static final IBind BIND_ELEM_REF = new IBind() {
|
88
|
0
|
public IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) {
|
89
|
|
assert false;
|
90
|
0
|
return null;
|
91
|
|
}
|
92
|
840
|
public IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException {
|
93
|
840
|
return aBinder.createInnerElemRefBinding(aComponent, aParentBinding, aUseBuiltInClassesOnly);
|
94
|
|
}
|
95
|
|
};
|
96
|
|
|
97
|
|
private static final IBind BIND_ELEM_DECL = new IBind() {
|
98
|
1160
|
public IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) throws XmlException {
|
99
|
1160
|
return aBinder.createGlobalElemDeclBinding(aComponent, aRootPackage, aUseBuiltInClassesOnly);
|
100
|
|
}
|
101
|
1480
|
public IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException {
|
102
|
1480
|
return aBinder.createInnerElemDeclBinding(aComponent, aParentBinding, aUseBuiltInClassesOnly);
|
103
|
|
}
|
104
|
|
};
|
105
|
|
|
106
|
|
private static final IBind BIND_ELEM_WILDCARD = new IBind() {
|
107
|
0
|
public IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) {
|
108
|
|
assert false;
|
109
|
0
|
return null;
|
110
|
|
}
|
111
|
108
|
public IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException {
|
112
|
108
|
return aBinder.createInnerElemWildcardBinding(aComponent, aParentBinding, aUseBuiltInClassesOnly);
|
113
|
|
}
|
114
|
|
};
|
115
|
|
|
116
|
|
private static final IBind BIND_ATTR_GROUP = new IBind() {
|
117
|
176
|
public IBinding bind(IBinder aBinder, IComponent aComponent, String aRootPackage, boolean aUseBuiltInClassesOnly) throws XmlException {
|
118
|
176
|
return aBinder.createGlobalAttrGroupBinding(aComponent, aRootPackage, aUseBuiltInClassesOnly);
|
119
|
|
}
|
120
|
1976
|
public IBinding bind(IBinder aBinder, IComponent aComponent, IBinding aParentBinding, boolean aUseBuiltInClassesOnly) throws XmlException {
|
121
|
1976
|
return aBinder.createInnerAttrGroupBinding(aComponent, aParentBinding, aUseBuiltInClassesOnly);
|
122
|
|
}
|
123
|
|
};
|
124
|
|
|
125
|
|
private UnsynchronizedStack myBindings = new UnsynchronizedStack();
|
126
|
|
private UnsynchronizedStack myComponents = new UnsynchronizedStack();
|
127
|
|
|
128
|
|
private String myRootPackage;
|
129
|
|
private boolean myUseBuiltInClassesOnly;
|
130
|
|
private IConstraintViolations myViolations;
|
131
|
|
|
132
|
159
|
public BindingVisitor(String aRootPackage, boolean aUseBuiltInClassesOnly, IConstraintViolations aViolations) {
|
133
|
159
|
myRootPackage = aRootPackage;
|
134
|
159
|
myUseBuiltInClassesOnly = aUseBuiltInClassesOnly;
|
135
|
159
|
myViolations = aViolations;
|
136
|
|
}
|
137
|
|
|
138
|
0
|
protected boolean doVisitComponentStart(IComponent aComponent) throws XmlException {
|
139
|
|
assert false;
|
140
|
0
|
return true;
|
141
|
|
}
|
142
|
|
|
143
|
3272
|
private boolean visitStart(IBind aBind, IComponent aComponent) throws XmlException {
|
144
|
|
assert null == aComponent.getBindings();
|
145
|
3272
|
IBinding[] bindings = new IBinding[CartridgeMgr.instance.getNbCartridges()];
|
146
|
3272
|
if (aComponent.isTopLevelComponent()) {
|
147
|
|
assert myBindings.size() == 0;
|
148
|
|
assert aComponent.getParentComponent() == null :
|
149
|
|
"TopLevelComponent " + aComponent.getName() + " has parent: " + aComponent.getLocation();
|
150
|
894
|
for (Iterator i = CartridgeMgr.instance.iterCartridges(); i.hasNext(); ) {
|
151
|
3576
|
ICartridge cartridge = (ICartridge)i.next();
|
152
|
3576
|
IBinding b = aBind.bind(cartridge, aComponent, myRootPackage, myUseBuiltInClassesOnly);
|
153
|
3576
|
bindings[cartridge.getCartridgeNo()] = b;
|
154
|
|
}
|
155
|
|
} else {
|
156
|
|
assert aComponent.getParentComponent() != null :
|
157
|
|
"inner component " + aComponent.getName() + "/" + aComponent + " has no parent: " + aComponent.getLocation();
|
158
|
2378
|
IBinding[] parentBindings = (IBinding[])myBindings.peek();
|
159
|
2378
|
for (Iterator i = CartridgeMgr.instance.iterCartridges(); i.hasNext(); ) {
|
160
|
9512
|
ICartridge cartridge = (ICartridge)i.next();
|
161
|
9512
|
IBinding parentBinding = parentBindings[cartridge.getCartridgeNo()];
|
162
|
9512
|
bindings[cartridge.getCartridgeNo()] = aBind.bind(cartridge, aComponent, parentBinding, myUseBuiltInClassesOnly);
|
163
|
|
}
|
164
|
|
}
|
165
|
|
|
166
|
3272
|
aComponent.setBindings(bindings);
|
167
|
3272
|
boolean b = false;
|
168
|
|
assert (b = true);
|
169
|
3272
|
if (b) {
|
170
|
3272
|
assertBindings(aComponent);
|
171
|
|
}
|
172
|
3272
|
myBindings.push(bindings);
|
173
|
3272
|
myComponents.push(aComponent);
|
174
|
|
|
175
|
3272
|
return true;
|
176
|
|
}
|
177
|
|
|
178
|
3272
|
private void assertBindings(IComponent aComponent) {
|
179
|
3272
|
for (IComponent c = aComponent; c != null; c = c.getParentComponent()) {
|
180
|
8158
|
assertBinding(c);
|
181
|
|
}
|
182
|
|
}
|
183
|
|
|
184
|
8158
|
private void assertBinding(IComponent aComponent) {
|
185
|
8158
|
IBinding[] bindings = aComponent.getBindings();
|
186
|
8158
|
IComponent parent = aComponent.getParentComponent();
|
187
|
8158
|
if (null != parent) {
|
188
|
4886
|
IBinding[] pbs = parent.getBindings();
|
189
|
4886
|
if (null == pbs) {
|
190
|
0
|
System.out.println();
|
191
|
0
|
System.out.println("component '" + aComponent.getName() + "': " + aComponent.getLocation());
|
192
|
0
|
System.out.println("parent '" + parent.getName() + "': " + parent.getLocation());
|
193
|
0
|
System.out.println("parent has no bindings - component bindings are: ");
|
194
|
0
|
outputBindings(bindings);
|
195
|
0
|
System.out.println("outer bindings:\n");
|
196
|
0
|
while (myBindings.size() != 0) {
|
197
|
0
|
IBinding[] bs = (IBinding[])myBindings.pop();
|
198
|
0
|
IComponent c = (IComponent)myComponents.pop();
|
199
|
0
|
System.out.println("parent component bindings at location: " + c.getLocation());
|
200
|
0
|
outputBindings(bs);
|
201
|
|
}
|
202
|
|
assert false;
|
203
|
|
} else {
|
204
|
4886
|
for (int i = 0; i < pbs.length; i++) {
|
205
|
19544
|
IBinding pb = pbs[i];
|
206
|
19544
|
IBinding b = bindings[i];
|
207
|
19544
|
if (b != null) {
|
208
|
9772
|
String pbFqName = pb.getFqName();
|
209
|
|
assert (null == pbFqName) || b.getFqName().startsWith(pbFqName) :
|
210
|
|
"b: " + b.getFqName() + "; pb: " + pbFqName;
|
211
|
|
}
|
212
|
|
}
|
213
|
|
}
|
214
|
|
}
|
215
|
|
}
|
216
|
|
|
217
|
0
|
private void outputBindings(IBinding[] aBindings) {
|
218
|
0
|
for (int i = 0; i < aBindings.length; i++) {
|
219
|
0
|
System.out.println(i + ": " + ((null != aBindings[i]) ? aBindings[i].getFqName() : ""));
|
220
|
|
}
|
221
|
|
}
|
222
|
|
|
223
|
3272
|
protected void doVisitComponentEnd(IComponent aComponent) throws XmlException {
|
224
|
3272
|
myBindings.pop();
|
225
|
3272
|
myComponents.pop();
|
226
|
|
}
|
227
|
|
|
228
|
701
|
protected boolean doVisitAnyTypeStart(IAnyType aComponent) throws XmlException {
|
229
|
701
|
return visitStart(BIND_ANY_TYPE, aComponent);
|
230
|
|
}
|
231
|
|
|
232
|
619
|
protected boolean doVisitAttrDeclStart(IAttrDecl aComponent) throws XmlException {
|
233
|
619
|
return visitStart(BIND_ATTR_DECL, aComponent);
|
234
|
|
}
|
235
|
|
|
236
|
30
|
protected boolean doVisitAttrRefStart(IAttrRef aComponent) throws XmlException {
|
237
|
30
|
return visitStart(BIND_ATTR_REF, aComponent);
|
238
|
|
}
|
239
|
|
|
240
|
66
|
protected boolean doVisitAttrWildcardStart(IAttrWildcard aComponent) throws XmlException {
|
241
|
66
|
return visitStart(BIND_ATTR_WILDCARD, aComponent);
|
242
|
|
}
|
243
|
|
|
244
|
421
|
protected boolean doVisitElemGroupDescStart(IElemGroupDesc aComponent) throws XmlException {
|
245
|
421
|
return visitStart(BIND_GROUP_REF_OR_DECL, aComponent);
|
246
|
|
}
|
247
|
|
|
248
|
210
|
protected boolean doVisitElemRefStart(IElemRef aComponent) throws XmlException {
|
249
|
210
|
return visitStart(BIND_ELEM_REF, aComponent);
|
250
|
|
}
|
251
|
|
|
252
|
660
|
protected boolean doVisitElemDeclStart(IElemDecl aComponent) throws XmlException {
|
253
|
660
|
return visitStart(BIND_ELEM_DECL, aComponent);
|
254
|
|
}
|
255
|
|
|
256
|
27
|
protected boolean doVisitElemWildcardStart(IElemWildcard aComponent) throws XmlException {
|
257
|
27
|
return visitStart(BIND_ELEM_WILDCARD, aComponent);
|
258
|
|
}
|
259
|
|
|
260
|
538
|
protected boolean doVisitAttrGroupDescStart(IAttrGroupDesc aComponent) throws XmlException {
|
261
|
538
|
return visitStart(BIND_ATTR_GROUP, aComponent);
|
262
|
|
}
|
263
|
|
|
264
|
|
}
|
265
|
|
|