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.HashSet;
|
13
|
|
import java.util.Iterator;
|
14
|
|
import java.util.Set;
|
15
|
|
|
16
|
|
import org.jbind.xml.base.IRef;
|
17
|
|
import org.jbind.xml.base.ProcessContentsType;
|
18
|
|
import org.jbind.xml.core.cmp.IWildcard;
|
19
|
|
import org.jbind.xml.core.content.IDataRefOrDecl;
|
20
|
|
|
21
|
|
public class Wildcard implements IWildcard {
|
22
|
|
|
23
|
|
private boolean myIsNegative;
|
24
|
|
|
25
|
|
/**
|
26
|
|
* <i>(required)</i>. Contains namespace strings.
|
27
|
|
*/
|
28
|
|
private Set myNamespaces = new HashSet(17);
|
29
|
|
|
30
|
|
private ProcessContentsType myProcessContentsType = null;
|
31
|
|
|
32
|
486
|
public Wildcard(boolean anIsNegative, Set aNamespaces, ProcessContentsType aProcessContentsType) {
|
33
|
486
|
myIsNegative = anIsNegative;
|
34
|
486
|
myNamespaces = aNamespaces;
|
35
|
486
|
myProcessContentsType = aProcessContentsType;
|
36
|
|
}
|
37
|
|
|
38
|
186
|
public boolean getIsNegative() {
|
39
|
186
|
return myIsNegative;
|
40
|
|
}
|
41
|
|
|
42
|
265
|
public Set getNamespaces() {
|
43
|
265
|
return myNamespaces;
|
44
|
|
}
|
45
|
|
|
46
|
568
|
public ProcessContentsType getProcessContentsType() {
|
47
|
568
|
return myProcessContentsType;
|
48
|
|
}
|
49
|
|
|
50
|
343
|
public boolean isAllowed(IDataRefOrDecl aDecl) {
|
51
|
343
|
return isAllowed(aDecl.getGlobalRef());
|
52
|
|
}
|
53
|
|
|
54
|
753
|
public boolean isAllowed(IRef aRef) {
|
55
|
753
|
return isAllowed(aRef.getNamespace());
|
56
|
|
}
|
57
|
|
|
58
|
1056
|
public boolean isAllowed(String aNamespace) {
|
59
|
1056
|
boolean contains = myNamespaces.contains(aNamespace);
|
60
|
1056
|
return myIsNegative ? !contains : contains;
|
61
|
|
}
|
62
|
|
|
63
|
0
|
private Set intersect(Set aSet1, Set aSet2) {
|
64
|
0
|
Set res = new HashSet(17);
|
65
|
0
|
res.addAll(aSet1);
|
66
|
0
|
res.retainAll(aSet2);
|
67
|
0
|
return res;
|
68
|
|
}
|
69
|
|
|
70
|
13
|
private Set subtract(Set aSet1, Set aSet2) {
|
71
|
13
|
Set res = new HashSet(17);
|
72
|
13
|
res.addAll(aSet1);
|
73
|
13
|
res.removeAll(aSet2);
|
74
|
13
|
return res;
|
75
|
|
}
|
76
|
|
|
77
|
80
|
private Set union(Set aSet1, Set aSet2) {
|
78
|
80
|
Set res = new HashSet(17);
|
79
|
80
|
res.addAll(aSet1);
|
80
|
80
|
res.addAll(aSet2);
|
81
|
80
|
return res;
|
82
|
|
}
|
83
|
|
|
84
|
93
|
public IWildcard union(IWildcard aWildcard, ProcessContentsType aProcessContentsType) {
|
85
|
93
|
Set newSet = null;
|
86
|
93
|
boolean negative = true;
|
87
|
|
|
88
|
93
|
if (myIsNegative) {
|
89
|
13
|
if (aWildcard.getIsNegative()) {
|
90
|
|
// both wildcards are negative
|
91
|
0
|
newSet = intersect(myNamespaces, aWildcard.getNamespaces());
|
92
|
|
|
93
|
|
} else {
|
94
|
|
// this wildcard is negative and the other is positive
|
95
|
13
|
newSet = subtract(myNamespaces, aWildcard.getNamespaces());
|
96
|
|
|
97
|
|
}
|
98
|
|
} else {
|
99
|
80
|
if (aWildcard.getIsNegative()) {
|
100
|
|
// this wildcard is positive and the other one is negative
|
101
|
0
|
newSet = subtract(aWildcard.getNamespaces(), myNamespaces);
|
102
|
|
|
103
|
|
} else {
|
104
|
|
// both wildcards are positive
|
105
|
80
|
newSet = union(myNamespaces, aWildcard.getNamespaces());
|
106
|
80
|
negative = false;
|
107
|
|
|
108
|
|
}
|
109
|
|
}
|
110
|
|
|
111
|
93
|
return new Wildcard(negative, newSet, aProcessContentsType);
|
112
|
|
}
|
113
|
|
|
114
|
279
|
public IWildcard negate() {
|
115
|
279
|
return new Wildcard(!myIsNegative, myNamespaces, myProcessContentsType);
|
116
|
|
}
|
117
|
|
|
118
|
93
|
public IWildcard intersect(IWildcard aWildcard, ProcessContentsType aProcessContentsType) {
|
119
|
93
|
return negate().union(aWildcard.negate(), aProcessContentsType).negate();
|
120
|
|
}
|
121
|
|
|
122
|
93
|
public boolean isSubset(IWildcard aWildcard) {
|
123
|
93
|
IWildcard i = intersect(aWildcard, null);
|
124
|
93
|
return equals(i);
|
125
|
|
}
|
126
|
|
|
127
|
93
|
public boolean equals(IWildcard aWildcard) {
|
128
|
93
|
boolean res = myIsNegative == aWildcard.getIsNegative();
|
129
|
93
|
if (res) {
|
130
|
93
|
for (Iterator i = myNamespaces.iterator(); res && i.hasNext(); ) {
|
131
|
79
|
Object n = i.next();
|
132
|
79
|
res = aWildcard.getNamespaces().contains(n);
|
133
|
|
}
|
134
|
93
|
for (Iterator i = aWildcard.getNamespaces().iterator();
|
135
|
172
|
res && i.hasNext(); ) {
|
136
|
79
|
Object n = i.next();
|
137
|
79
|
res = myNamespaces.contains(n);
|
138
|
|
}
|
139
|
|
}
|
140
|
93
|
return res;
|
141
|
|
}
|
142
|
|
|
143
|
|
}
|
144
|
|
|