Reviewed javadoc.

This commit is contained in:
Ulrik Sandberg
2007-10-08 21:51:56 +00:00
parent 42c2e80e85
commit a1888ab5e1
12 changed files with 354 additions and 369 deletions

View File

@@ -24,35 +24,28 @@ package org.springframework.ldap.filter;
*/
public abstract class AbstractFilter implements Filter {
protected AbstractFilter() {
super();
}
protected AbstractFilter() {
super();
}
/**
* Prints the query with LDAP encoding to a stringbuffer
*
* @param buff
* The stringbuffer
* @return The very same stringbuffer
*/
public abstract StringBuffer encode(StringBuffer buff);
/*
* @see org.springframework.ldap.filter.Filter#encode(java.lang.StringBuffer)
*/
public abstract StringBuffer encode(StringBuffer buff);
/**
* Encodes the filter to a string using the (@link #encode(StringBuffer)
* method.
*
* @return The encoded filter
*/
public String encode() {
StringBuffer buff = new StringBuffer(256);
buff = encode(buff);
return buff.toString();
}
/*
* @see org.springframework.ldap.filter.Filter#encode()
*/
public String encode() {
StringBuffer buf = new StringBuffer(256);
buf = encode(buf);
return buf.toString();
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
return encode();
}
/*
* @see java.lang.Object#toString()
*/
public String toString() {
return encode();
}
}

View File

@@ -17,7 +17,7 @@
package org.springframework.ldap.filter;
/**
* A filter for a logical AND. E.g.:
* A filter for a logical AND. Example:
*
* <pre>
* AndFilter filter = new AndFilter();
@@ -34,26 +34,24 @@ package org.springframework.ldap.filter;
*/
public class AndFilter extends BinaryLogicalFilter {
private static final String AMPERSAND = "&";
private static final String AMPERSAND = "&";
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.filter.BinaryLogicalFilter#getLogicalOperator()
*/
protected String getLogicalOperator() {
return AMPERSAND;
}
/*
* @see org.springframework.ldap.filter.BinaryLogicalFilter#getLogicalOperator()
*/
protected String getLogicalOperator() {
return AMPERSAND;
}
/**
* Add a query to the and expression
*
* @param query
* The query to and with the rest of the and:ed queries.
* @return This LdapAndQuery
*/
public AndFilter and(Filter query) {
queryList.add(query);
return this;
}
/**
* Add a query to the AND expression.
*
* @param query The expression to AND with the rest of the AND:ed
* expressions.
* @return This LdapAndQuery
*/
public AndFilter and(Filter query) {
queryList.add(query);
return this;
}
}

View File

@@ -24,72 +24,74 @@ import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* Abstract superclass for binary logical operations, that is &quot;and&quot;
* and &quot;or&quot; operations.
* Abstract superclass for binary logical operations, that is &quot;AND&quot;
* and &quot;OR&quot; operations.
*
* @author Mattias Arthursson
*/
public abstract class BinaryLogicalFilter extends AbstractFilter {
protected List queryList = new LinkedList();
protected List queryList = new LinkedList();
/**
* @see org.springframework.ldap.filter.Filter#encode(java.lang.StringBuffer)
*/
public StringBuffer encode(StringBuffer buff) {
if (queryList.size() <= 0) {
/*
* @see org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer)
*/
public StringBuffer encode(StringBuffer buff) {
if (queryList.size() <= 0) {
// only output query if contains anything
return buff;
// only output query if contains anything
return buff;
} else if (queryList.size() == 1) {
}
else if (queryList.size() == 1) {
// don't add the &
Filter query = (Filter) queryList.get(0);
return query.encode(buff);
// don't add the &
Filter query = (Filter) queryList.get(0);
return query.encode(buff);
} else {
buff.append("(" + getLogicalOperator());
}
else {
buff.append("(" + getLogicalOperator());
for (Iterator i = queryList.iterator(); i.hasNext();) {
Filter query = (Filter) i.next();
buff = query.encode(buff);
}
for (Iterator i = queryList.iterator(); i.hasNext();) {
Filter query = (Filter) i.next();
buff = query.encode(buff);
}
buff.append(")");
buff.append(")");
return buff;
}
}
return buff;
}
}
/**
* Implement this in subclass to return the logical operator, for example
* &qout;&amp;&qout;.
*
* @return the logical operator.
*/
protected abstract String getLogicalOperator();
/**
* Implement this in subclass to return the logical operator, for example
* &qout;&amp;&qout;.
*
* @return the logical operator.
*/
protected abstract String getLogicalOperator();
/**
* Compares each filter in turn
*
* @see org.springframework.ldap.filter.Filter#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj instanceof BinaryLogicalFilter
&& this.getClass() == obj.getClass()) {
return EqualsBuilder.reflectionEquals(this, obj);
} else {
return false;
}
}
/**
* Compares each filter in turn.
*
* @see org.springframework.ldap.filter.Filter#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj instanceof BinaryLogicalFilter && this.getClass() == obj.getClass()) {
return EqualsBuilder.reflectionEquals(this, obj);
}
else {
return false;
}
}
/**
* Hashes all contained data
*
* @see org.springframework.ldap.filter.Filter#hashCode()
*/
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
/**
* Hashes all contained data.
*
* @see org.springframework.ldap.filter.Filter#hashCode()
*/
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
}

View File

@@ -21,103 +21,103 @@ import org.apache.commons.lang.builder.HashCodeBuilder;
import org.springframework.ldap.core.LdapEncoder;
/**
* Abstract superclass for filters to compare values.
* Abstract superclass for filters that compare values.
*
* @author Mattias Arthursson
*/
public abstract class CompareFilter extends AbstractFilter {
private final String attribute;
private final String attribute;
private final String value;
private final String value;
private final String encodedValue;
private final String encodedValue;
public CompareFilter(String attribute, String value) {
this.attribute = attribute;
this.value = value;
this.encodedValue = encodeValue(value);
}
public CompareFilter(String attribute, String value) {
this.attribute = attribute;
this.value = value;
this.encodedValue = encodeValue(value);
}
/**
* For testing purposes.
*
* @return the encoded value.
*/
String getEncodedValue() {
return encodedValue;
}
/**
* For testing purposes.
*
* @return the encoded value.
*/
String getEncodedValue() {
return encodedValue;
}
/**
* Override to perform special encoding in subclass.
*
* @param value
* the value to encode.
* @return properly escaped value.
*/
protected String encodeValue(String value) {
return LdapEncoder.filterEncode(value);
}
/**
* Override to perform special encoding in subclass.
*
* @param value the value to encode.
* @return properly escaped value.
*/
protected String encodeValue(String value) {
return LdapEncoder.filterEncode(value);
}
/**
* Convenience constructor for int values.
*
* @param attribute
* @param value
*/
public CompareFilter(String attribute, int value) {
this.attribute = attribute;
this.value = String.valueOf(value);
this.encodedValue = LdapEncoder.filterEncode(this.value);
}
/**
* Convenience constructor for <code>int</code> values.
*
* @param attribute Name of attribute in filter.
* @param value The value of the attribute in the filter.
*/
public CompareFilter(String attribute, int value) {
this.attribute = attribute;
this.value = String.valueOf(value);
this.encodedValue = LdapEncoder.filterEncode(this.value);
}
/*
* @see org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer)
*/
public StringBuffer encode(StringBuffer buff) {
buff.append('(');
buff.append(attribute).append(getCompareString()).append(encodedValue);
buff.append(')');
/*
* @see org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer)
*/
public StringBuffer encode(StringBuffer buff) {
buff.append('(');
buff.append(attribute).append(getCompareString()).append(encodedValue);
buff.append(')');
return buff;
}
return buff;
}
/**
* Compares key and value before encoding.
*
* @see org.springframework.ldap.filter.Filter#equals(java.lang.Object)
*/
public boolean equals(Object o) {
if (o instanceof CompareFilter && o.getClass() == this.getClass()) {
CompareFilter that = (CompareFilter) o;
EqualsBuilder builder = new EqualsBuilder();
builder.append(this.attribute, that.attribute);
builder.append(this.value, that.value);
return builder.isEquals();
} else {
return false;
}
}
/**
* Compares key and value before encoding.
*
* @see org.springframework.ldap.filter.Filter#equals(java.lang.Object)
*/
public boolean equals(Object o) {
if (o instanceof CompareFilter && o.getClass() == this.getClass()) {
CompareFilter that = (CompareFilter) o;
EqualsBuilder builder = new EqualsBuilder();
builder.append(this.attribute, that.attribute);
builder.append(this.value, that.value);
return builder.isEquals();
}
else {
return false;
}
}
/**
* Calculate the hash code for the attribute and the value.
*
* @see org.springframework.ldap.filter.Filter#hashCode()
*/
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(attribute);
builder.append(value);
return builder.toHashCode();
}
/**
* Calculate the hash code for the attribute and the value.
*
* @see org.springframework.ldap.filter.Filter#hashCode()
*/
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(attribute);
builder.append(value);
return builder.toHashCode();
}
/**
* Implement this method in subclass to return a String representing the
* operator. The {@link EqualsFilter#getCompareString()} would for example
* return an equals sign, &quot;=&quot;.
*
* @return the String to use as operator in the comparison for the specific
* subclass.
*/
protected abstract String getCompareString();
/**
* Implement this method in subclass to return a String representing the
* operator. The {@link EqualsFilter#getCompareString()} would for example
* return an equals sign, &quot;=&quot;.
*
* @return the String to use as operator in the comparison for the specific
* subclass.
*/
protected abstract String getCompareString();
}

View File

@@ -16,7 +16,6 @@
package org.springframework.ldap.filter;
/**
* A filter for 'equals'. The following code:
*
@@ -26,32 +25,35 @@ package org.springframework.ldap.filter;
* </pre>
*
* would result in:
* <pre>(cn=Some CN)</pre>
*
* <pre>
* (cn=Some CN)
* </pre>
*
* @author Adam Skogman
*/
public class EqualsFilter extends CompareFilter {
private static final String EQUALS_SIGN = "=";
private static final String EQUALS_SIGN = "=";
public EqualsFilter(String attribute, String value) {
super(attribute, value);
}
public EqualsFilter(String attribute, String value) {
super(attribute, value);
}
/**
* Convenience constructor for int values.
*
* @param attribute Name of attribute in filter.
* @param value The value of the attribute in the filter.
*/
public EqualsFilter(String attribute, int value) {
super(attribute, value);
}
/**
* Convenience constructor for int values.
*
* @param attribute Name of attribute in filter.
* @param value The value of the attribute in the filter.
*/
public EqualsFilter(String attribute, int value) {
super(attribute, value);
}
/*
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
*/
protected String getCompareString() {
return EQUALS_SIGN;
}
/*
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
*/
protected String getCompareString() {
return EQUALS_SIGN;
}
}

View File

@@ -17,28 +17,28 @@
package org.springframework.ldap.filter;
/**
* Common interface for filters.
* Common interface for LDAP filters.
*
* @author Adam Skogman
* @see http://www.ietf.org/rfc/rfc1960.txt
*/
public interface Filter {
/**
* Encodes the filter to a string using the (@link #encode(StringBuffer)
* method.
* Encodes the filter to a String.
*
* @return The encoded filter
* @return The encoded filter in the standard String format
*/
public String encode();
/**
* Prints the query with LDAP encoding to a stringbuffer
* Encodes the filter to a StringBuffer.
*
* @param buff
* The stringbuffer
* @return The very same stringbuffer
* @param buf
* The StringBuffer to encode the filter to
* @return The same StringBuffer as was given
*/
public StringBuffer encode(StringBuffer buff);
public StringBuffer encode(StringBuffer buf);
/**
* All filters must implement equals.
@@ -49,9 +49,10 @@ public interface Filter {
public boolean equals(Object o);
/**
* All filters must implement hashCode()
* All filters must implement hashCode.
*
* @return hascode
* @return the hash code according to the contract in
* {@link Object#hashCode()}
*/
public int hashCode();

View File

@@ -21,8 +21,7 @@ package org.springframework.ldap.filter;
* code:
*
* <pre>
* GreaterThanOrEqualsFilter filter = new GreaterThanOrEqualsFilter(&quot;cn&quot;,
* &quot;Some CN&quot;);
* GreaterThanOrEqualsFilter filter = new GreaterThanOrEqualsFilter(&quot;cn&quot;, &quot;Some CN&quot;);
* System.out.println(filter.ecode());
* </pre>
*
@@ -36,17 +35,20 @@ package org.springframework.ldap.filter;
*/
public class GreaterThanOrEqualsFilter extends CompareFilter {
private static final String GREATER_THAN_OR_EQUALS = ">=";
private static final String GREATER_THAN_OR_EQUALS = ">=";
public GreaterThanOrEqualsFilter(String attribute, String value) {
super(attribute, value);
}
public GreaterThanOrEqualsFilter(String attribute, String value) {
super(attribute, value);
}
public GreaterThanOrEqualsFilter(String attribute, int value) {
super(attribute, value);
}
public GreaterThanOrEqualsFilter(String attribute, int value) {
super(attribute, value);
}
protected String getCompareString() {
return GREATER_THAN_OR_EQUALS;
}
/*
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
*/
protected String getCompareString() {
return GREATER_THAN_OR_EQUALS;
}
}

View File

@@ -35,17 +35,20 @@ package org.springframework.ldap.filter;
*/
public class LessThanOrEqualsFilter extends CompareFilter {
private static final String LESS_THAN_OR_EQUALS = "<=";
private static final String LESS_THAN_OR_EQUALS = "<=";
public LessThanOrEqualsFilter(String attribute, String value) {
super(attribute, value);
}
public LessThanOrEqualsFilter(String attribute, String value) {
super(attribute, value);
}
public LessThanOrEqualsFilter(String attribute, int value) {
super(attribute, value);
}
public LessThanOrEqualsFilter(String attribute, int value) {
super(attribute, value);
}
protected String getCompareString() {
return LESS_THAN_OR_EQUALS;
}
/*
* @see org.springframework.ldap.filter.CompareFilter#getCompareString()
*/
protected String getCompareString() {
return LESS_THAN_OR_EQUALS;
}
}

View File

@@ -38,41 +38,38 @@ import org.springframework.ldap.core.LdapEncoder;
*/
public class LikeFilter extends EqualsFilter {
public LikeFilter(String attribute, String value) {
super(attribute, value);
}
public LikeFilter(String attribute, String value) {
super(attribute, value);
}
/**
* Encodes a value according to the rules for this filter.
*
* @param value
* Value to encode.
* @return Encoded value.
*/
protected String encodeValue(String value) {
// just return if blank string
if (value == null) {
return "";
}
/*
* @see org.springframework.ldap.filter.CompareFilter#encodeValue(java.lang.String)
*/
protected String encodeValue(String value) {
// just return if blank string
if (value == null) {
return "";
}
String[] substrings = value.split("\\*", -2);
String[] substrings = value.split("\\*", -2);
if (substrings.length == 1) {
return LdapEncoder.filterEncode(substrings[0]);
}
if (substrings.length == 1) {
return LdapEncoder.filterEncode(substrings[0]);
}
StringBuffer buff = new StringBuffer();
for (int i = 0; i < substrings.length; i++) {
buff.append(LdapEncoder.filterEncode(substrings[i]));
if (i < substrings.length - 1) {
buff.append("*");
} else {
if (substrings[i].equals("")) {
continue;
}
}
}
StringBuffer buff = new StringBuffer();
for (int i = 0; i < substrings.length; i++) {
buff.append(LdapEncoder.filterEncode(substrings[i]));
if (i < substrings.length - 1) {
buff.append("*");
}
else {
if (substrings[i].equals("")) {
continue;
}
}
}
return buff.toString();
}
return buff.toString();
}
}

View File

@@ -23,67 +23,62 @@ import org.apache.commons.lang.Validate;
*
* <pre>
* Filter filter = new NotFilter(new EqualsFilter(&quot;cn&quot;, &quot;foo&quot;);
* System.out.println(filter.ecode());
* System.out.println(filter.encode());
* </pre>
*
* would result in:
*
* <pre>(!(cn=foo))</pre>
* <pre>
* (!(cn = foo))
* </pre>
*
* @author Adam Skogman
*/
public class NotFilter extends AbstractFilter {
private final Filter filter;
private final Filter filter;
static private final int HASH = "!".hashCode();
static private final int HASH = "!".hashCode();
/**
* Create a filter that negates the outcome of the given <code>filter</code>.
*
* @param filter
* The filter that should be negated.
*/
public NotFilter(Filter filter) {
Validate.notNull(filter);
this.filter = filter;
}
/**
* Create a filter that negates the outcome of the given <code>filter</code>.
*
* @param filter The filter that should be negated.
*/
public NotFilter(Filter filter) {
Validate.notNull(filter);
this.filter = filter;
}
/**
* @see org.springframework.ldap.filter.Filter#encode(java.lang.StringBuffer)
*/
public StringBuffer encode(StringBuffer buff) {
/*
* @see org.springframework.ldap.filter.AbstractFilter#encode(java.lang.StringBuffer)
*/
public StringBuffer encode(StringBuffer buff) {
buff.append("(!");
filter.encode(buff);
buff.append(')');
buff.append("(!");
filter.encode(buff);
buff.append(')');
return buff;
return buff;
}
}
/*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o) {
/**
* Compares key and value before encoding
*
* @see org.springframework.ldap.filter.Filter#equals(java.lang.Object)
*/
public boolean equals(Object o) {
if (o instanceof NotFilter && o.getClass() == this.getClass()) {
NotFilter f = (NotFilter) o;
return this.filter.equals(f.filter);
}
if (o instanceof NotFilter && o.getClass() == this.getClass()) {
NotFilter f = (NotFilter) o;
return this.filter.equals(f.filter);
}
return false;
}
/**
* hash attribute and value
*
* @see org.springframework.ldap.filter.Filter#hashCode()
*/
public int hashCode() {
return HASH ^ filter.hashCode();
}
return false;
}
/*
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return HASH ^ filter.hashCode();
}
}

View File

@@ -34,26 +34,23 @@ package org.springframework.ldap.filter;
*/
public class OrFilter extends BinaryLogicalFilter {
private static final String PIPE_SIGN = "|";
private static final String PIPE_SIGN = "|";
/**
* Add a query to the OR expression
*
* @param query
* The query to or with the rest of the or:ed queries.
* @return This LdapOrQuery
*/
public OrFilter or(Filter query) {
queryList.add(query);
return this;
}
/**
* Add a query to the OR expression
*
* @param query The query to or with the rest of the or:ed queries.
* @return This LdapOrQuery
*/
public OrFilter or(Filter query) {
queryList.add(query);
return this;
}
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.filter.BinaryLogicalFilter#getLogicalOperator()
*/
protected String getLogicalOperator() {
return PIPE_SIGN;
}
/*
* @see org.springframework.ldap.filter.BinaryLogicalFilter#getLogicalOperator()
*/
protected String getLogicalOperator() {
return PIPE_SIGN;
}
}

View File

@@ -27,8 +27,7 @@ import org.springframework.ldap.core.LdapEncoder;
* following code:
*
* <pre>
* WhitespaceWildcardsFilter filter = new WhitespaceWildcardsFilter(&quot;cn&quot;,
* &quot;Some CN&quot;);
* WhitespaceWildcardsFilter filter = new WhitespaceWildcardsFilter(&quot;cn&quot;, &quot;Some CN&quot;);
* System.out.println(filter.ecode());
* </pre>
*
@@ -38,47 +37,43 @@ import org.springframework.ldap.core.LdapEncoder;
* @author Mattias Arthursson
*/
public class WhitespaceWildcardsFilter extends EqualsFilter {
private static Pattern starReplacePattern = Pattern.compile("\\s+");
private static Pattern starReplacePattern = Pattern.compile("\\s+");
public WhitespaceWildcardsFilter(String attribute, String value) {
super(attribute, value);
}
public WhitespaceWildcardsFilter(String attribute, String value) {
super(attribute, value);
}
/**
* Encodes a value according to the rules for this filter.
*
* @param value
* Value to encode.
* @return Encoded value.
*/
protected String encodeValue(String value) {
/*
* @see org.springframework.ldap.filter.CompareFilter#encodeValue(java.lang.String)
*/
protected String encodeValue(String value) {
// blank string means just ONE star
if (StringUtils.isBlank(value)) {
return "*";
}
// blank string means just ONE star
if (StringUtils.isBlank(value)) {
return "*";
}
// trim value, we will add in stars first and last anywhay
value = value.trim();
// trim value, we will add in stars first and last anywhay
value = value.trim();
// filter encode so that any stars etc. are preserved
String filterEncoded = LdapEncoder.filterEncode(value);
// filter encode so that any stars etc. are preserved
String filterEncoded = LdapEncoder.filterEncode(value);
// Now replace all whitespace with stars
Matcher m = starReplacePattern.matcher(filterEncoded);
// Now replace all whitespace with stars
Matcher m = starReplacePattern.matcher(filterEncoded);
// possibly 2 longer (stars at ends)
StringBuffer buff = new StringBuffer(value.length() + 2);
// possibly 2 longer (stars at ends)
StringBuffer buff = new StringBuffer(value.length() + 2);
buff.append('*');
buff.append('*');
while (m.find()) {
m.appendReplacement(buff, "*");
}
m.appendTail(buff);
while (m.find()) {
m.appendReplacement(buff, "*");
}
m.appendTail(buff);
buff.append('*');
buff.append('*');
return buff.toString();
}
return buff.toString();
}
}