View Javadoc
1   package org.codehaus.mojo.taglist.beans;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.plexus.util.IOUtil;
23  import java.io.BufferedReader;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.InputStreamReader;
29  import java.io.Reader;
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * Report for a file.
38   *
39   * @author <a href="mailto:bellingard.NO-SPAM@gmail.com">Fabrice Bellingard </a>
40   */
41  public class FileReport
42      implements Comparable
43  {
44  
45      /**
46       * The file being analyzed.
47       */
48      private final File file;
49  
50      /**
51       * The character encoding of the source file
52       */
53      private final String encoding;
54  
55      /**
56       * The name of the class corresponding to this file.
57       */
58      private String className;
59  
60      /**
61       * Pair values of a line number and a comment. Map of [Integer,String].
62       */
63      private final Map tagListing;
64  
65      /**
66       * The package identification string.
67       */
68      private static final String PACKAGE_STR = "package";
69  
70      /**
71       * Constructor.
72       *
73       * @param file The file to analyze.
74       * @param encoding the file encoding to use for the report.
75       */
76      public FileReport( File file, String encoding )
77      {
78          this.file = file;
79          this.encoding = encoding;
80          this.tagListing = new HashMap();
81      }
82  
83      /**
84       * Adds a new entry to the list of tags found for this file report.
85       *
86       * @param comment the comment string containing the 'todo'.
87       * @param lineIndex the line number of the comment (or first line if multi-lined).
88       */
89      public void addComment( String comment, int lineIndex )
90      {
91          tagListing.put( new Integer( lineIndex ), comment );
92      }
93  
94      /**
95       * Returns the path corresponding to the analyzed class, for instance:
96       * org/apache/maven/plugins/taglist/beans/FileReport.
97       *
98       * @return the file path.
99       */
100     public String getClassNameWithSlash()
101     {
102         return className.replace( '.', '/' );
103     }
104 
105     /**
106      * Access an input reader that uses the current file encoding.
107      *
108      * @param fileToRead the file to open in the reader.
109      * @throws IOException the IO exception.
110      * @return a reader with the current file encoding.
111      */
112     private Reader getReader( File fileToRead ) throws IOException
113     {
114         InputStream in = new FileInputStream( fileToRead );
115         return ( encoding == null ) ? new InputStreamReader( in ) : new InputStreamReader( in, encoding );
116     }
117 
118     /**
119      * Returns the complete name of the analyzed class, for instance: org.codehaus.mojo.taglist.beans.FileReport.
120      *
121      * @return the full class name.
122      */
123     public String getClassName()
124     {
125         if ( className != null )
126         {
127             return className;
128         }
129         // need to compute it (only once)
130         BufferedReader reader = null;
131         String packageName = null;
132         try
133         {
134             reader = new BufferedReader( getReader( file ) );
135             String currentLine = reader.readLine();
136             if ( currentLine != null )
137             {
138                 currentLine = currentLine.trim();
139             }
140             while ( currentLine != null )
141             {
142                 if ( currentLine.startsWith( PACKAGE_STR ) )
143                 {
144                     packageName = currentLine.substring( PACKAGE_STR.length() ).trim().replaceAll( ";", "" ).trim();
145                     break;
146                 }
147                 String nextLine = reader.readLine();
148                 if ( nextLine == null )
149                 {
150                     currentLine = nextLine;
151                 }
152                 else
153                 {
154                     currentLine = nextLine.trim();
155                 }
156             }
157         }
158         catch ( IOException e )
159         {
160             packageName = "unknown";
161         }
162         finally
163         {
164             IOUtil.close( reader );
165         }
166 
167         className = packageName + "." + file.getName().replaceAll( "\\.java$", "" );
168 
169         return className;
170     }
171 
172     /**
173      * Returns the list of the comment line indexes.
174      *
175      * @return Collection of Integer.
176      */
177     public Collection getLineIndexes()
178     {
179         ArrayList list = new ArrayList();
180         list.addAll( tagListing.keySet() );
181         Collections.sort( list );
182         return list;
183     }
184 
185     /**
186      * Returns the comment for the corresponding line index.
187      *
188      * @param lineIndex the index of the line.
189      * @return the comment.
190      */
191     public String getComment( Integer lineIndex )
192     {
193         return (String) tagListing.get( lineIndex );
194     }
195 
196     /**
197      * {@inheritDoc}
198      *
199      * @see Comparable#compareTo(Object)
200      */
201     public int compareTo( Object o )
202     {
203         if ( o instanceof FileReport )
204         {
205             FileReport fileReport = (FileReport) o;
206             return this.getClassName().compareTo( fileReport.getClassName() );
207         }
208         else
209         {
210             return 0;
211         }
212     }
213     
214     /**
215      * {@inheritDoc}
216      *
217      * @see Object#equals(Object)
218      */
219     public boolean equals( Object r )
220     {
221         // In Java 5 the PriorityQueue.remove method uses the 
222         // compareTo method, while in Java 6 it uses the equals method.
223         return ( this.compareTo( r ) == 0 );
224     }
225     
226     /**
227      * {@inheritDoc}
228      *
229      * @see Object#hashCode()
230      */
231     public int hashCode() 
232     {
233         assert false : "hashCode not designed";
234         return 1; // any arbitrary constant will do 
235     }
236 
237 }