use of org.apache.directory.api.ldap.model.filter.SimpleNode in project directory-ldap-api by apache.
the class SearchRequestDsml method toDsml.
/**
* Recursively converts the filter of the Search Request into a DSML representation and adds
* it to the XML Element corresponding to the Search Request
*
* @param element
* the parent Element
* @param filter
* the filter to convert
*/
private void toDsml(Element element, ExprNode filter) {
// AND FILTER
if (filter instanceof AndNode) {
Element newElement = element.addElement("and");
List<ExprNode> filterList = ((AndNode) filter).getChildren();
for (int i = 0; i < filterList.size(); i++) {
toDsml(newElement, filterList.get(i));
}
} else // OR FILTER
if (filter instanceof OrNode) {
Element newElement = element.addElement("or");
List<ExprNode> filterList = ((OrNode) filter).getChildren();
for (int i = 0; i < filterList.size(); i++) {
toDsml(newElement, filterList.get(i));
}
} else // NOT FILTER
if (filter instanceof NotNode) {
Element newElement = element.addElement("not");
toDsml(newElement, ((NotNode) filter).getFirstChild());
} else // SUBSTRING FILTER
if (filter instanceof SubstringNode) {
Element newElement = element.addElement("substrings");
SubstringNode substringFilter = (SubstringNode) filter;
newElement.addAttribute(NAME, substringFilter.getAttribute());
String initial = substringFilter.getInitial();
if ((initial != null) && (!"".equals(initial))) {
newElement.addElement("initial").setText(initial);
}
List<String> anyList = substringFilter.getAny();
for (int i = 0; i < anyList.size(); i++) {
newElement.addElement("any").setText(anyList.get(i));
}
String finalString = substringFilter.getFinal();
if ((finalString != null) && (!"".equals(finalString))) {
newElement.addElement("final").setText(finalString);
}
} else // APPROXMATCH, EQUALITYMATCH, GREATEROREQUALS & LESSOREQUAL FILTERS
if (filter instanceof SimpleNode) {
Element newElement;
if (filter instanceof ApproximateNode) {
newElement = element.addElement("approxMatch");
} else if (filter instanceof EqualityNode) {
newElement = element.addElement("equalityMatch");
} else if (filter instanceof GreaterEqNode) {
newElement = element.addElement("greaterOrEqual");
} else // it is a LessEqNode )
{
newElement = element.addElement("lessOrEqual");
}
String attributeName = ((SimpleNode<?>) filter).getAttribute();
newElement.addAttribute(NAME, attributeName);
Value value = ((SimpleNode<?>) filter).getValue();
if (value != null) {
if (ParserUtils.needsBase64Encoding(value)) {
Namespace xsdNamespace = new Namespace("xsd", ParserUtils.XML_SCHEMA_URI);
Namespace xsiNamespace = new Namespace("xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI);
element.getDocument().getRootElement().add(xsdNamespace);
element.getDocument().getRootElement().add(xsiNamespace);
Element valueElement = newElement.addElement(VALUE).addText(ParserUtils.base64Encode(value));
valueElement.addAttribute(new QName("type", xsiNamespace), "xsd:" + ParserUtils.BASE64BINARY);
} else {
newElement.addElement(VALUE).setText(value.getValue());
}
}
} else // PRESENT FILTER
if (filter instanceof PresenceNode) {
Element newElement = element.addElement("present");
newElement.addAttribute(NAME, ((PresenceNode) filter).getAttribute());
} else // EXTENSIBLEMATCH
if (filter instanceof ExtensibleNode) {
Element newElement = element.addElement("extensibleMatch");
Value value = ((ExtensibleNode) filter).getValue();
if (value != null) {
if (ParserUtils.needsBase64Encoding(value)) {
Namespace xsdNamespace = new Namespace("xsd", ParserUtils.XML_SCHEMA_URI);
Namespace xsiNamespace = new Namespace("xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI);
element.getDocument().getRootElement().add(xsdNamespace);
element.getDocument().getRootElement().add(xsiNamespace);
Element valueElement = newElement.addElement(VALUE).addText(ParserUtils.base64Encode(value.getValue()));
valueElement.addAttribute(new QName("type", xsiNamespace), "xsd:" + ParserUtils.BASE64BINARY);
} else {
newElement.addElement(VALUE).setText(value.getValue());
}
}
if (((ExtensibleNode) filter).hasDnAttributes()) {
newElement.addAttribute("dnAttributes", "true");
}
String matchingRule = ((ExtensibleNode) filter).getMatchingRuleId();
if ((matchingRule != null) && ("".equals(matchingRule))) {
newElement.addAttribute("matchingRule", matchingRule);
}
}
}
use of org.apache.directory.api.ldap.model.filter.SimpleNode in project structr by structr.
the class StructrLDAPWrapper method matches.
private boolean matches(final LDAPNode node, final ExprNode filter) throws FrameworkException, LdapInvalidAttributeValueException {
if (filter instanceof SimpleNode) {
return evaluateSimpleNode(node, (SimpleNode) filter);
} else if (filter instanceof SubstringNode) {
return evaluateSubstringNode(node, (SubstringNode) filter);
} else if (filter instanceof PresenceNode) {
final PresenceNode presence = (PresenceNode) filter;
final Attribute attribute = new DefaultAttribute(presence.getAttributeType());
return findAttribute(node, attribute.getId()) != null;
} else if (filter instanceof OrNode) {
final OrNode orNode = (OrNode) filter;
for (final ExprNode child : orNode.getChildren()) {
if (matches(node, child)) {
return true;
}
}
return false;
} else if (filter instanceof AndNode) {
final AndNode andNode = (AndNode) filter;
boolean result = true;
for (final ExprNode child : andNode.getChildren()) {
result &= matches(node, child);
}
return result;
} else {
System.out.println("Unsupported filter type " + filter.getClass());
}
return false;
}
Aggregations