Skip to content

Commit b045499

Browse files
committed
much closer. Creates valid files for Jmol
1 parent 6100948 commit b045499

File tree

17 files changed

+537
-495
lines changed

17 files changed

+537
-495
lines changed
-736 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20231109163511
1+
20231110160943
-736 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20231109163511
1+
20231110160943

sources/net.sf.j2s.core/doc/howItWorks.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
***Java2Script: How It Works***
22

3+
**java2script/Jmol**
4+
5+
Jmol was adapted for JavaScript between 2013 and 2015, before SwingJS was conceptualized.
6+
As such, it use the "legacy" version of the transpiler created and formerly maintained
7+
solely by Zhou Renjien. This code was the basis for SwingJS, and by careful adapting Jmol
8+
to fit it, we could make it work with this somewhat quirky code. (For example, there is no
9+
support for boxing and unboxing numbers -- int i = Integer.valueOf(3) is not properly turned
10+
into just the number 3. I got around this in those early days by just going through all
11+
(and I mean ALL) of the Jmol code, making sure there were no implicit boxing or unboxing.
12+
13+
314
**java2script/SwingJS**
415

516
The full java2script/SwingJS operation involves two parts: Creating the JavaScript from the abstract syntax tree (java2script), and running that within the browser (SwingJS). Both are discussed below.
@@ -9,11 +20,10 @@ The code for these two parts are well-separated:
920
net.sf.j2s.core java2script transpiler
1021
net.sf.j2s.java.core SwingJS runtime
1122

12-
[Note: You may notice that the java2script project includes several other net.sf.... projects. Frankly, I have no idea what they are for. My guess is that they don't work. I think perhaps they were early attempts to get all this working within Eclipse at runtime, with "hot" connections to code. But that never ever worked for me, and what we have now -- direct creation of a site directory that can be debugged in a standard external browser is way better, anyway. I have left them there just because I haven't taken the time to get rid of them.]
1323

1424
**java2script transpiler**
1525

16-
[Note: Changes in java2script should be necessary only when core Java syntax changes or is supplemented in the core Java. For example, Java 8 allows switch cases that are String constants, while Java 6 does not. When we went to Java 8, we had to modify Java2ScriptVisitor.java to account for that. If ever there is a need to fix something that the java2script compiler is doing wrong or to adapt to new Java syntax, as for Java 11, look in net.sf.j2s.core.Java2ScriptVisitor.]
26+
[Note: Changes in java2script should be necessary only when core Java syntax changes or is supplemented in the core Java. For example, Java 8 allows switch cases that are String constants, while Java 6 does not. When we went to Java 8, we had to modify Java2ScriptVisitor.java to account for that. If ever there is a need to fix something that the java2script compiler is doing wrong or to adapt to new Java syntax, as for Java 11, look in j2s.swingjs.Java2ScriptVisitor.]
1727

1828
A compiler converts code in one computer language to another. Typically this is from a higher-level language to a lower-level "machine code" language. In the case of the Java compiler, this is from written Java code (*.java) to "Java byte code" (*.class). In the case of of java2script, this is from Java to JavaScript. There are two basic requirements of a compiler: reading and writing. The reading process involves converting the written Java code to an <i>abstract syntax tree</i> [https://en.wikipedia.org/wiki/Abstract_syntax_tree]. The writing process involves scanning that tree, creating one or more output files from the original input file.
1929

sources/net.sf.j2s.core/schema/extendedASTScriptVisitor.exsd

Lines changed: 0 additions & 112 deletions
This file was deleted.

sources/net.sf.j2s.core/schema/extendedCompiler.exsd

Lines changed: 0 additions & 112 deletions
This file was deleted.

sources/net.sf.j2s.core/src/j2s/core/Java2ScriptCompiler.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public abstract class Java2ScriptCompiler {
101101
protected String j2sPath;
102102
protected String excludedPaths;
103103

104+
protected String[] packageFixes;
105+
104106
protected List<String> lstExcludedPaths;
105107

106108
protected boolean isCleanBuild;
@@ -148,10 +150,18 @@ public static String getJ2SConfigName(IJavaProject project) {
148150
}
149151
}
150152

153+
public static Java2ScriptCompiler newCompiler(IJavaProject project) {
154+
String j2stype = getJ2SConfigName(project);
155+
return ( J2S_CONFIG_JMOL.equals(j2stype) ?
156+
new Java2ScriptLegacyCompiler()
157+
: J2S_CONFIG_SWINGJS.equals(j2stype) ?
158+
new Java2ScriptSwingJSCompiler() : null);
159+
}
160+
151161
protected Java2ScriptCompiler(boolean isSwingJS, String j2sConfigFileName) {
152162
this.isSwingJS = isSwingJS;
153163
this.j2sConfigFileName = j2sConfigFileName;
154-
System.out.println("Java2ScriptCompiler " + this + " " + isSwingJS + " " + j2sConfigFileName);
164+
System.out.println("Java2ScriptCompiler " + this + " isSwingJS=" + isSwingJS + " " + j2sConfigFileName);
155165
// initialized only once for SwingJS and once for legacy version
156166
}
157167

@@ -326,7 +336,7 @@ protected void createJSFile(String j2sPath, String packageName, String elementNa
326336
writeToFile(f, js);
327337
}
328338

329-
protected String getFileContents(File file) {
339+
protected static String getFileContents(File file) {
330340
try {
331341
StringBuilder sb = new StringBuilder();
332342
FileInputStream is = new FileInputStream(file);
@@ -378,8 +388,15 @@ private int copyNonclassFiles(File dir, File target, int n) {
378388
File f = null;
379389
if (files != null)
380390
try {
381-
if (!target.exists())
382-
Files.createDirectories(target.toPath());
391+
File p;
392+
if (packageFixes != null) {
393+
p = new File(fixPackageName(target.toString().replace('\\','/')));
394+
} else {
395+
p = target;
396+
}
397+
398+
if (!p.exists())
399+
Files.createDirectories(p.toPath());
383400
for (int i = 0; i < files.length; i++) {
384401
f = files[i];
385402
if (f == null) {
@@ -392,10 +409,11 @@ private int copyNonclassFiles(File dir, File target, int n) {
392409
//
393410
copiedResourcePackages.add(path);
394411
n++;
395-
Files.copy(f.toPath(), new File(target, f.getName()).toPath(),
412+
File fnew = new File(p, f.getName());
413+
Files.copy(f.toPath(), fnew.toPath(),
396414
StandardCopyOption.REPLACE_EXISTING);
397415
if (isDebugging)
398-
System.out.println("J2S copied to site: " + path);
416+
System.out.println("J2S copied to site: " + path + " as " + fnew.toPath());
399417
}
400418
}
401419
}
@@ -406,7 +424,7 @@ private int copyNonclassFiles(File dir, File target, int n) {
406424
return n;
407425
}
408426

409-
protected int checkCopiedResources(String packageName, String sourceLocation) {
427+
protected int checkCopiedResources(String packageName, String sourceLocation, String outputDir) {
410428
int pt = packageName.indexOf(".");
411429
if (pt >= 0)
412430
packageName = packageName.substring(0, pt);
@@ -423,16 +441,19 @@ protected int checkCopiedResources(String packageName, String sourceLocation) {
423441
}
424442
String sourceDir = sourceLocation.substring(0, pt);
425443
File src = new File(sourceDir, packageName);
426-
File dest = new File(j2sPath, packageName);
444+
File dest = new File(outputDir, packageName);
427445
return copySiteResources(src, dest);
428446
}
429447

430-
public static Java2ScriptCompiler newCompiler(IJavaProject project) {
431-
String j2stype = getJ2SConfigName(project);
432-
return ( J2S_CONFIG_JMOL.equals(j2stype) ?
433-
new Java2ScriptLegacyCompiler()
434-
: J2S_CONFIG_SWINGJS.equals(j2stype) ?
435-
new Java2ScriptSwingJSCompiler() : null);
448+
protected String fixPackageName(String name) {
449+
if (packageFixes == null)
450+
return name;
451+
for (int i = 0; i < packageFixes.length; i++) {
452+
name = name.replaceAll(packageFixes[i++], packageFixes[i]);
453+
}
454+
return name;
436455
}
437-
456+
457+
458+
438459
}

0 commit comments

Comments
 (0)