Coverage Report - fr.paris.lutece.plugins.releaser.util.pom.PomParser
 
Classes in this File Line Coverage Branch Coverage Complexity
PomParser
64 %
37/57
50 %
4/8
2,143
 
 1  
 /*
 2  
  * Copyright (c) 2002-2015, Mairie de Paris
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  *
 9  
  *  1. Redistributions of source code must retain the above copyright notice
 10  
  *     and the following disclaimer.
 11  
  *
 12  
  *  2. Redistributions in binary form must reproduce the above copyright notice
 13  
  *     and the following disclaimer in the documentation and/or other materials
 14  
  *     provided with the distribution.
 15  
  *
 16  
  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 17  
  *     contributors may be used to endorse or promote products derived from
 18  
  *     this software without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  *
 32  
  * License 1.0
 33  
  */
 34  
 package fr.paris.lutece.plugins.releaser.util.pom;
 35  
 
 36  
 import java.io.FileInputStream;
 37  
 import java.io.FileNotFoundException;
 38  
 import java.io.IOException;
 39  
 import java.io.InputStream;
 40  
 import java.io.StringReader;
 41  
 import java.util.ArrayList;
 42  
 import java.util.List;
 43  
 
 44  
 import javax.xml.bind.JAXBContext;
 45  
 import javax.xml.bind.JAXBElement;
 46  
 import javax.xml.bind.JAXBException;
 47  
 import javax.xml.bind.Unmarshaller;
 48  
 
 49  
 import org.xml.sax.InputSource;
 50  
 
 51  
 import fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model;
 52  
 import fr.paris.lutece.plugins.releaser.business.Component;
 53  
 import fr.paris.lutece.plugins.releaser.business.Dependency;
 54  
 import fr.paris.lutece.plugins.releaser.business.Site;
 55  
 import fr.paris.lutece.portal.service.util.AppLogService;
 56  
 
 57  
 /**
 58  
  * PomParser
 59  
  */
 60  2
 public class PomParser
 61  
 {
 62  
     // Tags
 63  
 
 64  
 
 65  
 
 66  2
     private ArrayList<Dependency> _listDependencies = new ArrayList<Dependency>( );
 67  
 
 68  
     public List<Dependency> getDependencies( )
 69  
     {
 70  0
         return _listDependencies;
 71  
     }
 72  
 
 73  
     public void parse( Site site, String strPOM )
 74  
     {
 75  
         try
 76  
         {
 77  1
             InputSource isPOM = new InputSource( new StringReader( strPOM ) );
 78  1
             Model model = unmarshal( Model.class, isPOM );
 79  
          
 80  1
             filledSite( site, model );
 81  
          
 82  1
              fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies=model.getDependencies( );
 83  
              
 84  1
             if(dependencies!=null)
 85  
             {
 86  1
                 for (  fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency: dependencies.getDependency( ))
 87  
                 {
 88  28
                       filledDependency( site, jaxDependency );
 89  
                     
 90  28
                 }
 91  
             }
 92  
         }
 93  0
         catch ( JAXBException e )
 94  
         {
 95  0
            AppLogService.error( e );
 96  1
         }
 97  1
     }
 98  
     
 99  
     public void parse( Component component , String strPOM )
 100  
     {
 101  
         
 102  
         try
 103  
         {
 104  0
             InputSource isPOM = new InputSource( new StringReader( strPOM ) );
 105  0
             Model model = unmarshal( Model.class, isPOM );
 106  0
             component.setArtifactId( model.getArtifactId( ) );
 107  0
             component.setGroupId( model.getGroupId( ) );
 108  0
             component.setCurrentVersion( model.getVersion( ) );
 109  0
             if(model.getScm() !=null)
 110  
             {
 111  0
                 component.setScmDeveloperConnection( model.getScm( ).getDeveloperConnection( ) );
 112  
                 
 113  
             }
 114  
         
 115  
         }
 116  0
         catch ( JAXBException e )
 117  
         {
 118  0
            AppLogService.error( e );
 119  0
         }
 120  
     
 121  
 
 122  0
     }
 123  
     public void parse( Component component , InputStream inputStream )
 124  
     {
 125  
         
 126  
         try
 127  
         {
 128  1
              Model model = PomUpdater.unmarshal( Model.class, inputStream );
 129  1
             component.setArtifactId( model.getArtifactId( ) );
 130  1
             component.setGroupId( model.getGroupId( ) );
 131  1
             component.setCurrentVersion( model.getVersion( ) );
 132  1
             if(model.getScm() !=null)
 133  
             {
 134  1
                 component.setScmDeveloperConnection( model.getScm( ).getDeveloperConnection( ) );
 135  
                 
 136  
             }
 137  
         
 138  
         }
 139  0
         catch ( JAXBException e )
 140  
         {
 141  0
            AppLogService.error( e );
 142  
         }
 143  
         finally
 144  
         {
 145  
             
 146  0
             try
 147  
             {
 148  1
                 inputStream.close( );
 149  
             }
 150  0
             catch( IOException e )
 151  
             {
 152  0
                AppLogService.error( e );
 153  1
             }
 154  0
         }
 155  
     
 156  
 
 157  1
     }
 158  
 
 159  
     private void filledSite( Site site, Model model )
 160  
     {
 161  1
         site.setArtifactId(model.getArtifactId( ) );
 162  1
         site.setGroupId( model.getGroupId( ) );
 163  1
         site.setVersion( model.getVersion( ) );
 164  1
     }
 165  
 
 166  
     private void filledDependency( Site site,fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency  jaxDependency )
 167  
     {
 168  28
         Dependency dep = new Dependency( );
 169  28
         dep.setArtifactId( jaxDependency.getArtifactId( ) );
 170  28
         dep.setVersion( jaxDependency.getVersion( ));
 171  28
         dep.setGroupId( jaxDependency.getGroupId( ) );
 172  28
         dep.setType( jaxDependency.getType( ) );
 173  
         
 174  28
         site.addCurrentDependency( dep );
 175  28
     }
 176  
     
 177  
     
 178  
 
 179  
     public static <T> T unmarshal( Class<T> docClass, InputSource inputSource )
 180  
         throws JAXBException
 181  
     {
 182  1
         String packageName = docClass.getPackage(  ).getName(  );
 183  1
         JAXBContext jc = JAXBContext.newInstance( packageName );
 184  1
         Unmarshaller u = jc.createUnmarshaller(  );
 185  1
         JAXBElement<T> doc = (JAXBElement<T>) u.unmarshal( inputSource );
 186  
 
 187  1
         return doc.getValue(  );
 188  
     }
 189  
 
 190  
    
 191  
 
 192  
 }