KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > annotate > AnnotateBuilder


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.annotate;
20
21 import java.io.*;
22
23 import org.netbeans.lib.cvsclient.command.*;
24 import org.netbeans.lib.cvsclient.event.*;
25
26 /**
27  * Handles the building of a annotate information object and the firing of
28  * events when complete objects are built.
29  *
30  * @author Milos Kleint
31  */

32 public class AnnotateBuilder implements Builder {
33     private static final String JavaDoc UNKNOWN = ": nothing known about"; //NOI18N
34
private static final String JavaDoc ANNOTATING = "Annotations for "; //NOI18N
35
private static final String JavaDoc STARS = "***************"; //NOI18N
36

37     /**
38      * The Annotate object that is currently being built.
39      */

40     private AnnotateInformation annotateInformation;
41
42     /**
43      * The event manager to use.
44      */

45     private final EventManager eventManager;
46
47     private final String JavaDoc localPath;
48     private String JavaDoc relativeDirectory;
49     private int lineNum;
50     private File tempDir;
51
52     public AnnotateBuilder(EventManager eventManager, BasicCommand annotateCommand) {
53         this.eventManager = eventManager;
54         this.localPath = annotateCommand.getLocalDirectory();
55         tempDir = annotateCommand.getGlobalOptions().getTempDir();
56     }
57
58     public void outputDone() {
59         if (annotateInformation == null) {
60             return;
61         }
62
63         try {
64             annotateInformation.closeTempFile();
65         }
66         catch (IOException exc) {
67             // ignore
68
}
69         eventManager.fireCVSEvent(new FileInfoEvent(this, annotateInformation));
70         annotateInformation = null;
71     }
72
73     public void parseLine(String JavaDoc line, boolean isErrorMessage) {
74         if (isErrorMessage && line.startsWith(ANNOTATING)) {
75             outputDone();
76             annotateInformation = new AnnotateInformation(tempDir);
77             annotateInformation.setFile(createFile(line.substring(ANNOTATING.length())));
78             lineNum = 0;
79             return;
80         }
81
82         if (isErrorMessage && line.startsWith(STARS)) {
83             // skip
84
return;
85         }
86
87         if (!isErrorMessage) {
88             processLines(line);
89         }
90     }
91
92     private File createFile(String JavaDoc fileName) {
93         return new File(localPath, fileName);
94     }
95
96     public void parseEnhancedMessage(String JavaDoc key, Object JavaDoc value) {
97     }
98
99     private void processLines(String JavaDoc line) {
100         if (annotateInformation != null) {
101             try {
102                 annotateInformation.addToTempFile(line);
103             }
104             catch (IOException exc) {
105                 // just ignore, should not happen.. if it does the worst thing that happens is a annotate info without data..
106
}
107         }
108 /*
109         AnnotateLine annLine = processLine(line);
110         if (annotateInformation != null && annLine != null) {
111             annLine.setLineNum(lineNum);
112             annotateInformation.addLine(annLine);
113             lineNum++;
114         }
115  */

116     }
117
118     public static AnnotateLine processLine(String JavaDoc line) {
119         int indexOpeningBracket = line.indexOf('(');
120         int indexClosingBracket = line.indexOf(')');
121         AnnotateLine annLine = null;
122         if (indexOpeningBracket > 0 && indexClosingBracket > indexOpeningBracket) {
123             String JavaDoc revision = line.substring(0, indexOpeningBracket).trim();
124             String JavaDoc userDate = line.substring(indexOpeningBracket + 1, indexClosingBracket);
125             String JavaDoc contents = line.substring(indexClosingBracket + 3);
126             int lastSpace = userDate.lastIndexOf(' ');
127             String JavaDoc user = userDate;
128             String JavaDoc date = userDate;
129             if (lastSpace > 0) {
130                 user = userDate.substring(0, lastSpace).trim();
131                 date = userDate.substring(lastSpace).trim();
132             }
133             annLine = new AnnotateLine();
134             annLine.setContent(contents);
135             annLine.setAuthor(user);
136             annLine.setDateString(date);
137             annLine.setRevision(revision);
138         }
139         return annLine;
140     }
141 }
142
Popular Tags