package org.jboss.loom.utils;

import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.jboss.loom.ex.MigrationException;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author Ondrej Zizka, ozizka at redhat.com
 */
public class XmlUtils {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger( XmlUtils.class );
    
    public static <T> T unmarshallBean( File docFile, String xpath, Class<T> cls ) throws MigrationException{
        
        DocumentBuilder docBuilder = Utils.createXmlDocumentBuilder();
        try {
            Document doc = docBuilder.parse(docFile);

            XPath xp = XPathFactory.newInstance().newXPath();
            String exp = "/deployment/bean[@name='BootstrapProfileFactory']/property[@name='applicationURIs']//list[@elementClass='java.net.URI']";
            Node node = (Node) xp.evaluate(exp, doc, XPathConstants.NODE);

            Unmarshaller unmarshaller = JAXBContext.newInstance(cls).createUnmarshaller();
            T bean = (T) unmarshaller.unmarshal(node);
            return bean;
        }
        catch( SAXException | IOException | XPathExpressionException | JAXBException ex ) {
            throw new MigrationException("Failed parsing bean from a XML file " + docFile.getPath() + ": " + ex.getMessage(), ex);
        }
    }
    
}// class

@@ -65,32 +65,84 @@
 io.IOException;%0A
+import java.util.LinkedList;%0Aimport java.util.List;%0A
 import javax.xml
@@ -561,16 +561,45 @@
 m.Node;%0A
+import org.w3c.dom.NodeList;%0A
 import o
@@ -915,24 +915,260 @@
 on%7B%0A        
+return unmarshallBeans( docFile, xpath, cls ).get(0);%0A    %7D%0A        %0A    public static %3CT%3E List%3CT%3E unmarshallBeans( File docFile, String xpath, Class%3CT%3E cls ) throws MigrationException%7B%0A        %0A        List%3CT%3E beans = new LinkedList();
 %0A        Doc
@@ -1374,173 +1374,30 @@
     
-String exp = %22/deployment/bean%5B@name='BootstrapProfileFactory'%5D/property%5B@name='applicationURIs'%5D//list%5B@elementClass='java.net.URI'%5D%22;%0A            
 Node
+List
  node
+s
  = (Node
 ) xp
@@ -1392,16 +1392,20 @@
  = (Node
+List
 ) xp.eva
@@ -1414,11 +1414,13 @@
 ate(
-e
 xp
+ath
 , do
@@ -1441,19 +1441,34 @@
 nts.NODE
-);%0A
+SET);%0A            
 %0A       
@@ -1547,24 +1547,132 @@
 rshaller();%0A
+            for( int i = 0; i %3C nodes.getLength(); i++ ) %7B%0A                Node node = nodes.item( i );%0A    
             
@@ -1730,20 +1730,44 @@
     
-return bean;
+    beans.add( bean );%0A            %7D
 %0A   
@@ -2002,24 +2002,46 @@
 ;%0A        %7D%0A
+        return beans;%0A
     %7D%0A    %0A%7D

Add unmarshallBeans() to parse into a list.