View Javadoc
1   package org.apache.maven.plugin.ear;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Writer;
23  
24  import org.apache.maven.plugin.ear.util.JavaEEVersion;
25  import org.codehaus.plexus.util.xml.XMLWriter;
26  
27  /**
28   * An <tt>XmlWriter</tt> based implementation used to generate an <tt>application.xml</tt> file
29   * 
30   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
31   * @version $Id: ApplicationXmlWriter.java 1648055 2014-12-27 14:59:45Z khmarbaise $
32   */
33  final class ApplicationXmlWriter
34      extends AbstractXmlWriter
35  {
36      public static final String DOCTYPE_1_3 = "application PUBLIC\n"
37          + "\t\"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN\"\n"
38          + "\t\"http://java.sun.com/dtd/application_1_3.dtd\"";
39  
40      private static final String APPLICATION_ELEMENT = "application";
41  
42      private final JavaEEVersion version;
43  
44      private final Boolean generateModuleId;
45  
46      ApplicationXmlWriter( JavaEEVersion version, String encoding, Boolean generateModuleId )
47      {
48          super( encoding );
49          this.version = version;
50          this.generateModuleId = generateModuleId;
51      }
52  
53      public void write( ApplicationXmlWriterContext context )
54          throws EarPluginException
55      {
56          Writer w = initializeWriter( context.getDestinationFile() );
57  
58          XMLWriter writer = null;
59          if ( JavaEEVersion.ONE_DOT_THREE.eq( version ) )
60          {
61              writer = initializeRootElementOneDotThree( w );
62          }
63          else if ( JavaEEVersion.ONE_DOT_FOUR.eq( version ) )
64          {
65              writer = initializeRootElementOneDotFour( w );
66          }
67          else if ( JavaEEVersion.FIVE.eq( version ) )
68          {
69              writer = initializeRootElementFive( w );
70          }
71          else if ( JavaEEVersion.SIX.eq( version ) )
72          {
73              writer = initializeRootElementSix( w );
74          }
75          else if ( JavaEEVersion.SEVEN.eq( version ) )
76          {
77              writer = initializeRootElementSeven( w );
78          }
79  
80          // writer is still on root element, so we can still add this attribute
81          if ( context.getApplicationId() != null )
82          {
83              writer.addAttribute( "id", context.getApplicationId() );
84          }
85  
86          // As from JavaEE6
87          if ( version.ge( JavaEEVersion.SIX ) )
88          {
89              writeApplicationName( context.getApplicationName(), writer );
90          }
91  
92          // IMPORTANT: the order of the description and display-name elements was
93          // reversed between J2EE 1.3 and J2EE 1.4.
94          if ( version.eq( JavaEEVersion.ONE_DOT_THREE ) )
95          {
96              writeDisplayName( context.getDisplayName(), writer );
97              writeDescription( context.getDescription(), writer );
98          }
99          else
100         {
101             writeDescription( context.getDescription(), writer );
102             writeDisplayName( context.getDisplayName(), writer );
103         }
104 
105         // As from JavaEE6
106         if ( version.ge( JavaEEVersion.SIX ) )
107         {
108             writeInitializeInOrder( context.getInitializeInOrder(), writer );
109         }
110 
111         // Do not change this unless you really know what you're doing :)
112         for ( EarModule module : context.getEarModules() )
113         {
114             module.appendModule( writer, version.getVersion(), generateModuleId );
115         }
116 
117         for ( SecurityRole securityRole : context.getSecurityRoles() )
118         {
119             securityRole.appendSecurityRole( writer );
120         }
121 
122         if ( version.ge( JavaEEVersion.FIVE ) )
123         {
124             writeLibraryDirectory( context.getLibraryDirectory(), writer );
125         }
126 
127         if ( version.ge( JavaEEVersion.SIX ) )
128         {
129             for ( EnvEntry envEntry : context.getEnvEntries() )
130             {
131                 envEntry.appendEnvEntry( writer );
132             }
133             for ( EjbRef ejbEntry : context.getEjbEntries() )
134             {
135                 ejbEntry.appendEjbRefEntry( writer );
136             }
137         }
138 
139         writer.endElement();
140 
141         close( w );
142     }
143 
144     private void writeApplicationName( String applicationName, XMLWriter writer )
145     {
146         if ( applicationName != null )
147         {
148             writer.startElement( "application-name" );
149             writer.writeText( applicationName );
150             writer.endElement();
151         }
152     }
153 
154     private void writeDescription( String description, XMLWriter writer )
155     {
156         if ( description != null )
157         {
158             writer.startElement( "description" );
159             writer.writeText( description );
160             writer.endElement();
161         }
162     }
163 
164     private void writeDisplayName( String displayName, XMLWriter writer )
165     {
166         if ( displayName != null )
167         {
168             writer.startElement( "display-name" );
169             writer.writeText( displayName );
170             writer.endElement();
171         }
172     }
173 
174     private void writeInitializeInOrder( Boolean initializeInOrder, XMLWriter writer )
175     {
176         if ( initializeInOrder != null )
177         {
178             writer.startElement( "initialize-in-order" );
179             writer.writeText( initializeInOrder.toString() );
180             writer.endElement();
181         }
182     }
183 
184     private void writeLibraryDirectory( String libraryDirectory, XMLWriter writer )
185     {
186         if ( libraryDirectory != null )
187         {
188             writer.startElement( "library-directory" );
189             writer.writeText( libraryDirectory );
190             writer.endElement();
191         }
192     }
193 
194     private XMLWriter initializeRootElementOneDotThree( Writer w )
195     {
196         XMLWriter writer = initializeXmlWriter( w, DOCTYPE_1_3 );
197         writer.startElement( APPLICATION_ELEMENT );
198         return writer;
199     }
200 
201     private XMLWriter initializeRootElementOneDotFour( Writer w )
202     {
203         XMLWriter writer = initializeXmlWriter( w, null );
204         writer.startElement( APPLICATION_ELEMENT );
205         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/j2ee" );
206         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
207         writer.addAttribute( "xsi:schemaLocation",
208                              "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" );
209         writer.addAttribute( "version", "1.4" );
210         return writer;
211     }
212 
213     private XMLWriter initializeRootElementFive( Writer w )
214     {
215         XMLWriter writer = initializeXmlWriter( w, null );
216         writer.startElement( APPLICATION_ELEMENT );
217         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
218         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
219         writer.addAttribute( "xsi:schemaLocation",
220                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" );
221         writer.addAttribute( "version", "5" );
222         return writer;
223     }
224 
225     private XMLWriter initializeRootElementSix( Writer w )
226     {
227         XMLWriter writer = initializeXmlWriter( w, null );
228         writer.startElement( APPLICATION_ELEMENT );
229         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
230         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
231         writer.addAttribute( "xsi:schemaLocation",
232                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" );
233         writer.addAttribute( "version", "6" );
234         return writer;
235     }
236 
237     private XMLWriter initializeRootElementSeven( Writer w )
238     {
239         XMLWriter writer = initializeXmlWriter( w, null );
240         writer.startElement( APPLICATION_ELEMENT );
241         writer.addAttribute( "xmlns", "http://xmlns.jcp.org/xml/ns/javaee" );
242         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
243         // CHECKSTYLE_OFF: LineLength
244         writer.addAttribute( "xsi:schemaLocation",
245                              "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" );
246         // CHECKSTYLE_ON: LineLength
247         writer.addAttribute( "version", "7" );
248         return writer;
249     }
250 }