Search in sources :

Example 26 with MatchingRule

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

the class MatchingRuleDescriptionSchemaParserTest method testRequiredElements.

/**
 * Test required elements.
 *
 * @throws ParseException
 */
@Test
public void testRequiredElements() throws ParseException, NamingException {
    String value = null;
    MatchingRule matchingRule = null;
    value = "( 1.2.3.4.5.6.7.8.9.0 SYNTAX 1.1 )";
    matchingRule = parser.parseMatchingRuleDescription(value);
    assertNotNull(matchingRule.getSyntaxOid());
    if (!parser.isQuirksMode()) {
        value = "( 1.2.3.4.5.6.7.8.9.0 )";
        try {
            parser.parseMatchingRuleDescription(value);
            fail("Exception expected, SYNTAX is required");
        } catch (ParseException pe) {
            assertTrue(true);
        }
    }
}
Also used : ParseException(java.text.ParseException) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) Test(org.junit.Test)

Example 27 with MatchingRule

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

the class DefaultSchemaLoader method loadMatchingRules.

private void loadMatchingRules(Attribute matchingRules) throws LdapException {
    if (matchingRules == null) {
        return;
    }
    for (Value value : matchingRules) {
        String desc = value.getValue();
        try {
            MatchingRule matchingRule = MR_DESCR_SCHEMA_PARSER.parseMatchingRuleDescription(desc);
            updateSchemas(matchingRule);
        } catch (ParseException pe) {
            throw new LdapException(pe);
        }
    }
}
Also used : Value(org.apache.directory.api.ldap.model.entry.Value) ParseException(java.text.ParseException) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 28 with MatchingRule

use of org.apache.directory.api.ldap.model.schema.MatchingRule 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 29 with MatchingRule

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

the class Value method equals.

/**
 * We compare two values using their Comparator, if any.
 *
 * @see Object#equals(Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj instanceof String) {
        String other = (String) obj;
        if (!isHR) {
            return false;
        }
        if (attributeType == null) {
            if (upValue != null) {
                return upValue.equals(other);
            } else {
                return obj == null;
            }
        } else {
            // We have an AttributeType, we use the associated comparator
            try {
                LdapComparator<String> comparator = (LdapComparator<String>) getLdapComparator();
                Normalizer normalizer = null;
                if (attributeType.getEquality() != null) {
                    normalizer = attributeType.getEquality().getNormalizer();
                }
                if (normalizer == null) {
                    if (comparator == null) {
                        return normValue.equals(other);
                    } else {
                        return comparator.compare(normValue, other) == 0;
                    }
                }
                String thisNormValue = normValue;
                String otherNormValue = normalizer.normalize(other);
                // Compare normalized values
                if (comparator == null) {
                    return thisNormValue.equals(otherNormValue);
                } else {
                    return comparator.compare(thisNormValue, otherNormValue) == 0;
                }
            } catch (LdapException ne) {
                return false;
            }
        }
    }
    if (!(obj instanceof Value)) {
        return false;
    }
    Value other = (Value) obj;
    // Check if the values aren't of the same type
    if (isHR != other.isHR) {
        // Both values must be HR or not HR
        return false;
    }
    if (!isHR) {
        // Shortcut for binary values
        return Arrays.equals(bytes, other.bytes);
    }
    // HR values
    if (bytes == null) {
        return other.bytes == null;
    }
    // Special case
    if (other.bytes == null) {
        return false;
    }
    // Not null, but empty. We try to avoid a spurious String Preparation
    if (bytes.length == 0) {
        return other.bytes.length == 0;
    } else if (other.bytes.length == 0) {
        return false;
    }
    // Ok, now, let's see if we have an AttributeType at all. If both have one,
    // and if they aren't equal, then we get out. If one of them has an AttributeType and
    // not the other, we will assume that this is the AttributeType to use.
    MatchingRule equalityMR;
    if (attributeType == null) {
        if (other.attributeType != null) {
            // Use the Other value AT
            equalityMR = other.attributeType.getEquality();
            // We may not have an Equality MR, and in tjis case, we compare the bytes
            if (equalityMR == null) {
                return Arrays.equals(bytes, other.bytes);
            }
            LdapComparator<Object> ldapComparator = equalityMR.getLdapComparator();
            if (ldapComparator == null) {
                // This is an error !
                LOG.error(I18n.err(I18n.ERR_13249_NO_COMPARATOR_FOR_AT, other.attributeType));
                return false;
            }
            return ldapComparator.compare(normValue, other.normValue) == 0;
        } else {
            // or the bytes otherwise.
            if (upValue != null) {
                return upValue.equals(other.upValue);
            } else {
                return Arrays.equals(bytes, other.bytes);
            }
        }
    } else {
        if (other.attributeType != null) {
            // Both attributeType must be equal
            if (!attributeType.equals(other.attributeType)) {
                return false;
            }
            // We have an AttributeType, we use the associated comparator
            try {
                LdapComparator<String> comparator = (LdapComparator<String>) getLdapComparator();
                if (other.attributeType.getEquality() == null) {
                    // No equality ? Default to comparing using a String comparator
                    return stringComparator.compare(normValue, other.normValue) == 0;
                }
                Normalizer normalizer = other.attributeType.getEquality().getNormalizer();
                if (normalizer == null) {
                    if (comparator == null) {
                        return normValue.equals(other.normValue);
                    } else {
                        return comparator.compare(normValue, other.normValue) == 0;
                    }
                }
                String thisNormValue = normalizer.normalize(normValue);
                // Compare normalized values
                if (comparator == null) {
                    return thisNormValue.equals(other.normValue);
                } else {
                    return comparator.compare(thisNormValue, other.normValue) == 0;
                }
            } catch (LdapException ne) {
                return false;
            }
        }
        // No attributeType
        if (normValue == null) {
            return other.normValue == null;
        } else {
            return normValue.equals(other.normValue);
        }
    }
}
Also used : LdapComparator(org.apache.directory.api.ldap.model.schema.LdapComparator) NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) Normalizer(org.apache.directory.api.ldap.model.schema.Normalizer) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule)

Example 30 with MatchingRule

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

the class SchemaManagerAddTest method testAddValidMatchingRule.

// =========================================================================
// DitContentRule addition tests
// -------------------------------------------------------------------------
// TODO
// =========================================================================
// DitStructureRule addition tests
// -------------------------------------------------------------------------
// TODO
// =========================================================================
// MatchingRule addition tests
// -------------------------------------------------------------------------
/**
 * Try to inject a new MatchingRule
 */
@Test
public void testAddValidMatchingRule() throws Exception {
    SchemaManager schemaManager = loadSystem();
    int mrrSize = schemaManager.getMatchingRuleRegistry().size();
    int goidSize = schemaManager.getGlobalOidRegistry().size();
    MutableMatchingRule matchingRule = new MutableMatchingRule("1.1.0");
    matchingRule.setSyntaxOid("1.3.6.1.4.1.1466.115.121.1.26");
    // It should not fail
    assertTrue(schemaManager.add(matchingRule));
    assertTrue(isMRPresent(schemaManager, "1.1.0"));
    // The C and N must have default values
    MatchingRule added = schemaManager.lookupMatchingRuleRegistry("1.1.0");
    assertEquals(NoOpNormalizer.class.getName(), added.getNormalizer().getClass().getName());
    assertEquals(ComparableComparator.class.getName(), added.getLdapComparator().getClass().getName());
    assertEquals(mrrSize + 1, schemaManager.getMatchingRuleRegistry().size());
    assertEquals(goidSize + 1, schemaManager.getGlobalOidRegistry().size());
}
Also used : MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) NoOpNormalizer(org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) ComparableComparator(org.apache.directory.api.ldap.model.schema.comparators.ComparableComparator) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) Test(org.junit.Test)

Aggregations

MatchingRule (org.apache.directory.api.ldap.model.schema.MatchingRule)36 Test (org.junit.Test)21 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)11 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)11 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)9 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)7 MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)7 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)6 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)5 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)5 ParseException (java.text.ParseException)4 LdapProtocolErrorException (org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException)4 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)4 NoOpNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer)4 LdapComparator (org.apache.directory.api.ldap.model.schema.LdapComparator)3 ObjectClass (org.apache.directory.api.ldap.model.schema.ObjectClass)3 SchemaObject (org.apache.directory.api.ldap.model.schema.SchemaObject)3 Entry (org.apache.directory.api.ldap.model.entry.Entry)2 Value (org.apache.directory.api.ldap.model.entry.Value)2 DitContentRule (org.apache.directory.api.ldap.model.schema.DitContentRule)2