Coverage Report - com.aurea.maven.plugins.sonic.sdm.AbstractSdmMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractSdmMojo
0%
0/42
0%
0/20
0
 
 1  
 package com.aurea.maven.plugins.sonic.sdm;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.IOException;
 5  
 import java.util.HashSet;
 6  
 import java.util.Properties;
 7  
 import java.util.Set;
 8  
 
 9  
 import javax.xml.parsers.DocumentBuilder;
 10  
 import javax.xml.parsers.DocumentBuilderFactory;
 11  
 import javax.xml.parsers.ParserConfigurationException;
 12  
 
 13  
 import org.apache.maven.plugin.MojoExecutionException;
 14  
 import org.codehaus.plexus.util.xml.XmlStreamReader;
 15  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 16  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 17  
 import org.w3c.dom.Document;
 18  
 import org.w3c.dom.Element;
 19  
 import org.w3c.dom.NodeList;
 20  
 import org.xml.sax.SAXException;
 21  
 
 22  
 import com.aurea.maven.plugins.sonic.AbstractSonicMojo;
 23  
 
 24  0
 public abstract class AbstractSdmMojo extends AbstractSonicMojo {
 25  
 
 26  
     private static final String TOPOLOGY_XSD = "/SDM/schema/2015/Topology.xsd";
 27  
         //indicates mandatory elements in topology.xsd - the elements that have minOccurs > 0
 28  
     private Set<String> mandatoryElements;
 29  
   /**
 30  
    * @return location of the SDM directory
 31  
    */
 32  
   protected File getTargetSDMDir() {
 33  
   
 34  0
     return new File(getOutputDirectory(), "sonicesb/sdm");
 35  
   }
 36  
 
 37  
   /**
 38  
    * @return the generated tailoring properties file
 39  
    */
 40  
   protected File getGeneratedPropertiesFile() {
 41  
   
 42  0
     return new File(getTargetSDMDir(), "/generated.tailoring.properties");
 43  
   }
 44  
   
 45  
   /**
 46  
    * @return the generated Topology file
 47  
    */
 48  
   protected File getGeneratedTopologyFile() {
 49  
   
 50  0
     return new File(getTargetSDMDir(), "/generated.Topology.xml");
 51  
   }
 52  
 
 53  
   /**
 54  
    * @return the generated Topology file
 55  
    */
 56  
   protected File getMasterTopologyFile() {
 57  
   
 58  0
     return new File(getTargetSDMDir(), "/Topology.xml");
 59  
   }
 60  
   
 61  
 
 62  
   protected Properties buildPropertiesFromTopology(File topoFile) throws MojoExecutionException {
 63  
     try {
 64  0
       Properties generated = new Properties();
 65  0
       XmlStreamReader topoReader = new XmlStreamReader(topoFile);
 66  0
       Xpp3Dom topology = Xpp3DomBuilder.build(topoReader);
 67  
 
 68  0
       Xpp3Dom parameters = topology.getChild("Parameters");
 69  0
       for (Xpp3Dom parameter : parameters.getChildren()) {
 70  0
             String id = getChildElementValue(parameter, "Id");
 71  0
         String value = getChildElementValue(parameter, "Value");
 72  0
         value = !value.isEmpty() ? value : getChildElementValue(parameter,"EncryptedValue");
 73  
         
 74  0
         if(value.isEmpty())
 75  0
             continue;
 76  
         
 77  0
                 getLog().debug("Adding Property: " + id + " -- " + value);
 78  0
                 generated.put(id, value);
 79  
       }
 80  0
       return generated;
 81  
       
 82  0
     } catch (Exception e) {
 83  0
       throw new MojoExecutionException("Exception while reading topology file: " + topoFile.getName(), e);
 84  
     }
 85  
   }
 86  
   
 87  
   protected String getChildElementValue(Xpp3Dom parent, String name) 
 88  
                   throws ParserConfigurationException, SAXException, IOException, IllegalArgumentException {
 89  0
           Xpp3Dom childElement = parent.getChild(name);
 90  0
           if(childElement != null)
 91  0
                   return childElement.getValue();
 92  0
           else if (!getMandatoryElements().contains(name))
 93  0
               return "";
 94  
           
 95  0
           throw new IllegalArgumentException("Child element " + name + " does not exist in element " + parent.getName());
 96  
   }
 97  
   
 98  
   /*
 99  
    * parsing Toplogy.xsd and loading the elements which have the minOccurs > 0
 100  
    * the access to mandatoryElements through this method ensures a proper and 
 101  
    * lazy loading of the required elements
 102  
    */
 103  
   private Set<String> getMandatoryElements() 
 104  
                   throws ParserConfigurationException, SAXException, IOException {
 105  0
       if(mandatoryElements == null) {
 106  0
           mandatoryElements = new HashSet<>();
 107  
           
 108  0
           DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 109  0
           dbf.setNamespaceAware(true);
 110  0
           DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 111  0
           Document document = docBuilder.parse(getClass().getResourceAsStream(TOPOLOGY_XSD));
 112  0
           document.getDocumentElement().normalize();
 113  0
           NodeList nodes = document.getElementsByTagName("xsd:element");
 114  
           
 115  0
           for(int i = 0; i < nodes.getLength(); i++) {
 116  0
               Element item = (Element)nodes.item(i);
 117  0
               String itemMinOccurrence = item.getAttribute("minOccurs");
 118  0
               if(itemMinOccurrence == null || itemMinOccurrence.isEmpty()) {
 119  0
                   continue;
 120  
               }
 121  
               
 122  0
               if(Integer.parseInt(itemMinOccurrence) > 0) {
 123  0
                   mandatoryElements.add(item.getAttribute("name"));
 124  
               }
 125  
           }
 126  
       }
 127  
       
 128  0
       return this.mandatoryElements;
 129  
   }
 130  
 
 131  
 }