View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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  // TODO: Auto-generated Javadoc
58  /**
59   * PomParser.
60   */
61  public class PomParser
62  {
63      // Tags
64  
65      /** The list dependencies. */
66      private ArrayList<Dependency> _listDependencies = new ArrayList<Dependency>( );
67  
68      /**
69       * Gets the dependencies.
70       *
71       * @return the dependencies
72       */
73      public List<Dependency> getDependencies( )
74      {
75          return _listDependencies;
76      }
77  
78      /**
79       * Parses the.
80       *
81       * @param site
82       *            the site
83       * @param strPOM
84       *            the str POM
85       */
86      public void parse( Site site, String strPOM )
87      {
88          try
89          {
90              InputSource isPOM = new InputSource( new StringReader( strPOM ) );
91              Model model = unmarshal( Model.class, isPOM );
92  
93              filledSite( site, model );
94  
95              fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies = model.getDependencies( );
96  
97              if ( dependencies != null )
98              {
99                  for ( fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency : dependencies.getDependency( ) )
100                 {
101                     filledDependency( site, jaxDependency );
102 
103                 }
104             }
105         }
106         catch( JAXBException e )
107         {
108             AppLogService.error( e );
109         }
110     }
111 
112     /**
113      * Parses the.
114      *
115      * @param component
116      *            the component
117      * @param strPOM
118      *            the str POM
119      */
120     public void parse( Component component, String strPOM )
121     {
122 
123         try
124         {
125             InputSource isPOM = new InputSource( new StringReader( strPOM ) );
126             Model model = unmarshal( Model.class, isPOM );
127             component.setArtifactId( model.getArtifactId( ) );
128             component.setGroupId( model.getGroupId( ) );
129             component.setCurrentVersion( model.getVersion( ) );
130             if ( model.getScm( ) != null )
131             {
132                 component.setScmDeveloperConnection( model.getScm( ).getDeveloperConnection( ) );
133                 component.setBranchReleaseVersion( model.getVersion( ) );
134 
135             }
136 
137         }
138         catch( JAXBException e )
139         {
140             AppLogService.error( e );
141         }
142 
143     }
144 
145     /**
146      * Parses the.
147      *
148      * @param component
149      *            the component
150      * @param inputStream
151      *            the input stream
152      */
153     public void parse( Component component, InputStream inputStream )
154     {
155 
156         try
157         {
158             Model model = PomUpdater.unmarshal( Model.class, inputStream );
159             component.setArtifactId( model.getArtifactId( ) );
160             component.setGroupId( model.getGroupId( ) );
161             component.setCurrentVersion( model.getVersion( ) );
162             if ( model.getScm( ) != null )
163             {
164                 component.setScmDeveloperConnection( model.getScm( ).getDeveloperConnection( ) );
165 
166             }
167 
168         }
169         catch( JAXBException e )
170         {
171             AppLogService.error( e );
172         }
173         finally
174         {
175 
176             try
177             {
178                 inputStream.close( );
179             }
180             catch( IOException e )
181             {
182                 AppLogService.error( e );
183             }
184         }
185 
186     }
187 
188     /**
189      * Filled site.
190      *
191      * @param site
192      *            the site
193      * @param model
194      *            the model
195      */
196     private void filledSite( Site site, Model model )
197     {
198         site.setArtifactId( model.getArtifactId( ) );
199         site.setGroupId( model.getGroupId( ) );
200         site.setVersion( model.getVersion( ) );
201     }
202 
203     /**
204      * Filled dependency.
205      *
206      * @param site
207      *            the site
208      * @param jaxDependency
209      *            the jax dependency
210      */
211     private void filledDependency( Site site, fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency )
212     {
213         Dependencyins/releaser/business/Dependency.html#Dependency">Dependency dep = new Dependency( );
214         dep.setArtifactId( jaxDependency.getArtifactId( ) );
215         dep.setVersion( jaxDependency.getVersion( ) );
216         dep.setGroupId( jaxDependency.getGroupId( ) );
217         dep.setType( jaxDependency.getType( ) );
218 
219         site.addCurrentDependency( dep );
220     }
221 
222     /**
223      * Unmarshal.
224      *
225      * @param <T>
226      *            the generic type
227      * @param docClass
228      *            the doc class
229      * @param inputSource
230      *            the input source
231      * @return the t
232      * @throws JAXBException
233      *             the JAXB exception
234      */
235     public static <T> T unmarshal( Class<T> docClass, InputSource inputSource ) throws JAXBException
236     {
237         String packageName = docClass.getPackage( ).getName( );
238         JAXBContext jc = JAXBContext.newInstance( packageName );
239         Unmarshaller u = jc.createUnmarshaller( );
240         JAXBElement<T> doc = (JAXBElement<T>) u.unmarshal( inputSource );
241 
242         return doc.getValue( );
243     }
244 
245 }