Search in sources :

Example 16 with MatchingRule

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

the class Ava method compareTo.

/**
 * @see Comparable#compareTo(Object)
 */
@Override
public int compareTo(Ava that) {
    if (that == null) {
        return 1;
    }
    int comp;
    if (schemaManager == null) {
        // Compare the ATs
        comp = normType.compareTo(that.normType);
        if (comp != 0) {
            return comp;
        }
        // and compare the values
        if (value == null) {
            if (that.value == null) {
                return 0;
            } else {
                return -1;
            }
        } else {
            if (that.value == null) {
                return 1;
            } else {
                comp = value.compareTo((Value) that.value);
                return comp;
            }
        }
    } else {
        if (that.schemaManager == null) {
            // Problem : we will apply the current Ava SchemaManager to the given Ava
            try {
                that.apply(schemaManager);
            } catch (LdapInvalidDnException lide) {
                return 1;
            }
        }
        // First compare the AT OID
        comp = attributeType.getOid().compareTo(that.attributeType.getOid());
        if (comp != 0) {
            return comp;
        }
        // Now, compare the two values using the ordering matchingRule comparator, if any
        MatchingRule orderingMR = attributeType.getOrdering();
        if (orderingMR != null) {
            LdapComparator<Object> comparator = (LdapComparator<Object>) orderingMR.getLdapComparator();
            if (comparator != null) {
                comp = value.compareTo(that.value);
                return comp;
            } else {
                comp = compareValues(that);
                return comp;
            }
        } else {
            comp = compareValues(that);
            return comp;
        }
    }
}
Also used : LdapComparator(org.apache.directory.api.ldap.model.schema.LdapComparator) Value(org.apache.directory.api.ldap.model.entry.Value) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)

Example 17 with MatchingRule

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

the class MatchingRuleDescriptionSchemaParserTest method testSun1.

@Test
public void testSun1() throws ParseException, NamingException {
    String value = "( 2.5.13.5 NAME 'caseExactMatch' DESC 'Case Exact Matching on Directory String [defined in X.520]' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )";
    MatchingRule matchingRule = parser.parseMatchingRuleDescription(value);
    assertEquals("2.5.13.5", matchingRule.getOid());
    assertEquals(1, matchingRule.getNames().size());
    assertEquals("caseExactMatch", matchingRule.getNames().get(0));
    assertEquals("Case Exact Matching on Directory String [defined in X.520]", matchingRule.getDescription());
    assertFalse(matchingRule.isObsolete());
    assertEquals("1.3.6.1.4.1.1466.115.121.1.15", matchingRule.getSyntaxOid());
    assertEquals(0, matchingRule.getExtensions().size());
}
Also used : MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) Test(org.junit.Test)

Example 18 with MatchingRule

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

the class MatchingRuleDescriptionSchemaParserTest method testSyntax.

@Test
public void testSyntax() throws ParseException, NamingException {
    String value = null;
    MatchingRule matchingRule = null;
    // simple
    value = "( 1.1 SYNTAX 0.1.2.3.4.5.6.7.8.9 )";
    matchingRule = parser.parseMatchingRuleDescription(value);
    assertEquals("0.1.2.3.4.5.6.7.8.9", matchingRule.getSyntaxOid());
    // simple
    value = "(1.1 SYNTAX 123.456.789.0)";
    matchingRule = parser.parseMatchingRuleDescription(value);
    assertEquals("123.456.789.0", matchingRule.getSyntaxOid());
    // simple with spaces
    value = "( 1.1    SYNTAX    0.1.2.3.4.5.6.7.8.9    )";
    matchingRule = parser.parseMatchingRuleDescription(value);
    assertEquals("0.1.2.3.4.5.6.7.8.9", matchingRule.getSyntaxOid());
    // quoted value in parentheses
    value = "( 1.1    SYNTAX ('0.1.2.3.4.5.6.7.8.9')    )";
    matchingRule = parser.parseMatchingRuleDescription(value);
    assertEquals("0.1.2.3.4.5.6.7.8.9", matchingRule.getSyntaxOid());
    // SYNTAX must only appear once
    value = "( 1.1 SYNTAX 2.2 SYNTAX 3.3 )";
    try {
        matchingRule = parser.parseMatchingRuleDescription(value);
        fail("Exception expected, SYNTAX appears twice");
    } catch (ParseException pe) {
        assertTrue(true);
    }
    if (!parser.isQuirksMode()) {
        // non-numeric not allowed
        value = "( test )";
        try {
            parser.parse(value);
            fail("Exception expected, SYNTAX is require");
        } catch (ParseException pe) {
        // expected
        }
        // SYNTAX is required
        value = "( 1.1 )";
        try {
            matchingRule = parser.parseMatchingRuleDescription(value);
            fail("Exception expected, SYNTAX is required");
        } catch (ParseException pe) {
        // expected
        }
    }
}
Also used : ParseException(java.text.ParseException) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) Test(org.junit.Test)

Example 19 with MatchingRule

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

the class MatchingRuleDescriptionSchemaParserTest method testRfc1.

// //////////////////////////////////////////////////////////////
// Some real-world matching rule descriptons          //
// //////////////////////////////////////////////////////////////
@Test
public void testRfc1() throws ParseException, NamingException {
    String value = "( 2.5.13.5 NAME 'caseExactMatch' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )";
    MatchingRule matchingRule = parser.parseMatchingRuleDescription(value);
    assertEquals("2.5.13.5", matchingRule.getOid());
    assertEquals(1, matchingRule.getNames().size());
    assertEquals("caseExactMatch", matchingRule.getNames().get(0));
    assertNull(matchingRule.getDescription());
    assertFalse(matchingRule.isObsolete());
    assertEquals("1.3.6.1.4.1.1466.115.121.1.15", matchingRule.getSyntaxOid());
    assertEquals(0, matchingRule.getExtensions().size());
}
Also used : MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) Test(org.junit.Test)

Example 20 with MatchingRule

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

the class Registries method clone.

/**
 * Clone the Registries. This is done in two steps :
 * - first clone the SchemaObjetc registries
 * - second restore the relation between them
 */
// False positive
@Override
public Registries clone() throws CloneNotSupportedException {
    // First clone the structure
    Registries clone = (Registries) super.clone();
    // Now, clone the oidRegistry
    clone.globalOidRegistry = globalOidRegistry.copy();
    // We have to clone every SchemaObject registries now
    clone.attributeTypeRegistry = attributeTypeRegistry.copy();
    clone.comparatorRegistry = comparatorRegistry.copy();
    clone.ditContentRuleRegistry = ditContentRuleRegistry.copy();
    clone.ditStructureRuleRegistry = ditStructureRuleRegistry.copy();
    clone.ldapSyntaxRegistry = ldapSyntaxRegistry.copy();
    clone.matchingRuleRegistry = matchingRuleRegistry.copy();
    clone.matchingRuleUseRegistry = matchingRuleUseRegistry.copy();
    clone.nameFormRegistry = nameFormRegistry.copy();
    clone.normalizerRegistry = normalizerRegistry.copy();
    clone.objectClassRegistry = objectClassRegistry.copy();
    clone.syntaxCheckerRegistry = syntaxCheckerRegistry.copy();
    // Store all the SchemaObjects into the globalOid registry
    for (AttributeType attributeType : clone.attributeTypeRegistry) {
        clone.globalOidRegistry.put(attributeType);
    }
    for (DitContentRule ditContentRule : clone.ditContentRuleRegistry) {
        clone.globalOidRegistry.put(ditContentRule);
    }
    for (DitStructureRule ditStructureRule : clone.ditStructureRuleRegistry) {
        clone.globalOidRegistry.put(ditStructureRule);
    }
    for (MatchingRule matchingRule : clone.matchingRuleRegistry) {
        clone.globalOidRegistry.put(matchingRule);
    }
    for (MatchingRuleUse matchingRuleUse : clone.matchingRuleUseRegistry) {
        clone.globalOidRegistry.put(matchingRuleUse);
    }
    for (NameForm nameForm : clone.nameFormRegistry) {
        clone.globalOidRegistry.put(nameForm);
    }
    for (ObjectClass objectClass : clone.objectClassRegistry) {
        clone.globalOidRegistry.put(objectClass);
    }
    for (LdapSyntax syntax : clone.ldapSyntaxRegistry) {
        clone.globalOidRegistry.put(syntax);
    }
    // Clone the schema list
    clone.loadedSchemas = new HashMap<>();
    for (Map.Entry<String, Set<SchemaObjectWrapper>> entry : schemaObjects.entrySet()) {
        // We don't clone the schemas
        clone.loadedSchemas.put(entry.getKey(), loadedSchemas.get(entry.getKey()));
    }
    // Clone the Using and usedBy structures
    // They will be empty
    clone.using = new HashMap<>();
    clone.usedBy = new HashMap<>();
    // Last, rebuild the using and usedBy references
    clone.buildReferences();
    // Now, check the registries. We don't care about errors
    clone.checkRefInteg();
    clone.schemaObjects = new HashMap<>();
    // SchemaObjects
    for (Map.Entry<String, Set<SchemaObjectWrapper>> entry : schemaObjects.entrySet()) {
        Set<SchemaObjectWrapper> objects = new HashSet<>();
        for (SchemaObjectWrapper schemaObjectWrapper : entry.getValue()) {
            SchemaObject original = schemaObjectWrapper.get();
            try {
                if (!(original instanceof LoadableSchemaObject)) {
                    SchemaObject copy = clone.globalOidRegistry.getSchemaObject(original.getOid());
                    SchemaObjectWrapper newWrapper = new SchemaObjectWrapper(copy);
                    objects.add(newWrapper);
                } else {
                    SchemaObjectWrapper newWrapper = new SchemaObjectWrapper(original);
                    objects.add(newWrapper);
                }
            } catch (LdapException ne) {
            // Nothing to do
            }
        }
        clone.schemaObjects.put(entry.getKey(), objects);
    }
    return clone;
}
Also used : SchemaObject(org.apache.directory.api.ldap.model.schema.SchemaObject) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) ObjectClass(org.apache.directory.api.ldap.model.schema.ObjectClass) HashSet(java.util.HashSet) Set(java.util.Set) MatchingRuleUse(org.apache.directory.api.ldap.model.schema.MatchingRuleUse) NameForm(org.apache.directory.api.ldap.model.schema.NameForm) SchemaObjectWrapper(org.apache.directory.api.ldap.model.schema.SchemaObjectWrapper) LoadableSchemaObject(org.apache.directory.api.ldap.model.schema.LoadableSchemaObject) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) MutableAttributeType(org.apache.directory.api.ldap.model.schema.MutableAttributeType) DitStructureRule(org.apache.directory.api.ldap.model.schema.DitStructureRule) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) DitContentRule(org.apache.directory.api.ldap.model.schema.DitContentRule) MatchingRule(org.apache.directory.api.ldap.model.schema.MatchingRule) MutableMatchingRule(org.apache.directory.api.ldap.model.schema.MutableMatchingRule) HashMap(java.util.HashMap) Map(java.util.Map) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) HashSet(java.util.HashSet)

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