org.apache.directory.shared.ldap.ldif
Class LdifReader

java.lang.Object
  extended by org.apache.directory.shared.ldap.ldif.LdifReader
All Implemented Interfaces:
java.lang.Iterable<LdifEntry>
Direct Known Subclasses:
LdifAttributesReader

public class LdifReader
extends java.lang.Object
implements java.lang.Iterable<LdifEntry>

  <ldif-file> ::= "version:" <fill> <number> <seps> <dn-spec> <sep> 
  <ldif-content-change>
  
  <ldif-content-change> ::= 
    <number> <oid> <options-e> <value-spec> <sep> 
    <attrval-specs-e> <ldif-attrval-record-e> | 
    <alpha> <chars-e> <options-e> <value-spec> <sep> 
    <attrval-specs-e> <ldif-attrval-record-e> | 
    "control:" <fill> <number> <oid> <spaces-e> 
    <criticality> <value-spec-e> <sep> <controls-e> 
        "changetype:" <fill> <changerecord-type> <ldif-change-record-e> |
    "changetype:" <fill> <changerecord-type> <ldif-change-record-e>
                              
  <ldif-attrval-record-e> ::= <seps> <dn-spec> <sep> <attributeType> 
    <options-e> <value-spec> <sep> <attrval-specs-e> 
    <ldif-attrval-record-e> | e
                              
  <ldif-change-record-e> ::= <seps> <dn-spec> <sep> <controls-e> 
    "changetype:" <fill> <changerecord-type> <ldif-change-record-e> | e
                              
  <dn-spec> ::= "dn:" <fill> <safe-string> | "dn::" <fill> <base64-string>
                              
  <controls-e> ::= "control:" <fill> <number> <oid> <spaces-e> <criticality> 
    <value-spec-e> <sep> <controls-e> | e
                              
  <criticality> ::= "true" | "false" | e
                              
  <oid> ::= '.' <number> <oid> | e
                              
  <attrval-specs-e> ::= <number> <oid> <options-e> <value-spec> 
  <sep> <attrval-specs-e> | 
    <alpha> <chars-e> <options-e> <value-spec> <sep> <attrval-specs-e> | e
                              
  <value-spec-e> ::= <value-spec> | e
  
  <value-spec> ::= ':' <fill> <safe-string-e> | 
    "::" <fill> <base64-chars> | 
    ":<" <fill> <url>
  
  <attributeType> ::= <number> <oid> | <alpha> <chars-e>
  
  <options-e> ::= ';' <char> <chars-e> <options-e> |e
                              
  <chars-e> ::= <char> <chars-e> |  e
  
  <changerecord-type> ::= "add" <sep> <attributeType> 
  <options-e> <value-spec> <sep> <attrval-specs-e> | 
    "delete" <sep> | 
    "modify" <sep> <mod-type> <fill> <attributeType> 
    <options-e> <sep> <attrval-specs-e> <sep> '-' <sep> <mod-specs-e> | 
    "moddn" <sep> <newrdn> <sep> "deleteoldrdn:" 
    <fill> <0-1> <sep> <newsuperior-e> <sep> |
    "modrdn" <sep> <newrdn> <sep> "deleteoldrdn:" 
    <fill> <0-1> <sep> <newsuperior-e> <sep>
  
  <newrdn> ::= ':' <fill> <safe-string> | "::" <fill> <base64-chars>
  
  <newsuperior-e> ::= "newsuperior" <newrdn> | e
  
  <mod-specs-e> ::= <mod-type> <fill> <attributeType> <options-e> 
    <sep> <attrval-specs-e> <sep> '-' <sep> <mod-specs-e> | e
  
  <mod-type> ::= "add:" | "delete:" | "replace:"
  
  <url> ::= <a Uniform Resource Locator, as defined in [6]>
  
  
  
  LEXICAL
  -------
  
  <fill>           ::= ' ' <fill> | e
  <char>           ::= <alpha> | <digit> | '-'
  <number>         ::= <digit> <digits>
  <0-1>            ::= '0' | '1'
  <digits>         ::= <digit> <digits> | e
  <digit>          ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  <seps>           ::= <sep> <seps-e> 
  <seps-e>         ::= <sep> <seps-e> | e
  <sep>            ::= 0x0D 0x0A | 0x0A
  <spaces>         ::= ' ' <spaces-e>
  <spaces-e>       ::= ' ' <spaces-e> | e
  <safe-string-e>  ::= <safe-string> | e
  <safe-string>    ::= <safe-init-char> <safe-chars>
  <safe-init-char> ::= [0x01-0x09] | 0x0B | 0x0C | [0x0E-0x1F] | [0x21-0x39] | 0x3B | [0x3D-0x7F]
  <safe-chars>     ::= <safe-char> <safe-chars> | e
  <safe-char>      ::= [0x01-0x09] | 0x0B | 0x0C | [0x0E-0x7F]
  <base64-string>  ::= <base64-char> <base64-chars>
  <base64-chars>   ::= <base64-char> <base64-chars> | e
  <base64-char>    ::= 0x2B | 0x2F | [0x30-0x39] | 0x3D | [0x41-9x5A] | [0x61-0x7A]
  <alpha>          ::= [0x41-0x5A] | [0x61-0x7A]
  
  COMMENTS
  --------
  - The ldap-oid VN is not correct in the RFC-2849. It has been changed from 1*DIGIT 0*1("." 1*DIGIT) to
  DIGIT+ ("." DIGIT+)*
  - The mod-spec lacks a sep between *attrval-spec and "-".
  - The BASE64-UTF8-STRING should be BASE64-CHAR BASE64-STRING
  - The ValueSpec rule must accept multilines values. In this case, we have a LF followed by a 
  single space before the continued value.
 

Version:
$Rev$, $Date$
Author:
Apache Directory Project

Nested Class Summary
 class LdifReader.Position
          A private class to track the current position in a line
 
Field Summary
protected static int ATTRVAL_SPEC
           
protected static int ATTRVAL_SPEC_OR_SEP
           
protected static int CHANGE
           
protected  boolean containsChanges
          A flag set if the ldif contains changes
protected  boolean containsEntries
          A flag set if the ldif contains entries
protected static int DEFAULT_VERSION
          The ldif file version default value
protected  java.lang.Exception error
          An Exception to handle error message, has Iterator.next() can't throw exceptions
protected  java.io.Reader in
          The ldif Reader
protected static int LDIF_ENTRY
          Type of element read
protected  java.util.List<java.lang.String> lines
          A list of read lines
protected static int MOD_SPEC
          State values for the modify operation
protected  LdifReader.Position position
          The current position
protected  LdifEntry prefetched
          Iterator prefetched entry
protected static long SIZE_LIMIT_DEFAULT
          The default size limit : 1Mo
protected  long sizeLimit
          Size limit for file contained values
protected static int UNKNOWN
           
protected  int version
          The ldif version
 
Constructor Summary
LdifReader()
          Constructors
LdifReader(java.io.BufferedReader in)
          A constructor which takes a BufferedReader
LdifReader(java.io.File in)
          A constructor which takes a File
LdifReader(java.io.InputStream in)
          A constructor which takes an InputStream
LdifReader(java.io.Reader in)
          A constructor which takes a Reader
LdifReader(java.lang.String ldifFileName)
          A constructor which takes a file name
 
Method Summary
 boolean containsEntries()
           
 java.lang.Exception getError()
           
 long getSizeLimit()
           
 int getVersion()
           
 boolean hasError()
           
 boolean hasNext()
          Tests to see if another LDIF is on the input channel.
 java.util.Iterator<LdifEntry> iterator()
           
 LdifEntry next()
          Gets the next LDIF on the channel.
 void parseAttributeValue(LdifEntry entry, java.lang.String line, java.lang.String lowerLine)
          Parse an AttributeType/AttributeValue
static javax.naming.directory.Attribute parseAttributeValue(java.lang.String line)
          Parse an AttributeType/AttributeValue
 java.util.List<LdifEntry> parseLdif(java.io.BufferedReader inf)
          The main entry point of the LdifParser.
 java.util.List<LdifEntry> parseLdif(java.lang.String ldif)
          A method which parses a ldif string and returns a list of entries.
 java.util.List<LdifEntry> parseLdifFile(java.lang.String fileName)
          Parse a ldif file (using the default encoding).
 java.util.List<LdifEntry> parseLdifFile(java.lang.String fileName, java.lang.String encoding)
          Parse a ldif file, decoding it using the given charset encoding
protected static java.lang.Object parseSimpleValue(java.lang.String line, int pos)
          Parse the value part.
protected  java.lang.Object parseValue(java.lang.String line, int pos)
          Parse the value part.
protected  void readLines()
          Reads an entry in a ldif buffer, and returns the resulting lines, without comments, and unfolded.
 void remove()
          Always throws UnsupportedOperationException!
 void setSizeLimit(long sizeLimit)
          Set the maximum file size that can be accepted for an attribute value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lines

protected java.util.List<java.lang.String> lines
A list of read lines


position

protected LdifReader.Position position
The current position


DEFAULT_VERSION

protected static final int DEFAULT_VERSION
The ldif file version default value

See Also:
Constant Field Values

version

protected int version
The ldif version


LDIF_ENTRY

protected static final int LDIF_ENTRY
Type of element read

See Also:
Constant Field Values

CHANGE

protected static final int CHANGE
See Also:
Constant Field Values

UNKNOWN

protected static final int UNKNOWN
See Also:
Constant Field Values

sizeLimit

protected long sizeLimit
Size limit for file contained values


SIZE_LIMIT_DEFAULT

protected static final long SIZE_LIMIT_DEFAULT
The default size limit : 1Mo

See Also:
Constant Field Values

MOD_SPEC

protected static final int MOD_SPEC
State values for the modify operation

See Also:
Constant Field Values

ATTRVAL_SPEC

protected static final int ATTRVAL_SPEC
See Also:
Constant Field Values

ATTRVAL_SPEC_OR_SEP

protected static final int ATTRVAL_SPEC_OR_SEP
See Also:
Constant Field Values

prefetched

protected LdifEntry prefetched
Iterator prefetched entry


in

protected java.io.Reader in
The ldif Reader


containsEntries

protected boolean containsEntries
A flag set if the ldif contains entries


containsChanges

protected boolean containsChanges
A flag set if the ldif contains changes


error

protected java.lang.Exception error
An Exception to handle error message, has Iterator.next() can't throw exceptions

Constructor Detail

LdifReader

public LdifReader()
Constructors


LdifReader

public LdifReader(java.lang.String ldifFileName)
           throws javax.naming.NamingException
A constructor which takes a file name

Parameters:
ldifFileName - A file name containing ldif formated input
Throws:
javax.naming.NamingException - If the file cannot be processed or if the format is incorrect

LdifReader

public LdifReader(java.io.BufferedReader in)
           throws javax.naming.NamingException
A constructor which takes a BufferedReader

Parameters:
in - A BufferedReader containing ldif formated input
Throws:
javax.naming.NamingException - If the file cannot be processed or if the format is incorrect

LdifReader

public LdifReader(java.io.Reader in)
           throws javax.naming.NamingException
A constructor which takes a Reader

Parameters:
in - A Reader containing ldif formated input
Throws:
javax.naming.NamingException - If the file cannot be processed or if the format is incorrect

LdifReader

public LdifReader(java.io.InputStream in)
           throws javax.naming.NamingException
A constructor which takes an InputStream

Parameters:
in - An InputStream containing ldif formated input
Throws:
javax.naming.NamingException - If the file cannot be processed or if the format is incorrect

LdifReader

public LdifReader(java.io.File in)
           throws javax.naming.NamingException
A constructor which takes a File

Parameters:
in - A File containing ldif formated input
Throws:
javax.naming.NamingException - If the file cannot be processed or if the format is incorrect
Method Detail

getVersion

public int getVersion()
Returns:
The ldif file version

getSizeLimit

public long getSizeLimit()
Returns:
The maximum size of a file which is used into an attribute value.

setSizeLimit

public void setSizeLimit(long sizeLimit)
Set the maximum file size that can be accepted for an attribute value

Parameters:
sizeLimit - The size in bytes

parseSimpleValue

protected static java.lang.Object parseSimpleValue(java.lang.String line,
                                                   int pos)
Parse the value part.

Parameters:
line - The line which contains the value
pos - The starting position in the line
Returns:
A String or a byte[], depending of the kind of value we get

parseValue

protected java.lang.Object parseValue(java.lang.String line,
                                      int pos)
                               throws javax.naming.NamingException
Parse the value part.

Parameters:
line - The line which contains the value
pos - The starting position in the line
Returns:
A String or a byte[], depending of the kind of value we get
Throws:
javax.naming.NamingException - If something went wrong

parseAttributeValue

public static javax.naming.directory.Attribute parseAttributeValue(java.lang.String line)
Parse an AttributeType/AttributeValue

Parameters:
line - The line to parse
Returns:
the parsed Attribute

parseAttributeValue

public void parseAttributeValue(LdifEntry entry,
                                java.lang.String line,
                                java.lang.String lowerLine)
                         throws javax.naming.NamingException
Parse an AttributeType/AttributeValue

Parameters:
entry - The entry where to store the value
line - The line to parse
lowerLine - The same line, lowercased
Throws:
javax.naming.NamingException - If anything goes wrong

readLines

protected void readLines()
                  throws javax.naming.NamingException
Reads an entry in a ldif buffer, and returns the resulting lines, without comments, and unfolded. The lines represent *one* entry.

Throws:
javax.naming.NamingException - If something went wrong

parseLdifFile

public java.util.List<LdifEntry> parseLdifFile(java.lang.String fileName)
                                        throws javax.naming.NamingException
Parse a ldif file (using the default encoding).

Parameters:
fileName - The ldif file
Returns:
A list of entries
Throws:
javax.naming.NamingException - If the parsing fails

parseLdifFile

public java.util.List<LdifEntry> parseLdifFile(java.lang.String fileName,
                                               java.lang.String encoding)
                                        throws javax.naming.NamingException
Parse a ldif file, decoding it using the given charset encoding

Parameters:
fileName - The ldif file
encoding - The charset encoding to use
Returns:
A list of entries
Throws:
javax.naming.NamingException - If the parsing fails

parseLdif

public java.util.List<LdifEntry> parseLdif(java.lang.String ldif)
                                    throws javax.naming.NamingException
A method which parses a ldif string and returns a list of entries.

Parameters:
ldif - The ldif string
Returns:
A list of entries, or an empty List
Throws:
javax.naming.NamingException - If something went wrong

next

public LdifEntry next()
Gets the next LDIF on the channel.

Returns:
the next LDIF as a String.
Throws:
java.util.NoSuchElementException - If we can't read the next entry

hasNext

public boolean hasNext()
Tests to see if another LDIF is on the input channel.

Returns:
true if another LDIF is available false otherwise.

remove

public void remove()
Always throws UnsupportedOperationException!

See Also:
Iterator.remove()

iterator

public java.util.Iterator<LdifEntry> iterator()
Specified by:
iterator in interface java.lang.Iterable<LdifEntry>
Returns:
An iterator on the file

hasError

public boolean hasError()
Returns:
True if an error occured during parsing

getError

public java.lang.Exception getError()
Returns:
The exception that occurs during an entry parsing

parseLdif

public java.util.List<LdifEntry> parseLdif(java.io.BufferedReader inf)
                                    throws javax.naming.NamingException
The main entry point of the LdifParser. It reads a buffer and returns a List of entries.

Parameters:
inf - The buffer being processed
Returns:
A list of entries
Throws:
javax.naming.NamingException - If something went wrong

containsEntries

public boolean containsEntries()
Returns:
True if the ldif file contains entries, fals if it contains changes


Copyright © 2003-2011 Apache Software Foundation. All Rights Reserved.