@ThreadSafety(level=NOT_THREADSAFE) public final class LDIFReader extends java.lang.Object
LDIFReader ldifReader = new LDIFReader(pathToLDIFFile); int entriesRead = 0; int entriesAdded = 0; int errorsEncountered = 0; while (true) { Entry entry; try { entry = ldifReader.readEntry(); if (entry == null) { // All entries have been read. break; } entriesRead++; } catch (LDIFException le) { errorsEncountered++; if (le.mayContinueReading()) { // A recoverable error occurred while attempting to read a change // record, at or near line number le.getLineNumber() // The entry will be skipped, but we'll try to keep reading from the // LDIF file. continue; } else { // An unrecoverable error occurred while attempting to read an entry // at or near line number le.getLineNumber() // No further LDIF processing will be performed. break; } } catch (IOException ioe) { // An I/O error occurred while attempting to read from the LDIF file. // No further LDIF processing will be performed. errorsEncountered++; break; } LDAPResult addResult; try { addResult = connection.add(entry); // If we got here, then the change should have been processed // successfully. entriesAdded++; } catch (LDAPException le) { // If we got here, then the change attempt failed. addResult = le.toLDAPResult(); errorsEncountered++; } } ldifReader.close();
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BUFFER_SIZE
The default buffer size (128KB) that will be used when reading from the
data source.
|
Constructor and Description |
---|
LDIFReader(java.io.BufferedReader reader)
Creates a new LDIF reader that will use the provided buffered reader to
read the LDIF data.
|
LDIFReader(java.io.BufferedReader reader,
int numParseThreads)
Creates a new LDIF reader that will read data from the specified buffered
reader and parses the LDIF records asynchronously using the specified
number of threads.
|
LDIFReader(java.io.BufferedReader reader,
int numParseThreads,
LDIFReaderEntryTranslator entryTranslator)
Creates a new LDIF reader that will read data from the specified buffered
reader and parses the LDIF records asynchronously using the specified
number of threads.
|
LDIFReader(java.io.File file)
Creates a new LDIF reader that will read data from the specified file.
|
LDIFReader(java.io.File[] files,
int numParseThreads,
LDIFReaderEntryTranslator entryTranslator)
Creates a new LDIF reader that will read data from the specified files in
the order in which they are provided and optionally parses the LDIF records
asynchronously using the specified number of threads.
|
LDIFReader(java.io.File file,
int numParseThreads)
Creates a new LDIF reader that will read data from the specified file
and optionally parses the LDIF records asynchronously using the specified
number of threads.
|
LDIFReader(java.io.InputStream inputStream)
Creates a new LDIF reader that will read data from the provided input
stream.
|
LDIFReader(java.io.InputStream inputStream,
int numParseThreads)
Creates a new LDIF reader that will read data from the specified stream
and parses the LDIF records asynchronously using the specified number of
threads.
|
LDIFReader(java.io.InputStream inputStream,
int numParseThreads,
LDIFReaderEntryTranslator entryTranslator)
Creates a new LDIF reader that will read data from the specified stream
and parses the LDIF records asynchronously using the specified number of
threads.
|
LDIFReader(java.lang.String path)
Creates a new LDIF reader that will read data from the specified file.
|
LDIFReader(java.lang.String path,
int numParseThreads)
Creates a new LDIF reader that will read data from the specified file
and parses the LDIF records asynchronously using the specified number of
threads.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this LDIF reader and the underlying LDIF source.
|
static LDIFChangeRecord |
decodeChangeRecord(boolean ignoreDuplicateValues,
Schema schema,
boolean defaultAdd,
java.lang.String... ldifLines)
Decodes the provided set of LDIF lines as an LDIF change record.
|
static LDIFChangeRecord |
decodeChangeRecord(boolean defaultAdd,
java.lang.String... ldifLines)
Decodes the provided set of LDIF lines as an LDIF change record.
|
static LDIFChangeRecord |
decodeChangeRecord(java.lang.String... ldifLines)
Decodes the provided set of LDIF lines as an LDIF change record.
|
static Entry |
decodeEntry(boolean ignoreDuplicateValues,
Schema schema,
java.lang.String... ldifLines)
Decodes the provided set of LDIF lines as an entry.
|
static Entry |
decodeEntry(java.lang.String... ldifLines)
Decodes the provided set of LDIF lines as an entry.
|
DuplicateValueBehavior |
getDuplicateValueBehavior()
Retrieves the behavior that should be exhibited if the LDIF reader
encounters an entry with duplicate values.
|
java.lang.String |
getRelativeBasePath()
Retrieves the base path that will be prepended to relative paths in order
to obtain an absolute path.
|
Schema |
getSchema()
Retrieves the schema that will be used when reading LDIF records, if
defined.
|
TrailingSpaceBehavior |
getTrailingSpaceBehavior()
Retrieves the behavior that should be exhibited when encountering attribute
values which are not base64-encoded but contain trailing spaces.
|
boolean |
ignoreDuplicateValues()
Deprecated.
Use the
getDuplicateValueBehavior() method instead. |
LDIFChangeRecord |
readChangeRecord()
Reads an LDIF change record from the LDIF source.
|
LDIFChangeRecord |
readChangeRecord(boolean defaultAdd)
Reads an LDIF change record from the LDIF source.
|
static java.util.List<Entry> |
readEntries(java.io.File file)
Reads entries from the specified LDIF file and returns them as a
List . |
static java.util.List<Entry> |
readEntries(java.io.InputStream inputStream)
Reads and decodes LDIF entries from the provided input stream and
returns them as a
List . |
static java.util.List<Entry> |
readEntries(java.lang.String path)
Reads entries from the LDIF file with the specified path and returns them
as a
List . |
Entry |
readEntry()
Reads an entry from the LDIF source.
|
LDIFRecord |
readLDIFRecord()
Reads a record from the LDIF source.
|
void |
setDuplicateValueBehavior(DuplicateValueBehavior duplicateValueBehavior)
Specifies the behavior that should be exhibited if the LDIF reader
encounters an entry with duplicate values.
|
void |
setIgnoreDuplicateValues(boolean ignoreDuplicateValues)
Deprecated.
Use the
setDuplicateValueBehavior(com.unboundid.ldif.DuplicateValueBehavior) method instead. |
void |
setRelativeBasePath(java.io.File relativeBasePath)
Specifies the base path that will be prepended to relative paths in order
to obtain an absolute path.
|
void |
setRelativeBasePath(java.lang.String relativeBasePath)
Specifies the base path that will be prepended to relative paths in order
to obtain an absolute path.
|
void |
setSchema(Schema schema)
Specifies the schema that should be used when reading LDIF records.
|
void |
setStripTrailingSpaces(boolean stripTrailingSpaces)
Deprecated.
Use the
setTrailingSpaceBehavior(com.unboundid.ldif.TrailingSpaceBehavior) method instead. |
void |
setTrailingSpaceBehavior(TrailingSpaceBehavior trailingSpaceBehavior)
Specifies the behavior that should be exhibited when encountering attribute
values which are not base64-encoded but contain trailing spaces.
|
boolean |
stripTrailingSpaces()
Deprecated.
Use the
getTrailingSpaceBehavior() method instead. |
public static final int DEFAULT_BUFFER_SIZE
public LDIFReader(java.lang.String path) throws java.io.IOException
path
- The path to the file from which the data is to be read. It
must not be null
.java.io.IOException
- If a problem occurs while opening the file for
reading.public LDIFReader(java.lang.String path, int numParseThreads) throws java.io.IOException
path
- The path to the file from which the data is to be read. It
must not be null
.numParseThreads
- If this value is greater than zero, then the
specified number of threads will be used to
asynchronously read and parse the LDIF file.java.io.IOException
- If a problem occurs while opening the file for
reading.constructor for more details about asynchronous processing.
public LDIFReader(java.io.File file) throws java.io.IOException
file
- The file from which the data is to be read. It must not be
null
.java.io.IOException
- If a problem occurs while opening the file for
reading.public LDIFReader(java.io.File file, int numParseThreads) throws java.io.IOException
file
- The file from which the data is to be read. It
must not be null
.numParseThreads
- If this value is greater than zero, then the
specified number of threads will be used to
asynchronously read and parse the LDIF file.java.io.IOException
- If a problem occurs while opening the file for
reading.public LDIFReader(java.io.File[] files, int numParseThreads, LDIFReaderEntryTranslator entryTranslator) throws java.io.IOException
files
- The files from which the data is to be read. It
must not be null
or empty.numParseThreads
- If this value is greater than zero, then the
specified number of threads will be used to
asynchronously read and parse the LDIF file.entryTranslator
- The LDIFReaderEntryTranslator to apply to entries
before they are returned. This is normally
null
, which causes entries to be returned
unaltered. This is particularly useful when
parsing the input file in parallel because the
entry translation is also done in parallel.java.io.IOException
- If a problem occurs while opening the file for
reading.public LDIFReader(java.io.InputStream inputStream)
inputStream
- The input stream from which the data is to be read.
It must not be null
.public LDIFReader(java.io.InputStream inputStream, int numParseThreads)
inputStream
- The input stream from which the data is to be read.
It must not be null
.numParseThreads
- If this value is greater than zero, then the
specified number of threads will be used to
asynchronously read and parse the LDIF file.constructor for more details about asynchronous processing.
public LDIFReader(java.io.InputStream inputStream, int numParseThreads, LDIFReaderEntryTranslator entryTranslator)
inputStream
- The input stream from which the data is to be read.
It must not be null
.numParseThreads
- If this value is greater than zero, then the
specified number of threads will be used to
asynchronously read and parse the LDIF file.entryTranslator
- The LDIFReaderEntryTranslator to apply to read
entries before they are returned. This is normally
null
, which causes entries to be returned
unaltered. This is particularly useful when parsing
the input file in parallel because the entry
translation is also done in parallel.constructor for more details about asynchronous processing.
public LDIFReader(java.io.BufferedReader reader)
reader
- The buffered reader that will be used to read the LDIF
data. It must not be null
.public LDIFReader(java.io.BufferedReader reader, int numParseThreads)
reader
- The buffered reader that will be used to read the LDIF data.
It must not be null
.numParseThreads
- If this value is greater than zero, then the
specified number of threads will be used to
asynchronously read and parse the LDIF file.constructor for more details about asynchronous processing.
public LDIFReader(java.io.BufferedReader reader, int numParseThreads, LDIFReaderEntryTranslator entryTranslator)
reader
- The buffered reader that will be used to read the LDIF data.
It must not be null
.numParseThreads
- If this value is greater than zero, then the
specified number of threads will be used to
asynchronously read and parse the LDIF file.
This should only be set to greater than zero when
performance analysis has demonstrated that reading
and parsing the LDIF is a bottleneck. The default
synchronous processing is normally fast enough.
There is little benefit in passing in a value
greater than four (unless there is an
LDIFReaderEntryTranslator that does time-consuming
processing). A value of zero implies the
default behavior of reading and parsing LDIF
records synchronously when one of the read
methods is called.entryTranslator
- The LDIFReaderEntryTranslator to apply to read
entries before they are returned. This is normally
null
, which causes entries to be returned
unaltered. This is particularly useful when parsing
the input file in parallel because the entry
translation is also done in parallel.public static java.util.List<Entry> readEntries(java.lang.String path) throws java.io.IOException, LDIFException
List
. This is a convenience method that should only be used
for data sets that are small enough so that running out of memory isn't a
concern.path
- The path to the LDIF file containing the entries to be read.java.io.IOException
- If a problem occurs while attempting to read data
from the specified file.LDIFException
- If a problem is encountered while attempting to
decode data read as LDIF.public static java.util.List<Entry> readEntries(java.io.File file) throws java.io.IOException, LDIFException
List
. This is a convenience method that should only be used for
data sets that are small enough so that running out of memory isn't a
concern.file
- A reference to the LDIF file containing the entries to be
read.java.io.IOException
- If a problem occurs while attempting to read data
from the specified file.LDIFException
- If a problem is encountered while attempting to
decode data read as LDIF.public static java.util.List<Entry> readEntries(java.io.InputStream inputStream) throws java.io.IOException, LDIFException
List
. This is a convenience method that should
only be used for data sets that are small enough so that running out of
memory isn't a concern.inputStream
- The input stream from which the entries should be
read. The input stream will be closed before
returning.java.io.IOException
- If a problem occurs while attempting to read data
from the input stream.LDIFException
- If a problem is encountered while attempting to
decode data read as LDIF.public void close() throws java.io.IOException
java.io.IOException
- If a problem occurs while closing the underlying LDIF
source.@Deprecated public boolean ignoreDuplicateValues()
getDuplicateValueBehavior()
method instead.true
if duplicate values should be ignored, or
false
if any LDIF records containing duplicate values
should be rejected.@Deprecated public void setIgnoreDuplicateValues(boolean ignoreDuplicateValues)
setDuplicateValueBehavior(com.unboundid.ldif.DuplicateValueBehavior)
method instead.ignoreDuplicateValues
- Indicates whether to ignore duplicate
attribute values encountered while reading
LDIF records.public DuplicateValueBehavior getDuplicateValueBehavior()
public void setDuplicateValueBehavior(DuplicateValueBehavior duplicateValueBehavior)
duplicateValueBehavior
- The behavior that should be exhibited if
the LDIF reader encounters an entry with
duplicate values.@Deprecated public boolean stripTrailingSpaces()
getTrailingSpaceBehavior()
method instead.LDIFException
will be thrown
for that record.
true
if illegal trailing spaces should be stripped off, or
false
if LDIF records containing illegal trailing spaces
should be rejected.@Deprecated public void setStripTrailingSpaces(boolean stripTrailingSpaces)
setTrailingSpaceBehavior(com.unboundid.ldif.TrailingSpaceBehavior)
method instead.LDIFException
will be thrown
for that record.
stripTrailingSpaces
- Indicates whether to strip off any illegal
trailing spaces, or false
if LDIF
records containing them should be rejected.public TrailingSpaceBehavior getTrailingSpaceBehavior()
public void setTrailingSpaceBehavior(TrailingSpaceBehavior trailingSpaceBehavior)
trailingSpaceBehavior
- The behavior that should be exhibited when
encountering attribute values which are not
base64-encoded but contain trailing spaces.public java.lang.String getRelativeBasePath()
public void setRelativeBasePath(java.lang.String relativeBasePath)
relativeBasePath
- The base path that will be prepended to relative
paths in order to obtain an absolute path.public void setRelativeBasePath(java.io.File relativeBasePath)
relativeBasePath
- The base path that will be prepended to relative
paths in order to obtain an absolute path.public Schema getSchema()
null
if no schema should be used and all attributes should
be treated as case-insensitive strings.public void setSchema(Schema schema)
schema
- The schema that should be used when reading LDIF records,
or null
if no schema should be used and all
attributes should be treated as case-insensitive strings.public LDIFRecord readLDIFRecord() throws java.io.IOException, LDIFException
null
if there are
no more entries to be read.java.io.IOException
- If a problem occurs while trying to read from the
LDIF source.LDIFException
- If the data read could not be parsed as an entry or
an LDIF change record.public Entry readEntry() throws java.io.IOException, LDIFException
null
if there are
no more entries to be read.java.io.IOException
- If a problem occurs while attempting to read from the
LDIF source.LDIFException
- If the data read could not be parsed as an entry.public LDIFChangeRecord readChangeRecord() throws java.io.IOException, LDIFException
null
if
there are no more records to be read.java.io.IOException
- If a problem occurs while attempting to read from the
LDIF source.LDIFException
- If the data read could not be parsed as an LDIF
change record.public LDIFChangeRecord readChangeRecord(boolean defaultAdd) throws java.io.IOException, LDIFException
defaultAdd
- Indicates whether an LDIF record not containing a
changetype should be retrieved as an add change record.
If this is false
and the record read does not
include a changetype, then an LDIFException
will be thrown.null
if
there are no more records to be read.java.io.IOException
- If a problem occurs while attempting to read from the
LDIF source.LDIFException
- If the data read could not be parsed as an LDIF
change record.public static Entry decodeEntry(java.lang.String... ldifLines) throws LDIFException
ldifLines
- The set of lines that comprise the LDIF representation
of the entry. It must not be null
or empty.LDIFException
- If the provided LDIF data cannot be decoded as an
entry.public static Entry decodeEntry(boolean ignoreDuplicateValues, Schema schema, java.lang.String... ldifLines) throws LDIFException
ignoreDuplicateValues
- Indicates whether to ignore duplicate
attribute values encountered while parsing.schema
- The schema to use when parsing the record,
if applicable.ldifLines
- The set of lines that comprise the LDIF
representation of the entry. It must not be
null
or empty.LDIFException
- If the provided LDIF data cannot be decoded as an
entry.public static LDIFChangeRecord decodeChangeRecord(java.lang.String... ldifLines) throws LDIFException
ldifLines
- The set of lines that comprise the LDIF representation
of the change record. It must not be null
or
empty.LDIFException
- If the provided LDIF data cannot be decoded as a
change record.public static LDIFChangeRecord decodeChangeRecord(boolean defaultAdd, java.lang.String... ldifLines) throws LDIFException
defaultAdd
- Indicates whether an LDIF record not containing a
changetype should be retrieved as an add change record.
If this is false
and the record read does not
include a changetype, then an LDIFException
will be thrown.ldifLines
- The set of lines that comprise the LDIF representation
of the change record. It must not be null
or
empty.LDIFException
- If the provided LDIF data cannot be decoded as a
change record.public static LDIFChangeRecord decodeChangeRecord(boolean ignoreDuplicateValues, Schema schema, boolean defaultAdd, java.lang.String... ldifLines) throws LDIFException
ignoreDuplicateValues
- Indicates whether to ignore duplicate
attribute values encountered while parsing.schema
- The schema to use when processing the change
record, or null
if no schema should
be used and all values should be treated as
case-insensitive strings.defaultAdd
- Indicates whether an LDIF record not
containing a changetype should be retrieved
as an add change record. If this is
false
and the record read does not
include a changetype, then an
LDIFException
will be thrown.ldifLines
- The set of lines that comprise the LDIF
representation of the change record. It
must not be null
or empty.LDIFException
- If the provided LDIF data cannot be decoded as a
change record.