Coverage Report - org.apache.maven.plugin.eclipse.writers.rad.RadWebSettingsWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
RadWebSettingsWriter
0%
0/64
0%
0/16
3
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  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,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.maven.plugin.eclipse.writers.rad;
 20  
 
 21  
 import java.io.File;
 22  
 import java.io.FileOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.OutputStreamWriter;
 25  
 import java.io.Writer;
 26  
 
 27  
 import org.apache.maven.plugin.MojoExecutionException;
 28  
 import org.apache.maven.plugin.eclipse.Constants;
 29  
 import org.apache.maven.plugin.eclipse.Messages;
 30  
 import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
 31  
 import org.apache.maven.plugin.ide.IdeDependency;
 32  
 import org.apache.maven.plugin.ide.IdeUtils;
 33  
 import org.apache.maven.plugin.ide.JeeUtils;
 34  
 import org.codehaus.plexus.util.IOUtil;
 35  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 36  
 import org.codehaus.plexus.util.xml.XMLWriter;
 37  
 
 38  
 /**
 39  
  * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
 40  
  * 
 41  
  * @author <a href="mailto:nir@cfc.at">Richard van Nieuwenhoven </a>
 42  
  */
 43  
 public class RadWebSettingsWriter
 44  
     extends AbstractEclipseWriter
 45  
 {
 46  
 
 47  
     private static final String COM_IBM_ETOOLS_SITEEDIT_WIZARDS_PROJECTFEATURE_WEB_SITE_FEATURE =
 48  
         "com.ibm.etools.siteedit.wizards.projectfeature.WebSiteFeature";
 49  
 
 50  
     private static final String WEBSETTINGS_CONTEXT_ROOT = "context-root";
 51  
 
 52  
     private static final String WEBSETTINGS_FEATURE = "feature";
 53  
 
 54  
     private static final String WEBSETTINGS_FEATURE_ID = "feature-id";
 55  
 
 56  
     private static final String WEBSETTINGS_FEATURES = "features";
 57  
 
 58  
     private static final String WEBSETTINGS_FILENAME = ".websettings";
 59  
 
 60  
     private static final String WEBSETTINGS_JSP_LEVEL = "jsp-level";
 61  
 
 62  
     private static final String WEBSETTINGS_PROJECT_TYPE = "project-type";
 63  
 
 64  
     private static final String WEBSETTINGS_TEMPLATEFEATURE = "templatefeature";
 65  
 
 66  
     private static final String WEBSETTINGS_VERSION = "version";
 67  
 
 68  
     private static final String WEBSETTINGS_WEBCONTENT = "webcontent";
 69  
 
 70  
     private static final String WEBSETTINGS_WEBSETTINGS = "websettings";
 71  
 
 72  
     private static final String WEBSETTINGS_LIBMODULES = "lib-modules";
 73  
 
 74  
     private static final String WEBSETTINGS_LIBMODULE = "lib-module";
 75  
 
 76  
     private static final String WEBSETTINGS_LM_JAR = "jar";
 77  
 
 78  
     private static final String WEBSETTINGS_LM_PROJECT = "project";
 79  
 
 80  
     /**
 81  
      * the context root to use for this project
 82  
      */
 83  
     private String warContextRoot;
 84  
 
 85  
     /**
 86  
      * required default constructor.
 87  
      * 
 88  
      * @param warContextRoot the context root to use for this project
 89  
      */
 90  
     public RadWebSettingsWriter( String warContextRoot )
 91  0
     {
 92  0
         this.warContextRoot = warContextRoot;
 93  0
     }
 94  
 
 95  
     /**
 96  
      * write the websettings file for RAD6 if needed.
 97  
      * 
 98  
      * @throws MojoExecutionException when writing the config files was not possible
 99  
      */
 100  
     public void write()
 101  
         throws MojoExecutionException
 102  
     {
 103  
         Writer w;
 104  0
         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( config.getPackaging() ) )
 105  
         {
 106  
             try
 107  
             {
 108  0
                 w =
 109  
                     new OutputStreamWriter( new FileOutputStream( new File( config.getEclipseProjectDirectory(),
 110  
                                                                             WEBSETTINGS_FILENAME ) ), "UTF-8" );
 111  
             }
 112  0
             catch ( IOException ex )
 113  
             {
 114  0
                 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 115  0
             }
 116  
 
 117  0
             XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
 118  0
             writeModuleTypeFacetCore( writer );
 119  0
             IOUtil.close( w );
 120  
         }
 121  0
     }
 122  
 
 123  
     /**
 124  
      * write the websettings file for RAD6.
 125  
      * 
 126  
      * @param writer where to write to
 127  
      * @throws MojoExecutionException
 128  
      */
 129  
     private void writeModuleTypeFacetCore( XMLWriter writer )
 130  
         throws MojoExecutionException
 131  
     {
 132  0
         writer.startElement( WEBSETTINGS_WEBSETTINGS );
 133  0
         writer.addAttribute( WEBSETTINGS_VERSION, "600" );
 134  0
         writer.startElement( WEBSETTINGS_WEBCONTENT );
 135  
 
 136  
         // Generating web content settings based on war plug-in warSourceDirectory property
 137  0
         File warSourceDirectory =
 138  
             new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
 139  
                                                  "warSourceDirectory", //$NON-NLS-1$
 140  
                                                  config.getProject().getBasedir() + "/src/main/webapp" ) ); //$NON-NLS-1$
 141  0
         String webContentDir =
 142  
             IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
 143  
 
 144  0
         writer.writeText( webContentDir );
 145  
 
 146  0
         writer.endElement();
 147  0
         writer.startElement( WEBSETTINGS_CONTEXT_ROOT );
 148  0
         writer.writeText( getContextRoot( warContextRoot ) );
 149  0
         writer.endElement();
 150  0
         writer.startElement( WEBSETTINGS_PROJECT_TYPE );
 151  0
         writer.writeText( "J2EE" );
 152  0
         writer.endElement();
 153  0
         writer.startElement( WEBSETTINGS_JSP_LEVEL );
 154  0
         writer.writeText( JeeUtils.resolveJspVersion( config.getProject() ) );
 155  0
         writer.endElement();
 156  0
         writer.startElement( WEBSETTINGS_FEATURES );
 157  0
         writer.startElement( WEBSETTINGS_FEATURE );
 158  0
         writer.startElement( WEBSETTINGS_FEATURE_ID );
 159  0
         writer.writeText( WEBSETTINGS_TEMPLATEFEATURE );
 160  0
         writer.endElement();
 161  0
         writer.endElement();
 162  0
         writer.startElement( WEBSETTINGS_FEATURE );
 163  0
         writer.startElement( WEBSETTINGS_FEATURE_ID );
 164  0
         writer.writeText( COM_IBM_ETOOLS_SITEEDIT_WIZARDS_PROJECTFEATURE_WEB_SITE_FEATURE );
 165  0
         writer.endElement();
 166  0
         writer.endElement();
 167  0
         writer.endElement();
 168  
 
 169  
         // library modules
 170  0
         writer.startElement( WEBSETTINGS_LIBMODULES );
 171  
 
 172  
         // iterate relevant dependencies (non-test, non-provided, project)
 173  0
         IdeDependency[] deps = config.getDepsOrdered();
 174  0
         if ( deps != null )
 175  
         {
 176  0
             for ( int i = 0; i < deps.length; i++ )
 177  
             {
 178  0
                 final IdeDependency dependency = deps[i];
 179  0
                 log.debug( "RadWebSettingsWriter: checking dependency " + dependency.toString() );
 180  
 
 181  0
                 if ( dependency.isReferencedProject() && !dependency.isTestDependency() && !dependency.isProvided() )
 182  
                 {
 183  0
                     log.debug( "RadWebSettingsWriter: dependency " + dependency.toString()
 184  
                         + " selected for inclusion as lib-module" );
 185  
 
 186  0
                     String depName = dependency.getEclipseProjectName();
 187  0
                     String depJar = dependency.getArtifactId() + ".jar";
 188  
 
 189  0
                     writer.startElement( WEBSETTINGS_LIBMODULE );
 190  
 
 191  0
                     writer.startElement( WEBSETTINGS_LM_JAR );
 192  0
                     writer.writeText( depJar );
 193  0
                     writer.endElement(); // jar
 194  
 
 195  0
                     writer.startElement( WEBSETTINGS_LM_PROJECT );
 196  0
                     writer.writeText( depName );
 197  0
                     writer.endElement(); // project
 198  
 
 199  0
                     writer.endElement(); // libmodule
 200  
                 }
 201  
             }
 202  
         }
 203  
 
 204  0
         writer.endElement(); // libmodules
 205  0
         writer.endElement(); // websettings
 206  
 
 207  0
     }
 208  
 
 209  
     /**
 210  
      * Create the ContextRoot for this project, the default is the artifact id
 211  
      * 
 212  
      * @param warContextRoot set as a configuration property.
 213  
      * @return the context root to use
 214  
      */
 215  
     private String getContextRoot( String warContextRoot )
 216  
     {
 217  0
         if ( warContextRoot == null || warContextRoot.length() == 0 )
 218  
         {
 219  0
             return config.getProject().getArtifactId();
 220  
         }
 221  
         else
 222  
         {
 223  0
             return warContextRoot;
 224  
         }
 225  
     }
 226  
 
 227  
 }