You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ke...@apache.org on 2006/11/07 18:07:35 UTC

svn commit: r472171 - /maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/BuildCommand.java

Author: kenney
Date: Tue Nov  7 09:07:35 2006
New Revision: 472171

URL: http://svn.apache.org/viewvc?view=rev&rev=472171
Log:
Merge some methods from portions of patch from MECLIPSE-139 submitted by Jochen Kuhnle; adds Xpp3Dom read capabilities to preserve .project files

Modified:
    maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/BuildCommand.java

Modified: maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/BuildCommand.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/BuildCommand.java?view=diff&rev=472171&r1=472170&r2=472171
==============================================================================
--- maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/BuildCommand.java (original)
+++ maven/plugins/trunk/maven-eclipse-plugin/src/main/java/org/apache/maven/plugin/eclipse/BuildCommand.java Tue Nov  7 09:07:35 2006
@@ -1,22 +1,53 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
 package org.apache.maven.plugin.eclipse;
 
-import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
 import java.util.Properties;
 
+import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.XMLWriter;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
 
 /**
  *
  * @author <a href="mailto:kenneyw@neonics.com">Kenney Westerhof</a>
- *
+ * @author Jochen Kuhnle
  */
 public class BuildCommand
 {
+    /** Builder name */
     private String name;
 
-    private Properties arguments;
+    /** Trigger names (comma-delimited list) */
+    private String triggers;
+
+    /** Argument map */
+    private Map arguments;
+
+    /**
+     * Creates a new build command
+     *
+     * @param name Command name
+     */
+    public BuildCommand( String name )
+    {
+        this( name, null );
+    }
 
-    public BuildCommand( String name, Properties arguments )
+    public BuildCommand( String name, Map arguments )
     {
         this.name = name;
         this.arguments = arguments;
@@ -24,19 +55,94 @@
 
     public BuildCommand( String name, String argName, String argValue )
     {
-        this.name=name;
-        arguments=new Properties();
-        arguments.setProperty( argName, argValue );
+        this.name = name;
+        arguments = new Properties();
+        arguments.put( argName, argValue );
     }
 
-    public String getName()
+    /**
+     * Creates a new build command
+     *
+     * @param name Command name
+     * @param triggers Command triggers
+     * @param arguments Command arguments
+     */
+    public BuildCommand( String name, String triggers, Map arguments )
     {
-        return name;
+        if ( name == null )
+        {
+            throw new IllegalArgumentException( "Name must not be null." );
+        }
+
+        this.name = name;
+        this.triggers = triggers;
+
+        if ( arguments == null )
+        {
+            this.arguments = new HashMap();
+        }
+        else
+        {
+            this.arguments = new HashMap( arguments );
+        }
+
     }
 
-    public Properties getProperties()
+    /**
+     * Creates a new build command from a DOM subtree
+     * <p>
+     * The subtree must represent a &lt;buildCommand&gt; section from an Eclipse .project file
+     *
+     * @param node DOM node
+     */
+    public BuildCommand( Xpp3Dom node )
     {
-        return arguments;
+        Xpp3Dom nameNode = node.getChild( "name" );
+
+        if ( nameNode == null )
+        {
+            throw new IllegalArgumentException( "No name node." );
+        }
+
+        name = nameNode.getValue();
+
+        Xpp3Dom triggersNode = node.getChild( "triggers" );
+
+        if ( triggersNode != null )
+        {
+            triggers = triggersNode.getValue();
+        }
+
+        Xpp3Dom argumentsNode = node.getChild( "arguments" );
+
+        arguments = new HashMap();
+
+        if ( argumentsNode != null )
+        {
+            for ( int i = 0; i < argumentsNode.getChildCount(); ++i )
+            {
+                Xpp3Dom entry = argumentsNode.getChild( i );
+
+                if ( entry.getName().equals( "dictionary" ) )
+                {
+                    Xpp3Dom key = entry.getChild( "key" );
+                    Xpp3Dom value = entry.getChild( "value" );
+
+                    if ( key != null && value != null )
+                    {
+                        this.arguments.put( key.getValue(), value.getValue() );
+                    }
+                    else
+                    {
+                        // TODO: log warning about illegal key/value pair
+                    }
+                }
+                else
+                {
+                    // TODO: log warning about unknown argument tag
+                }
+            }
+        }
     }
 
     public void print( XMLWriter writer )
@@ -46,31 +152,57 @@
         writer.writeText( name );
         writer.endElement();
 
+        if ( !StringUtils.isEmpty( triggers ) )
+        {
+            writer.startElement( "triggers" );
+            writer.writeText( triggers );
+            writer.endElement();
+        }
+
         if ( arguments != null && !arguments.isEmpty() )
         {
             writer.startElement( "arguments" );
 
-            for ( Enumeration n = arguments.propertyNames(); n.hasMoreElements(); )
-            {
-                String key = (String) n.nextElement();
+            writer.startElement( "dictionary" );
 
-                // TODO: not sure about location of dictionary - it's usually just 1 argument.
-                writer.startElement( "dictionary" );
+            for ( Iterator it = arguments.keySet().iterator(); it.hasNext(); )
+            {
+                String key = (String) it.next();
 
                 writer.startElement( "key" );
                 writer.writeText( key );
                 writer.endElement();
 
                 writer.startElement( "value" );
-                writer.writeText( arguments.getProperty( key ) );
-                writer.endElement();
-
+                writer.writeText( (String) arguments.get( key ) );
                 writer.endElement();
             }
 
             writer.endElement();
+
+            writer.endElement();
         }
 
         writer.endElement();
+    }
+
+    public boolean equals( Object obj )
+    {
+        if ( obj instanceof BuildCommand )
+        {
+            BuildCommand b = (BuildCommand) obj;
+            return name.equals( b.name ) && ( triggers == null ? b.triggers == null : triggers.equals( b.triggers ) )
+                && ( arguments == null ? b.arguments == null : arguments.equals( b.arguments ) );
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public int hashCode()
+    {
+        return name.hashCode() + ( triggers == null ? 0 : 13 * triggers.hashCode() )
+            + ( arguments == null ? 0 : 17 * arguments.hashCode() );
     }
 }