1 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 33 public class CommitBuilder 34 implements Builder { 35 36 39 public static final String UNKNOWN = "commit: nothing known about `"; public static final String EXAM_DIR = ": Examining"; public static final String REMOVING = "Removing "; public static final String NEW_REVISION = "new revision:"; public static final String INITIAL_REVISION = "initial revision:"; public static final String DELETED_REVISION = "delete"; public static final String DONE = "done"; public static final String RCS_FILE = "RCS file: "; public static final String ADD = "commit: use `cvs add' to create an entry for "; public static final String COMMITTED = " <-- "; 50 53 private CommitInformation commitInformation; 54 55 59 private File fileDirectory; 60 61 64 private final EventManager eventManager; 65 66 private final String localPath; 67 68 private final String repositoryRoot; 69 70 private boolean isAdding; 71 72 public CommitBuilder(EventManager eventManager, String localPath, String 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 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 fileName = line.substring(c + COMMITTED.length()).trim(); 97 int nameIndex = fileName.lastIndexOf('/'); 98 if (nameIndex != -1) { fileName = fileName.substring(nameIndex+1); 100 } 101 File file; 102 if (fileDirectory == null) { 103 String 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); 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 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 fileName) { 148 return new File(localPath, fileName); 149 } 150 151 private void processUnknownFile(String line) { 152 commitInformation = new CommitInformation(); 153 commitInformation.setType(CommitInformation.UNKNOWN); 154 int index = line.indexOf('\''); 155 String fileName = line.substring(0, index).trim(); 156 commitInformation.setFile(createFile(fileName)); 157 outputDone(); 158 } 159 160 private void processToAddFile(String line) { 161 commitInformation = new CommitInformation(); 162 commitInformation.setType(CommitInformation.TO_ADD); 163 String fileName = line.trim(); 164 if (fileName.endsWith(";")) { fileName = fileName.substring(0, fileName.length() - 2); 166 } 167 commitInformation.setFile(createFile(fileName)); 168 outputDone(); 169 } 170 171 private void processFile(String filename) { 172 if (commitInformation == null) { 173 commitInformation = new CommitInformation(); 174 } 175 176 if (filename.startsWith("no file")) { 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 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 key, Object value) { 203 } 204 205 private File findFile(String fileName, String reposPath) { 206 File dir = new File(localPath); 207 if (reposPath.endsWith("/Attic")) { 209 reposPath = reposPath.substring(0, reposPath.length() - 6); 210 } 211 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 fileName, String 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 fileName, String 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 reposPath) { 247 try { 248 String repository = eventManager.getClientServices().getRepositoryForDirectory(dir); 249 String 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 |