A method for the analytical evaluation of singular and nearly singular layer potentials arising in the collocation boundary element method for the Laplace and Helmholtz equation is developed for flat boundary elements with polynomial shape functions. The method is based on dimension-reduction via the divergence theorem and a Recursive scheme for evaluating the resulting line Integrals for Polynomial Elements (RIPE). It is used to evaluate single layer, double layer, adjoint double layer, and hypersingular potentials, for both the Laplace and the Helmholtz kernels. It naturally supports nearly singular, singular, and hypersingular integrals under a single framework. The developed recursive algorithm allows accurate evaluation of layer potentials associated with O(p2) density functions used in a pth order boundary element in O(p3) time for the Laplace case.