1 package org.apache.maven.plugins.ear;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.Writer;
24
25 import org.apache.maven.plugins.ear.util.JavaEEVersion;
26 import org.codehaus.plexus.util.xml.XMLWriter;
27
28
29
30
31
32
33 final class ApplicationXmlWriter
34 extends AbstractXmlWriter
35 {
36
37 public static final String DOCTYPE_1_3 = "application PUBLIC\n"
38 + "\t\"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN\"\n"
39 + "\t\"http://java.sun.com/dtd/application_1_3.dtd\"";
40
41
42 private static final String APPLICATION_ELEMENT = "application";
43
44 private final JavaEEVersion version;
45
46 private final Boolean generateModuleId;
47
48 ApplicationXmlWriter( JavaEEVersion version, String encoding, Boolean generateModuleId )
49 {
50 super( encoding );
51 this.version = version;
52 this.generateModuleId = generateModuleId;
53 }
54
55 void write( ApplicationXmlWriterContext context )
56 throws EarPluginException
57 {
58 try ( Writer w = initializeWriter( context.getDestinationFile() ) )
59 {
60 XMLWriter writer = null;
61 if ( JavaEEVersion.ONE_DOT_THREE.eq( version ) )
62 {
63 writer = initializeRootElementOneDotThree( w );
64 }
65 else if ( JavaEEVersion.ONE_DOT_FOUR.eq( version ) )
66 {
67 writer = initializeRootElementOneDotFour( w );
68 }
69 else if ( JavaEEVersion.FIVE.eq( version ) )
70 {
71 writer = initializeRootElementFive( w );
72 }
73 else if ( JavaEEVersion.SIX.eq( version ) )
74 {
75 writer = initializeRootElementSix( w );
76 }
77 else if ( JavaEEVersion.SEVEN.eq( version ) )
78 {
79 writer = initializeRootElementSeven( w );
80 }
81 else if ( JavaEEVersion.EIGHT.eq( version ) )
82 {
83 writer = initializeRootElementEight( w );
84 }
85 else if ( JavaEEVersion.NINE.eq( version ) )
86 {
87 writer = initializeRootElementNine( w );
88 }
89 else if ( JavaEEVersion.TEN.eq( version ) )
90 {
91 writer = initializeRootElementTen( w );
92 }
93
94
95 if ( context.getApplicationId() != null )
96 {
97 writer.addAttribute( "id", context.getApplicationId() );
98 }
99
100
101 if ( version.ge( JavaEEVersion.SIX ) )
102 {
103 writeApplicationName( context.getApplicationName(), writer );
104 }
105
106
107
108 if ( version.eq( JavaEEVersion.ONE_DOT_THREE ) )
109 {
110 writeDisplayName( context.getDisplayName(), writer );
111 writeDescription( context.getDescription(), writer );
112 }
113 else
114 {
115 writeDescription( context.getDescription(), writer );
116 writeDisplayName( context.getDisplayName(), writer );
117 }
118
119
120 if ( version.ge( JavaEEVersion.SIX ) )
121 {
122 writeInitializeInOrder( context.getInitializeInOrder(), writer );
123 }
124
125
126 for ( EarModule module : context.getEarModules() )
127 {
128 module.appendModule( writer, version.getVersion(), generateModuleId );
129 }
130
131 for ( SecurityRole securityRole : context.getSecurityRoles() )
132 {
133 securityRole.appendSecurityRole( writer );
134 }
135
136 if ( version.ge( JavaEEVersion.FIVE ) )
137 {
138 writeLibraryDirectory( context.getLibraryDirectory(), writer );
139 }
140
141 if ( version.ge( JavaEEVersion.SIX ) )
142 {
143 for ( EnvEntry envEntry : context.getEnvEntries() )
144 {
145 envEntry.appendEnvEntry( writer );
146 }
147 for ( EjbRef ejbEntry : context.getEjbEntries() )
148 {
149 ejbEntry.appendEjbRefEntry( writer );
150 }
151 for ( ResourceRef resourceEntry : context.getResourceRefs() )
152 {
153 resourceEntry.appendResourceRefEntry( writer );
154 }
155 }
156
157 writer.endElement();
158 }
159 catch ( IOException ex )
160 {
161
162 }
163 }
164
165 private void writeApplicationName( String applicationName, XMLWriter writer )
166 {
167 if ( applicationName != null )
168 {
169 writer.startElement( "application-name" );
170 writer.writeText( applicationName );
171 writer.endElement();
172 }
173 }
174
175 private void writeDescription( String description, XMLWriter writer )
176 {
177 if ( description != null )
178 {
179 writer.startElement( "description" );
180 writer.writeText( description );
181 writer.endElement();
182 }
183 }
184
185 private void writeDisplayName( String displayName, XMLWriter writer )
186 {
187 if ( displayName != null )
188 {
189 writer.startElement( "display-name" );
190 writer.writeText( displayName );
191 writer.endElement();
192 }
193 }
194
195 private void writeInitializeInOrder( Boolean initializeInOrder, XMLWriter writer )
196 {
197 if ( initializeInOrder != null )
198 {
199 writer.startElement( "initialize-in-order" );
200 writer.writeText( initializeInOrder.toString() );
201 writer.endElement();
202 }
203 }
204
205 private void writeLibraryDirectory( String libraryDirectory, XMLWriter writer )
206 {
207 if ( libraryDirectory != null )
208 {
209 writer.startElement( "library-directory" );
210 writer.writeText( libraryDirectory );
211 writer.endElement();
212 }
213 }
214
215 private XMLWriter initializeRootElementOneDotThree( Writer w )
216 {
217 XMLWriter writer = initializeXmlWriter( w, DOCTYPE_1_3 );
218 writer.startElement( APPLICATION_ELEMENT );
219 return writer;
220 }
221
222 private XMLWriter initializeRootElementOneDotFour( Writer w )
223 {
224 XMLWriter writer = initializeXmlWriter( w, null );
225 writer.startElement( APPLICATION_ELEMENT );
226 writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/j2ee" );
227 writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
228 writer.addAttribute( "xsi:schemaLocation",
229 "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" );
230 writer.addAttribute( "version", "1.4" );
231 return writer;
232 }
233
234 private XMLWriter initializeRootElementFive( Writer w )
235 {
236 XMLWriter writer = initializeXmlWriter( w, null );
237 writer.startElement( APPLICATION_ELEMENT );
238 writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
239 writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
240 writer.addAttribute( "xsi:schemaLocation",
241 "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" );
242 writer.addAttribute( "version", "5" );
243 return writer;
244 }
245
246 private XMLWriter initializeRootElementSix( Writer w )
247 {
248 XMLWriter writer = initializeXmlWriter( w, null );
249 writer.startElement( APPLICATION_ELEMENT );
250 writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
251 writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
252 writer.addAttribute( "xsi:schemaLocation",
253 "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" );
254 writer.addAttribute( "version", "6" );
255 return writer;
256 }
257
258 private XMLWriter initializeRootElementSeven( Writer w )
259 {
260 XMLWriter writer = initializeXmlWriter( w, null );
261 writer.startElement( APPLICATION_ELEMENT );
262 writer.addAttribute( "xmlns", "http://xmlns.jcp.org/xml/ns/javaee" );
263 writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
264 writer.addAttribute( "xsi:schemaLocation",
265 "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" );
266 writer.addAttribute( "version", "7" );
267 return writer;
268 }
269
270 private XMLWriter initializeRootElementEight( Writer w )
271 {
272 XMLWriter writer = initializeXmlWriter( w, null );
273 writer.startElement( APPLICATION_ELEMENT );
274 writer.addAttribute( "xmlns", "http://xmlns.jcp.org/xml/ns/javaee" );
275 writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
276 writer.addAttribute( "xsi:schemaLocation",
277 "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_8.xsd" );
278 writer.addAttribute( "version", "8" );
279 return writer;
280 }
281
282 private XMLWriter initializeRootElementNine( Writer w )
283 {
284 XMLWriter writer = initializeXmlWriter( w, null );
285 writer.startElement( APPLICATION_ELEMENT );
286 writer.addAttribute( "xmlns", "https://jakarta.ee/xml/ns/jakartaee" );
287 writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
288 writer.addAttribute( "xsi:schemaLocation",
289 "https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/application_9.xsd" );
290 writer.addAttribute( "version", "9" );
291 return writer;
292 }
293
294 private XMLWriter initializeRootElementTen( Writer w )
295 {
296 XMLWriter writer = initializeXmlWriter( w, null );
297 writer.startElement( APPLICATION_ELEMENT );
298 writer.addAttribute( "xmlns", "https://jakarta.ee/xml/ns/jakartaee" );
299 writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
300 writer.addAttribute( "xsi:schemaLocation",
301 "https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/application_10.xsd" );
302 writer.addAttribute( "version", "10" );
303 return writer;
304 }
305 }