KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > commit > CommitBuilder


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

33 public class CommitBuilder
34         implements Builder {
35
36     /**
37      * Parsing constants.
38      */

39     public static final String JavaDoc UNKNOWN = "commit: nothing known about `"; //NOI18N
40
public static final String JavaDoc EXAM_DIR = ": Examining"; //NOI18N
41
public static final String JavaDoc REMOVING = "Removing "; //NOI18N
42
public static final String JavaDoc NEW_REVISION = "new revision:"; //NOI18N
43
public static final String JavaDoc INITIAL_REVISION = "initial revision:"; //NOI18N
44
public static final String JavaDoc DELETED_REVISION = "delete"; //NOI18N
45
public static final String JavaDoc DONE = "done"; //NOI18N
46
public static final String JavaDoc RCS_FILE = "RCS file: "; //NOI18N
47
public static final String JavaDoc ADD = "commit: use `cvs add' to create an entry for "; //NOI18N
48
public static final String JavaDoc COMMITTED = " <-- "; // NOI18N
49

50     /**
51      * The status object that is currently being built.
52      */

53     private CommitInformation commitInformation;
54
55     /**
56      * The directory in which the file being processed lives. This is
57      * absolute inside the local directory
58      */

59     private File fileDirectory;
60
61     /**
62      * The event manager to use.
63      */

64     private final EventManager eventManager;
65
66     private final String JavaDoc localPath;
67     
68     private final String JavaDoc repositoryRoot;
69
70     private boolean isAdding;
71
72     public CommitBuilder(EventManager eventManager, String JavaDoc localPath, String JavaDoc repositoryRoot) {
73         this.eventManager = eventManager;
74         this.localPath = localPath;
75         this.repositoryRoot = repositoryRoot;
76     }
77
78     public void outputDone() {
79         if (commitInformation != null) {
80             eventManager.fireCVSEvent(new FileInfoEvent(this, commitInformation));
81             commitInformation = null;
82         }
83     }
84
85     public void parseLine(String JavaDoc line, boolean isErrorMessage) {
86         int c;
87         if (line.indexOf(UNKNOWN) >= 0) {
88             outputDone();
89             processUnknownFile(line.substring(line.indexOf(UNKNOWN) + UNKNOWN.length()).trim());
90         }
91         else if (line.indexOf(ADD) > 0) {
92             processToAddFile(line.substring(line.indexOf(ADD) + ADD.length()).trim());
93         }
94         else if ((c = line.indexOf(COMMITTED)) > 0) {
95             outputDone();
96             String JavaDoc fileName = line.substring(c + COMMITTED.length()).trim();
97             int nameIndex = fileName.lastIndexOf('/');
98             if (nameIndex != -1) { //#73181 happens with 1.12 servers: /usr/cvsrepo/Java112/nbproject/project.properties,v <-- nbproject/project.properties
99
fileName = fileName.substring(nameIndex+1);
100             }
101             File file;
102             if (fileDirectory == null) {
103                 String JavaDoc reposPath = line.substring(0, c).trim();
104                 if (reposPath.startsWith(repositoryRoot)) {
105                     reposPath = reposPath.substring(repositoryRoot.length());
106                     if (reposPath.startsWith("/")) reposPath = reposPath.substring(1);
107                 }
108                 c = reposPath.lastIndexOf('/');
109                 if (c > 0) reposPath = reposPath.substring(0, c); // remove the file name
110
file = findFile(fileName, reposPath);
111             } else {
112                 file = new File(fileDirectory, fileName);
113             }
114             processFile(file);
115             if (isAdding) {
116                 commitInformation.setType(CommitInformation.ADDED);
117                 isAdding = false;
118             }
119             else {
120                 commitInformation.setType(CommitInformation.CHANGED);
121             }
122         }
123         else if (line.startsWith(REMOVING)) {
124             outputDone();
125             processFile(line.substring(REMOVING.length(), line.length() - 1));
126             // - 1 means to cut the ';' character
127
commitInformation.setType(CommitInformation.REMOVED);
128         }
129         else if (line.indexOf(EXAM_DIR) >= 0) {
130             fileDirectory = new File(localPath, line.substring(line.indexOf(EXAM_DIR) + EXAM_DIR.length()).trim());
131         }
132         else if (line.startsWith(RCS_FILE)) {
133             isAdding = true;
134         }
135         else if (line.startsWith(DONE)) {
136             outputDone();
137         }
138         else if (line.startsWith(INITIAL_REVISION)) {
139             processRevision(line.substring(INITIAL_REVISION.length()));
140             commitInformation.setType(CommitInformation.ADDED);
141         }
142         else if (line.startsWith(NEW_REVISION)) {
143             processRevision(line.substring(NEW_REVISION.length()));
144         }
145     }
146
147     private File createFile(String JavaDoc fileName) {
148         return new File(localPath, fileName);
149     }
150
151     private void processUnknownFile(String JavaDoc line) {
152         commitInformation = new CommitInformation();
153         commitInformation.setType(CommitInformation.UNKNOWN);
154         int index = line.indexOf('\'');
155         String JavaDoc fileName = line.substring(0, index).trim();
156         commitInformation.setFile(createFile(fileName));
157         outputDone();
158     }
159
160     private void processToAddFile(String JavaDoc line) {
161         commitInformation = new CommitInformation();
162         commitInformation.setType(CommitInformation.TO_ADD);
163         String JavaDoc fileName = line.trim();
164         if (fileName.endsWith(";")) { //NOI18N
165
fileName = fileName.substring(0, fileName.length() - 2);
166         }
167         commitInformation.setFile(createFile(fileName));
168         outputDone();
169     }
170
171     private void processFile(String JavaDoc filename) {
172         if (commitInformation == null) {
173             commitInformation = new CommitInformation();
174         }
175
176         if (filename.startsWith("no file")) { //NOI18N
177
filename = filename.substring(8);
178         }
179         commitInformation.setFile(createFile(filename));
180     }
181
182     private void processFile(File file) {
183         if (commitInformation == null) {
184             commitInformation = new CommitInformation();
185         }
186
187         commitInformation.setFile(file);
188     }
189
190     private void processRevision(String JavaDoc revision) {
191         int index = revision.indexOf(';');
192         if (index >= 0) {
193             revision = revision.substring(0, index);
194         }
195         revision = revision.trim();
196         if (DELETED_REVISION.equals(revision)) {
197             commitInformation.setType(CommitInformation.REMOVED);
198         }
199         commitInformation.setRevision(revision);
200     }
201
202     public void parseEnhancedMessage(String JavaDoc key, Object JavaDoc value) {
203     }
204     
205     private File findFile(String JavaDoc fileName, String JavaDoc reposPath) {
206         File dir = new File(localPath);
207         // happens when adding a new file to a branch
208
if (reposPath.endsWith("/Attic")) {
209             reposPath = reposPath.substring(0, reposPath.length() - 6);
210         }
211         // use quick finder and fallback to original algorithm just in case
212
File file = quickFindFile(dir, fileName, reposPath);
213         if (file != null) return file;
214         return findFile(dir, fileName, reposPath);
215     }
216     
217     private File findFile(File dir, String JavaDoc fileName, String JavaDoc reposPath) {
218         if (isWorkForRepository(dir, reposPath)) {
219             return new File(dir, fileName);
220         } else {
221             File file = null;
222             File[] subFiles = dir.listFiles();
223             if (subFiles != null) {
224                 for (int i = 0; i < subFiles.length; i++) {
225                     if (subFiles[i].isDirectory()) {
226                         file = findFile(subFiles[i], fileName, reposPath);
227                         if (file != null) break;
228                     }
229                 }
230             }
231             return file;
232         }
233     }
234     
235     private File quickFindFile(File dir, String JavaDoc fileName, String JavaDoc reposPath) {
236         for (;;) {
237             File deepDir = new File(dir, reposPath);
238             if (isWorkForRepository(deepDir, reposPath)) {
239                 return new File(deepDir, fileName);
240             }
241             dir = dir.getParentFile();
242             if (dir == null) return null;
243         }
244     }
245     
246     private boolean isWorkForRepository(File dir, String JavaDoc reposPath) {
247         try {
248             String JavaDoc repository = eventManager.getClientServices().getRepositoryForDirectory(dir);
249             String JavaDoc root = eventManager.getClientServices().getRepository();
250             if (repository.startsWith(root)) repository = repository.substring(root.length() + 1);
251             return reposPath.equals(repository);
252         } catch (IOException e) {
253             return false;
254         }
255     }
256 }
257
Popular Tags