2323import  org .w3c .dom .Document ;
2424import  org .w3c .dom .Node ;
2525import  org .w3c .dom .NodeList ;
26+ import  org .xml .sax .SAXException ;
2627
2728import  javax .xml .namespace .QName ;
2829import  javax .xml .parsers .DocumentBuilder ;
2930import  javax .xml .parsers .DocumentBuilderFactory ;
31+ import  javax .xml .parsers .ParserConfigurationException ;
3032import  javax .xml .xpath .XPath ;
3133import  javax .xml .xpath .XPathConstants ;
34+ import  javax .xml .xpath .XPathExpressionException ;
3235import  javax .xml .xpath .XPathFactory ;
36+ import  java .io .IOException ;
3337import  java .nio .file .Files ;
3438import  java .nio .file .Path ;
3539import  java .util .ArrayList ;
@@ -48,7 +52,7 @@ public XmlUtilsTaskCommon(Path workDir) {
4852    /** 
4953     * Evaluates the expression and returns a single {@link String} value. 
5054     */ 
51-     public  String  xpathString (String  file , String  expression ) throws  Exception  {
55+     public  String  xpathString (String  file , String  expression ) throws  XPathExpressionException ,  ParserConfigurationException ,  IOException ,  SAXException  {
5256        Node  n  = (Node ) eval (workDir , file , expression , XPathConstants .NODE );
5357
5458        if  (n  == null ) {
@@ -66,7 +70,7 @@ public String xpathString(String file, String expression) throws Exception {
6670    /** 
6771     * Evaluates the expression and returns a list of {@link String} values. 
6872     */ 
69-     public  List <String > xpathListOfStrings (String  file , String  expression ) throws  Exception  {
73+     public  List <String > xpathListOfStrings (String  file , String  expression ) throws  XPathExpressionException ,  ParserConfigurationException ,  IOException ,  SAXException  {
7074        NodeList  l  = (NodeList ) eval (workDir , file , expression , XPathConstants .NODESET );
7175
7276        if  (l  == null ) {
@@ -91,7 +95,7 @@ public List<String> xpathListOfStrings(String file, String expression) throws Ex
9195     * Uses XPath to return {@code groupId + artifactId + version} attributes from a Maven pom.xml file. 
9296     * Knows how to handle the {@code <parent>} tag, i.e. parent GAV values are merged with the pom's own GAV. 
9397     */ 
94-     public  Map <String , String > mavenGav (String  file ) throws  Exception  {
98+     public  Map <String , String > mavenGav (String  file ) throws  ParserConfigurationException ,  IOException ,  SAXException ,  XPathExpressionException  {
9599        Document  document  = assertDocument (workDir , file );
96100        XPath  xpath  = XPathFactory .newInstance ().newXPath ();
97101
@@ -106,23 +110,29 @@ public Map<String, String> mavenGav(String file) throws Exception {
106110        return  result ;
107111    }
108112
109-     private  static  Object  eval (Path  workDir , String  file , String  expression , QName  returnType ) throws  Exception  {
113+     private  static  Object  eval (Path  workDir , String  file , String  expression , QName  returnType ) throws  ParserConfigurationException ,  IOException ,  SAXException ,  XPathExpressionException  {
110114        Document  document  = assertDocument (workDir , file );
111115        XPath  xpath  = XPathFactory .newInstance ().newXPath ();
112116        return  xpath .evaluate (expression , document , returnType );
113117    }
114118
115-     private  static  Document  assertDocument (Path  workDir , String  file ) throws  Exception  {
119+     private  static  Document  assertDocument (Path  workDir , String  file ) throws  ParserConfigurationException ,  IOException ,  SAXException  {
116120        Path  src  = workDir .resolve (file );
117121        if  (!Files .exists (src )) {
118122            throw  new  IllegalArgumentException ("File not found: "  + file );
119123        }
120124
121-         DocumentBuilder  builder  = DocumentBuilderFactory .newInstance ().newDocumentBuilder ();
125+         DocumentBuilderFactory  dbf  = DocumentBuilderFactory .newInstance ();
126+         dbf .setFeature ("http://apache.org/xml/features/disallow-doctype-decl" ,true );
127+         dbf .setFeature ("http://xml.org/sax/features/external-general-entities" ,false );
128+         dbf .setFeature ("http://xml.org/sax/features/external-parameter-entities" ,false );
129+ 
130+         DocumentBuilder  builder  = dbf .newDocumentBuilder ();
131+ 
122132        return  builder .parse (src .toFile ());
123133    }
124134
125-     private  static  Map <String , String > toGav (String  file , XPath  xpath , Document  document , String  expression ) throws  Exception  {
135+     private  static  Map <String , String > toGav (String  file , XPath  xpath , Document  document , String  expression ) throws  XPathExpressionException  {
126136        NodeList  l  = (NodeList ) xpath .evaluate (expression , document , XPathConstants .NODESET );
127137
128138        Map <String , String > result  = new  HashMap <>(l .getLength ());
0 commit comments