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.codehaus.plexus.util.xml.XMLWriter;
23  
24  import java.io.Writer;
25  import java.util.Iterator;
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 899968 2010-01-16 14:53:20Z snicoll $
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 String version;
45  
46      ApplicationXmlWriter( String version, String encoding )
47      {
48          super( encoding );
49          this.version = version;
50      }
51  
52      public void write( ApplicationXmlWriterContext context )
53          throws EarPluginException
54      {
55          Writer w = initializeWriter( context.getDestinationFile() );
56  
57          XMLWriter writer = null;
58          if ( GenerateApplicationXmlMojo.VERSION_1_3.equals( version ) )
59          {
60              writer = initializeRootElementOneDotThree( w );
61          }
62          else if ( GenerateApplicationXmlMojo.VERSION_1_4.equals( version ) )
63          {
64              writer = initializeRootElementOneDotFour( w );
65          }
66          else if ( GenerateApplicationXmlMojo.VERSION_5.equals( version ) )
67          {
68              writer = initializeRootElementFive( w );
69          }
70          else if ( GenerateApplicationXmlMojo.VERSION_6.equals( version ) )
71          {
72              writer = initializeRootElementSix( w );
73          }
74  
75          // IMPORTANT: the order of the description and display-name elements was
76          // reversed between J2EE 1.3 and J2EE 1.4.
77          if ( GenerateApplicationXmlMojo.VERSION_1_3.equals( version ) )
78          {
79              writeDisplayName( context.getDisplayName(), writer );
80              writeDescription( context.getDescription(), writer );
81          }
82          else
83          {
84              writeDescription( context.getDescription(), writer );
85              writeDisplayName( context.getDisplayName(), writer );
86          }
87          // Do not change this unless you really know what you're doing :)
88  
89  
90          final Iterator moduleIt = context.getEarModules().iterator();
91          while ( moduleIt.hasNext() )
92          {
93              EarModule module = (EarModule) moduleIt.next();
94              module.appendModule( writer, version );
95          }
96  
97          final Iterator securityRoleIt = context.getSecurityRoles().iterator();
98          while ( securityRoleIt.hasNext() )
99          {
100             SecurityRole securityRole = (SecurityRole) securityRoleIt.next();
101             securityRole.appendSecurityRole( writer );
102         }
103 
104         if ( GenerateApplicationXmlMojo.VERSION_5.equals( version ) )
105         {
106             writeLibraryDirectory( context.getLibraryDirectory(), writer );
107         }
108 
109         writer.endElement();
110 
111         close( w );
112     }
113 
114     private void writeDescription( String description, XMLWriter writer )
115     {
116         if ( description != null )
117         {
118             writer.startElement( "description" );
119             writer.writeText( description );
120             writer.endElement();
121         }
122     }
123 
124     private void writeDisplayName( String displayName, XMLWriter writer )
125     {
126         if ( displayName != null )
127         {
128             writer.startElement( "display-name" );
129             writer.writeText( displayName );
130             writer.endElement();
131         }
132     }
133 
134     private void writeLibraryDirectory( String libraryDirectory, XMLWriter writer )
135     {
136         if ( libraryDirectory != null )
137         {
138             writer.startElement( "library-directory" );
139             writer.writeText( libraryDirectory );
140             writer.endElement();
141         }
142     }
143 
144     private XMLWriter initializeRootElementOneDotThree( Writer w )
145     {
146         XMLWriter writer = initializeXmlWriter( w, DOCTYPE_1_3 );
147         writer.startElement( APPLICATION_ELEMENT );
148         return writer;
149     }
150 
151     private XMLWriter initializeRootElementOneDotFour( Writer w )
152     {
153         XMLWriter writer = initializeXmlWriter( w, null );
154         writer.startElement( APPLICATION_ELEMENT );
155         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/j2ee" );
156         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
157         writer.addAttribute( "xsi:schemaLocation",
158                              "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" );
159         writer.addAttribute( "version", "1.4" );
160         return writer;
161     }
162 
163     private XMLWriter initializeRootElementFive( Writer w )
164     {
165         XMLWriter writer = initializeXmlWriter( w, null );
166         writer.startElement( APPLICATION_ELEMENT );
167         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
168         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
169         writer.addAttribute( "xsi:schemaLocation",
170                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" );
171         writer.addAttribute( "version", "5" );
172         return writer;
173     }
174 
175     private XMLWriter initializeRootElementSix( Writer w )
176     {
177         XMLWriter writer = initializeXmlWriter( w, null );
178         writer.startElement( APPLICATION_ELEMENT );
179         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
180         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
181         writer.addAttribute( "xsi:schemaLocation",
182                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" );
183         writer.addAttribute( "version", "6" );
184         return writer;
185     }
186 
187 }