X Tutup
Skip to content

Commit 125480d

Browse files
author
sebastigurin
committed
some corrections and new example to extending j2s compiler section
1 parent ddcad71 commit 125480d

File tree

3 files changed

+149
-25
lines changed

3 files changed

+149
-25
lines changed

incubator/net.sf.j2s.doc.user-guide/j2s-user-guide/chap-j2s-sources.xml

Lines changed: 117 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,13 @@ each method invocation, and then the normal method invocation javascript code. <
476476

477477
<formalpara><title>ASTScriptVisitor</title> <para>
478478
In one side we have an ASTScriptVisitor that is responsible for converting basic java elements to the corresponding javascript code. J2S provides with the default
479-
implementation net.sf.j2s.core.astvisitors.ASTScriptVisitor that can be extended for building customized AST script visitors.
479+
implementation net.sf.j2s.core.astvisitors.SWTScriptVisitor that can be extended for building customized AST script visitors.
480480
</para></formalpara>
481481

482482
<formalpara><title>DependencyASTVisitor</title> <para>
483483
The other type if visitor is DependencyASTVisitor that is responsible for creating a class dependency trees,
484-
in other words an import list for each java file. J2S provides with a default implemenation net.sf.j2s.core.astvisitors.DependencyASTVisitor. It is
485-
recommended that the user extends it for defining its own AST dependency visitors. </para></formalpara>
484+
in other words an import list for each java file. J2S provides with a default implemenation net.sf.j2s.core.astvisitors.SWTDependencyVisitor. It is
485+
recommended that the user extends this class for defining its own AST dependency visitors. </para></formalpara>
486486

487487
<para>go to the "extensions" tab and add the extension point <code>net.sf.j2s.core.extendedASTScriptVisitor</code>:</para>
488488
<figure>
@@ -532,6 +532,7 @@ package j2s.extension1;
532532

533533
import net.sf.j2s.core.astvisitors.ASTScriptVisitor;
534534
import net.sf.j2s.core.astvisitors.DependencyASTVisitor;
535+
import net.sf.j2s.core.astvisitors.SWTDependencyASTVisitor;
535536
import net.sf.j2s.core.compiler.IExtendedVisitor;
536537

537538
public class MethodInvocationComment1 implements IExtendedVisitor {
@@ -571,9 +572,9 @@ package j2s.extension1;
571572

572573
import org.eclipse.jdt.core.dom.MethodInvocation;
573574

574-
import net.sf.j2s.core.astvisitors.ASTScriptVisitor;
575+
import net.sf.j2s.core.astvisitors.SWTScriptVisitor;
575576

576-
public class MethodInvocationComment1ScriptVisitor extends ASTScriptVisitor {
577+
public class MethodInvocationComment1ScriptVisitor extends SWTScriptVisitor {
577578
@Override
578579
public boolean visit(MethodInvocation node) {
579580
buffer.append("/* A COMMENT ADDED WITH our J2S compiler extension plugin */\n");
@@ -639,39 +640,130 @@ java.lang.System.out.println ("hee");
639640
</programlisting>
640641

641642
<para>as you can see, out javascript custom comment was appended just before the method declaration at <code>java.lang.System.out.println ("hee");</code> </para>
643+
642644
</section>
643645

644646

645-
<programlistingco>
646-
<areaspec>
647-
<area id="j2s-compiler-ext-getScriptVisitor2" coords='14'/>
648-
</areaspec>
649-
<programlisting>
650647

651-
</programlisting>
652-
<calloutlist>
653-
<callout arearefs="j2s-compiler-ext-getScriptVisitor1">
654-
<para> </para>
655-
</callout>
656-
</calloutlist>
657-
</programlistingco>
658648

659649

660-
661-
<para>TODO</para>
662-
650+
651+
663652

664-
</section>
665653

666654
<section><title>Other example: html attributes with javadoc annotations</title>
667-
<para>This second example uses J2S extension mechanism so the user can load html in class fields
668-
from javadoc. We usually have to reference html string in our web applications code, and sometimes it is nasty
669-
to do that using java or javascript strings. The idea of this plugin is to load a string to a class attribute from javadoc, like this: </para>
655+
<para>In this second example we show how to load string from javadoc comments into java fields.
656+
In web applications, we usually have to reference html, json, xml or other web format strings
657+
in our code and sometimes it is nasty to do it in common java or javascript strings literals. </para>
658+
659+
<para>The following is the java class code that implements ASTScriptVisitor that will detect and load a
660+
string in javadoc after the tag <code>@j2sLoadString</code> .</para>
661+
670662

671663
<programlisting>
664+
package j2s.extension1;
665+
666+
import java.util.Iterator;
667+
import java.util.List;
668+
669+
import net.sf.j2s.core.astvisitors.SWTScriptVisitor;
670+
671+
import org.eclipse.jdt.core.dom.FieldDeclaration;
672+
import org.eclipse.jdt.core.dom.Javadoc;
673+
import org.eclipse.jdt.core.dom.TagElement;
674+
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
675+
676+
public class MethodInvocationComment1ScriptVisitor extends SWTScriptVisitor {
677+
678+
@Override
679+
public boolean visit(FieldDeclaration node) {
680+
boolean ret = super.visit(node); /* first visit the default visitor implementation */
681+
682+
Javadoc javadoc = node.getJavadoc();
683+
List tags = javadoc.tags();
684+
if(tags!=null&amp;&amp;tags.size()&gt;0) {
685+
for (Iterator it = tags.iterator(); it.hasNext();) {
686+
Object o = (Object) it.next();
687+
if(o instanceof TagElement) {
688+
TagElement tag = (TagElement) o;
689+
String tagName = tag.getTagName();
690+
if(tagName!=null&amp;&amp;tagName.equals("@j2sLoadString")) { /* annotation found. all the
691+
following javadoc comment until other tag is found will be loaded */
692+
StringBuffer sb = new StringBuffer();
693+
List frags = tag.fragments();
694+
for (Iterator it2 = frags.iterator(); it2.hasNext();) {
695+
sb.append(it2.next()+"\\n");
696+
}
697+
List fieldDeclFrags = node.fragments();
698+
/* now that we have the @j2sLoadString string, we add the javascript statement this.attributeName="str.." for loading the string field.*/
699+
if(fieldDeclFrags!=null&amp;&amp;fieldDeclFrags.size()&gt;0) {
700+
VariableDeclarationFragment f1 = (VariableDeclarationFragment) fieldDeclFrags.get(0);
701+
String str = sb.toString();
702+
buffer.append("this."+f1.getName()+"=\'");
703+
buffer.append(str);
704+
buffer.append("\';\n");
705+
}
706+
}
707+
}
708+
}
709+
}
710+
return ret;
711+
}
712+
}
713+
</programlisting>
672714

715+
<para>Using this compiler extension, you will be able to code complex strings like json, html, xml, etc inside javadoc.
716+
The following java test program define two fields, htmlCode1 and jsonCode1 which content is loaded from javadoc and not from string literals.
717+
As you can see there is much easier to put
718+
other language code inside javadoc instead using java string, in which you have to quote and concatenate:</para>
719+
720+
<programlisting>
721+
package org.foo;
722+
723+
public class Test1 {
724+
725+
public static void main(String[] args) {
726+
Test1 test1 = new Test1();
727+
System.out.println("loaded html code: "+test1.getHtmlCode1());
728+
System.out.println("loaded json code: "+test1.getJsonCode1());
729+
System.out.println("lot easier isn't it?");
730+
}
731+
/** this field will be loaded with the fololowing string:
732+
* @j2sLoadString
733+
* &lt;h1&gt;this is an &lt;b&gt;html&lt;/b&gt; string loaded from javadoc&lt;/h1&gt;
734+
*/
735+
String htmlCode1;
736+
737+
/**
738+
* this other too
739+
* @j2s LoadString
740+
* {p1: [1, 2, 3], p2: {p21: "thi sis a json loaded from javadoc"}}
741+
*/
742+
String jsonCode1;
743+
744+
public String getHtmlCode1() {
745+
return htmlCode1;
746+
}
747+
748+
public String getJsonCode1() {
749+
return jsonCode1;
750+
}
751+
}
673752
</programlisting>
674753

675-
<para>TODO: finish this</para>
754+
755+
<para>Compiling this using our new compiler extension, the output will be the following when executing this as a Java2Script application:</para>
756+
757+
758+
<programlisting>
759+
loaded html code:
760+
&lt;h1&gt;this is an &lt;b&gt;html&lt;/b&gt; string loaded from javadoc&lt;/h1&gt;
761+
762+
loaded json code: {p1: [1, 2, 3], p2: {p21: "thi sis a json loaded from javadoc"}}
763+
764+
lot easier isn't it?
765+
</programlisting>
676766
</section>
767+
</section>
768+
677769
</chapter>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4+
<head>
5+
<title>Java2Script User's Guide</title>
6+
</head>
7+
<body>
8+
<h1>Java2Script User's Guide</h1>
9+
10+
<h2>...Work in progress...</h2>
11+
<h4>Available formats: </h4>
12+
<ul>
13+
<li><a href="j2s-user-guide-htmls/book1.html">Multiple HTMLs</a></li>
14+
15+
<li><a href="j2s-user-guide.pdf">PDF</a></li>
16+
17+
<li><a href="j2s-user-guide-singlehtml/j2s-user-guide.html">Single HTML document</a></li>
18+
19+
<li><a href="j2s-user-guide-htmls.tgz">Multiple HTMLs archive for download</a></li>
20+
21+
</ul>
22+
</body>
23+
24+
</html>

incubator/net.sf.j2s.doc.user-guide/j2s-user-guide/make-all.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ mv $DOC dist/$DOC-htmls
2424

2525
tar cvfz dist/$DOC-htmls.tgz dist/$DOC-htmls
2626

27+
28+
#single html
29+
mkdir dist/$DOC-singlehtml
30+
db2html --nochunks $DOC.xml > dist/$DOC-singlehtml/$DOC.html
31+
cp -r images dist/$DOC-singlehtml/images
32+
33+
cp index.html dist
34+
2735
rm -r *.pdf *.tex *.dsl CATALOG* *~ db2html* *.dsl
2836

2937

0 commit comments

Comments
 (0)
X Tutup