View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.lutecetools.service;
35  
36  import java.io.IOException;
37  import java.io.StringWriter;
38  import java.util.ArrayList;
39  
40  import javax.xml.parsers.DocumentBuilder;
41  import javax.xml.parsers.DocumentBuilderFactory;
42  import javax.xml.parsers.ParserConfigurationException;
43  import javax.xml.transform.Transformer;
44  import javax.xml.transform.TransformerException;
45  import javax.xml.transform.TransformerFactory;
46  import javax.xml.transform.dom.DOMSource;
47  import javax.xml.transform.stream.StreamResult;
48  
49  import org.apache.commons.fileupload.FileItem;
50  import org.w3c.dom.Document;
51  import org.w3c.dom.Element;
52  import org.w3c.dom.Node;
53  import org.w3c.dom.NodeList;
54  import org.xml.sax.SAXException;
55  
56  import fr.paris.lutece.portal.service.util.AppLogService;
57  import fr.paris.lutece.portal.service.util.AppPropertiesService;
58  
59  /**
60   * Browser to select a directory
61   */
62  public class XMLParser
63  {
64      private static final String PROPERTY_MAVEN_REPO_URL = "lutecetools.maven.repository.url";
65      private static final String URL_MAVEN_REPO = AppPropertiesService.getProperty( PROPERTY_MAVEN_REPO_URL );
66      private static final String PROPERTY_MAVEN_PATH_PLUGINS = "lutecetools.maven.repository.path.plugins";
67      private static final String URL_PLUGINS = URL_MAVEN_REPO + AppPropertiesService.getProperty( PROPERTY_MAVEN_PATH_PLUGINS );
68      private static final String PROPERTY_MAVEN_PATH_CORE = "lutecetools.maven.repository.path.core";
69      private static final String URL_CORE = URL_MAVEN_REPO + AppPropertiesService.getProperty( PROPERTY_MAVEN_PATH_CORE );
70  
71      // Tags
72      private static final String TAG_DEPENDENCY = "dependency";
73      private static final String TAG_ARTIFACT_ID = "artifactId";
74      private static final String TAG_VERSION = "version";
75      private static final String TAG_MAIN_NODE = "project";
76  
77      // Messages
78      private static final String DIALOG_MESS_PART_1 = "Les dépendances :";
79      private static final String DIALOG_MESS_PART_2 = "ont conservé leur version car la nouvelle n'a pas pu être récupérée.";
80  
81      // Errors
82      private static final String RELEASE_NOT_FOUND = "Release not found";
83  
84      private static final String LUTECE_CORE = "lutece-core";
85      
86      private XMLParser( )
87      {
88      }
89  
90      public static String updatePOM( FileItem inputFile )
91      {
92          String strUpdated = "";
93          try
94          {
95              strUpdated = process( inputFile );
96          }
97          catch( IOException | ParserConfigurationException | SAXException | TransformerException e )
98          {
99              AppLogService.error( e.getMessage( ) );
100         }
101 
102         return strUpdated;
103     }
104 
105     private static String process( FileItem inputFile ) throws ParserConfigurationException, SAXException, IOException, TransformerException
106     {
107         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance( );
108         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder( );
109         Document doc = dBuilder.parse( inputFile.getInputStream( ) );
110         doc.getDocumentElement( ).normalize( );
111         NodeList nList = doc.getElementsByTagName( TAG_DEPENDENCY );
112 
113         ArrayList<String> arr = new ArrayList<>( );
114         for ( Integer tmp = 0; tmp < nList.getLength( ); tmp++ )
115         {
116             Node nNode = nList.item( tmp );
117 
118             if ( nNode.getNodeType( ) == Node.ELEMENT_NODE && nNode.getParentNode( ).getParentNode( ).getNodeName( ).equals( TAG_MAIN_NODE ) )
119             {
120                 Element eElement = (Element) nNode;
121                 String strArtifactId = eElement.getElementsByTagName( TAG_ARTIFACT_ID ).item( 0 ).getTextContent( );
122                 String strLastRelease;
123                 if ( strArtifactId.equals( LUTECE_CORE ) )
124                 {
125                     strLastRelease = MavenRepoService.instance( ).getVersion( URL_CORE );
126                 }
127                 else
128                 {
129                     strLastRelease = MavenRepoService.instance( ).getVersion( URL_PLUGINS + strArtifactId );
130                 }
131                 if ( !strLastRelease.equals( RELEASE_NOT_FOUND ) )
132                 {
133                     eElement.getElementsByTagName( TAG_VERSION ).item( 0 ).setTextContent( strLastRelease );
134                 }
135                 else
136                 {
137                     arr.add( strArtifactId );
138                 }
139             }
140         }
141 
142         String artifactIdList = "";
143         if ( !arr.isEmpty( ) )
144         {
145             for ( String list : arr )
146             {
147                 artifactIdList = artifactIdList.concat( "- " ).concat( list ).concat( "<br />" );
148             }
149             Globals._strWarningPomMessage = DIALOG_MESS_PART_1.concat( "<br />" ).concat( artifactIdList ).concat( DIALOG_MESS_PART_2 );
150         }
151 
152         TransformerFactory transformerFactory = TransformerFactory.newInstance( );
153         Transformer transformer = transformerFactory.newTransformer( );
154         DOMSource domSource = new DOMSource( doc );
155         StringWriter writer = new StringWriter( );
156         StreamResult streamResult = new StreamResult( writer );
157         transformer.transform( domSource, streamResult );
158         return writer.toString( );
159     }
160 }