KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > log > LogBuilder


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 Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.lib.cvsclient.command.log;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.text.ParseException JavaDoc;
25
26 import org.netbeans.lib.cvsclient.command.*;
27 import org.netbeans.lib.cvsclient.event.*;
28 import org.netbeans.lib.cvsclient.util.*;
29
30 /**
31  * Handles the building of a log information object and the firing of
32  * events when complete objects are built.
33  * @author Milos Kleint
34  */

35 public class LogBuilder implements Builder {
36     private static final String JavaDoc LOGGING_DIR = ": Logging "; //NOI18N
37
private static final String JavaDoc RCS_FILE = "RCS file: "; //NOI18N
38
private static final String JavaDoc WORK_FILE = "Working file: "; //NOI18N
39
private static final String JavaDoc REV_HEAD = "head: "; //NOI18N
40
private static final String JavaDoc BRANCH = "branch: "; //NOI18N
41
private static final String JavaDoc LOCKS = "locks: "; //NOI18N
42
private static final String JavaDoc ACCESS_LIST = "access list: "; //NOI18N
43
private static final String JavaDoc SYM_NAME = "symbolic names:"; //NOI18N
44
private static final String JavaDoc KEYWORD_SUBST = "keyword substitution: "; //NOI18N
45
private static final String JavaDoc TOTAL_REV = "total revisions: "; //NOI18N
46
private static final String JavaDoc SEL_REV = ";\tselected revisions: "; //NOI18N
47
private static final String JavaDoc DESCRIPTION = "description:"; //NOI18N
48
private static final String JavaDoc REVISION = "revision "; //NOI18N
49
private static final String JavaDoc DATE = "date: "; //NOI18N
50
private static final String JavaDoc BRANCHES = "branches: "; //NOI18N
51
private static final String JavaDoc AUTHOR = "author: "; //NOI18N
52
private static final String JavaDoc STATE = "state: "; //NOI18N
53
private static final String JavaDoc LINES = "lines: "; //NOI18N
54
private static final String JavaDoc COMMITID = "commitid: "; //NOI18N
55
private static final String JavaDoc SPLITTER = "----------------------------"; //NOI18N
56
private static final String JavaDoc FINAL_SPLIT = "============================================================================="; //NOI18N
57
private static final String JavaDoc ERROR = ": nothing known about "; //NOI18N
58
private static final String JavaDoc NO_FILE = "no file"; //NOI18N
59
/**
60      * The event manager to use
61      */

62     protected EventManager eventManager;
63     protected BasicCommand logCommand;
64     /**
65      * The log object that is currently being built
66      */

67     protected LogInformation logInfo;
68     protected LogInformation.Revision revision;
69     /**
70      * The directory in which the file being processed lives. This is
71      * relative to the local directory
72      */

73     protected String JavaDoc fileDirectory;
74     private boolean addingSymNames;
75     private boolean addingDescription;
76     private boolean addingLogMessage;
77     private StringBuffer JavaDoc tempBuffer = null;
78     
79     private List messageList;
80
81     private final SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss Z"); //NOI18N
82

83     public LogBuilder(EventManager eventMan, BasicCommand command) {
84         logCommand = command;
85         eventManager = eventMan;
86         addingSymNames = false;
87         addingDescription = false;
88         addingLogMessage = false;
89         logInfo = null;
90         revision = null;
91         messageList = new ArrayList(500);
92     }
93
94     public void outputDone() {
95         if (logInfo != null) {
96             eventManager.fireCVSEvent(new FileInfoEvent(this, logInfo));
97             logInfo = null;
98             messageList = null;
99         }
100     }
101
102     public void parseLine(String JavaDoc line, boolean isErrorMessage) {
103         if (line.equals(FINAL_SPLIT)) {
104             if (addingDescription) {
105                 addingDescription = false;
106                 logInfo.setDescription(tempBuffer.toString());
107             }
108             if (addingLogMessage) {
109                 addingLogMessage = false;
110                 revision.setMessage(CommandUtils.findUniqueString(tempBuffer.toString(), messageList));
111             }
112             if (revision != null) {
113                 logInfo.addRevision(revision);
114                 revision = null;
115             }
116             // fire the event and exit
117
if (logInfo != null) {
118                 eventManager.fireCVSEvent(new FileInfoEvent(this, logInfo));
119                 logInfo = null;
120                 tempBuffer = null;
121             }
122             return;
123         }
124         if (addingLogMessage) {
125             // first check for the branches tag
126
if (line.startsWith(BRANCHES)) {
127                 processBranches(line.substring(BRANCHES.length()));
128             }
129             else {
130                 processLogMessage(line);
131                 return;
132             }
133         }
134         if (addingSymNames) {
135             processSymbolicNames(line);
136         }
137         if (addingDescription) {
138             processDescription(line);
139         }
140         // revision stuff first -> will be the most common to parse
141
if (line.startsWith(REVISION)) {
142             processRevisionStart(line);
143         }
144         if (line.startsWith(DATE)) {
145             processRevisionDate(line);
146         }
147
148         if (line.startsWith(KEYWORD_SUBST)) {
149             logInfo.setKeywordSubstitution(line.substring(KEYWORD_SUBST.length()).trim().intern());
150             addingSymNames = false;
151             return;
152         }
153
154         if (line.startsWith(DESCRIPTION)) {
155             tempBuffer = new StringBuffer JavaDoc(line.substring(DESCRIPTION.length()));
156             addingDescription = true;
157         }
158
159         if (line.indexOf(LOGGING_DIR) >= 0) {
160             fileDirectory = line.substring(line.indexOf(LOGGING_DIR) + LOGGING_DIR.length()).trim();
161             return;
162         }
163         if (line.startsWith(RCS_FILE)) {
164             processRcsFile(line.substring(RCS_FILE.length()));
165             return;
166         }
167         if (line.startsWith(WORK_FILE)) {
168             processWorkingFile(line.substring(WORK_FILE.length()));
169             return;
170         }
171         if (line.startsWith(REV_HEAD)) {
172             logInfo.setHeadRevision(line.substring(REV_HEAD.length()).trim().intern());
173             return;
174         }
175         if (line.startsWith(BRANCH)) {
176             logInfo.setBranch(line.substring(BRANCH.length()).trim().intern());
177         }
178         if (line.startsWith(LOCKS)) {
179             logInfo.setLocks(line.substring(LOCKS.length()).trim().intern());
180         }
181         if (line.startsWith(ACCESS_LIST)) {
182             logInfo.setAccessList(line.substring(ACCESS_LIST.length()).trim().intern());
183         }
184         if (line.startsWith(SYM_NAME)) {
185             addingSymNames = true;
186         }
187         if (line.startsWith(TOTAL_REV)) {
188             int ind = line.indexOf(';');
189             if (ind < 0) {
190                 // no selected revisions here..
191
logInfo.setTotalRevisions(line.substring(TOTAL_REV.length()).trim().intern());
192                 logInfo.setSelectedRevisions("0"); //NOI18N
193
}
194             else {
195                 String JavaDoc total = line.substring(0, ind);
196                 String JavaDoc select = line.substring(ind, line.length());
197                 logInfo.setTotalRevisions(total.substring(TOTAL_REV.length()).trim().intern());
198                 logInfo.setSelectedRevisions(select.substring(SEL_REV.length()).trim().intern());
199             }
200         }
201     }
202
203     private String JavaDoc findUniqueString(String JavaDoc name, List list) {
204         if (name == null) {
205             return null;
206         }
207         int index = list.indexOf(name);
208         if (index >= 0) {
209             return (String JavaDoc)list.get(index);
210         }
211         else {
212             String JavaDoc newName = name;
213             list.add(newName);
214             return newName;
215         }
216     }
217     
218     private void processRcsFile(String JavaDoc line) {
219         if (logInfo != null) {
220             //do fire logcreated event;
221
}
222         logInfo = new LogInformation();
223         logInfo.setRepositoryFilename(line.trim());
224     }
225
226     private void processWorkingFile(String JavaDoc line) {
227         String JavaDoc fileName = line.trim();
228         if (fileName.startsWith(NO_FILE)) {
229             fileName = fileName.substring(8);
230         }
231
232         logInfo.setFile(createFile(line));
233     }
234
235     private void processBranches(String JavaDoc line) {
236         int ind = line.lastIndexOf(';');
237         if (ind > 0) {
238             line = line.substring(0, ind);
239         }
240         revision.setBranches(line.trim());
241     }
242
243     private void processLogMessage(String JavaDoc line) {
244         if (line.startsWith(SPLITTER)) {
245             addingLogMessage = false;
246             revision.setMessage(findUniqueString(tempBuffer.toString(), messageList));
247             return;
248         }
249         tempBuffer.append(line + "\n"); //NOI18N
250
}
251
252     private void processSymbolicNames(String JavaDoc line) {
253         if (!line.startsWith(KEYWORD_SUBST)) {
254             line = line.trim();
255             int index = line.indexOf(':');
256             if (index > 0) {
257                 String JavaDoc symName = line.substring(0, index).trim();
258                 String JavaDoc revName = line.substring(index + 1, line.length()).trim();
259                 logInfo.addSymbolicName(symName.intern(), revName.intern());
260             }
261         }
262     }
263
264     private void processDescription(String JavaDoc line) {
265         if (line.startsWith(SPLITTER)) {
266             addingDescription = false;
267             logInfo.setDescription(tempBuffer.toString());
268             return;
269         }
270         tempBuffer.append(line);
271     }
272
273     private void processRevisionStart(String JavaDoc line) {
274         if (revision != null) {
275             logInfo.addRevision(revision);
276         }
277         revision = logInfo.createNewRevision(
278                     line.substring(REVISION.length()).intern());
279     }
280
281     private void processRevisionDate(String JavaDoc line) {
282         StringTokenizer tokenizer = new StringTokenizer(line, ";", false); //NOI18N
283
while (tokenizer.hasMoreTokens()) {
284             String JavaDoc token = tokenizer.nextToken().trim();
285             if (token.startsWith(DATE)) {
286                 String JavaDoc dateString = token.substring(DATE.length());
287                 Date date = null;
288                 try {
289                     // some servers use dashes to separate date components, so replace with slashes
290
// also add a default GMT timezone at the end, if the server already put one in this one will be ignored by the parser
291
dateString = dateString.replace('/', '-') + " +0000"; //NOI18N
292
date = dateFormat.parse(dateString);
293                 } catch (ParseException JavaDoc e) {
294                     BugLog.getInstance().bug("Couldn't parse date " + dateString); //NOI18N
295
}
296                 revision.setDate(date, dateString);
297             }
298             else if (token.startsWith(AUTHOR)) revision.setAuthor(token.substring(AUTHOR.length()));
299             else if (token.startsWith(STATE)) revision.setState(token.substring(STATE.length()));
300             else if (token.startsWith(LINES)) revision.setLines(token.substring(LINES.length()));
301             else if (token.startsWith(COMMITID)) revision.setCommitID(token.substring(COMMITID.length()));
302         }
303         addingLogMessage = true;
304         tempBuffer = new StringBuffer JavaDoc();
305     }
306
307     /**
308      * @param fileName relative URL-path to command execution directory
309      */

310     protected File createFile(String JavaDoc fileName) {
311         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
312         path.append(logCommand.getLocalDirectory());
313         path.append(File.separator);
314
315         path.append(fileName.replace('/', File.separatorChar)); // NOI18N
316
return new File(path.toString());
317     }
318
319     public void parseEnhancedMessage(String JavaDoc key, Object JavaDoc value) {
320     }
321 }
322
Popular Tags