Search in sources :

Example 6 with NoOpNormalizer

use of org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer in project directory-ldap-api by apache.

the class Value method computeNormValue.

/**
 * Compute the normalized value
 *
 * @throws LdapException If we were'nt able to normalize the value
 */
private void computeNormValue() throws LdapException {
    if (upValue == null) {
        return;
    }
    Normalizer normalizer;
    // We should have a Equality MatchingRule
    MatchingRule equality = attributeType.getEquality();
    if (equality == null) {
        // Let's try with the Substring MatchingRule
        MatchingRule subString = attributeType.getSubstring();
        if (subString == null) {
            // last chance : ordering matching rule
            MatchingRule ordering = attributeType.getOrdering();
            if (ordering == null) {
                // Ok, no luck. Use a NoOp normalizer
                normalizer = new NoOpNormalizer();
            } else {
                normalizer = ordering.getNormalizer();
            }
        } else {
            normalizer = subString.getNormalizer();
        }
    } else {
        normalizer = equality.getNormalizer();
    }
    if (normalizer == null) {
        throw new IllegalArgumentException(I18n.err(I18n.ERR_13220_NO_NORMALIZER));
    }
    // Now, normalize the upValue
    normValue = normalizer.normalize(upValue);
}
Also used : NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule)

Example 7 with NoOpNormalizer

use of org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer in project directory-ldap-api by apache.

the class EntryUtils method getBytesAttributeType.

/* No protection */
static AttributeType getBytesAttributeType() {
    MutableAttributeType attributeType = new MutableAttributeType("1.2");
    LdapSyntax syntax = new LdapSyntax("1.2.1", "", false);
    syntax.setSyntaxChecker(new SyntaxChecker("1.2.1") {

        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            return (value == null) || (((byte[]) value).length < 5);
        }
    });
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.2.2");
    matchingRule.setSyntax(syntax);
    matchingRule.setLdapComparator(new ByteArrayComparator("1.2.2"));
    matchingRule.setNormalizer(new NoOpNormalizer("1.1.1"));
    attributeType.setEquality(matchingRule);
    attributeType.setSyntax(syntax);
    return attributeType;
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) ByteArrayComparator(org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator)

Example 8 with NoOpNormalizer

use of org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer in project directory-ldap-api by apache.

the class StringValueAttributeTypeTest method testConstrainedString.

/**
 * Presumes an attribute which constrains it's values to some constant
 * strings: LOW, MEDIUM, HIGH.  Normalization does nothing. MatchingRules
 * are exact case matching.
 *
 * @throws Exception on errors
 */
@Test
public void testConstrainedString() throws LdapInvalidAttributeValueException {
    s.setSyntaxChecker(new SyntaxChecker("1.1.1.1") {

        public static final long serialVersionUID = 1L;

        public boolean isValidSyntax(Object value) {
            if (value instanceof String) {
                String strval = (String) value;
                return strval.equals("HIGH") || strval.equals("LOW") || strval.equals("MEDIUM");
            }
            return false;
        }
    });
    mr.setSyntax(s);
    mr.setNormalizer(new NoOpNormalizer(mr.getOid()));
    at.setEquality(mr);
    at.setSyntax(s);
    // check that normalization and syntax checks work as expected
    Value value = new Value(at, "HIGH");
    assertEquals(value.getValue(), value.getValue());
    try {
        new Value(at, "high");
        fail();
    } catch (LdapInvalidAttributeValueException liave) {
    // expected
    }
    // create a bunch to best tested for equals and in containers
    Value v0 = new Value(at, "LOW");
    Value v1 = new Value(at, "LOW");
    Value v2 = new Value(at, "MEDIUM");
    Value v3 = new Value(at, "HIGH");
    // check equals
    assertTrue(v0.equals(v1));
    assertTrue(v1.equals(v0));
    assertEquals(0, v0.compareTo(v1));
    assertFalse(v2.equals(v3));
    assertFalse(v3.equals(v2));
    assertTrue(v2.compareTo(v3) > 0);
    assertTrue(v3.compareTo(v2) < 0);
    // add all except v1 and v5 to a set
    HashSet<Value> set = new HashSet<Value>();
    set.add(v0);
    set.add(v2);
    set.add(v3);
    // check contains method
    assertTrue("since v1.equals( v0 ) and v0 was added then this should be true", set.contains(v1));
    // check ordering based on the comparator
    List<Value> list = new ArrayList<Value>();
    list.add(v1);
    list.add(v3);
    list.add(v0);
    list.add(v2);
    Collections.sort(list);
    // High, low, low, medium
    assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(0).equals(v3));
    assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(1).equals(v0));
    assertTrue("since v2 \"MEDIUM\" should be at index 2", list.get(2).equals(v1));
    assertTrue("since v3 \"HIGH\" should be at index 3", list.get(3).equals(v2));
    assertEquals(4, list.size());
}
Also used : NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) OctetStringSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) ArrayList(java.util.ArrayList) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

NoOpNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer)8 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)5 Test (org.junit.Test)4 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)3 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)3 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)2 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)2 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)2 MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)2 SyntaxChecker (org.apache.directory.api.ldap.model.schema.SyntaxChecker)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)1 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)1 MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)1 ByteArrayComparator (org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator)1 ComparableComparator (org.apache.directory.api.ldap.model.schema.comparators.ComparableComparator)1 OidNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.OidNormalizer)1 OctetStringSyntaxChecker (org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker)1