Coverage Report - org.apache.maven.plugin.ear.ApplicationXmlWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
ApplicationXmlWriter
0%
0/87
0%
0/30
2.364
 
 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  0
         super( encoding );
 52  0
         this.version = version;
 53  0
         this.generateModuleId = generateModuleId;
 54  0
     }
 55  
 
 56  
     public void write( ApplicationXmlWriterContext context )
 57  
         throws EarPluginException
 58  
     {
 59  0
         Writer w = initializeWriter( context.getDestinationFile() );
 60  
 
 61  0
         XMLWriter writer = null;
 62  0
         if ( JavaEEVersion.OneDotThree.eq( version ) )
 63  
         {
 64  0
             writer = initializeRootElementOneDotThree( w );
 65  
         }
 66  0
         else if ( JavaEEVersion.OneDotFour.eq( version ) )
 67  
         {
 68  0
             writer = initializeRootElementOneDotFour( w );
 69  
         }
 70  0
         else if ( JavaEEVersion.Five.eq( version ) )
 71  
         {
 72  0
             writer = initializeRootElementFive( w );
 73  
         }
 74  0
         else if ( JavaEEVersion.Six.eq( version ) )
 75  
         {
 76  0
             writer = initializeRootElementSix( w );
 77  
         }
 78  
 
 79  
         // As from JavaEE6
 80  0
         if ( version.ge( JavaEEVersion.Six ) )
 81  
         {
 82  0
             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  0
         if ( version.eq( JavaEEVersion.OneDotThree ) )
 88  
         {
 89  0
             writeDisplayName( context.getDisplayName(), writer );
 90  0
             writeDescription( context.getDescription(), writer );
 91  
         }
 92  
         else
 93  
         {
 94  0
             writeDescription( context.getDescription(), writer );
 95  0
             writeDisplayName( context.getDisplayName(), writer );
 96  
         }
 97  
 
 98  
         // As from JavaEE6
 99  0
         if ( version.ge( JavaEEVersion.Six ) )
 100  
         {
 101  0
             writeInitializeInOrder( context.getInitializeInOrder(), writer );
 102  
         }
 103  
 
 104  
         // Do not change this unless you really know what you're doing :)
 105  
 
 106  0
         final Iterator moduleIt = context.getEarModules().iterator();
 107  0
         while ( moduleIt.hasNext() )
 108  
         {
 109  0
             EarModule module = (EarModule) moduleIt.next();
 110  0
             module.appendModule( writer, version.getVersion(), generateModuleId );
 111  0
         }
 112  
 
 113  0
         final Iterator securityRoleIt = context.getSecurityRoles().iterator();
 114  0
         while ( securityRoleIt.hasNext() )
 115  
         {
 116  0
             SecurityRole securityRole = (SecurityRole) securityRoleIt.next();
 117  0
             securityRole.appendSecurityRole( writer );
 118  0
         }
 119  
 
 120  0
         if ( version.ge( JavaEEVersion.Five ) )
 121  
         {
 122  0
             writeLibraryDirectory( context.getLibraryDirectory(), writer );
 123  
         }
 124  
 
 125  0
         writer.endElement();
 126  
 
 127  0
         close( w );
 128  0
     }
 129  
 
 130  
     private void writeApplicationName( String applicationName, XMLWriter writer )
 131  
     {
 132  0
         if ( applicationName != null )
 133  
         {
 134  0
             writer.startElement( "application-name" );
 135  0
             writer.writeText( applicationName );
 136  0
             writer.endElement();
 137  
         }
 138  0
     }
 139  
 
 140  
     private void writeDescription( String description, XMLWriter writer )
 141  
     {
 142  0
         if ( description != null )
 143  
         {
 144  0
             writer.startElement( "description" );
 145  0
             writer.writeText( description );
 146  0
             writer.endElement();
 147  
         }
 148  0
     }
 149  
 
 150  
     private void writeDisplayName( String displayName, XMLWriter writer )
 151  
     {
 152  0
         if ( displayName != null )
 153  
         {
 154  0
             writer.startElement( "display-name" );
 155  0
             writer.writeText( displayName );
 156  0
             writer.endElement();
 157  
         }
 158  0
     }
 159  
 
 160  
     private void writeInitializeInOrder( Boolean initializeInOrder, XMLWriter writer )
 161  
     {
 162  0
         if ( initializeInOrder != null )
 163  
         {
 164  0
             writer.startElement( "initialize-in-order" );
 165  0
             writer.writeText( initializeInOrder.toString() );
 166  0
             writer.endElement();
 167  
         }
 168  0
     }
 169  
 
 170  
     private void writeLibraryDirectory( String libraryDirectory, XMLWriter writer )
 171  
     {
 172  0
         if ( libraryDirectory != null )
 173  
         {
 174  0
             writer.startElement( "library-directory" );
 175  0
             writer.writeText( libraryDirectory );
 176  0
             writer.endElement();
 177  
         }
 178  0
     }
 179  
 
 180  
     private XMLWriter initializeRootElementOneDotThree( Writer w )
 181  
     {
 182  0
         XMLWriter writer = initializeXmlWriter( w, DOCTYPE_1_3 );
 183  0
         writer.startElement( APPLICATION_ELEMENT );
 184  0
         return writer;
 185  
     }
 186  
 
 187  
     private XMLWriter initializeRootElementOneDotFour( Writer w )
 188  
     {
 189  0
         XMLWriter writer = initializeXmlWriter( w, null );
 190  0
         writer.startElement( APPLICATION_ELEMENT );
 191  0
         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/j2ee" );
 192  0
         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
 193  0
         writer.addAttribute( "xsi:schemaLocation",
 194  
                              "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" );
 195  0
         writer.addAttribute( "version", "1.4" );
 196  0
         return writer;
 197  
     }
 198  
 
 199  
     private XMLWriter initializeRootElementFive( Writer w )
 200  
     {
 201  0
         XMLWriter writer = initializeXmlWriter( w, null );
 202  0
         writer.startElement( APPLICATION_ELEMENT );
 203  0
         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
 204  0
         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
 205  0
         writer.addAttribute( "xsi:schemaLocation",
 206  
                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" );
 207  0
         writer.addAttribute( "version", "5" );
 208  0
         return writer;
 209  
     }
 210  
 
 211  
     private XMLWriter initializeRootElementSix( Writer w )
 212  
     {
 213  0
         XMLWriter writer = initializeXmlWriter( w, null );
 214  0
         writer.startElement( APPLICATION_ELEMENT );
 215  0
         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
 216  0
         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
 217  0
         writer.addAttribute( "xsi:schemaLocation",
 218  
                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" );
 219  0
         writer.addAttribute( "version", "6" );
 220  0
         return writer;
 221  
     }
 222  
 
 223  
 }