View Javadoc

1   package com.explosion.expfmodules.search;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * 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, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.io.File;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import javax.swing.JOptionPane;
28  
29  import org.apache.log4j.LogManager;
30  import org.apache.log4j.Logger;
31  
32  import com.explosion.expf.Application;
33  import com.explosion.utilities.classes.ClassUtilities;
34  import com.explosion.utilities.exception.ExceptionManagerFactory;
35  import com.explosion.utilities.process.FileProcessor;
36  
37  /***
38   * @author Stephen Cowx Date created:@04-Feb-2003
39   */
40  public class ClassSearcher implements FileProcessor
41  {
42  
43      private static Logger log = LogManager.getLogger(ClassSearcher.class);
44      private String classname = null;
45      private List entries = null;
46      boolean caseSensitive = false;
47      boolean includeCLassesOfSameType = false;
48      private boolean classFound = false;
49      private FileSearchResultCollection fsrc;
50  
51      /***
52       * Constructor for LineSearcher.
53       */
54      public ClassSearcher(String classname, boolean caseInsensitive, boolean includeCLassesOfSameType)
55      {
56          this.classname = classname;
57          this.caseSensitive = caseInsensitive;
58          this.includeCLassesOfSameType = includeCLassesOfSameType;
59      }
60  
61      /***
62       * @see com.explosion.utilities.process.LineProcessor#open()
63       */
64      public void open() throws Exception
65      {
66          classFound = false;
67      }
68  
69      /***
70       * @see com.explosion.utilities.process.LineProcessor#close()
71       */
72      public void close() throws Exception
73      {}
74  
75      /***
76       *  
77       */
78      public void processFile(int fileNumber, String filename) throws Exception
79      {
80          File file = new File(filename);
81          log.debug("Looking for " + classname + " in " + filename);
82          try
83          {
84              if (includeCLassesOfSameType)
85              {
86                 List list = ClassUtilities.findClassesOfType(Class.forName(classname),filename);
87                 if (list.size() > 0)
88                 {
89                     fsrc = new FileSearchResultCollection(file.getAbsolutePath());
90                 }
91                 int index = 1;
92                 for (Iterator it = list.iterator(); it.hasNext(); )
93                 {
94                     fsrc.addResult(new SearchResult(index, file.getAbsolutePath(), it.next().toString()));
95                     index++;
96                 }
97                 
98              }
99              else
100             {
101                 classFound = ClassUtilities.containsClassWithName(classname, filename, caseSensitive);
102                 if (classFound)
103                 {
104                     fsrc = new FileSearchResultCollection(file.getAbsolutePath());
105                     fsrc.addResult(new SearchResult(1, file.getAbsolutePath(), "Found"));
106                 }
107             }
108 
109         }
110         catch (Throwable e)
111         {
112             ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while searching for class.");
113             if (stop())
114                 throw new Exception(e);
115             classFound = false;
116         }
117     }
118 
119     /***
120      * Asks the user if they would like to quit
121      * 
122      * @return
123      */
124     private boolean stop()
125     {
126         int stopDecision = JOptionPane.showConfirmDialog(Application.getApplicationFrame(), "An error has occurred.  Quit ?", "Error", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
127         if (stopDecision == JOptionPane.YES_OPTION)
128         {
129             return true;
130         }
131         else
132         {
133             return false;
134         }
135     }
136 
137     /***
138      * Returns the entries found in the file.
139      * 
140      * @return Vector
141      */
142     public boolean found()
143     {
144         return this.classFound;
145     }
146 
147     /***
148      * @return
149      */
150     public FileSearchResultCollection getResults()
151     {
152         return fsrc;
153     }
154 }