KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > add > AddBuilder


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.add;
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 add information object and the firing of
29  * events when complete objects are built.
30  *
31  * @author Milos Kleint
32  */

33 public class AddBuilder implements Builder {
34     private static final String JavaDoc UNKNOWN = ": nothing known about"; //NOI18N
35
private static final String JavaDoc ADDED = " added to the repository"; //NOI18N
36
private static final String JavaDoc WARNING = ": warning: "; //NOI18N
37
private static final String JavaDoc ALREADY_ENTERED = " has already been entered"; //NOI18N
38
private static final String JavaDoc SCHEDULING = ": scheduling file `"; //NOI18N
39
private static final String JavaDoc USE_COMMIT = ": use 'cvs commit' "; //NOI18N
40
private static final String JavaDoc DIRECTORY = "Directory "; //NOI18N
41
private static final String JavaDoc READDING = ": re-adding file "; //NOI18N
42
private static final String JavaDoc RESURRECTED = ", resurrected"; //NOI18N
43
private static final String JavaDoc RESUR_VERSION = ", version "; //NOI18N
44

45     private static final boolean DEBUG = false;
46
47     /**
48      * The status object that is currently being built.
49      */

50     private AddInformation addInformation;
51
52     /**
53      * The event manager to use.
54      */

55     private EventManager eventManager;
56
57     /**
58      * The directory in which the file being processed lives.
59      * This is relative to the local directory
60      */

61     private String JavaDoc fileDirectory;
62
63     private AddCommand addCommand;
64
65     private boolean readingTags;
66
67     public AddBuilder(EventManager eventManager, AddCommand addCommand) {
68         this.eventManager = eventManager;
69         this.addCommand = addCommand;
70     }
71
72     public void outputDone() {
73         if (addInformation != null) {
74             FileInfoEvent event = new FileInfoEvent(this, addInformation);
75             eventManager.fireCVSEvent(event);
76             addInformation = null;
77         }
78     }
79
80     public void parseLine(String JavaDoc line, boolean isErrorMessage) {
81         if (line.endsWith(ADDED)) {
82             String JavaDoc directory =
83                     line.substring(DIRECTORY.length(), line.indexOf(ADDED));
84             addDirectory(directory);
85         }
86         else if (line.indexOf(SCHEDULING) >= 0) {
87             String JavaDoc filename =
88                     line.substring(line.indexOf(SCHEDULING) + SCHEDULING.length(), line.indexOf('\'')).trim();
89             addFile(filename);
90         }
91         else if (line.indexOf(READDING) >= 0) {
92             String JavaDoc filename =
93                     line.substring(line.indexOf(READDING) + READDING.length(), line.indexOf('(')).trim();
94             addFile(filename);
95         }
96         else if (line.endsWith(RESURRECTED)) {
97             String JavaDoc filename =
98                     line.substring(0, line.length() - RESURRECTED.length());
99             resurrectFile(filename);
100         }
101         // ignore the rest..
102
}
103
104     private File createFile(String JavaDoc fileName) {
105         File locFile = addCommand.getFileEndingWith(fileName);
106         if (locFile == null) {
107             // in case the exact match was not achieved using the getFileEndingWith method
108
// let's try to find the best match possible.
109
// iterate from the back of the filename string and try to match the endings
110
// of getFiles(). the best match is picked then.
111
// Works ok for files and directories in add, should not probably be used
112
// elsewhere where it's possible to have recursive commands and where resulting files
113
// are not listed in getFiles()
114
String JavaDoc name = fileName.replace('\\', '/');
115             File[] files = addCommand.getFiles();
116             int maxLevel = name.length();
117             File bestMatch = null;
118             String JavaDoc[] paths = new String JavaDoc[files.length];
119             for (int index = 0; index < files.length; index++) {
120                 paths[index] = files[index].getAbsolutePath().replace('\\', '/');
121             }
122             int start = name.lastIndexOf('/');
123             String JavaDoc part = null;
124             if (start < 0) {
125                 part = name;
126             } else {
127                 part = name.substring(start + 1);
128             }
129             while (start >= 0 || part != null) {
130                 boolean wasMatch = false;
131                 for (int index = 0; index < paths.length; index++) {
132                     if (paths[index].endsWith(part)) {
133                         bestMatch = files[index];
134                         wasMatch = true;
135                     }
136                 }
137                 start = name.lastIndexOf('/', start - 1);
138                 if (start < 0 || !wasMatch) {
139                     break;
140                 }
141                 part = name.substring(start + 1);
142             }
143             return bestMatch;
144         }
145         return locFile;
146     }
147
148     private void addDirectory(String JavaDoc name) {
149         addInformation = new AddInformation();
150         addInformation.setType(AddInformation.FILE_ADDED);
151         String JavaDoc dirName = name.replace('\\', '/');
152 /* int index = dirName.lastIndexOf('/');
153         if (index > 0) {
154             dirName = dirName.substring(index + 1, dirName.length());
155         }
156  */

157         addInformation.setFile(createFile(dirName));
158         outputDone();
159     }
160
161     private void addFile(String JavaDoc name) {
162         addInformation = new AddInformation();
163         addInformation.setFile(createFile(name));
164         addInformation.setType(AddInformation.FILE_ADDED);
165         outputDone();
166     }
167
168     private void resurrectFile(String JavaDoc line) {
169         int versionIndex = line.lastIndexOf(RESUR_VERSION);
170         String JavaDoc version = line.substring(versionIndex + RESUR_VERSION.length()).trim();
171         String JavaDoc cutLine = line.substring(0, versionIndex).trim();
172         int fileIndex = cutLine.lastIndexOf(' ');
173         String JavaDoc name = cutLine.substring(fileIndex).trim();
174
175         if (DEBUG) {
176             System.out.println("line1=" + line); //NOI18N
177
System.out.println("versionIndex=" + versionIndex); //NOI18N
178
System.out.println("version=" + version); //NOI18N
179
System.out.println("fileindex=" + fileIndex); //NOI18N
180
System.out.println("filename=" + name); //NOI18N
181
}
182
183         addInformation = new AddInformation();
184         addInformation.setType(AddInformation.FILE_RESURRECTED);
185         addInformation.setFile(createFile(name));
186         outputDone();
187     }
188
189     public void parseEnhancedMessage(String JavaDoc key, Object JavaDoc value) {
190     }
191 }
192
Popular Tags