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