Coverage Report - org.apache.maven.plugin.eclipse.writers.EclipseProjectWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseProjectWriter
0%
0/119
0%
0/62
6.6
 
 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.FileInputStream;
 23  
 import java.io.FileOutputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStreamReader;
 26  
 import java.io.OutputStreamWriter;
 27  
 import java.io.Reader;
 28  
 import java.io.Writer;
 29  
 import java.util.Iterator;
 30  
 import java.util.LinkedHashSet;
 31  
 import java.util.List;
 32  
 import java.util.Set;
 33  
 
 34  
 import org.apache.maven.model.Resource;
 35  
 import org.apache.maven.plugin.MojoExecutionException;
 36  
 import org.apache.maven.plugin.eclipse.BuildCommand;
 37  
 import org.apache.maven.plugin.eclipse.Messages;
 38  
 import org.apache.maven.plugin.ide.IdeDependency;
 39  
 import org.apache.maven.plugin.ide.IdeUtils;
 40  
 import org.codehaus.plexus.util.IOUtil;
 41  
 import org.codehaus.plexus.util.StringUtils;
 42  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 43  
 import org.codehaus.plexus.util.xml.XMLWriter;
 44  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 45  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 46  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 47  
 
 48  
 /**
 49  
  * Writes eclipse .project file.
 50  
  * 
 51  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 52  
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
 53  
  * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
 54  
  * @version $Id: EclipseProjectWriter.java 616816 2008-01-30 17:23:08Z aheritier $
 55  
  */
 56  0
 public class EclipseProjectWriter
 57  
     extends AbstractEclipseWriter
 58  
 {
 59  
     private static final String ELT_NAME = "name"; //$NON-NLS-1$
 60  
 
 61  
     private static final String ELT_BUILD_COMMAND = "buildCommand"; //$NON-NLS-1$
 62  
 
 63  
     private static final String ELT_BUILD_SPEC = "buildSpec"; //$NON-NLS-1$
 64  
 
 65  
     private static final String ELT_NATURE = "nature"; //$NON-NLS-1$
 66  
 
 67  
     private static final String ELT_NATURES = "natures"; //$NON-NLS-1$
 68  
 
 69  
     private static final String FILE_DOT_PROJECT = ".project"; //$NON-NLS-1$
 70  
 
 71  
     /**
 72  
      * Constant for links to files.
 73  
      */
 74  
     private static final int LINK_TYPE_FILE = 1;
 75  
 
 76  
     /**
 77  
      * Constant for links to directories.
 78  
      */
 79  
     private static final int LINK_TYPE_DIRECTORY = 2;
 80  
 
 81  
     /**
 82  
      * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
 83  
      */
 84  
     public void write()
 85  
         throws MojoExecutionException
 86  
     {
 87  
 
 88  0
         Set projectnatures = new LinkedHashSet();
 89  0
         Set buildCommands = new LinkedHashSet();
 90  
 
 91  0
         File dotProject = new File( config.getEclipseProjectDirectory(), FILE_DOT_PROJECT );
 92  
 
 93  0
         if ( dotProject.exists() )
 94  
         {
 95  
 
 96  0
             log.info( Messages.getString( "EclipsePlugin.keepexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$
 97  
 
 98  
             // parse existing file in order to keep manually-added entries
 99  0
             Reader reader = null;
 100  
             try
 101  
             {
 102  0
                 reader = new InputStreamReader( new FileInputStream( dotProject ), "UTF-8" );
 103  0
                 Xpp3Dom dom = Xpp3DomBuilder.build( reader );
 104  
 
 105  0
                 Xpp3Dom naturesElement = dom.getChild( ELT_NATURES );
 106  0
                 if ( naturesElement != null )
 107  
                 {
 108  0
                     Xpp3Dom[] existingNatures = naturesElement.getChildren( ELT_NATURE );
 109  0
                     for ( int j = 0; j < existingNatures.length; j++ )
 110  
                     {
 111  
                         // adds all the existing natures
 112  0
                         projectnatures.add( existingNatures[j].getValue() );
 113  
                     }
 114  
                 }
 115  
 
 116  0
                 Xpp3Dom buildSpec = dom.getChild( ELT_BUILD_SPEC );
 117  0
                 if ( buildSpec != null )
 118  
                 {
 119  0
                     Xpp3Dom[] existingBuildCommands = buildSpec.getChildren( ELT_BUILD_COMMAND );
 120  0
                     for ( int j = 0; j < existingBuildCommands.length; j++ )
 121  
                     {
 122  0
                         Xpp3Dom buildCommandName = existingBuildCommands[j].getChild( ELT_NAME );
 123  0
                         if ( buildCommandName != null )
 124  
                         {
 125  0
                             buildCommands.add( new BuildCommand( existingBuildCommands[j] ) );
 126  
                         }
 127  
                     }
 128  
                 }
 129  
             }
 130  0
             catch ( XmlPullParserException e )
 131  
             {
 132  0
                 log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$
 133  
             }
 134  0
             catch ( IOException e )
 135  
             {
 136  0
                 log.warn( Messages.getString( "EclipsePlugin.cantparseexisting", dotProject.getAbsolutePath() ) ); //$NON-NLS-1$
 137  
             }
 138  
             finally
 139  
             {
 140  0
                 IOUtil.close( reader );
 141  0
             }
 142  
         }
 143  
 
 144  
         // adds new entries after the existing ones
 145  0
         for ( Iterator iter = config.getProjectnatures().iterator(); iter.hasNext(); )
 146  
         {
 147  0
             projectnatures.add( iter.next() );
 148  
         }
 149  
 
 150  0
         for ( Iterator iter = config.getBuildCommands().iterator(); iter.hasNext(); )
 151  
         {
 152  0
             buildCommands.add( (BuildCommand) iter.next() );
 153  
         }
 154  
 
 155  
         Writer w;
 156  
 
 157  
         try
 158  
         {
 159  0
             w = new OutputStreamWriter( new FileOutputStream( dotProject ), "UTF-8" );
 160  
         }
 161  0
         catch ( IOException ex )
 162  
         {
 163  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 164  0
         }
 165  
 
 166  0
         XMLWriter writer = new PrettyPrintXMLWriter( w );
 167  
 
 168  0
         writer.startElement( "projectDescription" ); //$NON-NLS-1$
 169  
 
 170  0
         writer.startElement( ELT_NAME );
 171  0
         writer.writeText( config.getEclipseProjectName() );
 172  0
         writer.endElement();
 173  
 
 174  
         // TODO: this entire element might be dropped if the comment is null.
 175  
         // but as the maven1 eclipse plugin does it, it's better to be safe than sorry
 176  
         // A eclipse developer might want to look at this.
 177  0
         writer.startElement( "comment" ); //$NON-NLS-1$
 178  
 
 179  0
         if ( config.getProject().getDescription() != null )
 180  
         {
 181  0
             writer.writeText( config.getProject().getDescription() );
 182  
         }
 183  
 
 184  0
         writer.endElement();
 185  
 
 186  0
         writer.startElement( "projects" ); //$NON-NLS-1$
 187  
 
 188  
         // referenced projects should not be added for plugins
 189  0
         if ( !config.isPde() )
 190  
         {
 191  0
             for ( int j = 0; j < config.getDepsOrdered().length; j++ )
 192  
             {
 193  0
                 IdeDependency dep = config.getDepsOrdered()[j];
 194  0
                 if ( dep.isReferencedProject() )
 195  
                 {
 196  0
                     writer.startElement( "project" ); //$NON-NLS-1$
 197  0
                     writer.writeText( dep.getEclipseProjectName() );
 198  0
                     writer.endElement();
 199  
                 }
 200  
             }
 201  
         }
 202  
 
 203  0
         writer.endElement(); // projects
 204  
 
 205  0
         writer.startElement( ELT_BUILD_SPEC );
 206  
 
 207  0
         for ( Iterator it = buildCommands.iterator(); it.hasNext(); )
 208  
         {
 209  0
             ( (BuildCommand) it.next() ).print( writer );
 210  
         }
 211  
 
 212  0
         writer.endElement(); // buildSpec
 213  
 
 214  0
         writer.startElement( ELT_NATURES );
 215  
 
 216  0
         for ( Iterator it = projectnatures.iterator(); it.hasNext(); )
 217  
         {
 218  0
             writer.startElement( ELT_NATURE );
 219  0
             writer.writeText( (String) it.next() );
 220  0
             writer.endElement(); // name
 221  
         }
 222  
 
 223  0
         writer.endElement(); // natures
 224  
 
 225  0
         boolean addLinks = !config.getProjectBaseDir().equals( config.getEclipseProjectDirectory() );
 226  
 
 227  0
         if ( addLinks || ( config.isPde() && config.getDepsOrdered().length > 0 ) )
 228  
         {
 229  0
             writer.startElement( "linkedResources" ); //$NON-NLS-1$
 230  
 
 231  0
             if ( addLinks )
 232  
             {
 233  
 
 234  0
                 addFileLink( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 235  
                              config.getProject().getFile() );
 236  
 
 237  0
                 addSourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 238  
                                 config.getProject().getCompileSourceRoots() );
 239  0
                 addResourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 240  
                                   config.getProject().getBuild().getResources() );
 241  
 
 242  0
                 addSourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 243  
                                 config.getProject().getTestCompileSourceRoots() );
 244  0
                 addResourceLinks( writer, config.getProjectBaseDir(), config.getEclipseProjectDirectory(),
 245  
                                   config.getProject().getBuild().getTestResources() );
 246  
 
 247  
             }
 248  
 
 249  0
             if ( config.isPde() )
 250  
             {
 251  0
                 for ( int j = 0; j < config.getDepsOrdered().length; j++ )
 252  
                 {
 253  0
                     IdeDependency dep = config.getDepsOrdered()[j];
 254  
 
 255  0
                     if ( dep.isAddedToClasspath() && !dep.isProvided() && !dep.isReferencedProject() &&
 256  
                         !dep.isTestDependency() && !dep.isOsgiBundle() )
 257  
                     {
 258  0
                         String name = dep.getFile().getName();
 259  0
                         addLink( writer, name, StringUtils.replace( IdeUtils.getCanonicalPath( dep.getFile() ), "\\",
 260  
                                                                     "/" ), LINK_TYPE_FILE );
 261  
                     }
 262  
                 }
 263  
             }
 264  
 
 265  0
             writer.endElement(); // linkedResources
 266  
         }
 267  
 
 268  0
         writer.endElement(); // projectDescription
 269  
 
 270  0
         IOUtil.close( w );
 271  0
     }
 272  
 
 273  
     private void addFileLink( XMLWriter writer, File projectBaseDir, File basedir, File file )
 274  
         throws MojoExecutionException
 275  
     {
 276  0
         if ( file.isFile() )
 277  
         {
 278  0
             String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, file, true );
 279  0
             String location = IdeUtils.getCanonicalPath( file ).replaceAll( "\\\\", "/" ); //$NON-NLS-1$ //$NON-NLS-2$
 280  
 
 281  0
             addLink( writer, name, location, LINK_TYPE_FILE );
 282  0
         }
 283  
         else
 284  
         {
 285  0
             log.warn( Messages.getString( "EclipseProjectWriter.notafile", file ) ); //$NON-NLS-1$
 286  
         }
 287  0
     }
 288  
 
 289  
     private void addSourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots )
 290  
         throws MojoExecutionException
 291  
     {
 292  0
         for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
 293  
         {
 294  0
             String sourceRootString = (String) it.next();
 295  0
             File sourceRoot = new File( sourceRootString );
 296  
 
 297  0
             if ( sourceRoot.isDirectory() )
 298  
             {
 299  0
                 String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, sourceRoot, true );
 300  0
                 String location = IdeUtils.getCanonicalPath( sourceRoot ).replaceAll( "\\\\", "/" ); //$NON-NLS-1$ //$NON-NLS-2$
 301  
 
 302  0
                 addLink( writer, name, location, LINK_TYPE_DIRECTORY );
 303  
             }
 304  0
         }
 305  0
     }
 306  
 
 307  
     private void addResourceLinks( XMLWriter writer, File projectBaseDir, File basedir, List sourceRoots )
 308  
         throws MojoExecutionException
 309  
     {
 310  0
         for ( Iterator it = sourceRoots.iterator(); it.hasNext(); )
 311  
         {
 312  0
             String resourceDirString = ( (Resource) it.next() ).getDirectory();
 313  0
             File resourceDir = new File( resourceDirString );
 314  
 
 315  0
             if ( resourceDir.isDirectory() )
 316  
             {
 317  0
                 String name = IdeUtils.toRelativeAndFixSeparator( projectBaseDir, resourceDir, true );
 318  0
                 String location = IdeUtils.getCanonicalPath( resourceDir ).replaceAll( "\\\\", "/" ); //$NON-NLS-1$ //$NON-NLS-2$
 319  
 
 320  0
                 addLink( writer, name, location, LINK_TYPE_DIRECTORY );
 321  
             }
 322  0
         }
 323  0
     }
 324  
 
 325  
     /**
 326  
      * @param writer
 327  
      * @param name
 328  
      * @param location
 329  
      */
 330  
     private void addLink( XMLWriter writer, String name, String location, int type )
 331  
     {
 332  0
         writer.startElement( "link" ); //$NON-NLS-1$
 333  
 
 334  0
         writer.startElement( ELT_NAME );
 335  0
         writer.writeText( name );
 336  0
         writer.endElement(); // name
 337  
 
 338  0
         writer.startElement( "type" ); //$NON-NLS-1$
 339  0
         writer.writeText( Integer.toString( type ) );
 340  0
         writer.endElement(); // type
 341  
 
 342  0
         writer.startElement( "location" ); //$NON-NLS-1$
 343  
 
 344  0
         writer.writeText( location );
 345  
 
 346  0
         writer.endElement(); // location
 347  
 
 348  0
         writer.endElement(); // link
 349  0
     }
 350  
 }