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