Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2008-05-22 11:07:12 -0400
committerpelder2008-05-22 11:07:12 -0400
commit6b47d8b5efddb46a24fd061c71f05fc335d062fe (patch)
tree4d293c7d751becae6ba051b58728d6087008960a
parent3e99a8f723c17ca6b094c09613ee293f66b29425 (diff)
downloadorg.eclipse.jet-6b47d8b5efddb46a24fd061c71f05fc335d062fe.tar.gz
org.eclipse.jet-6b47d8b5efddb46a24fd061c71f05fc335d062fe.tar.xz
org.eclipse.jet-6b47d8b5efddb46a24fd061c71f05fc335d062fe.zip
[205811] Define a mechanism for including copyright notices in all JET generated Java classes
-rw-r--r--doc/org.eclipse.jet.doc/references/syntax/jetSyntaxComments.xhtml35
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SafeJETASTVisitor.java4
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/V2CodeGenVisitor.java44
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/v2/JET2JavaGeneratorNew.java46
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/IndentTag.java6
-rw-r--r--plugins/org.eclipse.jet/templates/v2/jet2java.jet42
-rw-r--r--tests/org.eclipse.jet.tests/data/alljet2.jet7
-rw-r--r--tests/org.eclipse.jet.tests/data/alljet2.jet.expected45
-rw-r--r--tests/org.eclipse.jet.tests/data/alljet2.jet.expected.java11
9 files changed, 211 insertions, 29 deletions
diff --git a/doc/org.eclipse.jet.doc/references/syntax/jetSyntaxComments.xhtml b/doc/org.eclipse.jet.doc/references/syntax/jetSyntaxComments.xhtml
index 908905d..b66ee88 100644
--- a/doc/org.eclipse.jet.doc/references/syntax/jetSyntaxComments.xhtml
+++ b/doc/org.eclipse.jet.doc/references/syntax/jetSyntaxComments.xhtml
@@ -16,18 +16,37 @@
<h2>JET Comment Syntax Reference</h2>
<p>JET templates may contain comments between the characters
- <code>&lt;%--</code> and <code>--%&gt;</code>. JET ignores all
- content in a comment. </p>
+ <code>&lt;%--</code> and <code>--%&gt;</code>.
+ Comments have no impact on the execution of the template, except that they may influence
+ whitespace stripping rules.
+ JET comments are copied to the generated Java class as Java line comments. </p>
+ <p>
+ JET templates accept two special tags in the first non-blank line of a comment. The tag '@header' will
+ cause the comment to be emitted as the file header comment for the generated Java class.
+ This can be useful for inserting copyright notices into the generated Java code.
+ The tag '@class' will
+ cause the comment to be emitted as the class Java doc comment for the generated Java class.
+ </p>
<h3>Valid comments</h3>
<p>Comments may span several lines, and may contain any text. The
following are examples of a JET comments:</p>
- <pre>&lt;%-- This comment will not appear in the template output --%&gt;
-&lt;%-- This directive is not used
-&lt;%@taglib id=&quot;org.eclipse.jet.controlTags&quot; prefix=&quot;cc&quot;%&gt;
---%&gt;</pre>
+ <pre>
+ &lt;%-- @header
+ This comment will appear as the file header comment
+ in the generated Java code
+ --%&gt;
+ &lt;%--
+ @class
+ This comment will appear as the Java class doc comment
+ in the generated Java code
+ --%&gt;
+ &lt;%-- This comment will not appear in the template output --%&gt;
+ &lt;%-- This directive is not used
+ &lt;%@taglib id=&quot;org.eclipse.jet.controlTags&quot; prefix=&quot;cc&quot;%&gt;
+ --%&gt;</pre>
<h3>Invalid comments</h3>
<p>Comments may not appear within other JET elements. The following
@@ -43,7 +62,9 @@
<p>The following is an example of an escaped JET comments:</p>
- <pre>&lt;\%-- this will show in the template output --%\&gt;</pre>
+ <pre>&lt;\%-- this will show in the template output --%\&gt;
+
+ </pre>
</body>
</html>
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SafeJETASTVisitor.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SafeJETASTVisitor.java
index ed06d76..d9d0e61 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SafeJETASTVisitor.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/SafeJETASTVisitor.java
@@ -12,7 +12,7 @@
*
* </copyright>
*
- * $Id: SafeJETASTVisitor.java,v 1.3 2007/06/01 20:33:32 pelder Exp $
+ * $Id: SafeJETASTVisitor.java,v 1.4 2008/05/22 15:07:11 pelder Exp $
*/
package org.eclipse.jet.internal.compiler;
@@ -265,6 +265,7 @@ public class SafeJETASTVisitor extends JETASTVisitor
}
}
}
+
protected final JET2Context context;
protected final JET2Writer out;
private String indent;
@@ -317,4 +318,5 @@ public class SafeJETASTVisitor extends JETASTVisitor
}
}
+
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/V2CodeGenVisitor.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/V2CodeGenVisitor.java
index 722f546..6c5c10f 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/V2CodeGenVisitor.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/V2CodeGenVisitor.java
@@ -12,16 +12,20 @@
*
* </copyright>
*
- * $Id: V2CodeGenVisitor.java,v 1.2 2007/05/02 00:11:29 pelder Exp $
+ * $Id: V2CodeGenVisitor.java,v 1.3 2008/05/22 15:07:11 pelder Exp $
*/
package org.eclipse.jet.internal.compiler;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import org.eclipse.jet.JET2Context;
import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.core.parser.ast.JETASTElement;
import org.eclipse.jet.core.parser.ast.TextElement;
import org.eclipse.jet.core.parser.ast.XMLElement;
import org.eclipse.jet.internal.JavaUtil;
+import org.eclipse.jet.internal.taglib.format.IndentTag;
/**
* AST Visitor used by code generation
@@ -126,5 +130,43 @@ public class V2CodeGenVisitor extends SafeJETASTVisitor
public String nlsComment(TextElement text) {
return JavaUtil.nlsCommentsForJavaQuoteStringWithNLRemoved(text.getText());
}
+
+ private static final String NL_REGEX = "([\\n][\\r]?|[\\r][\\n]?)"; //$NON-NLS-1$
+ private static final Pattern NL_PATTERN = Pattern.compile(NL_REGEX, Pattern.MULTILINE);
+
+ protected boolean isClassComment(String comment) {
+ final String text = getFirstCommentLine(comment);
+ return text.equals("@class"); //$NON-NLS-1$
+ }
+
+ protected boolean isHeaderComment(String comment) {
+ final String text = getFirstCommentLine(comment);
+ return text.equals("@header"); //$NON-NLS-1$
+ }
+
+ protected boolean isNormalComment(String comment) {
+ final String text = getFirstCommentLine(comment);
+ return !text.equals("@header") && !text.equals("@class"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ protected String formatComment(String text, String leadIn) {
+ if(!isNormalComment(text)) {
+ text = text.replaceFirst("(?ms)^.*?$.*?^", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ return IndentTag.indent(text, leadIn, 1);
+ }
+
+ private String getFirstCommentLine(String text)
+ {
+ text = text.trim(); // remove any leading or trailing whitespace
+ final Matcher matcher = NL_PATTERN.matcher(text);
+ final String firstLine;
+ if(matcher.find()) {
+ firstLine = text.substring(0, matcher.start()).trim();
+ } else {
+ firstLine = text;
+ }
+ return firstLine;
+ }
+
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/v2/JET2JavaGeneratorNew.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/v2/JET2JavaGeneratorNew.java
index 4973975..970afed 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/v2/JET2JavaGeneratorNew.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/v2/JET2JavaGeneratorNew.java
@@ -35,6 +35,23 @@ public class JET2JavaGeneratorNew implements JET2Template {
});
final String nlConstantName = "NL"; //$NON-NLS-1$
+cu.accept(new V2CodeGenVisitor(context,out, "") { //$NON-NLS-1$
+ public boolean visit(Comment comment) {
+ final String text = comment.getCommentText();
+ final String leadIn = " * "; //$NON-NLS-1$
+ if(isHeaderComment(text)) {
+
+ out.write("/*"); //$NON-NLS-1$
+ out.write(NL);
+ out.write(formatComment(text, leadIn)) ;
+ out.write(" */"); //$NON-NLS-1$
+ out.write(NL);
+
+ }
+ return true;
+ }
+});
+
out.write("package "); //$NON-NLS-1$
out.write( cu.getOutputJavaPackage() );
out.write(";"); //$NON-NLS-1$
@@ -59,6 +76,23 @@ public class JET2JavaGeneratorNew implements JET2Template {
out.write(NL);
}
out.write(NL);
+cu.accept(new V2CodeGenVisitor(context,out, "") { //$NON-NLS-1$
+ public boolean visit(Comment comment) {
+ final String text = comment.getCommentText();
+ final String leadIn = " * "; //$NON-NLS-1$
+ if(isClassComment(text)) {
+
+ out.write("/**"); //$NON-NLS-1$
+ out.write(NL);
+ out.write(formatComment(text, leadIn)) ;
+ out.write(" */"); //$NON-NLS-1$
+ out.write(NL);
+
+ }
+ return true;
+ }
+});
+
out.write("public class "); //$NON-NLS-1$
out.write( cu.getOutputJavaClassName() );
out.write(" implements JET2Template {"); //$NON-NLS-1$
@@ -294,6 +328,18 @@ public class JET2JavaGeneratorNew implements JET2Template {
return true;
}
+
+
+ public boolean visit(Comment comment) {
+ final String text = comment.getCommentText();
+ final String leadIn = " //"; //$NON-NLS-1$
+ if(isNormalComment(text)) {
+ out.write(formatComment(text, leadIn)) ;
+ }
+ return true;
+ }
+
+
});
out.write(" }"); //$NON-NLS-1$
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/IndentTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/IndentTag.java
index ac73d3d..b495515 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/IndentTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/format/IndentTag.java
@@ -12,7 +12,7 @@
*
* </copyright>
*
- * $Id: IndentTag.java,v 1.1 2008/04/28 19:35:24 pelder Exp $
+ * $Id: IndentTag.java,v 1.2 2008/05/22 15:07:11 pelder Exp $
*/
package org.eclipse.jet.internal.taglib.format;
@@ -76,10 +76,10 @@ public class IndentTag extends AbstractFunctionTag
lineStart = matcher.end();
}
- // loook for final line without a NL character...
+ // look for final line without a NL character...
if(lineStart < content.length()) {
String line = content.substring(lineStart);
- result.append(totalIndent).append(line);
+ result.append(totalIndent).append(line).append(NL);
}
return result.toString();
diff --git a/plugins/org.eclipse.jet/templates/v2/jet2java.jet b/plugins/org.eclipse.jet/templates/v2/jet2java.jet
index d646fc7..167243b 100644
--- a/plugins/org.eclipse.jet/templates/v2/jet2java.jet
+++ b/plugins/org.eclipse.jet/templates/v2/jet2java.jet
@@ -22,6 +22,21 @@
});
final String nlConstantName = "NL"; //$NON-NLS-1$
%>
+<%cu.accept(new V2CodeGenVisitor(context,out, "") { //$NON-NLS-1$
+ public boolean visit(Comment comment) {
+ final String text = comment.getCommentText();
+ final String leadIn = " * "; //$NON-NLS-1$
+ if(isHeaderComment(text)) {
+%>
+/*
+<% out.write(formatComment(text, leadIn)) ;%>
+ */
+<%
+ }
+ return true;
+ }
+});
+%>
package <%= cu.getOutputJavaPackage() %>;
import org.eclipse.jet.JET2Context;
@@ -35,6 +50,21 @@ import org.eclipse.jet.taglib.TagInfo;
import <%= (String)i.next() %>;
<% } %>
+<%cu.accept(new V2CodeGenVisitor(context,out, "") { //$NON-NLS-1$
+ public boolean visit(Comment comment) {
+ final String text = comment.getCommentText();
+ final String leadIn = " * "; //$NON-NLS-1$
+ if(isClassComment(text)) {
+%>
+/**
+<% out.write(formatComment(text, leadIn)) ;%>
+ */
+<%
+ }
+ return true;
+ }
+});
+%>
public class <%= cu.getOutputJavaClassName() %> implements JET2Template {
<% for(int i = 0; i < tlrefs.length; i++ ) { %>
private static final String _jetns_<%= tlrefs[i].getPrefix() %> = "<%= tlrefs[i].getTagLibraryId() %>"; //$NON-NLS-1$
@@ -153,6 +183,18 @@ public class <%= cu.getOutputJavaClassName() %> implements JET2Template {
<%
return true;
}
+ %>
+ <%
+ public boolean visit(Comment comment) {
+ final String text = comment.getCommentText();
+ final String leadIn = " //"; //$NON-NLS-1$
+ if(isNormalComment(text)) {
+ out.write(formatComment(text, leadIn)) ;
+ }
+ return true;
+ }
+ %>
+<%
});
%>
}
diff --git a/tests/org.eclipse.jet.tests/data/alljet2.jet b/tests/org.eclipse.jet.tests/data/alljet2.jet
index 241c44d..f38e90f 100644
--- a/tests/org.eclipse.jet.tests/data/alljet2.jet
+++ b/tests/org.eclipse.jet.tests/data/alljet2.jet
@@ -1,4 +1,11 @@
<%@jet class="Foo" package="org.eclipse.emf.examples"%>
+<%-- @header
+This is the header comment for the file
+It has many lines
+--%>
+<%-- @class
+This is the class comment.
+It has many lines --%>
Some top level text.
<%= "An expression" %>
<%!
diff --git a/tests/org.eclipse.jet.tests/data/alljet2.jet.expected b/tests/org.eclipse.jet.tests/data/alljet2.jet.expected
index 43b65a2..1208675 100644
--- a/tests/org.eclipse.jet.tests/data/alljet2.jet.expected
+++ b/tests/org.eclipse.jet.tests/data/alljet2.jet.expected
@@ -3,39 +3,50 @@
<attribute name="package">org.eclipse.emf.examples</attribute>
</directive>
<data>
+</data>
+<comment start="(2,5)" stop="(5,1)"> @header
+This is the header comment for the file
+It has many lines
+</comment>
+<data>
+</data>
+<comment start="(6,5)" stop="(8,19)"> @class
+This is the class comment.
+It has many lines </comment>
+<data>
Some top level text.
</data>
-<expression start="(3,4)" stop="(3,21)"> "An expression" </expression>
+<expression start="(10,4)" stop="(10,21)"> "An expression" </expression>
<data>
</data>
-<declaration start="(4,4)" stop="(8,1)">
+<declaration start="(11,4)" stop="(15,1)">
int foo() {
return 3;
}
</declaration>
<data>
</data>
-<scriptlet start="(9,3)" stop="(13,1)">
+<scriptlet start="(16,3)" stop="(20,1)">
if(foo() > 0) {
foo();
}
</scriptlet>
<data>
</data>
-<comment start="(14,5)" stop="(14,16)"> a comment </comment>
+<comment start="(21,5)" stop="(21,16)"> a comment </comment>
<data>
</data>
-<tag name="test:container" start="(15,1)" stop="(15,47)">
+<tag name="test:container" start="(22,1)" stop="(22,47)">
<attribute name="one">test one</attribute>
<attribute name="two">test two</attribute>
<content>
<data>
</data>
- <tag name="test:empty" start="(16,5)" stop="(16,18)">
+ <tag name="test:empty" start="(23,5)" stop="(23,18)">
</tag>
<data>
</data>
- <tag name="test:container" start="(17,5)" stop="(17,21)">
+ <tag name="test:container" start="(24,5)" stop="(24,21)">
<content>
<data>
some text <nontest:text attributes="foo"/>
@@ -48,39 +59,39 @@ if(foo() > 0) {
</tag>
<data>
</data>
-<comment start="(21,5)" stop="(21,27)"> tags containing Java </comment>
+<comment start="(28,5)" stop="(28,27)"> tags containing Java </comment>
<data>
</data>
-<tag name="test:container" start="(22,1)" stop="(22,17)">
+<tag name="test:container" start="(29,1)" stop="(29,17)">
<content>
<data>
</data>
- <expression start="(23,8)" stop="(23,16)"> foo(); </expression>
+ <expression start="(30,8)" stop="(30,16)"> foo(); </expression>
<data>
</data>
- <scriptlet start="(24,7)" stop="(24,24)"> if(foo() > 0) { </scriptlet>
+ <scriptlet start="(31,7)" stop="(31,24)"> if(foo() > 0) { </scriptlet>
<data>
some text
</data>
- <scriptlet start="(26,7)" stop="(26,10)"> } </scriptlet>
+ <scriptlet start="(33,7)" stop="(33,10)"> } </scriptlet>
<data>
</data>
</content>
</tag>
<data>
</data>
-<comment start="(28,5)" stop="(28,26)"> Java container tags </comment>
+<comment start="(35,5)" stop="(35,26)"> Java container tags </comment>
<data>
</data>
-<scriptlet start="(29,3)" stop="(29,20)"> if(foo() > 0) { </scriptlet>
+<scriptlet start="(36,3)" stop="(36,20)"> if(foo() > 0) { </scriptlet>
<data>
some text
</data>
-<tag name="test:empty" start="(31,9)" stop="(31,22)">
+<tag name="test:empty" start="(38,9)" stop="(38,22)">
</tag>
<data>
</data>
-<tag name="test:container" start="(32,9)" stop="(32,25)">
+<tag name="test:container" start="(39,9)" stop="(39,25)">
<content>
<data>
</data>
@@ -88,6 +99,6 @@ if(foo() > 0) {
</tag>
<data>
</data>
-<scriptlet start="(34,3)" stop="(34,6)"> } </scriptlet>
+<scriptlet start="(41,3)" stop="(41,6)"> } </scriptlet>
<data>
</data>
diff --git a/tests/org.eclipse.jet.tests/data/alljet2.jet.expected.java b/tests/org.eclipse.jet.tests/data/alljet2.jet.expected.java
index 4d19cf8..80cdc4c 100644
--- a/tests/org.eclipse.jet.tests/data/alljet2.jet.expected.java
+++ b/tests/org.eclipse.jet.tests/data/alljet2.jet.expected.java
@@ -1,9 +1,17 @@
+/*
+ * This is the header comment for the file
+ * It has many lines
+ */
package org.eclipse.emf.examples;
import org.eclipse.jet.JET2Context;
import org.eclipse.jet.JET2Template;
import org.eclipse.jet.JET2Writer;
+/**
+ * This is the class comment.
+ * It has many lines
+ */
public class Foo implements JET2Template {
@@ -28,6 +36,7 @@ if(foo() > 0) {
foo();
}
+ // a comment
out.write("<test:container one=\"test one\" two='test two'>"); //$NON-NLS-1$
out.write(NL);
out.write(" <test:empty/>"); //$NON-NLS-1$
@@ -40,6 +49,7 @@ if(foo() > 0) {
out.write(NL);
out.write("</test:container>"); //$NON-NLS-1$
out.write(NL);
+ // tags containing Java
out.write("<test:container>"); //$NON-NLS-1$
out.write(NL);
out.write(" "); //$NON-NLS-1$
@@ -51,6 +61,7 @@ if(foo() > 0) {
}
out.write("</test:container>"); //$NON-NLS-1$
out.write(NL);
+ // Java container tags
if(foo() > 0) {
out.write(" some text"); //$NON-NLS-1$
out.write(NL);

Back to the top