use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.
the class AttributeTypeHelper method buildOrdering.
/**
* Build the ORDERING MR reference for an AttributeType
*/
private static void buildOrdering(MutableAttributeType attributeType, List<Throwable> errors, Registries registries) {
String orderingOid = attributeType.getOrderingOid();
if (orderingOid != null) {
MatchingRule currentOrdering = null;
try {
currentOrdering = registries.getMatchingRuleRegistry().lookup(orderingOid);
} catch (LdapException ne) {
// Not allowed.
String msg = I18n.err(I18n.ERR_13758_CANNOT_FIND_ORDERING_MR_OBJECT, orderingOid, attributeType.getName());
LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_ORDERING_MATCHING_RULE, msg, ne);
ldapSchemaException.setSourceObject(attributeType);
ldapSchemaException.setRelatedId(orderingOid);
errors.add(ldapSchemaException);
LOG.info(msg);
return;
}
if (currentOrdering != null) {
attributeType.setOrdering(currentOrdering);
} else {
// Not allowed.
String msg = I18n.err(I18n.ERR_13759_CANNOT_FIND_ORDERING_MR_INSTANCE, orderingOid, attributeType.getName());
LdapSchemaException ldapSchemaException = new LdapSchemaException(LdapSchemaExceptionCodes.AT_NONEXISTENT_ORDERING_MATCHING_RULE, msg);
ldapSchemaException.setSourceObject(attributeType);
ldapSchemaException.setRelatedId(orderingOid);
errors.add(ldapSchemaException);
LOG.info(msg);
}
} else {
AttributeType superior = attributeType.getSuperior();
// If the AT has a superior, take its Ordering MR if any
if ((superior != null) && (superior.getOrdering() != null)) {
attributeType.setOrdering(superior.getOrdering());
}
}
}
use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.
the class ConcreteNameComponentNormalizer method normalizeByName.
/**
* {@inheritDoc}
*/
@Override
public Object normalizeByName(String name, String value) throws LdapException {
AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry(name);
Normalizer normalizer = lookup(name);
if (attributeType.getSyntax().isHumanReadable()) {
return normalizer.normalize(value);
} else {
String unescaped = unescape(value);
return normalizer.normalize(unescaped);
}
}
use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.
the class ConcreteNameComponentNormalizer method lookup.
/**
* Looks up the Normalizer to use for a name component using the attributeId
* for the name component. First the attribute is resolved, then its
* equality matching rule is looked up. The normalizer of that matching
* rule is returned.
*
* @param id the name or oid of the attribute in the name component to
* normalize the value of
* @return the Normalizer to use for normalizing the value of the attribute
* @throws LdapException if there are failures resolving the Normalizer
*/
private Normalizer lookup(String id) throws LdapException {
AttributeType type = schemaManager.lookupAttributeTypeRegistry(id);
MatchingRule mrule = type.getEquality();
if (mrule == null) {
return new NoOpNormalizer(id);
}
return mrule.getNormalizer();
}
use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.
the class DefaultAttributeTypeRegistry method descendants.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public Iterator<AttributeType> descendants(String ancestorId) throws LdapException {
try {
String oid = getOidByName(ancestorId);
Set<AttributeType> descendants = oidToDescendantSet.get(oid);
if (descendants == null) {
return Collections.EMPTY_SET.iterator();
}
return descendants.iterator();
} catch (LdapException ne) {
throw new LdapNoSuchAttributeException(ne.getMessage(), ne);
}
}
use of org.apache.directory.api.ldap.model.schema.AttributeType in project directory-ldap-api by apache.
the class LdifReader method parseModify.
/**
* Parse a modify change type.
*
* The grammar is :
* <pre>
* <changerecord> ::= "changetype:" FILL "modify" SEP <mod-spec> <mod-specs-e>
* <mod-spec> ::= "add:" <mod-val> | "delete:" <mod-val-del> | "replace:" <mod-val>
* <mod-specs-e> ::= <mod-spec>
* <mod-specs-e> | e
* <mod-val> ::= FILL ATTRIBUTE-DESCRIPTION SEP ATTRVAL-SPEC <attrval-specs-e> "-" SEP
* <mod-val-del> ::= FILL ATTRIBUTE-DESCRIPTION SEP <attrval-specs-e> "-" SEP
* <attrval-specs-e> ::= ATTRVAL-SPEC <attrval-specs> | e
* </pre>
*
* @param entry The entry to feed
* @param iter The lines
* @exception LdapLdifException If the modify operation is invalid
*/
private void parseModify(LdifEntry entry, Iterator<String> iter) throws LdapLdifException {
int state = MOD_SPEC;
String modified = null;
ModificationOperation modificationType = ModificationOperation.ADD_ATTRIBUTE;
Attribute attribute = null;
// The following flag is used to deal with empty modifications
boolean isEmptyValue = true;
while (iter.hasNext()) {
String line = iter.next();
String lowerLine = Strings.toLowerCaseAscii(line);
if (lowerLine.startsWith("-")) {
if ((state != ATTRVAL_SPEC_OR_SEP) && (state != ATTRVAL_SPEC)) {
String msg = I18n.err(I18n.ERR_13413_BAD_MODIFY_SEPARATOR, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
} else {
if (isEmptyValue) {
if (state == ATTRVAL_SPEC_OR_SEP) {
entry.addModification(modificationType, modified);
} else {
// Update the entry with a null value
entry.addModification(modificationType, modified, null);
}
} else {
// Update the entry with the attribute
entry.addModification(modificationType, attribute);
}
state = MOD_SPEC;
isEmptyValue = true;
}
} else if (lowerLine.startsWith("add:")) {
if ((state != MOD_SPEC) && (state != ATTRVAL_SPEC)) {
String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
modified = Strings.trim(line.substring("add:".length()));
modificationType = ModificationOperation.ADD_ATTRIBUTE;
attribute = new DefaultAttribute(modified);
state = ATTRVAL_SPEC;
} else if (lowerLine.startsWith("delete:")) {
if ((state != MOD_SPEC) && (state != ATTRVAL_SPEC)) {
String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
modified = Strings.trim(line.substring("delete:".length()));
modificationType = ModificationOperation.REMOVE_ATTRIBUTE;
attribute = new DefaultAttribute(modified);
isEmptyValue = false;
state = ATTRVAL_SPEC_OR_SEP;
} else if (lowerLine.startsWith("replace:")) {
if ((state != MOD_SPEC) && (state != ATTRVAL_SPEC)) {
String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
modified = Strings.trim(line.substring("replace:".length()));
modificationType = ModificationOperation.REPLACE_ATTRIBUTE;
if (schemaManager != null) {
AttributeType attributeType = schemaManager.getAttributeType(modified);
attribute = new DefaultAttribute(modified, attributeType);
} else {
attribute = new DefaultAttribute(modified);
}
state = ATTRVAL_SPEC_OR_SEP;
} else {
if ((state != ATTRVAL_SPEC) && (state != ATTRVAL_SPEC_OR_SEP)) {
String msg = I18n.err(I18n.ERR_13413_BAD_MODIFY_SEPARATOR, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
// A standard AttributeType/AttributeValue pair
int colonIndex = line.indexOf(':');
String attributeType = line.substring(0, colonIndex);
if (!attributeType.equalsIgnoreCase(modified)) {
LOG.error(I18n.err(I18n.ERR_13415_MOD_ATTR_AND_VALUE_SPEC_NOT_EQUAL, lineNumber));
throw new LdapLdifException(I18n.err(I18n.ERR_13454_BAD_MODIFY_ATTRIBUTE));
}
// We should *not* have a Dn twice
if ("dn".equalsIgnoreCase(attributeType)) {
LOG.error(I18n.err(I18n.ERR_13400_ENTRY_WITH_TWO_DNS, lineNumber));
throw new LdapLdifException(I18n.err(I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS));
}
Object attributeValue = parseValue(attributeType, line, colonIndex);
try {
if (attributeValue instanceof String) {
attribute.add((String) attributeValue);
} else {
attribute.add((byte[]) attributeValue);
}
} catch (LdapInvalidAttributeValueException liave) {
throw new LdapLdifException(liave.getMessage(), liave);
}
isEmptyValue = false;
state = ATTRVAL_SPEC_OR_SEP;
}
}
if (state != MOD_SPEC) {
String msg = I18n.err(I18n.ERR_13414_BAD_MODIFY_SEPARATOR_2, lineNumber);
LOG.error(msg);
throw new LdapLdifException(msg);
}
}
Aggregations