| 1 |
package com.thaiopensource.relaxng.output.xsd.basic; |
| 2 |
|
| 3 |
import com.thaiopensource.xml.util.Name; |
| 4 |
|
| 5 |
import java.util.Collections; |
| 6 |
import java.util.HashSet; |
| 7 |
import java.util.Set; |
| 8 |
|
| 9 |
public class Wildcard { |
| 10 |
private final boolean positive; |
| 11 |
private final Set<String> namespaces; |
| 12 |
private final Set<Name> excludedNames; |
| 13 |
|
| 14 |
public Wildcard(boolean positive, Set<String> namespaces, Set<Name> excludedNames) { |
| 15 |
this.positive = positive; |
| 16 |
this.namespaces = Collections.unmodifiableSet(namespaces); |
| 17 |
this.excludedNames = Collections.unmodifiableSet(excludedNames); |
| 18 |
} |
| 19 |
|
| 20 |
public boolean isPositive() { |
| 21 |
return positive; |
| 22 |
} |
| 23 |
|
| 24 |
public Set<String> getNamespaces() { |
| 25 |
return namespaces; |
| 26 |
} |
| 27 |
|
| 28 |
public Set<Name> getExcludedNames() { |
| 29 |
return excludedNames; |
| 30 |
} |
| 31 |
|
| 32 |
public boolean equals(Object obj) { |
| 33 |
if (!(obj instanceof Wildcard)) |
| 34 |
return false; |
| 35 |
Wildcard other = (Wildcard)obj; |
| 36 |
return (this.positive == other.positive |
| 37 |
&& this.namespaces.equals(other.namespaces) |
| 38 |
&& this.excludedNames.equals(other.excludedNames)); |
| 39 |
} |
| 40 |
|
| 41 |
public int hashCode() { |
| 42 |
return namespaces.hashCode() ^ excludedNames.hashCode(); |
| 43 |
} |
| 44 |
|
| 45 |
public boolean contains(Name name) { |
| 46 |
return namespaces.contains(name.getNamespaceUri()) == positive && !excludedNames.contains(name); |
| 47 |
} |
| 48 |
|
| 49 |
public static Wildcard union(Wildcard wc1, Wildcard wc2) { |
| 50 |
boolean positive; |
| 51 |
Set<String> namespaces = new HashSet<String>(); |
| 52 |
if (wc1.isPositive() && wc2.isPositive()) { |
| 53 |
positive = true; |
| 54 |
namespaces.addAll(wc1.getNamespaces()); |
| 55 |
namespaces.addAll(wc2.getNamespaces()); |
| 56 |
} |
| 57 |
else { |
| 58 |
positive = false; |
| 59 |
if (!wc1.isPositive() && !wc2.isPositive()) { |
| 60 |
namespaces.addAll(wc1.getNamespaces()); |
| 61 |
namespaces.retainAll(wc2.getNamespaces()); |
| 62 |
} |
| 63 |
else if (!wc1.isPositive()) { |
| 64 |
namespaces.addAll(wc1.getNamespaces()); |
| 65 |
namespaces.removeAll(wc2.getNamespaces()); |
| 66 |
} |
| 67 |
else { |
| 68 |
namespaces.addAll(wc2.getNamespaces()); |
| 69 |
namespaces.removeAll(wc1.getNamespaces()); |
| 70 |
} |
| 71 |
} |
| 72 |
Set<Name> excludedNames = new HashSet<Name>(); |
| 73 |
addExcludedNames(excludedNames, wc1, wc2); |
| 74 |
addExcludedNames(excludedNames, wc2, wc1); |
| 75 |
return new Wildcard(positive, namespaces, excludedNames); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Add to result all members of the excludedNames of wc1 that are not contained in wc2. |
| 80 |
*/ |
| 81 |
private static void addExcludedNames(Set<Name> result, Wildcard wc1, Wildcard wc2) { |
| 82 |
for (Name name : wc1.getExcludedNames()) { |
| 83 |
if (!wc2.contains(name)) |
| 84 |
result.add(name); |
| 85 |
} |
| 86 |
} |
| 87 |
} |