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 | |
|
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 | |
|
58 | |
|
59 | |
|
60 | 2 | public class PomParser |
61 | |
{ |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 2 | private ArrayList<Dependency> _listDependencies = new ArrayList<Dependency>( ); |
67 | |
|
68 | |
public List<Dependency> getDependencies( ) |
69 | |
{ |
70 | 0 | return _listDependencies; |
71 | |
} |
72 | |
|
73 | |
public void parse( Site site, String strPOM ) |
74 | |
{ |
75 | |
try |
76 | |
{ |
77 | 1 | InputSource isPOM = new InputSource( new StringReader( strPOM ) ); |
78 | 1 | Model model = unmarshal( Model.class, isPOM ); |
79 | |
|
80 | 1 | filledSite( site, model ); |
81 | |
|
82 | 1 | fr.paris.lutece.plugins.releaser.business.jaxb.maven.Model.Dependencies dependencies=model.getDependencies( ); |
83 | |
|
84 | 1 | if(dependencies!=null) |
85 | |
{ |
86 | 1 | for ( fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency: dependencies.getDependency( )) |
87 | |
{ |
88 | 28 | filledDependency( site, jaxDependency ); |
89 | |
|
90 | 28 | } |
91 | |
} |
92 | |
} |
93 | 0 | catch ( JAXBException e ) |
94 | |
{ |
95 | 0 | AppLogService.error( e ); |
96 | 1 | } |
97 | 1 | } |
98 | |
|
99 | |
public void parse( Component component , String strPOM ) |
100 | |
{ |
101 | |
|
102 | |
try |
103 | |
{ |
104 | 0 | InputSource isPOM = new InputSource( new StringReader( strPOM ) ); |
105 | 0 | Model model = unmarshal( Model.class, isPOM ); |
106 | 0 | component.setArtifactId( model.getArtifactId( ) ); |
107 | 0 | component.setGroupId( model.getGroupId( ) ); |
108 | 0 | component.setCurrentVersion( model.getVersion( ) ); |
109 | 0 | if(model.getScm() !=null) |
110 | |
{ |
111 | 0 | component.setScmDeveloperConnection( model.getScm( ).getDeveloperConnection( ) ); |
112 | |
|
113 | |
} |
114 | |
|
115 | |
} |
116 | 0 | catch ( JAXBException e ) |
117 | |
{ |
118 | 0 | AppLogService.error( e ); |
119 | 0 | } |
120 | |
|
121 | |
|
122 | 0 | } |
123 | |
public void parse( Component component , InputStream inputStream ) |
124 | |
{ |
125 | |
|
126 | |
try |
127 | |
{ |
128 | 1 | Model model = PomUpdater.unmarshal( Model.class, inputStream ); |
129 | 1 | component.setArtifactId( model.getArtifactId( ) ); |
130 | 1 | component.setGroupId( model.getGroupId( ) ); |
131 | 1 | component.setCurrentVersion( model.getVersion( ) ); |
132 | 1 | if(model.getScm() !=null) |
133 | |
{ |
134 | 1 | component.setScmDeveloperConnection( model.getScm( ).getDeveloperConnection( ) ); |
135 | |
|
136 | |
} |
137 | |
|
138 | |
} |
139 | 0 | catch ( JAXBException e ) |
140 | |
{ |
141 | 0 | AppLogService.error( e ); |
142 | |
} |
143 | |
finally |
144 | |
{ |
145 | |
|
146 | 0 | try |
147 | |
{ |
148 | 1 | inputStream.close( ); |
149 | |
} |
150 | 0 | catch( IOException e ) |
151 | |
{ |
152 | 0 | AppLogService.error( e ); |
153 | 1 | } |
154 | 0 | } |
155 | |
|
156 | |
|
157 | 1 | } |
158 | |
|
159 | |
private void filledSite( Site site, Model model ) |
160 | |
{ |
161 | 1 | site.setArtifactId(model.getArtifactId( ) ); |
162 | 1 | site.setGroupId( model.getGroupId( ) ); |
163 | 1 | site.setVersion( model.getVersion( ) ); |
164 | 1 | } |
165 | |
|
166 | |
private void filledDependency( Site site,fr.paris.lutece.plugins.releaser.business.jaxb.maven.Dependency jaxDependency ) |
167 | |
{ |
168 | 28 | Dependency dep = new Dependency( ); |
169 | 28 | dep.setArtifactId( jaxDependency.getArtifactId( ) ); |
170 | 28 | dep.setVersion( jaxDependency.getVersion( )); |
171 | 28 | dep.setGroupId( jaxDependency.getGroupId( ) ); |
172 | 28 | dep.setType( jaxDependency.getType( ) ); |
173 | |
|
174 | 28 | site.addCurrentDependency( dep ); |
175 | 28 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
public static <T> T unmarshal( Class<T> docClass, InputSource inputSource ) |
180 | |
throws JAXBException |
181 | |
{ |
182 | 1 | String packageName = docClass.getPackage( ).getName( ); |
183 | 1 | JAXBContext jc = JAXBContext.newInstance( packageName ); |
184 | 1 | Unmarshaller u = jc.createUnmarshaller( ); |
185 | 1 | JAXBElement<T> doc = (JAXBElement<T>) u.unmarshal( inputSource ); |
186 | |
|
187 | 1 | return doc.getValue( ); |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
} |