001    /*
002    
003    $Id: ConfigManager.java,v 1.2 2003/05/08 10:34:22 culdesac Exp $
004    
005    */
006    
007    package sharpster.daemon.filemanagement;
008    
009    import sharpster.common.*;
010    import java.io.*;
011    import java.util.*;
012    
013    public class ConfigManager {
014        public static boolean writeFileConfig(SharedFile file, File iofile) {
015            try {
016                File dir = iofile.getParentFile();
017                File cfgfile = new File(dir,"sharpster.files");
018                LinkedList buffer = new LinkedList();
019                boolean added = false;
020                String line;
021                
022                if(cfgfile.exists()) {
023                    BufferedReader in = new BufferedReader(new FileReader(cfgfile));
024                    String repository = in.readLine();
025                    while((line=in.readLine())!=null) {
026                        String[] params = line.split("(/)");
027                        if(params[0].equals(iofile.getName())) {
028                            line = iofile.getName() + "/" + file.getVersion() + "/";
029                            if(file.getBody().isBinary()) line += "binary";
030                            else line += "text";
031                            added = true;
032                        }
033                        buffer.add(new String(line));
034                    }
035                    in.close();
036                }
037                
038                BufferedWriter out = new BufferedWriter(new FileWriter(cfgfile));
039                out.write(file.getPathInCVS());
040                out.newLine();
041                for(int i=0;i<buffer.size();i++) {
042                    out.write((String)buffer.get(i));
043                    out.newLine();
044                }
045                if(!added) {
046                    line = iofile.getName() + "/" + file.getVersion() + "/";
047                    if(file.getBody().isBinary()) line += "binary";
048                    else line += "text";
049                    out.write(line);
050                    out.newLine();
051                }
052    
053                out.close();
054            }
055            catch(Exception e) {
056                e.printStackTrace();
057                return false;
058            }
059            
060            return true;
061        }
062    
063        public static SharedFile readFileConfig(String filename, String workingDirectory) {
064            return null;
065        }
066    
067        public static SharedFile translatePath(String path) {
068            String repository, filename, temp, cvsroot;
069            SharedFile file = null;
070                    
071            File cvsFilePath = new File(path);
072            if(!cvsFilePath.exists()) return null;
073    
074            try {
075                File cvsFileDir = cvsFilePath.getParentFile();
076                    
077                filename = cvsFilePath.getName();
078            
079                FileReader fr = new FileReader(cvsFileDir.getAbsolutePath()+
080                                               File.separator+"sharpster.files");
081                BufferedReader inFile = new BufferedReader(fr);             
082                    
083                repository = inFile.readLine();
084                
085                while((temp=inFile.readLine())!=null) {
086                    String[] params = temp.split("(/)");
087                    if(params[0].equals(filename)) {
088                        file = new SharedFile();
089                        file.setFileName(filename);
090                        file.setPathInCVS(repository);
091                        file.setVersion(params[1]);
092                        break;
093                    }
094                }   
095            
096                inFile.close();
097            }
098            catch(Exception e) {
099                e.printStackTrace();
100                return null;
101            }
102    
103            return file;
104        }
105    }