Skip to content

Commit

Permalink
parse source code from resource folder
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jul 22, 2023
1 parent 1992f02 commit a8ee7e0
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 17 deletions.
7 changes: 7 additions & 0 deletions jParser/base/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ plugins {

val moduleName = "jParser-base"

tasks {
withType<Jar> {
from(sourceSets["main"].allSource)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
}

dependencies {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.xpenatan.jparser.base.helper;

public class BoolArray {
public void copy(double [] array) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.xpenatan.jparser.base.helper;

public class CharArray {
public void copy(double [] array) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.xpenatan.jparser.base.helper;

import com.github.xpenatan.jparser.base.IDLBase;

public class DoubleArray extends IDLBase {
public void copy(double [] array) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.xpenatan.jparser.base.helper;

import com.github.xpenatan.jparser.base.IDLBase;

public class FloatArray extends IDLBase {

public void copy(float [] array) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.xpenatan.jparser.base.helper;

import com.github.xpenatan.jparser.base.IDLBase;

public class IntArray extends IDLBase {

public void copy(int [] array) {

}
}
5 changes: 0 additions & 5 deletions jParser/idl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ plugins {

val moduleName = "jParser-idl"

sourceSets["main"].resources.setSrcDirs(arrayListOf(
"../base/src/main/java/",
"../base/src/main/resources/"
))

dependencies {
implementation(project(":jParser:base"))
implementation(project(":jParser:core"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.github.xpenatan.jparser.idl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

// Class from https://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory
public class ResourceList {

public static Collection<String> getResources(
final Pattern pattern){
final ArrayList<String> retval = new ArrayList<>();
final String classPath = System.getProperty("java.class.path", ".");
final String[] classPathElements = classPath.split(System.getProperty("path.separator"));
for(final String element : classPathElements){
retval.addAll(getResources(element, pattern));
}
return retval;
}

private static Collection<String> getResources(
final String element,
final Pattern pattern){
final ArrayList<String> retval = new ArrayList<String>();
final File file = new File(element);
if(file.isDirectory()){
retval.addAll(getResourcesFromDirectory(file, pattern));
} else{
retval.addAll(getResourcesFromJarFile(file, pattern));
}
return retval;
}

private static Collection<String> getResourcesFromJarFile(
final File file,
final Pattern pattern){
final ArrayList<String> retval = new ArrayList<String>();
ZipFile zf;
try{
zf = new ZipFile(file);
} catch(final ZipException e){
throw new Error(e);
} catch(final IOException e){
throw new Error(e);
}
final Enumeration e = zf.entries();
while(e.hasMoreElements()){
final ZipEntry ze = (ZipEntry) e.nextElement();
final String fileName = ze.getName();
final boolean accept = pattern.matcher(fileName).matches();
if(accept){
retval.add(fileName);
}
}
try{
zf.close();
} catch(final IOException e1){
throw new Error(e1);
}
return retval;
}

private static Collection<String> getResourcesFromDirectory(
final File directory,
final Pattern pattern){
final ArrayList<String> retval = new ArrayList<String>();
final File[] fileList = directory.listFiles();
for(final File file : fileList){
if(file.isDirectory()){
retval.addAll(getResourcesFromDirectory(file, pattern));
} else{
try{
final String fileName = file.getCanonicalPath();
final boolean accept = pattern.matcher(fileName).matches();
if(accept){
retval.add(fileName);
}
} catch(final IOException e){
throw new Error(e);
}
}
}
return retval;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import com.github.xpenatan.jparser.idl.IDLClass;
import com.github.xpenatan.jparser.idl.IDLFile;
import com.github.xpenatan.jparser.idl.IDLReader;
import com.github.xpenatan.jparser.idl.ResourceList;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.regex.Pattern;

/**
* @author xpenatan
Expand Down Expand Up @@ -55,22 +58,24 @@ public void onParseStart(JParser jParser) {
String packagePath = File.separator + basePackage.replace(".", File.separator);
String genPath = new File(jParser.genDir + packagePath).getAbsolutePath();

createBaseUnitFromResources(jParser, genPath);
createBaseUnitFromResources(jParser);
generateIDLJavaClasses(jParser, genPath);
}

private void createBaseUnitFromResources(JParser jParser, String genPath) {
try {
BASE_CLASS_NAME = IDLBase.class.getSimpleName();
String basePath = IDLBase.class.getName().replaceAll("\\.", "/") + ".java";
baseClassUnit = StaticJavaParser.parseResource(basePath);
} catch(IOException e) {
throw new RuntimeException(e);
private void createBaseUnitFromResources(JParser jParser) {
BASE_CLASS_NAME = IDLBase.class.getSimpleName();
Collection<String> resources = ResourceList.getResources(Pattern.compile("/*.*/*.java"));
for(String resource : resources) {
try {
CompilationUnit compilationUnit = StaticJavaParser.parseResource(resource);
String genBaseClassPath = jParser.genDir + File.separator + resource;
jParser.unitArray.add(new JParserItem(compilationUnit, genBaseClassPath, genBaseClassPath));
} catch(IOException e) {
throw new RuntimeException(e);
}
}

String genBaseClassPath = genPath + File.separator + BASE_CLASS_NAME + ".java";
baseClassUnit.setPackageDeclaration(basePackage);
jParser.unitArray.add(new JParserItem(baseClassUnit, genBaseClassPath, genBaseClassPath));
JParserItem parserUnitItem = jParser.getParserUnitItem(BASE_CLASS_NAME);
baseClassUnit = parserUnitItem.unit;
}

private void generateIDLJavaClasses(JParser jParser, String genPath) {
Expand Down

0 comments on commit a8ee7e0

Please sign in to comment.