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.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      {
52      }
53  
54      /**
55       * Creates a new build command
56       * 
57       * @param name Command name
58       */
59      public BuildCommand( String name )
60      {
61          this( name, null );
62      }
63  
64      public BuildCommand( String name, Map arguments )
65      {
66          this.name = name;
67          this.arguments = arguments;
68      }
69  
70      public BuildCommand( String name, String argName, String argValue )
71      {
72          this.name = name;
73          arguments = new Properties();
74          arguments.put( argName, argValue );
75      }
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      {
86          if ( name == null )
87          {
88              throw new IllegalArgumentException( "Name must not be null." );
89          }
90  
91          this.name = name;
92          this.triggers = triggers;
93  
94          if ( arguments == null )
95          {
96              this.arguments = new HashMap();
97          }
98          else
99          {
100             this.arguments = new HashMap( arguments );
101         }
102 
103     }
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     {
114         Xpp3Dom nameNode = node.getChild( "name" );
115 
116         if ( nameNode == null )
117         {
118             throw new IllegalArgumentException( "No name node." );
119         }
120 
121         name = nameNode.getValue();
122 
123         Xpp3Dom triggersNode = node.getChild( "triggers" );
124 
125         if ( triggersNode != null )
126         {
127             triggers = triggersNode.getValue();
128         }
129 
130         Xpp3Dom argumentsNode = node.getChild( "arguments" );
131 
132         arguments = new HashMap();
133 
134         if ( argumentsNode != null )
135         {
136             for ( int i = 0; i < argumentsNode.getChildCount(); ++i )
137             {
138                 Xpp3Dom entry = argumentsNode.getChild( i );
139 
140                 if ( entry.getName().equals( "dictionary" ) )
141                 {
142                     Xpp3Dom key = entry.getChild( "key" );
143                     Xpp3Dom value = entry.getChild( "value" );
144 
145                     if ( key != null && value != null )
146                     {
147                         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     }
161 
162     public void print( XMLWriter writer )
163     {
164         writer.startElement( "buildCommand" );
165         writer.startElement( "name" );
166         writer.writeText( name );
167         writer.endElement();
168 
169         if ( !StringUtils.isEmpty( triggers ) )
170         {
171             writer.startElement( "triggers" );
172             writer.writeText( triggers );
173             writer.endElement();
174         }
175 
176         if ( arguments != null && !arguments.isEmpty() )
177         {
178             writer.startElement( "arguments" );
179 
180             writer.startElement( "dictionary" );
181 
182             for ( Iterator it = arguments.keySet().iterator(); it.hasNext(); )
183             {
184                 String key = (String) it.next();
185 
186                 writer.startElement( "key" );
187                 writer.writeText( key );
188                 writer.endElement();
189 
190                 writer.startElement( "value" );
191                 writer.writeText( (String) arguments.get( key ) );
192                 writer.endElement();
193             }
194 
195             writer.endElement();
196 
197             writer.endElement();
198         }
199 
200         writer.endElement();
201     }
202 
203     public boolean equals( Object obj )
204     {
205         if ( obj instanceof BuildCommand )
206         {
207             BuildCommand b = (BuildCommand) obj;
208 
209             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             return false;
217         }
218     }
219 
220     public int hashCode()
221     {
222         return name.hashCode() + ( triggers == null ? 0 : 13 * triggers.hashCode() )
223             + ( arguments == null ? 0 : 17 * arguments.hashCode() );
224     }
225 }