1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
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
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
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 }