LDIF Parsing

LDAP 目录交换格式 (LDIF) 文件是用于以扁平文件格式描述目录数据标准介质。此格式最常见的用途包括信息传输和归档。但是,此标准还定义了以扁平文件格式描述对存储数据的修改方法。此后类型的 LDIF 通常称为 changetypemodify LDIF。

LDAP Directory Interchange Format (LDIF) files are the standard medium for describing directory data in a flat-file format. The most common uses of this format include information transfer and archival. However, the standard also defines a way to describe modifications to stored data in a flat-file format. LDIFs of this later type are typically referred to as changetype or modify LDIFs.

org.springframework.ldap.ldif 包提供了解析 LDIF 文件并将它们反序列化为切实对象的所需的类。LdifParserorg.springframework.ldap.ldif 包的主要类,并且能够解析符合 RFC 2849 的文件。此类从资源中读取行并将它们组装到 LdapAttributes 对象中。

The org.springframework.ldap.ldif package provides the classes needed to parse LDIF files and deserialize them into tangible objects. The LdifParser is the main class of the org.springframework.ldap.ldif package and is capable of parsing files that comply with RFC 2849. This class reads lines from a resource and assembles them into an LdapAttributes object.

LdifParser 目前忽略 changetype LDIF 条目,因为它们在应用程序上下文中的实用性尚未确定。

The LdifParser currently ignores changetype LDIF entries, as their usefulness in the context of an application has yet to be determined.

Object Representation

org.springframework.ldap.core 包中的两个类提供了用代码表示 LDIF 的方法:

Two classes in the org.springframework.ldap.core package provide the means to represent an LDIF in code:

  • LdapAttribute: Extends javax.naming.directory.BasicAttribute adding support for LDIF options as defined in RFC2849.

  • LdapAttributes: Extends javax.naming.directory.BasicAttributes adding specialized support for DNs.

LdapAttribute 对象将选项表示为 Set<String>。添加到 LdapAttributes 对象的 DN 支持使用 javax.naming.ldap.LdapName 类。

LdapAttribute objects represent options as a Set<String>. The DN support added to the LdapAttributes object employs the javax.naming.ldap.LdapName class.

The Parser

Parser 接口为操作打下基础,并使用三个支持策略定义:

The Parser interface provides the foundation for operation and employs three supporting policy definitions:

  • SeparatorPolicy: Establishes the mechanism by which lines are assembled into attributes.

  • AttributeValidationPolicy: Ensures that attributes are correctly structured prior to parsing.

  • Specification: Provides a mechanism by which object structure can be validated after assembly.

这些接口的默认实现如下:

The default implementations of these interfaces are as follows:

  • org.springframework.ldap.ldif.parser.LdifParser

  • org.springframework.ldap.ldif.support.SeparatorPolicy

  • org.springframework.ldap.ldif.support.DefaultAttributeValidationPolicy

  • org.springframework.ldap.schema.DefaultSchemaSpecification

这四个类共同逐行解析资源,并将数据翻译成 LdapAttributes 对象。

Together, these four classes parse a resource line by line and translate the data into LdapAttributes objects.

SeparatorPolicy 确定如何解释从源文件中读取的各个行,因为 LDIF 规范允许属性跨多个行。默认策略根据所考虑行的读取顺序评估行,来确定行的性质。忽略 control 属性和 changetype 记录。

The SeparatorPolicy determines how individual lines read from the source file should be interpreted, as the LDIF specification lets attributes span multiple lines. The default policy assesses lines in the context of the order in which they were read to determine the nature of the line in consideration. control attributes and changetype records are ignored.

DefaultAttributeValidationPolicy 使用 REGEX 表达式来确保每个属性在解析后都符合有效属性格式(根据 RFC 2849)。如果属性验证失败,则记录 InvalidAttributeFormatException,并且跳过记录(解析器返回 null)。

The DefaultAttributeValidationPolicy uses REGEX expressions to ensure that each attribute conforms to a valid attribute format (according to RFC 2849) once parsed. If an attribute fails validation, an InvalidAttributeFormatException is logged, and the record is skipped (the parser returns null).

Schema Validation

验证针对模式的已解析对象的机制通过`org.springframework.ldap.schema`包中的 `Specification`接口提供。`DefaultSchemaSpecification`不执行任何验证,针对已知有效且无需检查的记录的实例提供。此选项消除了验证施加的性能损失。`BasicSchemaSpecification`应用基本检查,例如确保已提供 DN 和对象类声明。当前,对实际模式的验证需要实现 `Specification`接口。

A mechanism for validating parsed objects against a schema is available through the Specification interface in the org.springframework.ldap.schema package. The DefaultSchemaSpecification does not do any validation and is available for instances where records are known to be valid and need not be checked. This option saves the performance penalty that validation imposes. The BasicSchemaSpecification applies basic checks, such as ensuring DN and object class declarations have been provided. Currently, validation against an actual schema requires implementation of the Specification interface.

Spring Batch Integration

尽管 `LdifParser`可以被任何需要解析 LDIF 文件的应用程序使用,但 Spring 提供了批量处理框架,该框架为解析分隔文件(如 CSV)提供许多文件处理实用程序。`org.springframework.ldap.ldif.batch`包提供了所需的类,以将`LdifParser`用作 Spring Batch 框架中的有效配置选项。此包中有五个类。它们共同提供了三个基本用例:

While the LdifParser can be employed by any application that requires parsing of LDIF files, Spring offers a batch processing framework that offers many file-processing utilities for parsing delimited files such as CSV. The org.springframework.ldap.ldif.batch package offers the classes needed to use the LdifParser as a valid configuration option in the Spring Batch framework. There are five classes in this package. Together, they offer three basic use cases:

  • Reading LDIF records from a file and returning an LdapAttributes object.

  • Reading LDIF records from a file and mapping records to Java objects (POJOs).

  • Writing LDIF records to a file.

第一个用例由`LdifReader`完成。此类扩展 Spring Batch 的`AbstractItemCountingItemStreamItemReader`并实现了它的`ResourceAwareItemReaderItemStream`。它自然地适合框架,你可以使用它从文件中读取`LdapAttributes`对象。

The first use case is accomplished with LdifReader. This class extends Spring Batch’s AbstractItemCountingItemStreamItemReader and implements its ResourceAwareItemReaderItemStream. It fits naturally into the framework, and you can use it to read LdapAttributes objects from a file.

你可以使用`MappingLdifReader`将 LDIF 对象直接映射到任何 POJO。此类要求你提供`RecordMapper`接口的实现。此实现应实现将对象映射到 POJO 的逻辑。

You can use MappingLdifReader to map LDIF objects directly to any POJO. This class requires you to provide an implementation of the RecordMapper interface. This implementation should implement the logic for mapping objects to POJOs.

你可以实现 RecordCallbackHandler,并将实现提供给读者。可以使用此处理程序来操作跳过的记录。参见 Spring Batch API documentation 了解更多信息。

You can implement RecordCallbackHandler and provide the implementation to either reader. You can use this handler to operate on skipped records. See the Spring Batch API documentation for more information.

此包的最后一个成员`LdifAggregator`可用于将 LDIF 记录写入文件。此类调用`LdapAttributes`对象的`toString()`方法。

The last member of this package, the LdifAggregator, can be used to write LDIF records to a file. This class invokes the toString() method of the LdapAttributes object.