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