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 org.apache.maven.plugin.ear.util.JavaEEVersion;
23  import org.codehaus.plexus.util.xml.XMLWriter;
24  
25  import java.io.Writer;
26  import java.util.Iterator;
27  
28  /**
29   * An <tt>XmlWriter</tt> based implementation used to generate an
30   * <tt>application.xml</tt> file
31   *
32   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
33   * @version $Id: ApplicationXmlWriter.java 1050900 2010-12-19 17:10:52Z snicoll $
34   */
35  final class ApplicationXmlWriter
36      extends AbstractXmlWriter
37  {
38      public static final String DOCTYPE_1_3 =
39          "application PUBLIC\n" + "\t\"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN\"\n" +
40              "\t\"http://java.sun.com/dtd/application_1_3.dtd\"";
41  
42      private static final String APPLICATION_ELEMENT = "application";
43  
44  
45      private final JavaEEVersion version;
46  
47      private final Boolean generateModuleId;
48  
49      ApplicationXmlWriter( JavaEEVersion version, String encoding, Boolean generateModuleId )
50      {
51          super( encoding );
52          this.version = version;
53          this.generateModuleId = generateModuleId;
54      }
55  
56      public void write( ApplicationXmlWriterContext context )
57          throws EarPluginException
58      {
59          Writer w = initializeWriter( context.getDestinationFile() );
60  
61          XMLWriter writer = null;
62          if ( JavaEEVersion.OneDotThree.eq( version ) )
63          {
64              writer = initializeRootElementOneDotThree( w );
65          }
66          else if ( JavaEEVersion.OneDotFour.eq( version ) )
67          {
68              writer = initializeRootElementOneDotFour( w );
69          }
70          else if ( JavaEEVersion.Five.eq( version ) )
71          {
72              writer = initializeRootElementFive( w );
73          }
74          else if ( JavaEEVersion.Six.eq( version ) )
75          {
76              writer = initializeRootElementSix( w );
77          }
78  
79          // As from JavaEE6
80          if ( version.ge( JavaEEVersion.Six ) )
81          {
82              writeApplicationName( context.getApplicationName(), writer );
83          }
84  
85          // IMPORTANT: the order of the description and display-name elements was
86          // reversed between J2EE 1.3 and J2EE 1.4.
87          if ( version.eq( JavaEEVersion.OneDotThree ) )
88          {
89              writeDisplayName( context.getDisplayName(), writer );
90              writeDescription( context.getDescription(), writer );
91          }
92          else
93          {
94              writeDescription( context.getDescription(), writer );
95              writeDisplayName( context.getDisplayName(), writer );
96          }
97  
98          // As from JavaEE6
99          if ( version.ge( JavaEEVersion.Six ) )
100         {
101             writeInitializeInOrder( context.getInitializeInOrder(), writer );
102         }
103 
104         // Do not change this unless you really know what you're doing :)
105 
106         final Iterator moduleIt = context.getEarModules().iterator();
107         while ( moduleIt.hasNext() )
108         {
109             EarModule module = (EarModule) moduleIt.next();
110             module.appendModule( writer, version.getVersion(), generateModuleId );
111         }
112 
113         final Iterator securityRoleIt = context.getSecurityRoles().iterator();
114         while ( securityRoleIt.hasNext() )
115         {
116             SecurityRole securityRole = (SecurityRole) securityRoleIt.next();
117             securityRole.appendSecurityRole( writer );
118         }
119 
120         if ( version.ge( JavaEEVersion.Five ) )
121         {
122             writeLibraryDirectory( context.getLibraryDirectory(), writer );
123         }
124 
125         writer.endElement();
126 
127         close( w );
128     }
129 
130     private void writeApplicationName( String applicationName, XMLWriter writer )
131     {
132         if ( applicationName != null )
133         {
134             writer.startElement( "application-name" );
135             writer.writeText( applicationName );
136             writer.endElement();
137         }
138     }
139 
140     private void writeDescription( String description, XMLWriter writer )
141     {
142         if ( description != null )
143         {
144             writer.startElement( "description" );
145             writer.writeText( description );
146             writer.endElement();
147         }
148     }
149 
150     private void writeDisplayName( String displayName, XMLWriter writer )
151     {
152         if ( displayName != null )
153         {
154             writer.startElement( "display-name" );
155             writer.writeText( displayName );
156             writer.endElement();
157         }
158     }
159 
160     private void writeInitializeInOrder( Boolean initializeInOrder, XMLWriter writer )
161     {
162         if ( initializeInOrder != null )
163         {
164             writer.startElement( "initialize-in-order" );
165             writer.writeText( initializeInOrder.toString() );
166             writer.endElement();
167         }
168     }
169 
170     private void writeLibraryDirectory( String libraryDirectory, XMLWriter writer )
171     {
172         if ( libraryDirectory != null )
173         {
174             writer.startElement( "library-directory" );
175             writer.writeText( libraryDirectory );
176             writer.endElement();
177         }
178     }
179 
180     private XMLWriter initializeRootElementOneDotThree( Writer w )
181     {
182         XMLWriter writer = initializeXmlWriter( w, DOCTYPE_1_3 );
183         writer.startElement( APPLICATION_ELEMENT );
184         return writer;
185     }
186 
187     private XMLWriter initializeRootElementOneDotFour( Writer w )
188     {
189         XMLWriter writer = initializeXmlWriter( w, null );
190         writer.startElement( APPLICATION_ELEMENT );
191         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/j2ee" );
192         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
193         writer.addAttribute( "xsi:schemaLocation",
194                              "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" );
195         writer.addAttribute( "version", "1.4" );
196         return writer;
197     }
198 
199     private XMLWriter initializeRootElementFive( Writer w )
200     {
201         XMLWriter writer = initializeXmlWriter( w, null );
202         writer.startElement( APPLICATION_ELEMENT );
203         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
204         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
205         writer.addAttribute( "xsi:schemaLocation",
206                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" );
207         writer.addAttribute( "version", "5" );
208         return writer;
209     }
210 
211     private XMLWriter initializeRootElementSix( Writer w )
212     {
213         XMLWriter writer = initializeXmlWriter( w, null );
214         writer.startElement( APPLICATION_ELEMENT );
215         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
216         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
217         writer.addAttribute( "xsi:schemaLocation",
218                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" );
219         writer.addAttribute( "version", "6" );
220         return writer;
221     }
222 
223 }