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