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.net.URL;
|
13
|
|
import java.util.ArrayList;
|
14
|
|
import java.util.Collections;
|
15
|
|
import java.util.Comparator;
|
16
|
|
import java.util.Iterator;
|
17
|
|
import java.util.List;
|
18
|
|
import java.util.Map;
|
19
|
|
|
20
|
|
import org.jbind.xml.base.IAttribute;
|
21
|
|
import org.jbind.xml.base.ILocation;
|
22
|
|
import org.jbind.xml.base.IRef;
|
23
|
|
import org.jbind.xml.base.ISymbolspace;
|
24
|
|
import org.jbind.xml.base.Ref;
|
25
|
|
import org.jbind.xml.core.cmp.IAnnotation;
|
26
|
|
import org.jbind.xml.core.cmp.IBinding;
|
27
|
|
import org.jbind.xml.core.cmp.IComponent;
|
28
|
|
import org.jbind.xml.core.cmp.IComponentVisitor;
|
29
|
|
import org.jbind.xml.core.cmp.ISourceInfo;
|
30
|
|
import org.jbind.xml.msg.XmlException;
|
31
|
|
|
32
|
|
public abstract class Component implements IComponent {
|
33
|
|
|
34
|
|
private ISourceInfo mySourceInfo;
|
35
|
|
private String myNamespace;
|
36
|
|
private String myName;
|
37
|
|
|
38
|
|
private IBinding[] myBindings = null;
|
39
|
|
|
40
|
|
private IComponent myParent = null;
|
41
|
|
private List mySubComponents = null;
|
42
|
|
|
43
|
6286
|
public Component(ISourceInfo aSourceInfo, String aNamespace, String aName) {
|
44
|
6286
|
mySourceInfo = aSourceInfo;
|
45
|
6286
|
myNamespace = aNamespace;
|
46
|
6286
|
myName = aName;
|
47
|
|
}
|
48
|
|
|
49
|
42478
|
public final String getNamespace() {
|
50
|
42478
|
return myNamespace;
|
51
|
|
}
|
52
|
55535
|
public final String getName() {
|
53
|
55535
|
return myName;
|
54
|
|
}
|
55
|
34000
|
public final boolean isAnonymous() {
|
56
|
34000
|
return null == myName;
|
57
|
|
}
|
58
|
|
|
59
|
33563
|
public final IRef getGlobalRef() {
|
60
|
|
assert !isAnonymous() :
|
61
|
|
"anonymous component has no identifying reference: " + this;
|
62
|
33563
|
return new Ref(getNamespace(), getSymbolspace(), getName());
|
63
|
|
}
|
64
|
|
|
65
|
29672
|
public final IBinding[] getBindings() {
|
66
|
29672
|
return myBindings;
|
67
|
|
}
|
68
|
3413
|
public final void setBindings(IBinding[] aBindings) {
|
69
|
3413
|
myBindings = aBindings;
|
70
|
|
}
|
71
|
|
|
72
|
171
|
public ILocation getLocation() {
|
73
|
171
|
return mySourceInfo.getLocation();
|
74
|
|
}
|
75
|
|
|
76
|
|
// The binding attribute methods are overloaded in the AttrRef and ElemRef classes.
|
77
|
22998
|
public IAttribute getBindingAttribute(String aName) {
|
78
|
22998
|
return mySourceInfo.getBindingAttribute(aName);
|
79
|
|
}
|
80
|
10549
|
public IAttribute getLocalBindingAttribute(String aName) {
|
81
|
10549
|
return mySourceInfo.getLocalBindingAttribute(aName);
|
82
|
|
}
|
83
|
8201
|
public IAttribute getDefaultedLocalBindingAttribute(String aName) {
|
84
|
8201
|
return mySourceInfo.getDefaultedLocalBindingAttribute(aName);
|
85
|
|
}
|
86
|
|
|
87
|
9113
|
public final String getLocalStringBindingAttribute(String aName) {
|
88
|
9113
|
IAttribute a = getLocalBindingAttribute(aName);
|
89
|
9113
|
return (null != a) ? a.getStringValue() : null;
|
90
|
|
}
|
91
|
932
|
public final String getDefaultedLocalStringBindingAttribute(String aName) {
|
92
|
932
|
IAttribute a = getDefaultedLocalBindingAttribute(aName);
|
93
|
932
|
return (null != a) ? a.getStringValue() : null;
|
94
|
|
}
|
95
|
12414
|
public final String getStringBindingAttribute(String aName) {
|
96
|
12414
|
IAttribute a = getBindingAttribute(aName);
|
97
|
12414
|
return (null != a) ? a.getStringValue() : null;
|
98
|
|
}
|
99
|
|
|
100
|
119
|
public Map getPrefixToNamespaceMapping() {
|
101
|
119
|
return mySourceInfo.getPrefixToNamespaceMapping();
|
102
|
|
}
|
103
|
|
|
104
|
3965
|
public boolean isTopLevelComponent() {
|
105
|
|
// overloaded in AttrGroupDecl
|
106
|
3965
|
return mySourceInfo.isTopLevelComponent();
|
107
|
|
}
|
108
|
2257
|
public IAnnotation getAnnotation() {
|
109
|
2257
|
return mySourceInfo.getAnnotation();
|
110
|
|
}
|
111
|
2560
|
public void setParentComponent(IComponent aParent) {
|
112
|
2560
|
myParent = aParent;
|
113
|
|
}
|
114
|
19588
|
public IComponent getParentComponent() {
|
115
|
19588
|
return myParent;
|
116
|
|
}
|
117
|
|
|
118
|
2560
|
public synchronized void addSubComponent(IComponent aComponent) {
|
119
|
2560
|
if (null == mySubComponents) {
|
120
|
1390
|
mySubComponents = new ArrayList();
|
121
|
|
}
|
122
|
2560
|
mySubComponents.add(aComponent);
|
123
|
|
}
|
124
|
|
|
125
|
7180
|
protected void visitSubComponents(IComponentVisitor aVisitor) throws XmlException {
|
126
|
7180
|
if (aVisitor.shallVisitSubComponents() && (null != mySubComponents)) {
|
127
|
3010
|
ArrayList l = new ArrayList(mySubComponents);
|
128
|
3010
|
Collections.sort(l, COMPONENT_COMPARATOR);
|
129
|
3010
|
for (Iterator i = l.iterator(); i.hasNext(); ) {
|
130
|
5339
|
IComponent c = (IComponent)i.next();
|
131
|
5339
|
c.accept(aVisitor);
|
132
|
|
}
|
133
|
|
}
|
134
|
|
}
|
135
|
|
|
136
|
|
private static final Comparator COMPONENT_COMPARATOR = new Comparator() {
|
137
|
|
|
138
|
3359
|
public int compare(Object aComponent1, Object aComponent2) {
|
139
|
3359
|
int res;
|
140
|
3359
|
IComponent c1 = (IComponent)aComponent1;
|
141
|
3359
|
IComponent c2 = (IComponent)aComponent2;
|
142
|
3359
|
String namespace1 = c1.getNamespace();
|
143
|
3359
|
String namespace2 = c2.getNamespace();
|
144
|
3359
|
if ((namespace1 != null) && (namespace2 != null)) {
|
145
|
2340
|
res = namespace1.compareTo(namespace2);
|
146
|
2340
|
if (res != 0) {
|
147
|
76
|
return res;
|
148
|
|
}
|
149
|
1019
|
} else if (namespace1 != null) {
|
150
|
294
|
return 1;
|
151
|
725
|
} else if (namespace2 != null) {
|
152
|
90
|
return -1;
|
153
|
|
}
|
154
|
2899
|
ISymbolspace symbolspace1 = c1.getSymbolspace();
|
155
|
2899
|
ISymbolspace symbolspace2 = c2.getSymbolspace();
|
156
|
2899
|
if ((symbolspace1 != null) && (symbolspace2 != null)) {
|
157
|
2899
|
res = symbolspace1.getString().compareTo(symbolspace2.getString());
|
158
|
2899
|
if (res != 0) {
|
159
|
764
|
return res;
|
160
|
|
}
|
161
|
0
|
} else if (symbolspace1 != null) {
|
162
|
0
|
return 1;
|
163
|
0
|
} else if (symbolspace2 != null) {
|
164
|
0
|
return -1;
|
165
|
|
}
|
166
|
2135
|
String name1 = c1.getName();
|
167
|
2135
|
String name2 = c2.getName();
|
168
|
2135
|
if ((name1 != null) && (name2 != null)) {
|
169
|
2103
|
res = name1.compareTo(name2);
|
170
|
2103
|
if (res != 0) {
|
171
|
2103
|
return res;
|
172
|
|
}
|
173
|
32
|
} else if (name1 != null) {
|
174
|
0
|
return 1;
|
175
|
32
|
} else if (name2 != null) {
|
176
|
0
|
return -1;
|
177
|
|
}
|
178
|
32
|
ILocation l1 = c1.getLocation();
|
179
|
32
|
ILocation l2 = c2.getLocation();
|
180
|
32
|
if ((l1 != null) && (l2 != null)) {
|
181
|
32
|
URL sysId1 = l1.getExpandedSystemId();
|
182
|
32
|
URL sysId2 = l2.getExpandedSystemId();
|
183
|
32
|
if ((sysId1 != null) && (sysId2 != null)) {
|
184
|
32
|
res = sysId1.toString().compareTo(sysId2.toString());
|
185
|
32
|
if (res != 0) {
|
186
|
0
|
return res;
|
187
|
|
}
|
188
|
0
|
} else if (sysId1 != null) {
|
189
|
0
|
return 1;
|
190
|
0
|
} else if (sysId2 != null) {
|
191
|
0
|
return -1;
|
192
|
|
}
|
193
|
32
|
int ln1 = l1.getLineNumber();
|
194
|
32
|
int ln2 = l2.getLineNumber();
|
195
|
32
|
if (ln1 < ln2) {
|
196
|
32
|
return -1;
|
197
|
0
|
} else if (ln1 > ln2) {
|
198
|
0
|
return 1;
|
199
|
|
}
|
200
|
0
|
int cn1 = l1.getColumnNumber();
|
201
|
0
|
int cn2 = l2.getColumnNumber();
|
202
|
0
|
if (cn1 < cn2) {
|
203
|
0
|
return -1;
|
204
|
0
|
} else if (cn1 > cn2) {
|
205
|
0
|
return 1;
|
206
|
|
}
|
207
|
|
|
208
|
0
|
} else if (l1 != null) {
|
209
|
0
|
return 1;
|
210
|
0
|
} else if (l2 != null) {
|
211
|
0
|
return -1;
|
212
|
|
}
|
213
|
|
|
214
|
0
|
return 0;
|
215
|
|
}
|
216
|
|
};
|
217
|
|
}
|
218
|
|
|