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  import java.util.Optional;
44  
45  import javax.xml.bind.JAXBContext;
46  import javax.xml.bind.JAXBElement;
47  import javax.xml.bind.JAXBException;
48  import javax.xml.bind.Unmarshaller;
49  
50  import org.w3c.dom.Element;
51  import org.xml.sax.InputSource;
52  
53  import fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model;
54  import fr.paris.lutece.plugins.releaser.business.Component;
55  import fr.paris.lutece.plugins.releaser.business.Dependency;
56  import fr.paris.lutece.plugins.releaser.business.Site;
57  import fr.paris.lutece.portal.service.util.AppLogService;
58  
59  // TODO: Auto-generated Javadoc
60  /**
61   * PomParser.
62   */
63  public class PomParser
64  {
65      // Tags
66  
67      /** The list dependencies. */
68      private ArrayList<Dependency> _listDependencies = new ArrayList<Dependency>( );
69  
70      /**
71       * Gets the dependencies.
72       *
73       * @return the dependencies
74       */
75      public List<Dependency> getDependencies( )
76      {
77          return _listDependencies;
78      }
79  
80      /**
81       * Parses the.
82       *
83       * @param site
84       *            the site
85       * @param strPOM
86       *            the str POM
87       */
88      public void parse( Site site, String strPOM )
89      {
90          try
91          {
92              InputSource isPOM = new InputSource( new StringReader( strPOM ) );
93              Model model = unmarshal( Model.class, isPOM );
94  
95              filledSite( site, model );
96  
97              fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies = model.getDependencies( );
98  
99              if ( dependencies != null )
100             {
101                 for ( fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency : dependencies.getDependency( ) )
102                 {
103                     filledDependency( site, jaxDependency );
104 
105                 }
106             }
107             
108             if ( model.getParent().getVersion() != null )
109             {
110                 site.setParentVersion( model.getParent().getVersion() );
111             }
112         }
113         catch( JAXBException e )
114         {
115             AppLogService.error( e );
116         }
117     }
118 
119 
120 
121 
122     /**
123      * Parses the pom path.
124      *
125      * @param component
126      *            the component
127      * @param pomPath
128      *            the pom path
129      */   
130 	public void parsePomPath(Component component, String pomPath ) 
131 	{
132 	  try 
133 	  {	
134 	    parse(component,new FileInputStream(pomPath));
135 	  }
136 	  catch (FileNotFoundException e) 
137 	  {
138 		  AppLogService.error( e );
139 	  }
140 	}
141 	
142     /**
143      * Parses the.
144      *
145      * @param component
146      *            the component
147      * @param strPOM
148      *            the str POM
149      */
150     public void parse( Component component, String strPOM )
151     {
152 
153         try
154         {
155             InputSource isPOM = new InputSource( new StringReader( strPOM ) );
156             Model model = unmarshal( Model.class, isPOM );
157             component.setArtifactId( model.getArtifactId( ) );
158             component.setGroupId( model.getGroupId( ) );
159             component.setCurrentVersion( model.getVersion( ) );
160             
161             if(model.getProperties() != null ){
162                 Optional<Element> op= model.getProperties().getAny().stream().filter(x->x.getTagName().equals("targetJdk")).findFirst();
163                 if(op.isPresent())
164                 {
165                   component.setTargetJdk(op.get().getTextContent());
166                 }   
167             
168                 }
169             if ( model.getScm( ) != null )
170             {
171                 component.setScmDeveloperConnection( model.getScm( ).getDeveloperConnection( ) );
172                 component.setBranchReleaseVersion( model.getVersion( ) );
173 
174             }
175 
176         }
177         catch( JAXBException e )
178         {
179             AppLogService.error( e );
180         }
181 
182     }
183 
184     /**
185      * Parses the.
186      *
187      * @param component
188      *            the component
189      * @param inputStream
190      *            the input stream
191      */
192     public void parse( Component component, InputStream inputStream )
193     {
194 
195         try
196         {
197             Model model = PomUpdater.unmarshal( Model.class, inputStream );
198             component.setArtifactId( model.getArtifactId( ) );
199             component.setGroupId( model.getGroupId( ) );
200             component.setCurrentVersion( model.getVersion( ) );
201             if(model.getProperties() != null ){
202 
203                 Optional<Element> op= model.getProperties().getAny().stream().filter(x->x.getTagName().equals("targetJdk")).findFirst();
204                 if(op.isPresent())
205                 {
206                   component.setTargetJdk(op.get().getTextContent());
207                 }   
208             
209           } 
210             if ( model.getScm( ) != null )
211             {
212                 component.setScmDeveloperConnection( model.getScm( ).getDeveloperConnection( ) );
213                 component.setBranchReleaseVersion( model.getVersion( ) );
214 
215             }
216         }
217         catch( JAXBException e )
218         {
219             AppLogService.error( e );
220         }
221         finally
222         {
223 
224             try
225             {
226                 inputStream.close( );
227             }
228             catch( IOException e )
229             {
230                 AppLogService.error( e );
231             }
232         }
233 
234     }
235     
236     /**
237      * Filled site.
238      *
239      * @param site
240      *            the site
241      * @param model
242      *            the model
243      */
244     private void filledSite( Site site, Model model )
245     {
246         site.setArtifactId( model.getArtifactId( ) );
247         site.setGroupId( model.getGroupId( ) );
248         site.setVersion( model.getVersion( ) );
249     }
250 
251     /**
252      * Filled dependency.
253      *
254      * @param site
255      *            the site
256      * @param jaxDependency
257      *            the jax dependency
258      */
259     private void filledDependency( Site site, fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency )
260     {
261         Dependencyins/releaser/business/Dependency.html#Dependency">Dependency dep = new Dependency( );
262         dep.setArtifactId( jaxDependency.getArtifactId( ) );
263         dep.setVersion( jaxDependency.getVersion( ) );
264         dep.setGroupId( jaxDependency.getGroupId( ) );
265         dep.setType( jaxDependency.getType( ) );
266 
267         site.addCurrentDependency( dep );
268     }
269 
270     /**
271      * Unmarshal.
272      *
273      * @param <T>
274      *            the generic type
275      * @param docClass
276      *            the doc class
277      * @param inputSource
278      *            the input source
279      * @return the t
280      * @throws JAXBException
281      *             the JAXB exception
282      */
283     public static <T> T unmarshal( Class<T> docClass, InputSource inputSource ) throws JAXBException
284     {
285         String packageName = docClass.getPackage( ).getName( );
286         JAXBContext jc = JAXBContext.newInstance( packageName );
287         Unmarshaller u = jc.createUnmarshaller( );
288         JAXBElement<T> doc = (JAXBElement<T>) u.unmarshal( inputSource );
289 
290         return doc.getValue( );
291     }
292 
293 }