View Javadoc

1   package org.apache.maven.util;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.TreeMap;
30  import java.util.TreeSet;
31  
32  import org.apache.maven.project.Project;
33  import org.apache.tools.ant.types.Path;
34  
35  /**
36   * Context/pull tool for use in MavenSession templates.
37   *
38   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
39   */
40  public class MavenTool
41  {
42      /** The maven project the tool is processing */
43      private Project project;
44  
45      /** list of items to be counted. Key is the object, value is a list
46       of objects that have been added that are equal */
47      private Map counted = new HashMap();
48  
49      /** the counted items in ascending order of number of times counted */
50      private Map countedItems = null;
51  
52      /** the counted items in descending order of number of times counted */
53      private Map countedItemsReversed = null;
54  
55      /**
56       * Creates a new instance.
57       */
58      public MavenTool()
59      {
60          // FIXME: Need a path to the project descriptor.
61          //project = MavenUtils.getProject("project.xml");
62      }
63  
64      /**
65       * Accessor for the project property.
66       * @return the maven project being processed
67       */
68      public Project getProject()
69      {
70          return project;
71      }
72  
73      /**
74       * Resolves a path fragment for the first resource found in
75       * <code>directories</code>.
76       *
77       * <blockquote><code><pre>
78       * #set ($pathFragment =
79       * $maven.resolvePathFragment($context.toolbox.string.basedir, "foo/bar",
80       * ${src.set}))
81       * </pre></code></blockquote>
82       *
83       * @param baseDir The directory to which <code>directories</code>
84       * are relative.
85       * @param resource The path to the resource to find in
86       * <code>directories</code>.
87       * @param directories The list of paths to search for
88       * <code>resource</code> in..
89       * @return The path fragment to <code>resource</code>.
90       */
91      public String resolvePathFragment( String baseDir, String resource, Path directories )
92      {
93          return resolvePathFragment( baseDir, resource, directories.list() );
94      }
95  
96      /**
97       * Called by {@link #resolvePathFragment(String, String, Path)}.
98       *
99       * @see #resolvePathFragment(String, String, Path)
100      */
101     protected final String resolvePathFragment( String baseDir, String resource, String[] paths )
102     {
103         String path = null;
104         for ( int i = 0; i < paths.length; i++ )
105         {
106             path = paths[i];
107             File d = new File( baseDir, path );
108             if ( d.isDirectory() && new File( d, resource ).exists() )
109             {
110                 break;
111             }
112             else
113             {
114                 // Avoid returning a bogus value on the last iteration.
115                 path = null;
116             }
117         }
118         if ( path == null )
119         {
120             System.err.println( "Unable to resolve " + resource + " using a base diretory of " + baseDir );
121         }
122         return path;
123     }
124 
125     /**
126      * Clear the counter so that there are zero items
127      */
128     public void resetCount()
129     {
130         getCounted().clear();
131     }
132 
133     /**
134      * Add an object to the list of counted items.
135      * @param object the item to be added.
136      */
137     public void addToCount( Object object )
138     {
139         List list = null;
140         if ( getCounted().get( object ) == null )
141         {
142             list = new ArrayList();
143         }
144         else
145         {
146             list = (List) getCounted().get( object );
147         }
148         list.add( object );
149         getCounted().put( object, list );
150         if ( getCountedItems() != null )
151         {
152             setCountedItems( null );
153             setCountedItemsReversed( null );
154         }
155     }
156 
157     /** Fill the provided sorting map of items from the counted items
158      * @param map a map that will sort the objects added to it
159      */
160     private void fillSortingMap( Map map )
161     {
162         List list = null;
163         Object key = null;
164         for ( Iterator index = counted.keySet().iterator(); index.hasNext(); )
165         {
166             key = index.next();
167             list = (List) counted.get( key );
168             Integer count = new Integer( list.size() );
169             Set items = null;
170             if ( map.containsKey( count ) )
171             {
172                 items = (Set) map.get( count );
173             }
174             else
175             {
176                 items = new TreeSet();
177             }
178             items.add( key );
179             map.put( count, items );
180         }
181     }
182 
183     /**
184      * get a list of counted items with the key being the number of times
185      * counted (in descending order) and the value any arbitrary item that was
186      * counted
187      * @return a sorted map of #times counted -> list of objects
188      */
189     public Map getCountDescending()
190     {
191         if ( getCountedItemsReversed() == null )
192         {
193             setCountedItemsReversed( new TreeMap( Collections.reverseOrder() ) );
194             fillSortingMap( getCountedItemsReversed() );
195         }
196         return getCountedItemsReversed();
197     }
198 
199     /**
200      * get a list of counted items with the key being the number of times
201      * counted and the value any arbitrary item that was counted
202      * @return a sorted map of #times counted -> list of objects
203      */
204     public Map getCount()
205     {
206         if ( getCountedItems() == null )
207         {
208             setCountedItems( new TreeMap() );
209             fillSortingMap( getCountedItems() );
210         }
211         return getCountedItems();
212     }
213 
214     /** Getter for property counted.
215      * @return Value of property counted.
216      */
217     private Map getCounted()
218     {
219         return counted;
220     }
221 
222     /** Getter for property countedItems.
223      * @return Value of property countedItems.
224      */
225     private Map getCountedItems()
226     {
227         return countedItems;
228     }
229 
230     /** Setter for property countedItems.
231      * @param countedItems New value of property countedItems.
232      */
233     private void setCountedItems( Map countedItems )
234     {
235         this.countedItems = countedItems;
236     }
237 
238     /** Getter for property countedItemsReversed.
239      * @return Value of property countedItemsReversed.
240      */
241     private Map getCountedItemsReversed()
242     {
243         return countedItemsReversed;
244     }
245 
246     /** Setter for property countedItemsReverse.
247      * @param countedItemsReversed New value of property countedItemsReverse.
248      */
249     private void setCountedItemsReversed( Map countedItemsReversed )
250     {
251         this.countedItemsReversed = countedItemsReversed;
252     }
253 
254     public Integer toInteger( String s )
255     {
256         return Integer.valueOf( s );
257     }
258 }