1 33 34 package edu.rice.cs.drjava.project; 35 36 import java.util.ArrayList ; 37 import java.util.List ; 38 import java.util.Date ; 40 import java.text.SimpleDateFormat ; 41 import java.io.*; 42 43 import edu.rice.cs.plt.tuple.Pair; 44 import edu.rice.cs.plt.io.IOUtil; 45 import edu.rice.cs.drjava.config.FileOption; 46 import edu.rice.cs.drjava.Version; 47 import edu.rice.cs.util.FileOps; 48 import edu.rice.cs.util.UnexpectedException; 49 import edu.rice.cs.util.swing.Utilities; 50 import edu.rice.cs.drjava.model.DocumentRegion; 51 import edu.rice.cs.drjava.model.debug.DebugBreakpointData; 52 import edu.rice.cs.drjava.model.debug.DebugWatchData; 53 import edu.rice.cs.drjava.model.debug.DebugException; 54 55 import static edu.rice.cs.util.StringOps.*; 56 57 60 public class ProjectProfile implements ProjectFileIR { 61 62 63 64 private List <DocFile> _sourceFiles = new ArrayList <DocFile>(); 65 private List <DocFile> _auxFiles = new ArrayList <DocFile>(); 66 private List <String > _collapsedPaths = new ArrayList <String >(); 67 68 private File _buildDir = null; 69 private File _workDir = null; 70 71 private List <File> _classPathFiles = new ArrayList <File>(); 72 73 private File _mainClass = null; 74 75 76 private File _projectRoot; 77 78 private File _projectFile; 79 80 private File _createJarFile = null; 81 82 private int _createJarFlags = 0; 83 84 private List <DocumentRegion> _bookmarks = new ArrayList <DocumentRegion>(); 85 private List <DebugBreakpointData> _breakpoints = new ArrayList <DebugBreakpointData>(); 86 private List <DebugWatchData> _watches = new ArrayList <DebugWatchData>(); 87 88 89 public ProjectProfile(String fileName) throws IOException { this(new File(fileName)); } 90 91 95 public ProjectProfile(File f) throws IOException { 96 _projectFile = f; 97 _projectRoot = _projectFile.getParentFile(); 98 if (! _projectRoot.exists()) throw new IOException("Parent directory of project root " + _projectRoot + 99 " does not exist"); 100 } 101 102 103 104 105 public DocFile[] getSourceFiles() { return _sourceFiles.toArray(new DocFile[_sourceFiles.size()]); } 106 107 108 public DocFile[] getAuxiliaryFiles() { return _auxFiles.toArray(new DocFile[_auxFiles.size()]); } 109 110 111 public File getProjectFile() { return _projectFile; } 112 113 114 public File getBuildDirectory() { return _buildDir; } 115 116 117 public File getWorkingDirectory() { return _workDir; } 118 119 122 public String [] getCollapsedPaths() { return _collapsedPaths.toArray(new String [_collapsedPaths.size()]); } 123 124 125 public File[] getClassPaths() { return _classPathFiles.toArray(new File[_classPathFiles.size()]); } 126 127 128 public File getMainClass() { return _mainClass; } 129 130 131 public File getProjectRoot() { return _projectRoot; } 132 133 134 public File getCreateJarFile() { return _createJarFile; } 135 136 137 public int getCreateJarFlags() { return _createJarFlags; } 138 139 140 public DocumentRegion[] getBookmarks() { return _bookmarks.toArray(new DocumentRegion[_bookmarks.size()]); } 141 142 143 public DebugBreakpointData[] getBreakpoints() { return _breakpoints.toArray(new DebugBreakpointData[_breakpoints.size()]); } 144 145 146 public DebugWatchData[] getWatches() { return _watches.toArray(new DebugWatchData[_watches.size()]); } 147 148 149 150 public void addSourceFile(DocFile df) { _sourceFiles.add(df); } 151 152 public void addSourceFile(DocumentInfoGetter getter) { 153 if (!getter.isUntitled()) { 154 try { addSourceFile(docFileFromGetter(getter)); } 155 catch(IOException e) { throw new UnexpectedException(e); } 156 } 157 } 158 159 public void addAuxiliaryFile(DocFile df) { _auxFiles.add(df); } 160 161 public void addAuxiliaryFile(DocumentInfoGetter getter) { 162 if (! getter.isUntitled()) { 163 try { addAuxiliaryFile(docFileFromGetter(getter)); } 164 catch(IOException e) { throw new UnexpectedException(e); } 165 } 166 } 167 168 public void addClassPathFile(File cp) { if (cp != null) _classPathFiles.add(cp); } 169 public void addCollapsedPath(String cp) { if (cp != null) _collapsedPaths.add(cp); } 170 public void setBuildDirectory(File dir) { 171 _buildDir = dir; } 177 public void setWorkingDirectory(File dir) { _workDir = FileOps.validate(dir); } 178 public void setMainClass(File main) { _mainClass = main; } 179 public void setSourceFiles(List <DocFile> sf) { _sourceFiles = new ArrayList <DocFile>(sf); } 180 public void setClassPaths(List <? extends File> cpf) { _classPathFiles = new ArrayList <File>(cpf); } 181 public void setCollapsedPaths(List <String > cp) { _collapsedPaths = new ArrayList <String >(cp); } 182 public void setAuxiliaryFiles(List <DocFile> af) { _auxFiles = new ArrayList <DocFile>(af); } 183 184 185 public void setProjectRoot(File root) { 186 _projectRoot = root; 187 assert root.getParentFile() != null; 188 } 189 190 public void setCreateJarFile(File createJarFile) { _createJarFile = createJarFile; } 191 public void setCreateJarFlags(int createJarFlags) { _createJarFlags = createJarFlags; } 192 193 public void setBookmarks(List <? extends DocumentRegion> bms) { _bookmarks = new ArrayList <DocumentRegion>(bms); } 194 public void setBreakpoints(List <? extends DebugBreakpointData> bps) { _breakpoints = new ArrayList <DebugBreakpointData>(bps); } 195 public void setWatches(List <? extends DebugWatchData> ws) { _watches = new ArrayList <DebugWatchData>(ws); } 196 197 198 public void write() throws IOException { 199 FileWriter fw = new FileWriter(_projectFile); 200 201 fw.write(";; DrJava project file, written by build " + Version.getBuildTimeString()); 203 fw.write("\n;; files in the source tree are relative to: " + _projectRoot.getCanonicalPath()); 204 fw.write("\n;; other files with relative paths are rooted at (the parent of) this project file"); 205 206 209 if (_projectRoot != null) { 210 fw.write("\n(proj-root-and-base"); 211 fw.write("\n" + encodeFileRelative(_projectRoot, " ", _projectFile)); 213 fw.write(")"); 214 } 215 else fw.write("\n;; no project root; should never happen"); 216 217 221 if (!_sourceFiles.isEmpty()) { 222 fw.write("\n(source-files"); 223 DocFile active = null; 224 for(DocFile df: _sourceFiles) { 225 if(df.isActive()) { 226 active = df; 227 fw.write("\n" + encodeDocFileRelative(df, " ")); 228 break; } 230 } 231 for(DocFile df: _sourceFiles) { 232 if(df != active) 233 fw.write("\n" + encodeDocFileRelative(df, " ")); 234 } 235 fw.write(")"); } 237 else fw.write("\n;; no source files"); 238 239 if (!_auxFiles.isEmpty()) { 241 fw.write("\n(auxiliary"); 242 for(DocFile df: _auxFiles) { fw.write("\n" + encodeDocFileAbsolute(df, " ")); } 243 fw.write(")"); } 245 else fw.write("\n;; no aux files"); 246 247 if (!_collapsedPaths.isEmpty()) { 249 fw.write("\n(collapsed"); 250 for(String s: _collapsedPaths) { 251 fw.write("\n (path " + convertToLiteral(s) + ")"); 252 } 253 fw.write(")"); } 255 else fw.write("\n;; no collapsed branches"); 256 257 if (!_classPathFiles.isEmpty()) { 259 fw.write("\n(classpaths"); 260 for(File f: _classPathFiles) { 261 fw.write("\n" + encodeFileAbsolute(f, " ")); 262 } 263 fw.write(")"); } 265 else fw.write("\n;; no classpaths files"); 266 267 if (_buildDir != null && _buildDir.getPath() != "") { 269 fw.write("\n(build-dir"); 270 fw.write("\n" + encodeFileRelative(_buildDir, " ", _projectFile)); 271 fw.write(")"); 272 } 273 else fw.write("\n;; no build directory"); 274 275 if (_workDir != null && _workDir.getPath() != "") { 277 fw.write("\n(work-dir"); 278 fw.write("\n" + encodeFileRelative(_workDir, " ", _projectFile)); 279 fw.write(")"); 280 } 281 else fw.write("\n;; no working directory"); 282 283 if (_mainClass != null) { 285 fw.write("\n;; rooted at the (parent of the) project file"); 286 fw.write("\n(main-class"); 287 fw.write("\n" + encodeFileRelative(_mainClass, " ", _projectFile)); 288 fw.write(")"); 289 } 290 else fw.write("\n;; no main class"); 291 292 306 if (!_breakpoints.isEmpty()) { 308 fw.write("\n(breakpoints"); 309 for(DebugBreakpointData bp: _breakpoints) { fw.write("\n" + encodeBreakpointRelative(bp, " ")); } 310 fw.write(")"); } 312 else fw.write("\n;; no breakpoints"); 313 314 if (!_watches.isEmpty()) { 316 fw.write("\n(watches"); 317 for(DebugWatchData w: _watches) { fw.write("\n" + encodeWatch(w, " ")); } 318 fw.write(")"); } 320 else fw.write("\n;; no watches"); 321 322 if (!_bookmarks.isEmpty()) { 324 fw.write("\n(bookmarks"); 325 for(DocumentRegion bm: _bookmarks) { fw.write("\n" + encodeBookmarkRelative(bm, " ")); } 326 fw.write(")"); } 328 else fw.write("\n;; no bookmarks"); 329 330 fw.close(); 331 } 332 333 334 335 336 339 private DocFile docFileFromGetter(DocumentInfoGetter g) throws IOException { 340 return new DocFile(g.getFile().getCanonicalPath(), g.getSelection(), g.getScroll(), g.isActive(), g.getPackage()); 341 } 342 343 344 350 private String encodeFileRelative(File f, String prefix, File base) throws IOException { 351 String path = FileOps.makeRelativeTo(f, base).getPath(); 352 path = replace(path, File.separator, "/"); 353 return prefix + "(file (name " + convertToLiteral(path) + "))"; 354 } 355 356 357 private String encodeFileRelative(File f, String prefix) throws IOException { 358 return encodeFileRelative(f, prefix, _projectRoot); 359 } 360 361 366 private String encodeFileAbsolute(File f, String prefix) throws IOException { 367 String path = f.getCanonicalPath(); 368 path = replace(path,File.separator, "/"); 369 return prefix + "(file (name " + convertToLiteral(path) + "))"; 370 } 371 372 378 private String encodeDocFile(DocFile df, String prefix, boolean relative) throws IOException { 379 String ret = ""; 380 String path; 381 if (relative) path = FileOps.makeRelativeTo(df, _projectRoot).getPath(); 382 else path = IOUtil.attemptCanonicalFile(df).getPath(); 383 384 path = replace(path, File.separator, "/"); 385 ret += prefix + "(file (name " + convertToLiteral(path) + ")"; 386 387 Pair<Integer ,Integer > p1 = df.getSelection(); 388 Pair<Integer ,Integer > p2 = df.getScroll(); 389 long modDate = df.lastModified(); 391 if (p1 != null || p2 != null ) ret += "\n" + prefix + " "; 393 394 if (p1 != null) ret += "(select " + p1.first() + " " + p1.second() + ")"; 396 397 if (p2 != null) ret += "(scroll " + p2.first() + " " + p2.second() + ")"; 398 399 if (modDate > 0) { 400 String s = new SimpleDateFormat ("dd-MMM-yyyy HH:mm:ss").format(new Date (modDate)); 401 ret += "(mod-date " + convertToLiteral(s) + ")"; 402 } 403 404 406 String pack = df.getPackage(); 408 if (pack != null) { 409 ret += "\n" + prefix + " "; ret += "(package " + convertToLiteral(pack) + ")"; 411 } 412 413 ret += ")"; 415 return ret; 416 } 417 421 private String encodeDocFileRelative(DocFile df, String prefix) throws IOException { 422 return encodeDocFile(df, prefix, true); 423 } 424 private String encodeDocFileAbsolute(DocFile df, String prefix) throws IOException { 425 return encodeDocFile(df, prefix, false); 426 } 427 428 433 private String encodeBreakpointRelative(DebugBreakpointData bp, String prefix) throws IOException { 434 String ret = ""; 435 String path = FileOps.makeRelativeTo(bp.getFile(), _projectRoot).getPath(); 436 437 path = replace(path,File.separator,"/"); 438 ret += prefix + "(breakpoint (name " + convertToLiteral(path) + ")"; 439 440 int offset = bp.getOffset(); 441 int lineNumber = bp.getLineNumber(); 442 ret += "\n" + prefix + " "; 443 ret += "(offset " + offset + ")"; 444 ret += "(line " + lineNumber + ")"; 445 if (bp.isEnabled()) ret += "(enabled)"; 446 ret += ")"; 448 return ret; 449 } 450 451 456 private String encodeWatch(DebugWatchData w, String prefix) throws IOException { 457 String ret = ""; 458 459 ret += prefix + "(watch " + convertToLiteral(w.getName()) + ")"; 460 461 return ret; 462 } 463 464 469 private String encodeBookmarkRelative(DocumentRegion bp, String prefix) throws IOException { 470 String ret = ""; 471 String path = FileOps.makeRelativeTo(bp.getDocument().getFile(), _projectRoot).getPath(); 472 473 path = replace(path,File.separator,"/"); 474 ret += prefix + "(bookmark (name " + convertToLiteral(path) + ")"; 475 476 int startOffset = bp.getStartOffset(); 477 int endOffset = bp.getEndOffset(); 478 ret += "\n" + prefix + " "; 479 ret += "(start " + startOffset + ")"; 480 ret += "(end " + endOffset + ")"; 481 ret += ")"; 483 return ret; 484 } 485 } | Popular Tags |