Add some defensive coding around some XML activities that are triggered by web applications and are therefore at potential risk of a memory leak.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1589837 13f79535-47bb-0310-9956-ffa450edef68
More refactoring to reduce code duplication.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1589842 13f79535-47bb-0310-9956-ffa450edef68