class ActiveLdap::LdifInvalid

def detect_nearest(line, column)

def detect_nearest(line, column)
  lines = @ldif.lines
  nearest = lines[line - 1] || ""
  if column - 1 == nearest.size # for JRuby 1.0.2 :<
    nearest << NEAREST_MARK
  else
    nearest[column - 1, 0] = NEAREST_MARK
  end
  if nearest == NEAREST_MARK
    nearest = "#{lines[line - 2]}#{nearest}"
  end
  nearest
end

def generate_snippet

def generate_snippet
  nearest = @nearest.chomp
  column_column = ":#{@column}"
  target_position_info = "#{@line}#{column_column}: "
  if /\n/ =~ nearest
    snippet = "%#{Math.log10(@line).truncate}d" % (@line - 1)
    snippet << " " * column_column.size
    snippet << ": "
    snippet << nearest.gsub(/\n/, "\n#{target_position_info}")
  else
    snippet = "#{target_position_info}#{nearest}"
  end
  snippet
end

def initialize(ldif, reason=nil, line=nil, column=nil)

def initialize(ldif, reason=nil, line=nil, column=nil)
  @ldif = ldif
  @reason = reason
  @line = line
  @column = column
  @nearest = nil
  if @reason
    message = _("invalid LDIF: %s:") % @reason
  else
    message = _("invalid LDIF:")
  end
  if @line and @column
    @nearest = detect_nearest(@line, @column)
    snippet = generate_snippet
    message << "\n#{snippet}\n"
  end
  super("#{message}\n#{numbered_ldif}")
end

def numbered_ldif

def numbered_ldif
  return @ldif if @ldif.blank?
  lines = @ldif.lines
  format = "%#{Math.log10(lines.size).truncate + 1}d: %s"
  i = 0
  lines.collect do |line|
    i += 1
    format % [i, line]
  end.join
end