Skip to content
Permalink
Browse files

Added support for resource directories where a resource path can be a…

…dded with a path within the JAR where the interpriter will look for script files.
  • Loading branch information...
rsv-code committed Mar 6, 2019
1 parent 0e7bc7a commit 0ba3752505b4d400105bb7658d72cddf24fa8cf5
@@ -14,7 +14,7 @@ The easiest way to embed Cali interpreter is to add it as a Maven dependency. Fo
<dependency>
<groupId>com.cali-lang</groupId>
<artifactId>cali.lang.base</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
```

@@ -92,7 +92,7 @@ Requires Maven and a recent JDK. (1.6 or newer)
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building cali-lang.jar 1.0.1
[INFO] Building cali-lang.jar 1.0.2
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ cali.lang.base ---
@@ -101,7 +101,7 @@ Requires Maven and a recent JDK. (1.6 or newer)
[INFO] --- maven-assembly-plugin:2.4.1:single (make-assembly) @ cali.lang.base ---
[INFO] Building jar: /home/austin/git/cali.lang.base/cali.lang.base/target/cali.lang.base-1.0.1-jar-with-dependencies.jar
[INFO] Building jar: /home/austin/git/cali.lang.base/cali.lang.base/target/cali.lang.base-1.0.2-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
@@ -112,11 +112,11 @@ Requires Maven and a recent JDK. (1.6 or newer)
```

That's it. In the target directory, you should now have an executable JAR file
named cali.lang.base-1.0.1-jar-with-dependencies.jar.
named cali.lang.base-1.0.2-jar-with-dependencies.jar.

You can run the tests like this:
```
> java -jar target/cali.lang.base-1.0.1-jar-with-dependencies.jar tests/interpreter.ca
> java -jar target/cali.lang.base-1.0.2-jar-with-dependencies.jar tests/interpreter.ca
Running Test [ Cali-Lang Interpreter ]:
*** (local assignment) Assign variable null. ... PASSED
*** (local assignment) Assign variable bool. ... PASSED
@@ -3,7 +3,7 @@

<groupId>com.cali-lang</groupId>
<artifactId>cali.lang.base</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<packaging>jar</packaging>

<name>cali.lang.base</name>
@@ -87,6 +87,12 @@
*/
private boolean hasParseErrors = false;

/**
* Allowed resource include paths. These are includes that are
* located within the JAR package.
*/
private List<String> resourceIncludePaths = new ArrayList<String>();

/**
* Allowed include paths.
*/
@@ -157,6 +163,19 @@ public void addInclude(String Include) throws Exception {
this.parseString(Include, Lang.get().langIncludes.get(Include));
}
} else {
for (String pth : this.resourceIncludePaths) {
List<String> resDir = Lang.get().listResourceDirectory(pth);
String tinc = pth + Include;

for (String fname : resDir) {
if (fname.contains(tinc)) {
this.includes.add(tinc);
this.parseString(Include, Util.loadResource(tinc));
return;
}
}
}

for (String pth : this.includePaths) {
String tinc = pth + Include;
File f = new File(tinc);
@@ -191,6 +210,27 @@ public void addIncludePath(String Path) {
return this.includePaths;
}

/**
* Adds an include path for a resource directory with a JAR file
* to the list of resource include paths.
* @param Path is a String with the search resource path to add.
*/
public void addResourceIncludePath(String Path) {
String tinc = Path;
if (!tinc.endsWith("/")) {
tinc += "/";
}
this.resourceIncludePaths.add(tinc);
}

/**
* Gets a list of the resource search include paths.
* @return A List of Strings with the resource include paths.
*/
public List<String> getResourceIncludePath() {
return this.resourceIncludePaths;
}

/**
* Gets a list of current includes.
* @return A List of Strings with the current includes.
@@ -38,6 +38,9 @@ public static void main(String[] args) throws Exception {
// Sets debug output to true.
//eng.setDebug(true);

// Add resource include path for testing.
eng.addResourceIncludePath("/com/cali/stdlib/ca/");

// Parse the provided file name.
eng.parseFile(args[0]);

@@ -42,7 +42,7 @@
/**
* Defines the Cali version.
*/
private static final String version = "1.0.1";
private static final String version = "1.0.2";

/**
* Map of class definitions. This is used to hold the base lang clases. It allows
@@ -17,6 +17,7 @@
package com.cali;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
@@ -29,6 +30,7 @@
* @author austin
*/
public class Util {

/**
* Reads a file with the provided file name and returns it as
* a String.
@@ -16,9 +16,20 @@

package com.cali.stdlib;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import com.cali.Engine;
import com.cali.Main;
import com.cali.Util;

public class Lang {
@@ -27,6 +38,11 @@
*/
private static Lang instance = null;

/**
* Instance of this JAR file.
*/
private File jarFile = null;

/**
* Default constructor set to private to defeat instantiation. See get to get an
* instance of the object.
@@ -51,12 +67,38 @@ public static Lang get() {
* com.cali.stdlib.ca.
*/
private void init() {
this.jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());

langIncludes.put("lang.ca", Util.loadResource("/com/cali/stdlib/ca/lang.ca"));
langIncludes.put("sys.ca", Util.loadResource("/com/cali/stdlib/ca/sys.ca"));
langIncludes.put("reflect.ca", Util.loadResource("/com/cali/stdlib/ca/reflect.ca"));
langIncludes.put("cunit.ca", Util.loadResource("/com/cali/stdlib/ca/cunit.ca"));
langIncludes.put("math.ca", Util.loadResource("/com/cali/stdlib/ca/math.ca"));
}


public List<String> listResourceDirectory(String Path) throws IOException, URISyntaxException {
List<String> ret = new ArrayList<String>();

if(this.jarFile.isFile()) {
JarFile jar = new JarFile(this.jarFile);
Enumeration<JarEntry> entries = jar.entries();
while(entries.hasMoreElements()) {
final String name = entries.nextElement().getName();
if (name.startsWith(Path)) {
ret.add(name);
}
}
jar.close();
} else {
URL url = Engine.class.getResource(Path);
if (url != null) {
File entries = new File(url.toURI());
for (File entry : entries.listFiles()) {
ret.add(entry.getPath());
}
}
}

return ret;
}
}
@@ -0,0 +1,6 @@
/**
* This class only exists to test the resource loading system.
*/
class inctest {
public value = "worked";
}
@@ -17,6 +17,7 @@
include cunit;
include math;
include sys;
include inctest;

enum numName {
one;
@@ -522,6 +523,11 @@ class interpreterTests : test {
.add("(lang secman) Security manager set property.", ::secmanSetProp)
.add("(lang secman) Security manager set map.", ::secmanSetMap)
;

// resource loading tests
this
.add("(lang resource include) Include inctest object.", ::resNewInctest)
;
}

/**********************************************************************
@@ -1066,4 +1072,9 @@ class interpreterTests : test {
sman.setMap(mp);
return this.expect(sman.getProp('test.3') == 'some text here ...', true);
}

/**********************************************************************
* resource loading tests
*********************************************************************/
public resNewInctest() { tobj = new inctest(); return this.expect(tobj instanceof 'inctest', true); }
}

0 comments on commit 0ba3752

Please sign in to comment.
You can’t perform that action at this time.