View Javadoc

1   /* ==========================================================================
2    * Copyright 2003-2004 Mevenide Team
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   * =========================================================================
16   */
17  package org.mevenide.grammar.impl;
18  
19  import java.io.File;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.TreeSet;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.mevenide.context.DefaultQueryContext;
27  import org.mevenide.context.IQueryContext;
28  import org.mevenide.environment.ILocationFinder;
29  import org.mevenide.environment.LocationFinderAggregator;
30  import org.mevenide.goals.grabber.IGoalsGrabber;
31  import org.mevenide.goals.manager.GoalsGrabbersManager;
32  import org.mevenide.grammar.AttributeCompletion;
33  
34  /***
35   * Implementation of a attribute completion that keeps track of available maven
36   * goals. 
37   * 
38   * if basedir is set then custom goals will also be retrieved 
39   * 
40   * @author Milos Kleint (ca206216@tiscali.cz)
41   * @author <a href="mailto:rhill2@free.fr">Gilles Dodinet</a>
42   */
43  public class GoalsAttributeCompletionImpl implements AttributeCompletion {
44  
45      private static Log logger = LogFactory.getLog(GoalsAttributeCompletionImpl.class);
46      
47      /*** primary grabber that handles both global and custom goals */
48      private IGoalsGrabber grabber;
49      /*** fallback grabber - used if grabber isnot available */
50      private IGoalsGrabber defaultGrabber;
51  
52      private String basedir;
53      
54      /*** Creates a new instance of GoalsAttributeCompletionImpl */
55      public GoalsAttributeCompletionImpl() throws Exception {
56          ILocationFinder finder = new LocationFinderAggregator(DefaultQueryContext.getNonProjectContextInstance());
57          grabber = GoalsGrabbersManager.getDefaultGoalsGrabber(finder);
58          defaultGrabber = grabber;
59      }
60  
61      public String getName() {
62          return "goal";
63      }
64  
65      public void setBasedir(String basedir) {
66          this.basedir = basedir;
67          try {
68              IQueryContext context = new DefaultQueryContext(new File(basedir));
69              grabber = GoalsGrabbersManager.getGoalsGrabber(context, new LocationFinderAggregator(context));
70          }
71          catch (Exception e) {
72              grabber = defaultGrabber;
73              logger.error("Unable to set basedir. It is highly probable that maven.xml is badly formed.");
74          }
75      }
76      
77      public Collection getValueHints(String start) {
78          Collection toReturn = new TreeSet();
79          String[] plugins = grabber.getPlugins();
80          if (plugins != null) {
81              int colon = (start == null ? -1 : start.indexOf(':'));
82              String pluginMatch = (colon > -1 ? start.substring(0, colon) : start);
83              String goalMatch = null;
84              if (colon > -1 && colon < start.length() - 1) {
85                  goalMatch = start.substring(colon + 1);
86              }
87              Collection selectedPlugins = new ArrayList(plugins.length + 5);
88              for (int i = 0; i < plugins.length; i++) {
89                  if (pluginMatch == null || plugins[i].startsWith(pluginMatch)) {
90                      selectedPlugins.add(plugins[i]);
91                  }
92              }
93              Iterator it = selectedPlugins.iterator();
94              while (it.hasNext()) {
95                  String plugin = (String) it.next();
96                  String[] goals = grabber.getGoals(plugin);
97                  boolean hasDefault = false;
98                  for (int i = 0; i < goals.length; i++) {
99                      if (goalMatch == null || goals[i].startsWith(goalMatch)) {
100                         if (!"(default)".equals(goals[i])) {
101                             toReturn.add(plugin + ":" + goals[i]);
102                         }
103                     }
104                     if ("(default)".equals(goals[i])) {
105                         hasDefault = true;
106                     }
107                 }
108                 if (hasDefault && colon == -1) {
109                     toReturn.add(plugin);
110                 }
111             }
112         }
113         return toReturn;
114     }
115 }