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.File;
25  import java.io.Writer;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  /**
30   * An <tt>XmlWriter</tt> based implementation used to generate an
31   * <tt>application.xml</tt> file
32   *
33   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
34   * @version $Id: ApplicationXmlWriter.java 730922 2009-01-03 06:30:22Z snicoll $
35   */
36  final class ApplicationXmlWriter
37      extends AbstractXmlWriter
38  {
39      public static final String DOCTYPE_1_3 = "application PUBLIC\n" +
40          "\t\"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN\"\n" +
41          "\t\"http://java.sun.com/dtd/application_1_3.dtd\"";
42  
43      private static final String APPLICATION_ELEMENT = "application";
44  
45  
46      private final String version;
47  
48      ApplicationXmlWriter( String version, String encoding )
49      {
50          super( encoding );
51          this.version = version;
52      }
53  
54      public void write( ApplicationXmlWriterContext context )
55          throws EarPluginException
56      {
57          Writer w = initializeWriter( context.getDestinationFile() );
58  
59          XMLWriter writer = null;
60          if ( GenerateApplicationXmlMojo.VERSION_1_3.equals( version ) )
61          {
62              writer = initializeRootElementOneDotThree( w );
63              writeDisplayName( context.getDisplayName(), writer );
64              writeDescription( context.getDescription(), writer );
65          }
66          else if ( GenerateApplicationXmlMojo.VERSION_1_4.equals( version ) )
67          {
68              writer = initializeRootElementOneDotFour( w );
69              writeDescription( context.getDescription(), writer );
70              writeDisplayName( context.getDisplayName(), writer );
71          }
72          else if ( GenerateApplicationXmlMojo.VERSION_5.equals( version ) )
73          {
74              writer = initializeRootElementFive( w );
75              writeDescription( context.getDescription(), writer );
76              writeDisplayName( context.getDisplayName(), writer );
77          }
78  
79          final Iterator moduleIt = context.getEarModules().iterator();
80          while ( moduleIt.hasNext() )
81          {
82              EarModule module = (EarModule) moduleIt.next();
83              module.appendModule( writer, version );
84          }
85  
86          final Iterator securityRoleIt = context.getSecurityRoles().iterator();
87          while ( securityRoleIt.hasNext() )
88          {
89              SecurityRole securityRole = (SecurityRole) securityRoleIt.next();
90              securityRole.appendSecurityRole( writer );
91          }
92  
93          if ( GenerateApplicationXmlMojo.VERSION_5.equals( version ) )
94          {
95          	writeLibraryDirectory ( context.getLibraryDirectory(), writer );
96          }
97  
98          writer.endElement();
99  
100         close( w );
101     }
102 
103     private void writeDescription( String description, XMLWriter writer )
104     {
105         if ( description != null )
106         {
107             writer.startElement( "description" );
108             writer.writeText( description );
109             writer.endElement();
110         }
111     }
112 
113     private void writeDisplayName( String displayName, XMLWriter writer )
114     {
115         if ( displayName != null )
116         {
117             writer.startElement( "display-name" );
118             writer.writeText( displayName );
119             writer.endElement();
120         }
121     }
122 
123     private void writeLibraryDirectory( String libraryDirectory, XMLWriter writer )
124     {
125         if ( libraryDirectory != null )
126         {
127             writer.startElement( "library-directory" );
128             writer.writeText( libraryDirectory );
129             writer.endElement();
130         }
131     }
132 
133     private XMLWriter initializeRootElementOneDotThree( Writer w )
134     {
135         XMLWriter writer = initializeXmlWriter( w, DOCTYPE_1_3 );
136         writer.startElement( APPLICATION_ELEMENT );
137         return writer;
138     }
139 
140     private XMLWriter initializeRootElementOneDotFour( Writer w )
141     {
142         XMLWriter writer = initializeXmlWriter( w, null );
143         writer.startElement( APPLICATION_ELEMENT );
144         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/j2ee" );
145         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
146         writer.addAttribute( "xsi:schemaLocation",
147                              "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" );
148         writer.addAttribute( "version", "1.4" );
149         return writer;
150     }
151 
152     private XMLWriter initializeRootElementFive( Writer w )
153     {
154         XMLWriter writer = initializeXmlWriter( w, null );
155         writer.startElement( APPLICATION_ELEMENT );
156         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
157         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
158         writer.addAttribute( "xsi:schemaLocation",
159                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" );
160         writer.addAttribute( "version", "5" );
161         return writer;
162     }
163 }