KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > changelog > ChangeLogProcessor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Ralph Krueger.
17  */

18
19
20 package org.netbeans.modules.changelog;
21
22 /**
23  *
24  * @author Ralph Krueger
25  */

26
27 import org.apache.regexp.RE;
28 import org.apache.regexp.RESyntaxException;
29
30
31 import java.util.*;
32 import java.io.*;
33 import java.lang.reflect.*;
34 import org.openide.filesystems.*;
35 import org.openide.*;
36 import org.openide.loaders.*;
37 import org.openide.util.*;
38 import org.netbeans.modules.changelog.html.*;
39 import org.netbeans.modules.changelog.settings.*;
40
41 public class ChangeLogProcessor {
42
43     public static final int MESSAGE_FILTER_SUBSTRING = 0;
44     public static final int MESSAGE_FILTER_REGEXP = 1;
45     public static final int MESSAGE_FILTER_ALL_WORDS = 2;
46     public static final int MESSAGE_FILTER_SOME_WORDS = 3;
47     public static final int FILE_FILTER_SUBSTRING = 0;
48     public static final int FILE_FILTER_REGEXP = 1;
49     private static final String JavaDoc INITIAL_REV = "Initial revision"; //NOI18N
50
private static final String JavaDoc WAS_ADDED_ON_BRANCH = "was initially added on branch"; //NOI18N
51

52     public static final int SORT_BY_USER = 0;
53     public static final int SORT_BY_DATE = 1;
54     
55     private long revisionsNumber = 0;
56     
57     private HashMap fileObjectMap;
58     
59     private LinkedList rawResults;
60     
61     private List groupedResults;
62     
63     private List printerList;
64     
65     // properties
66
private String JavaDoc messageFilter;
67     private int messageFilterType;
68     private int minutesGap;
69     
70     private RE messageRE = null;
71
72     // properties
73
private String JavaDoc fileFilter;
74     private int fileFilterType;
75     
76     private RE fileRE = null;
77     
78     /** Holds value of property dateRange. */
79     private String JavaDoc dateRange;
80     
81     /** Holds value of property user. */
82     private String JavaDoc user;
83     
84     private int sortMode;
85     
86     /** Holds value of property revisionRange. */
87     private String JavaDoc revisionRange;
88     
89     /** Holds value of property toOutputWindow. */
90     private boolean toOutputWindow;
91     
92     /** Holds value of property toTextFile. */
93     private java.io.File JavaDoc toTextFile;
94     
95     /** Holds value of property toXmlFile. */
96     private java.io.File JavaDoc toXmlFile;
97     
98     /** Holds value of property toHtmlFile. */
99     private java.io.File JavaDoc toHtmlFile;
100     
101     /** Holds value of property toExplorer. */
102     private boolean toExplorer;
103     
104     /** Holds value of property descendingSort. */
105     private boolean descendingSort;
106     
107     private boolean viewInBrowser;
108     
109     /** Holds value of property htmlSiteProcessor. */
110     private ChangeLogHTMLService htmlSiteProcessor;
111     
112     /** Holds value of property includeQueryDescription. */
113     private boolean includeQueryDescription;
114     
115     /** Holds value of property includeSummary. */
116     private boolean includeSummary;
117     
118     /** Holds value of property includeBranchNames. */
119     private boolean includeBranchNames;
120     
121     /** Creates new ChangeLogDisplayer */
122     public ChangeLogProcessor() {
123         fileObjectMap = new HashMap(10);
124         rawResults = new LinkedList();
125         groupedResults = new LinkedList();
126         setMaxDateGap(10);
127         setSortMode(SORT_BY_DATE);
128         printerList = new LinkedList();
129         setViewInBrowser(true);
130         setIncludeSummary(true);
131         setIncludeQueryDescription(true);
132         
133         ChangeLogSettings settings = (ChangeLogSettings)SharedClassObject.findObject(ChangeLogSettings.class, true);
134         setIncludeBranchNames(settings.isShowBranchesByDefault());
135         String JavaDoc id = settings.getDefaultServerInfo();
136         if (!id.equals("")) {
137             Lookup.Template template = new Lookup.Template(ChangeLogHTMLService.class, id, null);
138             Lookup.Item item = Lookup.getDefault().lookupItem(template);
139             setHtmlSiteProcessor((ChangeLogHTMLService)item.getInstance());
140         }
141     }
142     
143     public void setViewInBrowser(boolean view) {
144         viewInBrowser = view;
145     }
146     public boolean isViewInBrowser() {
147         return viewInBrowser;
148     }
149     
150     public void setMessageFilter(int type, String JavaDoc filter) {
151         messageFilter = filter;
152         messageFilterType = type;
153         if (messageFilterType == MESSAGE_FILTER_REGEXP) {
154             try {
155                 messageRE = new RE(messageFilter);
156             } catch (RESyntaxException exc) {
157                 messageRE = null;
158             }
159         }
160         if (messageFilterType == MESSAGE_FILTER_SOME_WORDS ||
161              messageFilterType == MESSAGE_FILTER_ALL_WORDS)
162         {
163             try {
164                 String JavaDoc regExp = "";
165                 StringTokenizer tok = new StringTokenizer(messageFilter, " ");
166                 while (tok.hasMoreTokens()) {
167                     regExp = regExp + tok.nextToken();
168                     if (tok.hasMoreTokens()) {
169                         if (messageFilterType == MESSAGE_FILTER_ALL_WORDS) {
170                             regExp = regExp + '&';
171                         } else {
172                             regExp = regExp + '|';
173                         }
174                     }
175                 }
176                 messageRE = new RE(regExp);
177             } catch (RESyntaxException exc) {
178                 messageRE = null;
179             }
180         }
181     }
182     
183     public String JavaDoc getMessageFilter() {
184         return messageFilter;
185     }
186     
187     public int getMessageFilterType() {
188         return messageFilterType;
189     }
190
191     public void setFileFilter(int type, String JavaDoc filter) {
192         fileFilter = filter;
193         fileFilterType = type;
194         if (fileFilterType == FILE_FILTER_REGEXP) {
195             try {
196                 fileRE = new RE(fileFilter);
197             } catch (RESyntaxException exc) {
198                 fileRE = null;
199             }
200         }
201     }
202     
203     public String JavaDoc getFileFilter() {
204         return fileFilter;
205     }
206     
207     public int getFileFilterType() {
208         return fileFilterType;
209     }
210     
211     
212     public void setMaxDateGap(int numberOfMinutes) {
213         minutesGap = numberOfMinutes;
214     }
215     
216     public int getMaxDateGap() {
217         return minutesGap;
218     }
219     
220     private long getMaxDateGapInTime() {
221         return (long)((minutesGap * 60) * 1000);
222     }
223     
224     /**
225      * adds fileobjects to a map of selected fileobjects for this action.
226      * Is needed to convert correctly the resulting files back to
227      * fileobjects.
228      */

229 /* public void addFileObjects(FileObject[] fos) {
230         if (fos != null) {
231             for (int i=0; i < fos.length; i++) {
232                 File file = FileSystemCommand.toFile(fos[i]);
233                 if (file != null) {
234                     fileObjectMap.put(file, fos[i]);
235                 }
236             }
237         }
238     }
239  */

240
241     public boolean messageMatchesFilterPattern(String JavaDoc message) {
242         if (messageFilter == null || message == null) {
243             return true;
244         }
245         if (messageFilterType == MESSAGE_FILTER_SUBSTRING) {
246             if (message.indexOf(messageFilter) >= 0) {
247                 return true;
248             }
249         }
250         else if (messageFilterType == MESSAGE_FILTER_REGEXP ||
251                  messageFilterType == MESSAGE_FILTER_SOME_WORDS ||
252                  messageFilterType == MESSAGE_FILTER_ALL_WORDS) {
253             if (messageRE != null) {
254                 if (messageRE.match(message)) {
255                     return true;
256                 }
257             } else {
258                 return true;
259             }
260         }
261         return false;
262     }
263     
264     public boolean fileMatchesFilterPattern(String JavaDoc file) {
265         if (fileFilter == null || file == null) {
266             return true;
267         }
268         if (fileFilterType == FILE_FILTER_SUBSTRING) {
269             if (file.indexOf(fileFilter) >= 0) {
270                 return true;
271             }
272         }
273         else if (fileFilterType == FILE_FILTER_REGEXP) {
274             if (fileRE != null) {
275                 if (fileRE.match(file)) {
276                     return true;
277                 }
278             } else {
279                 return true;
280             }
281         }
282         return false;
283     }
284    
285     public void addRevision(LogInfoRevision rev, String JavaDoc message) {
286         Iterator it = groupedResults.iterator();
287         boolean found = false;
288         while (it.hasNext()) {
289             RevisionsGroup group = (RevisionsGroup)it.next();
290             // I wonder if the message first and then time comparison is faster then
291
// vice versa..
292
if (message != null &&
293                 group.getMessage() != null &&
294                 message.equals(group.getMessage()))
295             {
296                     
297                 long gap1 = Math.abs(rev.getDate().getTime() - group.getStartingDate().getTime());
298                 long gap2 = Math.abs(rev.getDate().getTime() - group.getTrailingDate().getTime());
299                 if (gap2 < getMaxDateGapInTime() || gap1 < getMaxDateGapInTime()) {
300                     group.addRevision(rev, message);
301                     found = true;
302                     break;
303                 }
304             }
305         }
306         if (!found) {
307             RevisionsGroup newGroup = new RevisionsGroup();
308             newGroup.addRevision(rev, message);
309             groupedResults.add(newGroup);
310         }
311     }
312     
313     
314     private List findFOsForFiles(List fileList) {
315 /**
316  needs to be rewritten to make the recognition by partial ath as well..
317  *
318  Iterator it = fileList.iterator();
319         List foList = new LinkedList();
320         while (it.hasNext()) {
321             StatusInformation info = (StatusInformation)it.next();
322             FileObject fo = (FileObject)fileObjectMap.get(info.getFile());
323             foList.add(fo);
324         }
325         return foList;
326  *
327  */

328         return null;
329     }
330     
331
332     /**
333      * returns a list of RevisionsGroup instances.
334      */

335     public List getGroupsList() {
336         return groupedResults;
337     }
338         
339     /** Getter for property startingdate.
340      * @return Value of property startingdate.
341      */

342     public String JavaDoc getDateRange() {
343         return this.dateRange;
344     }
345     
346     /** Setter for property startingdate.
347      * @param startingdate New value of property startingdate.
348      */

349     public void setDateRange(String JavaDoc dateRange) {
350         this.dateRange = dateRange;
351     }
352     
353     /** Getter for property user.
354      * @return Value of property user.
355      */

356     public String JavaDoc getUser() {
357         return this.user;
358     }
359     
360     /** Setter for property user.
361      * @param user New value of property user.
362      */

363     public void setUser(String JavaDoc user) {
364         this.user = user;
365     }
366     
367     /** Getter for property branch.
368      * @return Value of property branch.
369      */

370     public String JavaDoc getRevisionRange() {
371         return this.revisionRange;
372     }
373     
374     /** Setter for property branch.
375      * @param branch New value of property branch.
376      */

377     public void setRevisionRange(String JavaDoc revisionRange) {
378         this.revisionRange = revisionRange;
379     }
380     
381     /** Getter for property toOutputWindow.
382      * @return Value of property toOutputWindow.
383      */

384     public boolean isToOutputWindow() {
385         return this.toOutputWindow;
386     }
387     
388     /** Setter for property toOutputWindow.
389      * @param toOutputWindow New value of property toOutputWindow.
390      */

391     public void setToOutputWindow(boolean toOutputWindow) {
392         this.toOutputWindow = toOutputWindow;
393     }
394     
395     /** Getter for property toTextFile.
396      * @return Value of property toTextFile.
397      * Null means the text file is not created.
398      */

399     public java.io.File JavaDoc getToTextFile() {
400         return this.toTextFile;
401     }
402     
403     /** Setter for property toTextFile.
404      * @param toTextFile New value of property toTextFile.
405      */

406     public void setToTextFile(java.io.File JavaDoc toTextFile) {
407         this.toTextFile = toTextFile;
408     }
409     
410     /** Getter for property toXmlFile.
411      * @return Value of property toXmlFile.
412      * Null means the xml file will not be written.
413      */

414     public java.io.File JavaDoc getToXmlFile() {
415         return this.toXmlFile;
416     }
417     
418     /** Setter for property toXmlFile.
419      * @param toXmlFile New value of property toXmlFile.
420      */

421     public void setToXmlFile(java.io.File JavaDoc toXmlFile) {
422         this.toXmlFile = toXmlFile;
423     }
424     
425     /** Getter for property toHtmlFile.
426      * @return Value of property toHtmlFile.
427      * Null means the html file will not be written.
428      */

429     public java.io.File JavaDoc getToHtmlFile() {
430         return this.toHtmlFile;
431     }
432     
433     /** Setter for property toHtmlFile.
434      * @param toHtmlFile New value of property toHtmlFile.
435      */

436     public void setToHtmlFile(java.io.File JavaDoc toHtmlFile) {
437         this.toHtmlFile = toHtmlFile;
438     }
439     
440     /** Getter for property toExplorer.
441      * @return Value of property toExplorer.
442      */

443     public boolean isToExplorer() {
444         return this.toExplorer;
445     }
446     
447     /** Setter for property toExplorer.
448      * @param toExplorer New value of property toExplorer.
449      */

450     public void setToExplorer(boolean toExplorer) {
451         this.toExplorer = toExplorer;
452     }
453     
454     public void setSortMode(int mode) {
455         this.sortMode = mode;
456     }
457     
458     public int getSortMode() {
459         return sortMode;
460     }
461     
462     
463     public void finishProcessing() {
464         if (getFileFilter() != null) {
465             //postprocess the file based filter.
466
// (or do anything else that cannot be achieved by the log command.
467
postprocess();
468         }
469         sort();
470         if (isToOutputWindow()) {
471             printerList.add(new LogPrinter_Output());
472         }
473         if (getToTextFile() != null) {
474             printerList.add(new LogPrinter_Text(getToTextFile()));
475         }
476         if (getToXmlFile() != null) {
477             printerList.add(new LogPrinter_XML(getToXmlFile()));
478         }
479         if (getToHtmlFile() != null || isViewInBrowser()) {
480             printerList.add(new LogPrinter_HTML(getToHtmlFile(), isViewInBrowser(), getHtmlSiteProcessor()));
481         }
482         printResults();
483     }
484
485     private void postprocess() {
486         List groupedResults = getGroupsList();
487         Iterator it = groupedResults.iterator();
488         while (it.hasNext()) {
489             RevisionsGroup group = (RevisionsGroup)it.next();
490             List list = group.getList();
491             boolean ok = false;
492             Iterator it2 = list.iterator();
493             while (it2.hasNext()) {
494                 LogInfoRevision rev = (LogInfoRevision)it2.next();
495                 if (fileMatchesFilterPattern(rev.getLogInfoHeader().getRepositoryFilename())) {
496                     ok = true;
497                     break;
498                 }
499             }
500             if (!ok) {
501                 it.remove();
502             }
503         }
504         
505     }
506     
507     private void sort() {
508         Comparator comp;
509         if (getSortMode() == SORT_BY_DATE) {
510             comp = new RevisionsGroup.GroupDateComparator(isDescendingSort());
511         } else if (getSortMode() == SORT_BY_USER) {
512             comp = new RevisionsGroup.UserDateComparator(isDescendingSort());
513         } else {
514             comp = new RevisionsGroup.GroupDateComparator(false);
515         }
516         Collections.sort(getGroupsList(), comp);
517     }
518     
519     private void processPrinters(String JavaDoc methodName, Class JavaDoc[] paramTypes, Object JavaDoc[] params) {
520         Iterator it = printerList.iterator();
521         while (it.hasNext()) {
522             Object JavaDoc printer = it.next();
523             try {
524                 Method method = printer.getClass().getMethod(methodName, paramTypes);
525                 method.invoke(printer, params);
526             } catch (InvocationTargetException exc1) {
527                 org.openide.ErrorManager.getDefault().annotate(exc1.getTargetException(), "");
528             } catch (Exception JavaDoc exc) {
529                 org.openide.ErrorManager.getDefault().annotate(exc, "");
530             }
531         }
532     }
533     
534     private void printResults() {
535         List groupedResults = getGroupsList();
536         SummaryProcessor sumProc = new SummaryProcessor();
537         processPrinters("printHeader", new Class JavaDoc[] {ChangeLogProcessor.class}, new Object JavaDoc[] {this});
538         Iterator it = groupedResults.iterator();
539         while (it.hasNext()) {
540             RevisionsGroup group = (RevisionsGroup)it.next();
541             processPrinters("printGroupHeader", new Class JavaDoc[] {RevisionsGroup.class}, new Object JavaDoc[] {group});
542             sumProc.processGroup(group);
543             List list = group.getSortedList(RevisionsGroup.SORTING_BY_FILE_NAME);
544             Iterator it2 = list.iterator();
545             while (it2.hasNext()) {
546                 LogInfoRevision rev = (LogInfoRevision)it2.next();
547                 processPrinters("printSingleRevision", new Class JavaDoc[] {LogInfoRevision.class}, new Object JavaDoc[] {rev});
548             }
549             processPrinters("printGroupFooter", new Class JavaDoc[] {RevisionsGroup.class}, new Object JavaDoc[] {group});
550         }
551         processPrinters("printSummary", new Class JavaDoc[] {SummaryProcessor.class}, new Object JavaDoc[] {sumProc});
552         processPrinters("printFooter", new Class JavaDoc[] {ChangeLogProcessor.class}, new Object JavaDoc[] {this});
553     }
554
555     
556     
557     /** Getter for property descendingSort.
558      * @return Value of property descendingSort.
559      */

560     public boolean isDescendingSort() {
561         return this.descendingSort;
562     }
563     
564     /** Setter for property descendingSort.
565      * @param descendingSort New value of property descendingSort.
566      */

567     public void setDescendingSort(boolean descendingSort) {
568         this.descendingSort = descendingSort;
569     }
570     
571     /** Getter for property htmlSiteProcessor.
572      * @return Value of property htmlSiteProcessor.
573      */

574     public ChangeLogHTMLService getHtmlSiteProcessor() {
575         return this.htmlSiteProcessor;
576     }
577     
578     /** Setter for property htmlSiteProcessor.
579      * @param htmlSiteProcessor New value of property htmlSiteProcessor.
580      */

581     public void setHtmlSiteProcessor(ChangeLogHTMLService htmlSiteProcessor) {
582         this.htmlSiteProcessor = htmlSiteProcessor;
583     }
584     
585     /** Getter for property includeQueryDescription.
586      * @return Value of property includeQueryDescription.
587      */

588     public boolean isIncludeQueryDescription() {
589         return this.includeQueryDescription;
590     }
591     
592     /** Setter for property includeQueryDescription.
593      * @param includeQueryDescription New value of property includeQueryDescription.
594      */

595     public void setIncludeQueryDescription(boolean includeQueryDescription) {
596         this.includeQueryDescription = includeQueryDescription;
597     }
598     
599     /** Getter for property includeSummary.
600      * @return Value of property includeSummary.
601      */

602     public boolean isIncludeSummary() {
603         return this.includeSummary;
604     }
605     
606     /** Setter for property includeSummary.
607      * @param includeSummary New value of property includeSummary.
608      */

609     public void setIncludeSummary(boolean includeSummary) {
610         this.includeSummary = includeSummary;
611     }
612     
613     /** Getter for property includeBranchNames.
614      * @return Value of property includeBranchNames.
615      */

616     public boolean isIncludeBranchNames() {
617         return this.includeBranchNames;
618     }
619     
620     /** Setter for property includeBranchNames.
621      * @param includeBranchNames New value of property includeBranchNames.
622      */

623     public void setIncludeBranchNames(boolean includeBranchNames) {
624         this.includeBranchNames = includeBranchNames;
625     }
626     
627 }
628
Popular Tags