KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.changelog;
20
21
22 import java.util.*;
23 import org.openide.*;
24 import java.io.*;
25
26 import org.netbeans.modules.changelog.html.*;
27
28 /**
29  * prints the processed groups to text file..
30  * @author ralph
31  */

32 public class LogPrinter_HTML implements LogPrinter {
33
34     private static final String JavaDoc SPECIAL_KEY = "SPECIAL"; //NOI18N
35
private static final String JavaDoc PREV_REVISION = "previous_revision"; //NOI18N
36
private static final String JavaDoc FILE_PATH = "file_path"; //NOI18N
37
private static final String JavaDoc CURR_REVISION = "current_revision"; //NOI18N
38

39
40     private java.io.PrintWriter JavaDoc writer;
41
42     private File file;
43     
44     private boolean view;
45     
46     private boolean includeSummary;
47     
48     private boolean hasCommonBranch;
49     
50     private ChangeLogHTMLService service;
51     
52     public LogPrinter_HTML(File fileParam, boolean view) {
53         this.file = fileParam;
54         this.view = view;
55         writer = null;
56         try {
57             if (this.file == null) {
58                 this.file = File.createTempFile("chlog", ".html");
59                 file.deleteOnExit();
60             }
61             if (!file.exists()) {
62                 try {
63                     if (file.getParentFile() != null) {
64                         file.getParentFile().mkdirs();
65                     }
66                     file.createNewFile();
67                 } catch (IOException exc) {
68                     org.openide.ErrorManager.getDefault().notify(exc);
69                     System.out.println("error while creating file..");
70                 }
71             }
72             writer = new PrintWriter(new FileOutputStream(file));
73             
74         } catch (IOException exc) {
75             org.openide.ErrorManager.getDefault().notify(exc);
76             System.out.println("error while opening file..");
77         }
78     }
79     
80     public LogPrinter_HTML(File file, boolean view, ChangeLogHTMLService service) {
81         this(file, view);
82         this.service = service;
83     }
84     
85     public void printHeader(ChangeLogProcessor processor) {
86         includeSummary = processor.isIncludeSummary();
87         if (writer == null) {
88             return;
89         }
90         writer.println("<HTML>");
91         writer.println("<HEAD>");
92         writer.println("<TITLE> CVS Repository Query Results </TITLE>");
93         writer.println("</HEAD>");
94         writer.println("<BODY>");
95         
96         
97         if (processor.isIncludeQueryDescription()) {
98             writer.println("<H1> Query: </H1>");
99             if (processor.getDateRange() != null) {
100                 writer.println(" Date Range: " + ChangeLogUtils.escapeString(processor.getDateRange()));
101                 writer.println("<P>");
102             }
103             if (processor.getRevisionRange() != null) {
104                 writer.println(" Revision Filter:" + ChangeLogUtils.escapeString(processor.getRevisionRange()));
105                 writer.println("<P>");
106             }
107             if (processor.getUser() != null) {
108                 writer.println(" User Filter:" + ChangeLogUtils.escapeString(processor.getUser()));
109                 writer.println("<P>");
110             }
111             if (processor.getMessageFilter() != null) {
112                 String JavaDoc messageType = "";
113                 if (processor.getMessageFilterType() == ChangeLogProcessor.MESSAGE_FILTER_SUBSTRING) {
114                     messageType = "Substring";
115                 } else if (processor.getMessageFilterType() == ChangeLogProcessor.MESSAGE_FILTER_SOME_WORDS) {
116                     messageType = "Any of words";
117                 } else if (processor.getMessageFilterType() == ChangeLogProcessor.MESSAGE_FILTER_ALL_WORDS) {
118                     messageType = "All of words";
119                 } else if (processor.getMessageFilterType() == ChangeLogProcessor.MESSAGE_FILTER_REGEXP) {
120                     messageType = "Regular expression";
121                 }
122                 writer.println(" Message Filter (" + ChangeLogUtils.escapeString(messageType) + "):" + ChangeLogUtils.escapeString(processor.getMessageFilter()));
123                 writer.println("<P>");
124             }
125             if (processor.getFileFilter() != null) {
126                 String JavaDoc fileType = "";
127                 if (processor.getFileFilterType() == ChangeLogProcessor.FILE_FILTER_SUBSTRING) {
128                     fileType = "Substring";
129                 } else if (processor.getFileFilterType() == ChangeLogProcessor.FILE_FILTER_REGEXP) {
130                     fileType = "Regular expression";
131                 }
132                 writer.println(" Contained Files Filter (" + ChangeLogUtils.escapeString(fileType) + "):" + ChangeLogUtils.escapeString(processor.getFileFilter()));
133                 writer.println("<P>");
134             }
135             if (processor.getSortMode() == ChangeLogProcessor.SORT_BY_DATE) {
136                 writer.print(" Sort: by Date");
137             } else if (processor.getSortMode() == ChangeLogProcessor.SORT_BY_USER) {
138                 writer.print(" Sort: by User");
139             }
140             if (processor.isDescendingSort()) {
141                 writer.println(" (Descending)");
142             } else {
143                 writer.println(" (Ascending)");
144             }
145             writer.println("<P>");
146             if (processor.isIncludeBranchNames()) {
147                 writer.println("Includes Branch Names Information <P>");
148             }
149             writer.println("<BR>");
150             writer.println("<BR>");
151         }
152         writer.println("<H1> Results: </H1>");
153     }
154     
155     public void printGroupHeader(RevisionsGroup group) {
156         if (writer == null) {
157             return;
158         }
159         hasCommonBranch = false;
160         writer.println("<UL>");
161           writer.println("--------------------------------------------------");
162           writer.println("<LI>User: <I>" + ChangeLogUtils.escapeString(group.getUser()) + "</I>");
163           writer.println("<LI>Date: <I>" + ChangeLogUtils.escapeString(group.getStartingDate().toString()) + "</I>");
164           if (group.getCommonBranch() != null) {
165               writer.println("<LI>Branch: <B><I>" + ChangeLogUtils.escapeString(group.getCommonBranch()) + "</I></B>");
166               hasCommonBranch = true;
167           }
168           writer.println("<LI>Message: <B>" + ChangeLogUtils.escapeString(group.getMessage()) + "</B>");
169           writer.println("<UL>");
170     }
171     
172     public void printSingleRevision(LogInfoRevision revision) {
173         if (writer == null ) {
174             return;
175         }
176         String JavaDoc repoFileName = revision.getLogInfoHeader().getRepositoryFilename();
177         // remove the trailing ,v part..
178
repoFileName = repoFileName.substring(0, repoFileName.length() - 2);
179
180         writer.println("<LI>"); //NOI18N
181
HashMap replacementMap = computeReplacements(revision);
182         if (service != null) {
183             String JavaDoc url = ChangeLogUtils.replaceArguments(service.getRevisionURL(), replacementMap);
184             writer.println("<A HREF='" + url + "'>" + revision.getNumber() + "</A> "); //NOI18N
185
} else {
186             writer.println(revision.getNumber());
187         }
188         writer.println(" " + repoFileName);
189         if ((!replacementMap.containsKey(SPECIAL_KEY)) && //NOI18N
190
replacementMap.containsKey(PREV_REVISION) && //NOI18N
191
service != null) {
192             String JavaDoc diffUrl = ChangeLogUtils.replaceArguments(service.getDiffURL(), replacementMap);
193 // String diffUrl = "http://www.netbeans.org/source/browse/" + reducedRepoFileName
194
// + ".diff?r1=text&tr1=" + prevRevision + "&r2=text&tr2=" + revision.getNumber() + "&f=h";
195
writer.println("[<A HREF='" + diffUrl + "'> Diff </A>]");
196         } else {
197             // do nothing..
198
}
199         if (replacementMap.containsKey(SPECIAL_KEY)) { //NOI18N
200
writer.println(" ["+ replacementMap.get(SPECIAL_KEY) + "]");
201         }
202         if (!revision.getBranch().equals("") && !hasCommonBranch) {
203             writer.println(" On Branch: <B>" + revision.getBranch() + "</B>");
204         }
205     }
206     
207     private HashMap computeReplacements(LogInfoRevision revision) {
208         String JavaDoc repoFileName = revision.getLogInfoHeader().getRepositoryFilename();
209         // remove the trailing ,v part..
210
repoFileName = repoFileName.substring(0, repoFileName.length() - 2);
211         HashMap replacementMap = new HashMap();
212         if (service != null) {
213             String JavaDoc reducedRepoFileName;
214             if (repoFileName.startsWith(service.getCvsRootPrefix())) {
215                 reducedRepoFileName = repoFileName.substring(service.getCvsRootPrefix().length() + 1);
216             } else {
217                 reducedRepoFileName = repoFileName;
218             }
219             replacementMap.put(FILE_PATH, reducedRepoFileName); //NOI18N
220
} else {
221             replacementMap.put(FILE_PATH, repoFileName);
222         }
223         replacementMap.put(CURR_REVISION, revision.getNumber()); //NOI18N
224
String JavaDoc revNumber = revision.getNumber();
225         if (revision.getState().equals("dead")) { //NOI18N
226
replacementMap.put(SPECIAL_KEY, "Removed"); //NOI18N
227
} else {
228             int[] revNumbers = ChangeLogUtils.convertRevisionToIntArray(revision.getNumber());
229             if (revNumbers[revNumbers.length - 2] % 2 != 0 && revNumbers.length != 2) {
230                 replacementMap.put(SPECIAL_KEY, "Imported"); //NOI18N
231
} else {
232                 if (revNumbers[revNumbers.length - 1] == 1) {
233                     if (revNumbers.length == 2) {
234                         replacementMap.put(SPECIAL_KEY, "Added"); //NOI18N
235
} else {
236                         revNumbers[revNumbers.length - 1] = 0;
237                         revNumbers[revNumbers.length - 2] = 0;
238                         String JavaDoc prevrev = ChangeLogUtils.convertIntArrayToRevision(revNumbers);
239                         replacementMap.put(PREV_REVISION, prevrev);
240                     }
241                 } else {
242                     revNumbers[revNumbers.length - 1] = revNumbers[revNumbers.length - 1] - 1;
243                     String JavaDoc prevrev = ChangeLogUtils.convertIntArrayToRevision(revNumbers);
244                     replacementMap.put(PREV_REVISION, prevrev);
245                 }
246             }
247         }
248         return replacementMap;
249         
250     }
251     
252     
253     public void printGroupFooter(RevisionsGroup group) {
254         if (writer == null) {
255             return;
256         }
257         writer.println("</UL>");
258         writer.println("</UL>");
259     }
260     
261     public void printSummary(SummaryProcessor processor) {
262         if (writer == null) {
263             return;
264         }
265         if (includeSummary) {
266             writer.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
267             writer.println("<H1>Summary:</H1>");
268             writer.println("<B>Number of changes: " + processor.getCommitCount() + "</B>");
269             writer.println("<P>");
270             writer.print("<B>Developers: </B>");
271             String JavaDoc[] users = processor.getUserList();
272             for (int i = 0; i < users.length; i++) {
273                 if (i != users.length - 1) {
274                     writer.print(ChangeLogUtils.escapeString(users[i]) + ", ");
275                 } else {
276                     writer.print(ChangeLogUtils.escapeString(users[i]));
277                 }
278             }
279             writer.println("<P> <BR>");
280             String JavaDoc[] mostChanged = processor.getMostChangedFiles();
281             writer.println("<B>Most frequently changed files:</B>");
282             writer.println("<UL>");
283             for (int j = 0; j < mostChanged.length; j++) {
284                 writer.println("<LI>" + ChangeLogUtils.escapeString(mostChanged[j]));
285             }
286             writer.println("</UL>");
287             writer.println("<P> <BR>");
288             writer.println("<B>Most active developers (based on number of commits): </B>");
289             String JavaDoc[] mostActive= processor.getMostActiveUsers();
290             writer.println("<UL>");
291             for (int k = 0; k < mostActive.length; k++) {
292                 writer.println("<LI>" + ChangeLogUtils.escapeString(mostActive[k]));
293             }
294             writer.println("</UL>");
295             writer.println("<P> <BR>");
296         }
297     }
298     
299     public void printFooter(ChangeLogProcessor processor) {
300         if (writer == null) {
301             return;
302         }
303         writer.println("</BODY>");
304         writer.println("</HTML>");
305         writer.flush();
306         writer.close();
307         if (view) {
308             try {
309                 TopManager.getDefault().showUrl(file.toURL());
310             } catch (java.net.MalformedURLException JavaDoc exc) {
311                 System.out.println("malformed URL.");
312             }
313         }
314         
315         
316     }
317     
318 }
319
Popular Tags