Coverage Report - org.apache.maven.plugin.eclipse.BuildCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
BuildCommand
23%
16/69
34%
17/50
2.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;
 20  
 
 21  
 import java.util.HashMap;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 import java.util.Properties;
 25  
 
 26  
 import org.codehaus.plexus.util.StringUtils;
 27  
 import org.codehaus.plexus.util.xml.XMLWriter;
 28  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 29  
 
 30  
 /**
 31  
  * Represents a buildCommand section in the <code>.project</code> file.
 32  
  * 
 33  
  * @author <a href="mailto:kenneyw@neonics.com">Kenney Westerhof</a>
 34  
  * @author Jochen Kuhnle
 35  
  */
 36  
 public class BuildCommand
 37  
 {
 38  
     /** Builder name */
 39  
     private String name;
 40  
 
 41  
     /** Trigger names (comma-delimited list) */
 42  
     private String triggers;
 43  
 
 44  
     /** Argument map */
 45  
     private Map arguments;
 46  
 
 47  
     /**
 48  
      * no-arg constructor for plugin configuration.
 49  
      */
 50  
     public BuildCommand()
 51  0
     {
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Creates a new build command
 56  
      * 
 57  
      * @param name Command name
 58  
      */
 59  
     public BuildCommand( String name )
 60  
     {
 61  0
         this( name, null );
 62  0
     }
 63  
 
 64  
     public BuildCommand( String name, Map arguments )
 65  0
     {
 66  0
         this.name = name;
 67  0
         this.arguments = arguments;
 68  0
     }
 69  
 
 70  
     public BuildCommand( String name, String argName, String argValue )
 71  4
     {
 72  4
         this.name = name;
 73  4
         arguments = new Properties();
 74  4
         arguments.put( argName, argValue );
 75  4
     }
 76  
 
 77  
     /**
 78  
      * Creates a new build command
 79  
      * 
 80  
      * @param name Command name
 81  
      * @param triggers Command triggers
 82  
      * @param arguments Command arguments
 83  
      */
 84  
     public BuildCommand( String name, String triggers, Map arguments )
 85  40
     {
 86  40
         if ( name == null )
 87  
         {
 88  0
             throw new IllegalArgumentException( "Name must not be null." );
 89  
         }
 90  
 
 91  40
         this.name = name;
 92  40
         this.triggers = triggers;
 93  
 
 94  40
         if ( arguments == null )
 95  
         {
 96  12
             this.arguments = new HashMap();
 97  
         }
 98  
         else
 99  
         {
 100  28
             this.arguments = new HashMap( arguments );
 101  
         }
 102  
 
 103  40
     }
 104  
 
 105  
     /**
 106  
      * Creates a new build command from a DOM subtree
 107  
      * <p>
 108  
      * The subtree must represent a &lt;buildCommand&gt; section from an Eclipse .project file
 109  
      * 
 110  
      * @param node DOM node
 111  
      */
 112  
     public BuildCommand( Xpp3Dom node )
 113  0
     {
 114  0
         Xpp3Dom nameNode = node.getChild( "name" );
 115  
 
 116  0
         if ( nameNode == null )
 117  
         {
 118  0
             throw new IllegalArgumentException( "No name node." );
 119  
         }
 120  
 
 121  0
         name = nameNode.getValue();
 122  
 
 123  0
         Xpp3Dom triggersNode = node.getChild( "triggers" );
 124  
 
 125  0
         if ( triggersNode != null )
 126  
         {
 127  0
             triggers = triggersNode.getValue();
 128  
         }
 129  
 
 130  0
         Xpp3Dom argumentsNode = node.getChild( "arguments" );
 131  
 
 132  0
         arguments = new HashMap();
 133  
 
 134  0
         if ( argumentsNode != null )
 135  
         {
 136  0
             for ( int i = 0; i < argumentsNode.getChildCount(); ++i )
 137  
             {
 138  0
                 Xpp3Dom entry = argumentsNode.getChild( i );
 139  
 
 140  0
                 if ( entry.getName().equals( "dictionary" ) )
 141  
                 {
 142  0
                     Xpp3Dom key = entry.getChild( "key" );
 143  0
                     Xpp3Dom value = entry.getChild( "value" );
 144  
 
 145  0
                     if ( key != null && value != null )
 146  
                     {
 147  0
                         this.arguments.put( key.getValue(), value.getValue() );
 148  
                     }
 149  
                     else
 150  
                     {
 151  
                         // TODO: log warning about illegal key/value pair
 152  
                     }
 153  
                 }
 154  
                 else
 155  
                 {
 156  
                     // TODO: log warning about unknown argument tag
 157  
                 }
 158  
             }
 159  
         }
 160  0
     }
 161  
 
 162  
     public void print( XMLWriter writer )
 163  
     {
 164  0
         writer.startElement( "buildCommand" );
 165  0
         writer.startElement( "name" );
 166  0
         writer.writeText( name );
 167  0
         writer.endElement();
 168  
 
 169  0
         if ( !StringUtils.isEmpty( triggers ) )
 170  
         {
 171  0
             writer.startElement( "triggers" );
 172  0
             writer.writeText( triggers );
 173  0
             writer.endElement();
 174  
         }
 175  
 
 176  0
         if ( arguments != null && !arguments.isEmpty() )
 177  
         {
 178  0
             writer.startElement( "arguments" );
 179  
 
 180  0
             writer.startElement( "dictionary" );
 181  
 
 182  0
             for ( Iterator it = arguments.keySet().iterator(); it.hasNext(); )
 183  
             {
 184  0
                 String key = (String) it.next();
 185  
 
 186  0
                 writer.startElement( "key" );
 187  0
                 writer.writeText( key );
 188  0
                 writer.endElement();
 189  
 
 190  0
                 writer.startElement( "value" );
 191  0
                 writer.writeText( (String) arguments.get( key ) );
 192  0
                 writer.endElement();
 193  
             }
 194  
 
 195  0
             writer.endElement();
 196  
 
 197  0
             writer.endElement();
 198  
         }
 199  
 
 200  0
         writer.endElement();
 201  0
     }
 202  
 
 203  
     public boolean equals( Object obj )
 204  
     {
 205  48
         if ( obj instanceof BuildCommand )
 206  
         {
 207  48
             BuildCommand b = (BuildCommand) obj;
 208  
 
 209  48
             return name.equals( b.name )
 210  
                 && ( triggers == null ? b.triggers == null : triggers.equals( b.triggers ) )
 211  
                 && ( arguments == null || arguments.isEmpty() ? b.arguments == null || b.arguments.isEmpty()
 212  
                                 : arguments.equals( b.arguments ) );
 213  
         }
 214  
         else
 215  
         {
 216  0
             return false;
 217  
         }
 218  
     }
 219  
 
 220  
     public int hashCode()
 221  
     {
 222  0
         return name.hashCode() + ( triggers == null ? 0 : 13 * triggers.hashCode() )
 223  
             + ( arguments == null ? 0 : 17 * arguments.hashCode() );
 224  
     }
 225  
 }