Coverage Report - org.apache.maven.plugin.eclipse.writers.EclipseWtpFacetsWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseWtpFacetsWriter
68% 
75% 
4
 
 1  
 package org.apache.maven.plugin.eclipse.writers;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileWriter;
 21  
 import java.io.IOException;
 22  
 
 23  
 import org.apache.maven.plugin.MojoExecutionException;
 24  
 import org.apache.maven.plugin.eclipse.Messages;
 25  
 import org.codehaus.plexus.util.IOUtil;
 26  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 27  
 import org.codehaus.plexus.util.xml.XMLWriter;
 28  
 
 29  
 /**
 30  
  * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
 31  
  * 
 32  
  * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
 33  
  * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
 34  
  * @version $Id: EclipseWtpFacetsWriter.java 424062 2006-07-20 21:04:59Z fgiust $
 35  
  */
 36  2
 public class EclipseWtpFacetsWriter
 37  
     extends AbstractWtpResourceWriter
 38  
 {
 39  
 
 40  
     private static final String FACET_JST_EAR = "jst.ear"; //$NON-NLS-1$
 41  
 
 42  
     private static final String FACET_JST_UTILITY = "jst.utility"; //$NON-NLS-1$
 43  
 
 44  
     private static final String FACET_JST_EJB = "jst.ejb"; //$NON-NLS-1$
 45  
 
 46  
     private static final String FACET_JST_WEB = "jst.web"; //$NON-NLS-1$
 47  
 
 48  
     private static final String FACET_JST_JAVA = "jst.java"; //$NON-NLS-1$
 49  
 
 50  
     private static final String ATTR_VERSION = "version"; //$NON-NLS-1$
 51  
 
 52  
     private static final String ELT_INSTALLED = "installed"; //$NON-NLS-1$
 53  
 
 54  
     private static final String ATTR_FACET = "facet"; //$NON-NLS-1$
 55  
 
 56  
     private static final String ELT_FIXED = "fixed"; //$NON-NLS-1$
 57  
 
 58  
     private static final String ELT_FACETED_PROJECT = "faceted-project"; //$NON-NLS-1$
 59  
 
 60  
     /**
 61  
      * The .settings folder for Web Tools Project 1.x release.
 62  
      */
 63  
     private static final String DIR_WTP_SETTINGS = ".settings"; //$NON-NLS-1$
 64  
 
 65  
     /**
 66  
      * File name where Eclipse Project's Facet configuration will be stored.
 67  
      */
 68  
     private static final String FILE_FACET_CORE_XML = "org.eclipse.wst.common.project.facet.core.xml"; //$NON-NLS-1$
 69  
 
 70  
     /**
 71  
      * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
 72  
      */
 73  
     public void write()
 74  
         throws MojoExecutionException
 75  
     {
 76  
 
 77  
         // create a .settings directory (if not existing)
 78  2
         File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
 79  2
         settingsDir.mkdirs();
 80  
 
 81  
         FileWriter w;
 82  
 
 83  2
         String packaging = config.getProject().getPackaging();
 84  
 
 85  
         // Write out facet core xml
 86  
         try
 87  
         {
 88  2
             w = new FileWriter( new File( settingsDir, FILE_FACET_CORE_XML ) );
 89  
         }
 90  0
         catch ( IOException ex )
 91  
         {
 92  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 93  2
         }
 94  2
         XMLWriter writer = new PrettyPrintXMLWriter( w );
 95  2
         writeModuleTypeFacetCore( writer, packaging );
 96  2
         IOUtil.close( w );
 97  2
     }
 98  
 
 99  
     /**
 100  
      * Writes out the facet info for a faceted-project based on the packaging.
 101  
      * 
 102  
      * @param writer
 103  
      * @param packaging
 104  
      */
 105  
     private void writeModuleTypeFacetCore( XMLWriter writer, String packaging )
 106  
     {
 107  2
         writer.startElement( ELT_FACETED_PROJECT );
 108  
         // common facet
 109  2
         writer.startElement( ELT_FIXED );
 110  2
         writer.addAttribute( ATTR_FACET, FACET_JST_JAVA );
 111  2
         writer.endElement(); // element fixed
 112  2
         if ( "war".equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 113  
         {
 114  1
             writer.startElement( ELT_FIXED );
 115  1
             writer.addAttribute( ATTR_FACET, FACET_JST_WEB );
 116  1
             writer.endElement(); // fixed
 117  1
             writer.startElement( ELT_INSTALLED );
 118  1
             writer.addAttribute( ATTR_FACET, FACET_JST_WEB );
 119  1
             writer.addAttribute( ATTR_VERSION, resolveServletVersion() );
 120  1
             writer.endElement(); // installed
 121  1
         }
 122  1
         else if ( "ejb".equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 123  
         {
 124  0
             writer.startElement( ELT_FIXED );
 125  0
             writer.addAttribute( ATTR_FACET, FACET_JST_EJB );
 126  0
             writer.endElement(); // fixed
 127  0
             writer.startElement( ELT_INSTALLED );
 128  0
             writer.addAttribute( ATTR_FACET, FACET_JST_EJB );
 129  0
             writer.addAttribute( ATTR_VERSION, resolveEjbVersion() );
 130  0
             writer.endElement(); // installed
 131  0
         }
 132  1
         else if ( "ear".equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 133  
         {
 134  1
             writer.startElement( ELT_FIXED );
 135  1
             writer.addAttribute( ATTR_FACET, FACET_JST_EAR );
 136  1
             writer.endElement(); // fixed
 137  1
             writer.startElement( ELT_INSTALLED );
 138  1
             writer.addAttribute( ATTR_FACET, FACET_JST_EAR );
 139  1
             writer.addAttribute( ATTR_VERSION, resolveJ2eeVersion() );
 140  1
             writer.endElement(); // installed
 141  1
         }
 142  0
         else if ( "jar".equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 143  
         {
 144  0
             writer.startElement( ELT_FIXED );
 145  0
             writer.addAttribute( ATTR_FACET, FACET_JST_UTILITY );
 146  0
             writer.endElement(); // fixed
 147  0
             writer.startElement( ELT_INSTALLED );
 148  0
             writer.addAttribute( ATTR_FACET, FACET_JST_UTILITY );
 149  0
             writer.addAttribute( ATTR_VERSION, "1.0" ); //$NON-NLS-1$
 150  0
             writer.endElement(); // installed
 151  
         }
 152  
 
 153  
         // common installed element
 154  2
         writer.startElement( ELT_INSTALLED );
 155  2
         writer.addAttribute( ATTR_FACET, FACET_JST_JAVA );
 156  2
         writer.addAttribute( ATTR_VERSION, resolveJavaVersion() );
 157  2
         writer.endElement(); // installed
 158  2
         writer.endElement(); // faceted-project
 159  2
     }
 160  
 
 161  
 }