1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml.signature.validator;
18
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import javax.xml.namespace.QName;
23
24 import org.opensaml.xml.XMLObject;
25 import org.opensaml.xml.signature.PGPData;
26 import org.opensaml.xml.signature.PGPKeyID;
27 import org.opensaml.xml.signature.PGPKeyPacket;
28 import org.opensaml.xml.util.XMLConstants;
29 import org.opensaml.xml.validation.ValidationException;
30 import org.opensaml.xml.validation.Validator;
31
32
33
34
35 public class PGPDataSchemaValidator implements Validator<PGPData> {
36
37
38 private static final Set<QName> VALID_DS_CHILD_NAMES;
39
40
41 public void validate(PGPData xmlObject) throws ValidationException {
42 validateChildrenPresence(xmlObject);
43 validateChildrenNamespaces(xmlObject);
44 }
45
46
47
48
49
50
51
52 protected static Set<QName> getValidDSChildNames() {
53 return VALID_DS_CHILD_NAMES;
54 }
55
56
57
58
59
60
61
62 protected void validateChildrenPresence(PGPData xmlObject) throws ValidationException {
63 if (xmlObject.getPGPKeyID() == null && xmlObject.getPGPKeyPacket() == null) {
64 throw new ValidationException("PGPData must contain at least one of PGPKeyID or PGPKeyPacket");
65 }
66 }
67
68
69
70
71
72
73
74
75 protected void validateChildrenNamespaces(PGPData xmlObject) throws ValidationException {
76
77 for (XMLObject child : xmlObject.getUnknownXMLObjects()) {
78 QName childName = child.getElementQName();
79 if (! getValidDSChildNames().contains(childName)
80 && XMLConstants.XMLSIG_NS.equals(childName.getNamespaceURI())) {
81 throw new ValidationException("PGPData contains an illegal child extension element: " + childName);
82 }
83 }
84 }
85
86 static {
87 VALID_DS_CHILD_NAMES = new HashSet<QName>(5);
88 VALID_DS_CHILD_NAMES.add(PGPKeyID.DEFAULT_ELEMENT_NAME);
89 VALID_DS_CHILD_NAMES.add(PGPKeyPacket.DEFAULT_ELEMENT_NAME);
90 }
91 }