11package gprover ;
22
33import java .io .*;
4+ import java .net .URISyntaxException ;
5+ import java .net .URL ;
6+ import java .net .URLDecoder ;
7+ import java .nio .file .DirectoryStream ;
8+ import java .nio .file .Files ;
9+ import java .nio .file .Path ;
10+ import java .nio .file .Paths ;
11+ import java .util .Enumeration ;
412import java .util .Vector ;
13+ import java .util .jar .JarEntry ;
14+ import java .util .jar .JarFile ;
515
616/**
717 * The main class for the GProver application.
@@ -30,11 +40,11 @@ public static void main1(String[] args) {
3040 try {
3141 String user_directory = System .getProperty ("user.dir" );
3242 String sp = File .separator ;
43+ // examples are displayed through this method
3344 String dr = user_directory + sp + "examples" ;
34- File file = new File (dr );
3545
3646 Vector vm = new Vector ();
37- readThems (file , vm );
47+ readThems (dr , vm );
3848 for (int id = 0 ; id < vm .size (); id ++) {
3949 GTerm gt = (GTerm ) vm .get (id );
4050 System .out .print (id + " : " + gt .getName () + "\t \t " );
@@ -96,7 +106,7 @@ public static void main1(String[] args) {
96106 Cm .print ("Total = " + vm .size () + "; t = " + t + ", f = " + f + ", n = " + n );
97107 long t2 = System .currentTimeMillis ();
98108 Cm .print ("Time = " + (t2 - t1 ) / 1000.0 );
99- } catch (IOException ee ) {
109+ } catch (IOException | URISyntaxException ee ) {
100110 System .err .println ("IOException: " + ee );
101111 }
102112 }
@@ -108,27 +118,60 @@ public static void main1(String[] args) {
108118 * @param v the vector to store the geometric terms
109119 * @throws IOException if an I/O error occurs
110120 */
111- static void readThems (File file , Vector v ) throws IOException {
112- File [] sf = file .listFiles (new FileFilter () {
113- public boolean accept (File pathname ) {
114- String nm = pathname .getName ();
115- return !(nm .contains ("." ));
121+ /**
122+ * Recursively read all “.gex”‐style term files from a resource directory on the classpath.
123+ */
124+ static void readThems (String resourceDir , Vector <GTerm > v ) throws IOException , URISyntaxException {
125+ ClassLoader cl = Thread .currentThread ().getContextClassLoader ();
126+ URL dirUrl = cl .getResource (resourceDir + "/" );
127+ if (dirUrl == null ) return ;
128+
129+ if (dirUrl .getProtocol ().equals ("file" )) {
130+ // running in IDE or exploded build
131+ Path folder = Paths .get (dirUrl .toURI ());
132+ try (DirectoryStream <Path > ds = Files .newDirectoryStream (folder )) {
133+ for (Path p : ds ) {
134+ String nm = p .getFileName ().toString ();
135+ if (Files .isDirectory (p )) {
136+ readThems (resourceDir + "/" + nm , v );
137+ } else {
138+ loadTerms (cl .getResourceAsStream (resourceDir + "/" + nm ), nm , v );
139+ }
140+ }
116141 }
117- });
118- for (int i = 0 ; i < sf .length ; i ++) {
119- if (sf [i ].isDirectory ())
120- readThems (sf [i ], v );
121- else {
122- BufferedReader in = new BufferedReader (new FileReader (sf [i ]));
123-
124- while (true ) {
125- GTerm tm = new GTerm ();
126- if (!tm .readAterm (in )) break ;
127- tm .setName (sf [i ].getName ());
128- v .add (tm );
142+ } else if (dirUrl .getProtocol ().equals ("jar" )) {
143+ // running from JAR
144+ String path = dirUrl .getPath ();
145+ String jarPath = path .substring (path .indexOf ("file:" ) + 5 , path .indexOf ("!" ));
146+ try (JarFile jar = new JarFile (URLDecoder .decode (jarPath , "UTF-8" ))) {
147+ String prefix = resourceDir + "/" ;
148+ Enumeration <JarEntry > en = jar .entries ();
149+ while (en .hasMoreElements ()) {
150+ JarEntry entry = en .nextElement ();
151+ String name = entry .getName ();
152+ if (!name .startsWith (prefix ) || entry .isDirectory ()) continue ;
153+ String rel = name .substring (prefix .length ());
154+ if (rel .contains ("/" )) {
155+ String sub = rel .substring (0 , rel .indexOf ('/' ));
156+ readThems (resourceDir + "/" + sub , v );
157+ } else {
158+ loadTerms (cl .getResourceAsStream (name ), rel , v );
159+ }
129160 }
130- in .close ();
131161 }
132162 }
133163 }
164+
165+ private static void loadTerms (InputStream is , String fileName , Vector <GTerm > v ) throws IOException {
166+ if (is == null ) return ;
167+ try (BufferedReader in = new BufferedReader (new InputStreamReader (is ))) {
168+ while (true ) {
169+ GTerm tm = new GTerm ();
170+ if (!tm .readAterm (in )) break ;
171+ tm .setName (fileName );
172+ v .add (tm );
173+ }
174+ }
175+ }
176+
134177}
0 commit comments