00001 /* 00002 * NameFile.java 00003 * 00004 * Created on 29 October 2003, 15:16 00005 */ 00006 00007 package Sct.File; 00008 00009 import Sct.*; 00010 import java.util.*; 00011 import java.util.regex.*; 00012 00017 public class NameFile extends Sct.Name { 00018 00019 public NameFile(Serializable s) { 00020 this(s.getClassName(), s.getUniqueID()); 00021 } 00022 00023 public NameFile(String name) throws IllegalArgumentException { 00024 super(name); 00025 fileName = name; 00026 } 00027 00028 public NameFile(String className, String uniqueID) { 00029 super(className, uniqueID); 00030 path = getDefaultPath(); 00031 construct(); 00032 } 00033 00034 public String getName() { 00035 return fileName; 00036 } 00037 00038 public String getPath() { 00039 return path; 00040 } 00041 00045 public static void reset() { 00046 fileNamePattern = null; 00047 defaultPath = null; 00048 } 00049 00053 protected void parse(String name) throws IllegalArgumentException { 00054 try { 00055 Matcher m = getPattern().matcher(name); 00056 m.matches(); 00057 path = m.group(1); 00058 className = convertFromC(m.group(2)); 00059 uniqueID = m.group(3); 00060 } catch (IllegalStateException e) { 00061 IllegalArgumentException iae = new IllegalArgumentException("NameFile cannot parse File name - doesn't match expected format: " + name); 00062 iae.initCause(e); 00063 throw iae; 00064 } catch (IndexOutOfBoundsException e) { 00065 IllegalStateException iae = new IllegalStateException("NameFile internal error - regex does have expected number of groups. Name: " + name); 00066 iae.initCause(e); 00067 throw iae; 00068 } 00069 } 00070 00071 // This is used by the Unit tests 00072 public static String getDefaultPath() { 00073 if (defaultPath == null) { 00074 defaultPath = System.getProperty("Sct.Scratch.Dir", "/tmp"); 00075 } 00076 return defaultPath; 00077 } 00078 00079 private void construct() { 00080 fileName = getPath() + "/" + convertToC(className) + "." + uniqueID; 00081 } 00082 00083 private static Pattern getPattern() { 00084 if (fileNamePattern == null) 00085 fileNamePattern = Pattern.compile("(.*)/([\\S&&[^.]]*)\\.(.*)"); 00086 return fileNamePattern; 00087 } 00088 00089 private static Pattern fileNamePattern; 00090 private static String defaultPath; 00091 private String path; 00092 private String fileName; 00093 }