Lagrangian and Hamiltonian Formulation of Transmission Line Systems with Boundary Energy Flow