Coverage Report - org.apache.maven.plugin.eclipse.writers.EclipseLaunchConfigurationWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseLaunchConfigurationWriter
94%
60/64
64%
9/14
1.667
 
 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;
 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  
 import java.util.Collections;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 
 30  
 import org.apache.maven.plugin.MojoExecutionException;
 31  
 import org.apache.maven.plugin.eclipse.Messages;
 32  
 import org.apache.maven.plugin.logging.Log;
 33  
 import org.codehaus.plexus.util.IOUtil;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 36  
 import org.codehaus.plexus.util.xml.XMLWriter;
 37  
 
 38  
 /**
 39  
  * Base class for writing external launch configuration files.
 40  
  * 
 41  
  * @author <a href="mailto:kenneyw@neonics.com">Kenney Westerhof</a>
 42  
  */
 43  4
 public abstract class EclipseLaunchConfigurationWriter
 44  
     extends AbstractEclipseWriter
 45  
 {
 46  
     public static final String FILE_DOT_EXTERNAL_TOOL_BUILDERS = ".externalToolBuilders/";
 47  
 
 48  
     private String filename;
 49  
 
 50  
     private boolean initialized;
 51  
 
 52  
     /**
 53  
      * Filename including .launch
 54  
      * 
 55  
      * @param filename
 56  
      */
 57  
     protected EclipseWriter init( Log log, EclipseWriterConfig config, String filename )
 58  
     {
 59  4
         this.filename = filename;
 60  4
         initialized = true;
 61  4
         return super.init( log, config );
 62  
     }
 63  
 
 64  
     public void write()
 65  
         throws MojoExecutionException
 66  
     {
 67  4
         if ( !initialized )
 68  
         {
 69  0
             throw new MojoExecutionException( "Not initialized" );
 70  
         }
 71  
 
 72  
         Writer w;
 73  
 
 74  
         try
 75  
         {
 76  4
             File extToolsDir = new File( config.getEclipseProjectDirectory(), FILE_DOT_EXTERNAL_TOOL_BUILDERS );
 77  4
             if ( !extToolsDir.exists() && !extToolsDir.mkdir() )
 78  
             {
 79  0
                 throw new MojoExecutionException( "Error creating directory " + extToolsDir );
 80  
             }
 81  4
             w = new OutputStreamWriter( new FileOutputStream( new File( extToolsDir, filename ) ), "UTF-8" );
 82  
         }
 83  0
         catch ( IOException ex )
 84  
         {
 85  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 86  4
         }
 87  
 
 88  4
         XMLWriter writer = new PrettyPrintXMLWriter( w );
 89  
 
 90  4
         writer.startElement( "launchConfiguration" );
 91  4
         writer.addAttribute( "type", getLaunchConfigurationType() );
 92  
 
 93  4
         writeAttribute( writer, "org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND", isLaunchInBackground() );
 94  
 
 95  4
         writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS",
 96  
                         StringUtils.join( getRunBuildKinds(), "," ) );
 97  
 
 98  
         // i think this one means if the ATTR_RUN_BUILD_KINDS is not default.
 99  4
         writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED", true );
 100  
 
 101  4
         writeAttribute( writer, "org.eclipse.debug.core.appendEnvironmentVariables", isAppendEnvironmentVariables() );
 102  
 
 103  4
         writeAttribute( writer, "org.eclipse.jdt.launching.PROJECT_ATTR", config.getEclipseProjectName() );
 104  
 
 105  4
         writeAttribute( writer, "org.eclipse.jdt.launching.DEFAULT_CLASSPATH", true );
 106  
 
 107  4
         writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_LOCATION", getBuilderLocation() );
 108  
 
 109  4
         if ( getWorkingDirectory() != null )
 110  
         {
 111  4
             writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY", getWorkingDirectory() );
 112  
         }
 113  
 
 114  4
         if ( getRefreshScope() != null )
 115  
         {
 116  4
             writeAttribute( writer, "org.eclipse.debug.core.ATTR_REFRESH_SCOPE", getRefreshScope() );
 117  
         }
 118  
 
 119  4
         writeAttribute( writer, "org.eclipse.debug.core.capture_output", isCaptureOutput() );
 120  
 
 121  4
         String workingSet =
 122  
             "<?xml version='1.0'?>"
 123  
                 + "<launchConfigurationWorkingSet editPageId='org.eclipse.ui.resourceWorkingSetPage'"
 124  
                 + " factoryID='org.eclipse.ui.internal.WorkingSetFactory'" + " label='workingSet'"
 125  
                 + " name='workingSet'>";
 126  
 
 127  4
         for ( Iterator it = getMonitoredResources().iterator(); it.hasNext(); )
 128  
         {
 129  4
             MonitoredResource monitoredResource = (MonitoredResource) it.next();
 130  
 
 131  4
             workingSet += monitoredResource.print();
 132  
         }
 133  
 
 134  4
         workingSet += "</launchConfigurationWorkingSet>";
 135  
 
 136  4
         writeAttribute( writer, "org.eclipse.ui.externaltools.ATTR_BUILD_SCOPE", "${working_set:" + workingSet + "}" );
 137  
 
 138  4
         addAttributes( writer );
 139  
 
 140  4
         writer.endElement();
 141  
 
 142  4
         IOUtil.close( w );
 143  4
     }
 144  
 
 145  
     protected List getMonitoredResources()
 146  
     {
 147  4
         return Collections.singletonList( new MonitoredResource( config.getEclipseProjectName(),
 148  
                                                                  MonitoredResource.PROJECT ) );
 149  
     }
 150  
 
 151  
     protected abstract void addAttributes( XMLWriter writer );
 152  
 
 153  
     /**
 154  
      * Wheter to allocate a console.
 155  
      */
 156  
     private boolean isCaptureOutput()
 157  
     {
 158  4
         return false;
 159  
     }
 160  
 
 161  
     private String getWorkingDirectory()
 162  
     {
 163  8
         return "${build_project}";
 164  
     }
 165  
 
 166  
     protected String getRefreshScope()
 167  
     {
 168  8
         return "${project}";
 169  
     }
 170  
 
 171  
     protected abstract String getBuilderLocation();
 172  
 
 173  
     protected String[] getRunBuildKinds()
 174  
     {
 175  4
         return new String[] { "full", "incremental", "auto", "clean" };
 176  
     }
 177  
 
 178  
     protected boolean isAppendEnvironmentVariables()
 179  
     {
 180  4
         return true;
 181  
     }
 182  
 
 183  
     protected boolean isLaunchInBackground()
 184  
     {
 185  4
         return false;
 186  
     }
 187  
 
 188  
     protected abstract String getLaunchConfigurationType();
 189  
 
 190  
     protected static void writeAttribute( XMLWriter writer, String key, String value )
 191  
     {
 192  32
         writer.startElement( "stringAttribute" );
 193  32
         writer.addAttribute( "key", key );
 194  32
         writer.addAttribute( "value", value );
 195  32
         writer.endElement();
 196  32
     }
 197  
 
 198  
     protected static void writeAttribute( XMLWriter writer, String key, boolean value )
 199  
     {
 200  32
         writer.startElement( "booleanAttribute" );
 201  32
         writer.addAttribute( "key", key );
 202  32
         writer.addAttribute( "value", "" + value );
 203  32
         writer.endElement();
 204  32
     }
 205  
 
 206  
     protected static void writeAttribute( XMLWriter writer, String key, String[] values )
 207  
     {
 208  8
         writer.startElement( "listAttribute" );
 209  8
         writer.addAttribute( "key", key );
 210  
 
 211  16
         for ( int i = 0; i < values.length; i++ )
 212  
         {
 213  8
             String value = values[i];
 214  8
             writer.startElement( "listEntry" );
 215  8
             writer.addAttribute( "value", value );
 216  8
             writer.endElement();
 217  
         }
 218  
 
 219  8
         writer.endElement();
 220  8
     }
 221  
 
 222  
 }