Policy getElementPolicy(Extensible ex, boolean includeAttributes, DescriptionInfo di) {
if (null == ex || null == di) {
return null;
}
Policy elementPolicy = null;
List<UnknownExtensibilityElement> extensions =
ex.getExtensors(UnknownExtensibilityElement.class);
if (null != extensions) {
for (UnknownExtensibilityElement e : extensions) {
Policy p = null;
if (PolicyConstants.isPolicyElem(e.getElementType())) {
p = builder.getPolicy(e.getElement());
} else if (PolicyConstants.isPolicyRefElem(e.getElementType())) {
PolicyReference ref = builder.getPolicyReference(e.getElement());
if (null != ref) {
p = resolveReference(ref, di);
}
}
if (null != p) {
if (elementPolicy == null) {
elementPolicy = new Policy();
}
elementPolicy = elementPolicy.merge(p);
}
}
}
if (includeAttributes && ex.getExtensionAttributes() != null) {
for (Map.Entry<QName, Object> ent : ex.getExtensionAttributes().entrySet()) {
if (PolicyConstants.isPolicyURIsAttr(ent.getKey())) {
Object attr = ent.getValue();
// can be of type a String, a QName, a list of Srings or a list of QNames
String uris = null;
if (attr instanceof QName) {
uris = ((QName)attr).getLocalPart();
} else if (attr instanceof String) {
uris = (String)attr;
}
if (null != uris) {
StringTokenizer st = new StringTokenizer(uris);
while (st.hasMoreTokens()) {
String uri = st.nextToken();
PolicyReference ref = new PolicyReference();
ref.setURI(uri);
Policy p = resolveReference(ref, di);
if (null != p) {
elementPolicy = elementPolicy == null
? new Policy().merge(p) : elementPolicy.merge(p);
}
}
}
}
}