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.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
60
61
62
63 public class PomParser
64 {
65
66
67
68 private ArrayList<Dependency> _listDependencies = new ArrayList<Dependency>( );
69
70
71
72
73
74
75 public List<Dependency> getDependencies( )
76 {
77 return _listDependencies;
78 }
79
80
81
82
83
84
85
86
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
124
125
126
127
128
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
144
145
146
147
148
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
186
187
188
189
190
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
238
239
240
241
242
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
253
254
255
256
257
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
272
273
274
275
276
277
278
279
280
281
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 }