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
|
|
|
14
|
|
import org.jbind.xml.base.Compositor;
|
15
|
|
import org.jbind.xml.base.IRange;
|
16
|
|
import org.jbind.xml.base.IRef;
|
17
|
|
import org.jbind.xml.base.ISymbolspace;
|
18
|
|
import org.jbind.xml.base.ISymbolspaces;
|
19
|
|
import org.jbind.xml.base.Range;
|
20
|
|
import org.jbind.xml.core.cmp.ISourceInfo;
|
21
|
|
import org.jbind.xml.core.content.IContentDesc;
|
22
|
|
import org.jbind.xml.core.content.IElemGroupDesc;
|
23
|
|
import org.jbind.xml.core.content.IUseState;
|
24
|
|
import org.jbind.xml.instance.use.GroupUseStateProxy;
|
25
|
|
|
26
|
|
public abstract class ElemGroupDesc extends Content implements IElemGroupDesc {
|
27
|
|
|
28
|
2560
|
public ElemGroupDesc(ISourceInfo aSourceInfo, String aNamespace, String aName, IRange aRange) {
|
29
|
2560
|
super(aSourceInfo, aNamespace, aName, aRange);
|
30
|
|
}
|
31
|
|
|
32
|
1385
|
public final IUseState createUseStateProxy(IUseState aParent) {
|
33
|
1385
|
return new GroupUseStateProxy(this, aParent);
|
34
|
|
}
|
35
|
|
|
36
|
9177
|
public final IRange getRange(IRef aRef) {
|
37
|
9177
|
IRange res;
|
38
|
9177
|
IRange innerRange = getInnerRange(aRef);
|
39
|
9177
|
int min = getRange().getMinOccurs() * innerRange.getMinOccurs();
|
40
|
9177
|
if ((getRange().isUnbounded() && !innerRange.isZero()) || (!getRange().isZero() && innerRange.isUnbounded())) {
|
41
|
3196
|
res = new Range(min);
|
42
|
|
} else {
|
43
|
5981
|
if (getRange().isUnbounded() || innerRange.isUnbounded()) {
|
44
|
|
assert min == 0;
|
45
|
680
|
res = Range.ZERO;
|
46
|
|
} else {
|
47
|
5301
|
int max = getRange().getMaxOccurs() * innerRange.getMaxOccurs();
|
48
|
5301
|
res = new Range(min, max);
|
49
|
|
}
|
50
|
|
}
|
51
|
9177
|
return res;
|
52
|
|
}
|
53
|
|
|
54
|
896
|
public ISymbolspace getSymbolspace() {
|
55
|
896
|
return ISymbolspaces.ELEMENT_GROUP;
|
56
|
|
}
|
57
|
|
|
58
|
22
|
protected boolean isPointlessAll(IElemGroupDesc aParent) {
|
59
|
22
|
return getNbParticles() <= 1;
|
60
|
|
}
|
61
|
|
|
62
|
338
|
protected boolean isPointlessChoice(IElemGroupDesc aParent) {
|
63
|
338
|
boolean res = (0 == getNbParticles()) && (0 == aParent.getRange().getMinOccurs());
|
64
|
338
|
if (!res) {
|
65
|
338
|
res = getRange().isOne() && ((1 == getNbParticles()) || Compositor.CHOICE.equals(aParent.getCompositor()));
|
66
|
|
}
|
67
|
338
|
return res;
|
68
|
|
}
|
69
|
|
|
70
|
592
|
protected boolean isPointlessSequence(IElemGroupDesc aParent) {
|
71
|
592
|
boolean res = 0 == getNbParticles();
|
72
|
592
|
if (!res) {
|
73
|
592
|
res = getRange().isOne() && ((1 == getNbParticles()) || Compositor.SEQUENCE.equals(aParent.getCompositor()));
|
74
|
|
}
|
75
|
592
|
return res;
|
76
|
|
}
|
77
|
|
|
78
|
240
|
public IElemGroupDesc replaceSubstitutionsByChoice() {
|
79
|
240
|
IElemGroupDesc parent = new AllDecl(null, null, null, Range.ONE);
|
80
|
240
|
replaceSubstitutions(parent);
|
81
|
240
|
Iterator i = parent.iterContent();
|
82
|
240
|
IElemGroupDesc res = (IElemGroupDesc)i.next();
|
83
|
|
assert !i.hasNext();
|
84
|
240
|
return res;
|
85
|
|
}
|
86
|
|
|
87
|
368
|
public void replaceSubstitutions(IElemGroupDesc aParent) {
|
88
|
|
// replace this group by a copy because the original must not be modified
|
89
|
368
|
IElemGroupDesc group = createGroup();
|
90
|
368
|
aParent.add(group);
|
91
|
368
|
for (Iterator i = iterContent(); i.hasNext(); ) {
|
92
|
614
|
IContentDesc c = (IContentDesc)i.next();
|
93
|
614
|
c.replaceSubstitutions(group);
|
94
|
|
}
|
95
|
|
}
|
96
|
|
|
97
|
480
|
public IContentDesc removePointlessGroups() {
|
98
|
480
|
IContentDesc res = this;
|
99
|
|
|
100
|
480
|
boolean changed;
|
101
|
480
|
do {
|
102
|
910
|
AllDecl parent = new AllDecl(null, null, null, Range.ONE);
|
103
|
910
|
changed = res.addToParent(parent);
|
104
|
910
|
if (changed) {
|
105
|
430
|
Iterator i = parent.iterContent();
|
106
|
430
|
if (i.hasNext()) {
|
107
|
430
|
res = (IContentDesc)i.next();
|
108
|
|
assert !i.hasNext();
|
109
|
|
} else {
|
110
|
|
// The content model is completely empty.
|
111
|
|
// In this case return the result from the last iteration.
|
112
|
0
|
break;
|
113
|
|
}
|
114
|
|
}
|
115
|
910
|
} while (changed);
|
116
|
|
|
117
|
480
|
return res;
|
118
|
|
}
|
119
|
|
|
120
|
952
|
public boolean addToParent(IElemGroupDesc aParent) {
|
121
|
952
|
boolean res = false;
|
122
|
952
|
IElemGroupDesc parent;
|
123
|
952
|
if (isPointless(aParent)) {
|
124
|
398
|
res = true;
|
125
|
398
|
parent = aParent;
|
126
|
|
} else {
|
127
|
554
|
parent = createGroup();
|
128
|
554
|
aParent.add(parent);
|
129
|
|
}
|
130
|
952
|
for (Iterator i = iterContent(); i.hasNext(); ) {
|
131
|
2123
|
IContentDesc c = (IContentDesc)i.next();
|
132
|
2123
|
res |= c.addToParent(parent);
|
133
|
|
}
|
134
|
952
|
return res;
|
135
|
|
}
|
136
|
|
|
137
|
|
}
|
138
|
|
|