1    	package acl2s.lib.certify;
2    	
3    	import java.io.File;
4    	import java.io.IOException;
5    	import java.io.PrintWriter;
6    	
7    	import org.peterd.util.io.MiscIO;
8    	
9    	import acl2s.lib.parse.ParseException;
10   	import acl2s.lib.parse.obj.Sym;
11   	import acl2s.lib.session.BatchSession;
12   	
13   	public class Certifier {
14   		protected final File lispFile;
15   		protected final File certFile;
16   	
17   		protected final String fullBase;
18   		protected final String base;
19   		
20   		protected final CertificationSessionConfig config; 
21   		
22   		public Certifier(CertificationSessionConfig config) throws IllegalArgumentException {
23   			this.config = config;
24   			this.lispFile = config.getTarget();
25   			fullBase = MiscIO.removeFileExtension(lispFile.getAbsolutePath());
26   			base = MiscIO.removeFileExtension(lispFile.getName());
27   			certFile = new File(fullBase + ".cert");
28   		}
29   	
30   		public boolean certFileNewer() {
31   			return lispFile.exists() && certFileNewerThan(lispFile.lastModified());
32   		}
33   		
34   		public boolean certFileNewerThan(long time) {
35   			return certFile.exists() && certFile.lastModified() > time;
36   		}
37   		
38   		public boolean certFileAsNewAs(long time) {
39   			return certFile.exists() && certFile.lastModified() >= time;
40   		}
41   		
42   		public boolean certFileExists() {
43   			return certFile.exists();
44   		}
45   		
46   		public long getCertFileModificationTime() {
47   			return certFile.lastModified();
48   		}
49   		
50   		public void attemptRemoveCertFile() {
51   			certFile.delete();
52   		}
53   		
54   		
55   		// Various interfaces for building the preamble
56   		public void buildPreambleThrow() throws IOException, ParseException {
57   			buildPreambleThrow(false, null);
58   		}
59   		
60   		public void buildPreambleThrow(boolean force, PrintWriter commentary) throws IOException, ParseException {
61   			MaybeExtractPreamble.go(lispFile,force,commentary);
62   		}
63   		
64   		public String buildPreambleMessage() { return buildPreambleMessage(false); }
65   		
66   		public String buildPreambleMessage(boolean force) {
67   			try {
68   				buildPreambleThrow(force,null);
69   				return null;
70   			} catch (IOException ioe) {
71   				return "While extracting preamble: " + ioe.getMessage();
72   			} catch (ParseException pe) {
73   				return "Malformed preamble: " + pe.getMessage();
74   			}
75   		}
76   		
77   		// true on success
78   		public boolean buildPreambleCommentary(boolean force, PrintWriter commentary) {
79   			try {
80   				buildPreambleThrow(force,commentary);
81   				return true;
82   			} catch (IOException ioe) {
83   				commentary.print("While extracting preamble: ");
84   				commentary.println(ioe.getMessage());
85   				return false;
86   			} catch (ParseException pe) {
87   				commentary.print("Malformed preamble: ");
88   				commentary.println(pe.getMessage());
89   				return false;
90   			}
91   		}
92   		
93   		public File getLispFile() {
94   			return lispFile;
95   		}
96   		
97   		public String getBaseName() {
98   			return base;
99   		}
100  		
101  		public BatchSession createSession() {
102  			return new BatchSession(config, lispFile.getParentFile());
103  		}
104  		
105  		
106  		// ======= STATIC STUFF Re: Certification =======
107  		
108  		public static final Sym certifyBookSym = Sym.bootstrapCreate("CERTIFY-BOOK");
109  		public static final Sym questionSym = Sym.bootstrapCreate("?");
110  		
111  		/**
112  		 * Returns true iff the file extension is allowed for certification.
113  		 */
114  		public static boolean legalLispFile(File f) {
Event do_not_call: "java.lang.String.toLowerCase()" implicitly uses the environment's default character set, which might lead to unexpected behavior. Consider using toLowerCase(Locale locale).
115  			return f.getName().toLowerCase().endsWith(".lisp");
116  		}
117  	}