Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2009-04-30 12:27:33 -0400
committerpelder2009-04-30 12:27:33 -0400
commit4b2c520c53d9be47d43fbead79bbce229d245371 (patch)
treef946682a1e2543c1ad28077af338c7334cb09805
parent7714d5bae887506f7941b451c5c6b5da08011cbf (diff)
downloadorg.eclipse.jet-4b2c520c53d9be47d43fbead79bbce229d245371.tar.gz
org.eclipse.jet-4b2c520c53d9be47d43fbead79bbce229d245371.tar.xz
org.eclipse.jet-4b2c520c53d9be47d43fbead79bbce229d245371.zip
[262915] Apply Eclipse JDT formatter to jet-generated java sources
-rw-r--r--doc/org.eclipse.jet.doc/references/taglibs/formatTags/indentTag.html4
-rw-r--r--doc/org.eclipse.jet.doc/references/taglibs/javaTags/formatTag.html79
-rw-r--r--doc/org.eclipse.jet.doc/references/taglibs/javaTags/overview.html7
-rw-r--r--doc/org.eclipse.jet.doc/tocjavaTags.xml1
-rw-r--r--plugins/org.eclipse.jet/.options2
-rw-r--r--plugins/org.eclipse.jet/plugin.xml28
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaFormatTag.java149
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/Messages.java38
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/TagFactory.java29
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/messages.properties13
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/AllTests.java1
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTest.java78
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTestExpectedOutput1.txt11
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTestInput1.txt11
14 files changed, 436 insertions, 15 deletions
diff --git a/doc/org.eclipse.jet.doc/references/taglibs/formatTags/indentTag.html b/doc/org.eclipse.jet.doc/references/taglibs/formatTags/indentTag.html
index 5241cdc..8f8b0c7 100644
--- a/doc/org.eclipse.jet.doc/references/taglibs/formatTags/indentTag.html
+++ b/doc/org.eclipse.jet.doc/references/taglibs/formatTags/indentTag.html
@@ -28,7 +28,7 @@
<TD width="10%">required</TD>
<TD><SPAN style="font-family: monospace">
&lt;f:indent text=&quot;<I>value</I>&quot;&gt;<br>
- &nbsp;&nbsp;&nbsp;&nbsp;<I>content consumed by tag evaluation</I><BR>
+ &nbsp;&nbsp;&nbsp;&nbsp;<I>content to be written</I><BR>
&lt;/f:indent&gt;
</SPAN></TD>
</TR>
@@ -36,7 +36,7 @@
<TD width="10%">full tag</TD>
<TD><SPAN style="font-family: monospace">
&lt;f:indent text=&quot;<I>value</I>&quot; depth=&quot;<I>value</I>&quot;&gt;<br>
- &nbsp;&nbsp;&nbsp;&nbsp;<I>content consumed by tag evaluation</I><BR>
+ &nbsp;&nbsp;&nbsp;&nbsp;<I>content to be written</I><BR>
&lt;/f:indent&gt;
</SPAN></TD>
</TR>
diff --git a/doc/org.eclipse.jet.doc/references/taglibs/javaTags/formatTag.html b/doc/org.eclipse.jet.doc/references/taglibs/javaTags/formatTag.html
new file mode 100644
index 0000000..bdf1333
--- /dev/null
+++ b/doc/org.eclipse.jet.doc/references/taglibs/javaTags/formatTag.html
@@ -0,0 +1,79 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<HTML>
+<HEAD>
+<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+<META name="GENERATOR" content="IBM Software Development Platform">
+<link rel="STYLESHEET" href="../../../book.css" charset="ISO-8859-1" type="text/css"/>
+<TITLE>&lt;java:format&gt; - Standard JET2 Java Tags</TITLE>
+</HEAD>
+<BODY>
+<TABLE border="0" cellpadding="0" cellspacing="1" width="100%">
+ <TBODY>
+ <TR>
+ <TH align="left">
+ <P>&lt;java:format&gt;</P></TH>
+ <TH align="right"><A href="overview.html">Standard JET2 Java Tags</A></TH>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<P><B>format</B></P>
+<P><P> Invokes the Eclipse JDT Code formatter on the tag's content.<P><P>The tag content should be well-formed Java - otherwise it will be written out unformatted.<P> </P>
+<HR>
+<TABLE border="1" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Tag Summary</TH></TR>
+ <TR>
+ <TD width="10%">required</TD>
+ <TD><SPAN style="font-family: monospace">
+ &lt;java:format&gt;<br>
+ &nbsp;&nbsp;&nbsp;&nbsp;<I>content consumed by tag evaluation</I><BR>
+ &lt;/java:format&gt;
+ </SPAN></TD>
+ </TR>
+ <TR>
+ <TD width="10%">full tag</TD>
+ <TD><SPAN style="font-family: monospace">
+ &lt;java:format kind=&quot;<I>value</I>&quot; project=&quot;<I>value</I>&quot;&gt;<br>
+ &nbsp;&nbsp;&nbsp;&nbsp;<I>content consumed by tag evaluation</I><BR>
+ &lt;/java:format&gt;
+ </SPAN></TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<TABLE border="1" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Optional Attributes</TH></TR>
+ <TR>
+ <TR>
+ <TD width="10%">kind</TD>
+ <TD><BR> A string corresponding to one of these org.eclipse.jdt.core.ToolFactory constants : "K_COMPILATION_UNIT" , "K_CLASS_BODY_DECLARATIONS" ,<BR>"K_EXPRESSION", "K_STATEMENTS".<BR>If missing K_UNKNOWN will be used.<BR> </TD>
+ </TR>
+ <TR>
+ <TD width="10%">project</TD>
+ <TD><BR> The name of a java projects whose settings (like Java source level) should be used for the formatter options. <BR>If missing the workspace defaults are used.<BR> </TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<HR>
+<TABLE border="1" width="100%">
+ <TBODY>
+ <TR>
+ <TH colspan="2" align="left">Example</TH></TR>
+ <TR>
+ <TD width="10%"></TD>
+ <TD><SPAN style="font-family: monospace">
+<!-- Begin format example -->
+ &lt;java:format&gt;<br>
+ &nbsp;&nbsp;&nbsp;&nbsp;<I>content consumed by tag evaluation</I><BR>
+ &lt;/java:format&gt;
+<!-- End format example -->
+</SPAN></TD>
+ </TR>
+ </TBODY>
+</TABLE>
+<P></P></BODY>
+</HTML>
diff --git a/doc/org.eclipse.jet.doc/references/taglibs/javaTags/overview.html b/doc/org.eclipse.jet.doc/references/taglibs/javaTags/overview.html
index 41b3da9..a88e8db 100644
--- a/doc/org.eclipse.jet.doc/references/taglibs/javaTags/overview.html
+++ b/doc/org.eclipse.jet.doc/references/taglibs/javaTags/overview.html
@@ -32,6 +32,13 @@
</TD>
</TR>
<TR>
+ <TD width="10%"><A href="formatTag.html">&lt;java:format&gt;</A></TD>
+ <TD><!-- Begin format description -->
+ <P> Invokes the Eclipse JDT Code formatter on the tag's content
+ <!-- End format description -->
+ </TD>
+ </TR>
+ <TR>
<TD width="10%"><A href="impliedImportTag.html">&lt;java:impliedImport&gt;</A></TD>
<TD><!-- Begin impliedImport description -->
<P> Record a name as being implicitly imported
diff --git a/doc/org.eclipse.jet.doc/tocjavaTags.xml b/doc/org.eclipse.jet.doc/tocjavaTags.xml
index 902a614..8d4ce2e 100644
--- a/doc/org.eclipse.jet.doc/tocjavaTags.xml
+++ b/doc/org.eclipse.jet.doc/tocjavaTags.xml
@@ -4,6 +4,7 @@
<toc label="Standard JET2 Java Tags" link_to="toc.xml#tagReference">
<topic label="Standard JET2 Java Tags" href="references/taglibs/javaTags/overview.html" >
<topic label="java:class" href="references/taglibs/javaTags/classTag.html"/>
+ <topic label="java:format" href="references/taglibs/javaTags/formatTag.html"/>
<topic label="java:impliedImport" href="references/taglibs/javaTags/impliedImportTag.html"/>
<topic label="java:import" href="references/taglibs/javaTags/importTag.html"/>
<topic label="java:importsLocation" href="references/taglibs/javaTags/importsLocationTag.html"/>
diff --git a/plugins/org.eclipse.jet/.options b/plugins/org.eclipse.jet/.options
index 3716f92..03bd453 100644
--- a/plugins/org.eclipse.jet/.options
+++ b/plugins/org.eclipse.jet/.options
@@ -11,4 +11,6 @@ org.eclipse.jet/debug/savedState=false
org.eclipse.jet/debug/pluginProjectMonitor=false
org.eclipse.jet/debug/modelLoaderExtensions=false
org.eclipse.jet/debug/startup=false
+org.eclipse.jet/debug/javatag=false
+
diff --git a/plugins/org.eclipse.jet/plugin.xml b/plugins/org.eclipse.jet/plugin.xml
index 122bee7..64f8d96 100644
--- a/plugins/org.eclipse.jet/plugin.xml
+++ b/plugins/org.eclipse.jet/plugin.xml
@@ -1023,6 +1023,34 @@ This tag may occur only once in a template.
</description>
</attribute>
</emptyTag>
+ <functionTag
+ class="org.eclipse.jet.internal.taglib.java.JavaFormatTag"
+ name="format">
+ <description>
+ Invokes the Eclipse JDT Code formatter on the tag&apos;s content.
+
+The tag content should be well-formed Java - otherwise it will be written out unformatted.
+ </description>
+ <attribute
+ name="kind"
+ type="string"
+ use="optional">
+ <description>
+ A string corresponding to one of these org.eclipse.jdt.core.ToolFactory constants : &quot;K_COMPILATION_UNIT&quot; , &quot;K_CLASS_BODY_DECLARATIONS&quot; ,
+&quot;K_EXPRESSION&quot;, &quot;K_STATEMENTS&quot;.
+If missing K_UNKNOWN will be used.
+ </description>
+ </attribute>
+ <attribute
+ name="project"
+ type="string"
+ use="optional">
+ <description>
+ The name of a java projects whose settings (like Java source level) should be used for the formatter options.
+If missing the workspace defaults are used.
+ </description>
+ </attribute>
+ </functionTag>
</tagLibrary>
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaFormatTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaFormatTag.java
new file mode 100644
index 0000000..c74183d
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaFormatTag.java
@@ -0,0 +1,149 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: JavaFormatTag.java,v 1.1 2009/04/30 16:27:33 pelder Exp $
+ */
+package org.eclipse.jet.internal.taglib.java;
+
+
+import java.util.Map;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.ToolFactory;
+import org.eclipse.jdt.core.formatter.CodeFormatter;
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.internal.InternalJET2Platform;
+import org.eclipse.jet.taglib.AbstractFunctionTag;
+import org.eclipse.jet.taglib.JET2TagException;
+import org.eclipse.jet.taglib.TagInfo;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.text.edits.MalformedTreeException;
+import org.eclipse.text.edits.TextEdit;
+
+
+/**
+ * Invokes the Eclipse JDT Code formatter on its body
+ * @author Edoardo Comar ecomar@uk.ibm.com
+ */
+public class JavaFormatTag extends AbstractFunctionTag
+{
+ public static boolean DEBUG = InternalJET2Platform.getDefault().isDebugging()
+ && Boolean.valueOf(Platform.getDebugOption("org.eclipse.jet/debug/javatag")).booleanValue(); //$NON-NLS-1$
+
+ public JavaFormatTag()
+ {
+ }
+
+ public String doFunction(TagInfo td, JET2Context context, String bodyContent) throws JET2TagException
+ {
+ try
+ {
+ String snippetKindString = getAttribute("kind"); //$NON-NLS-1$
+ String projectName = getAttribute("project"); //$NON-NLS-1$
+
+ int snippetKind = getSnippetKind(snippetKindString, context);
+ Map options = getJavaCoreOptions(projectName, context);
+
+ String result = invokeJavaFormatterOn(bodyContent, options, snippetKind, context);
+ return result;
+ }
+ catch (JET2TagException j2tex)
+ {
+ throw j2tex;
+ }
+ catch (Exception e)
+ {
+ throw new JET2TagException(e);
+ }
+
+ }
+
+ private int getSnippetKind(String snippetKindString, JET2Context context)
+ {
+ if(DEBUG) System.out.println("JavaFormatTag.getSnippetKind - value specified=" + snippetKindString); //$NON-NLS-1$
+
+ if("K_COMPILATION_UNIT".equals(snippetKindString)) { //$NON-NLS-1$
+ return CodeFormatter.K_COMPILATION_UNIT + CodeFormatter.F_INCLUDE_COMMENTS;
+ }
+ else if("K_CLASS_BODY_DECLARATIONS".equals(snippetKindString)) { //$NON-NLS-1$
+ return CodeFormatter.K_CLASS_BODY_DECLARATIONS;
+ }
+ else if("K_EXPRESSION".equals(snippetKindString)) { //$NON-NLS-1$
+ return CodeFormatter.K_EXPRESSION;
+ }
+ else if("K_STATEMENTS".equals(snippetKindString)) { //$NON-NLS-1$
+ return CodeFormatter.K_STATEMENTS;
+ }
+
+ if(DEBUG) System.out.println("JavaFormatTag.getSnippetKind - Falling back to K_UNKNOWN"); //$NON-NLS-1$
+ return CodeFormatter.K_UNKNOWN + CodeFormatter.F_INCLUDE_COMMENTS;
+ }
+
+ private Map getJavaCoreOptions(String projectName, JET2Context context) throws CoreException
+ {
+ if(DEBUG) System.out.println("JavaFormatTag.getJavaCoreOptions - project specified=" + projectName); //$NON-NLS-1$
+ Map options = null;
+
+ if(projectName!=null) {
+ IWorkspaceRoot wsroot = ResourcesPlugin.getWorkspace().getRoot();
+ IProject project = wsroot.getProject(projectName);
+ if(project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) {
+ IJavaProject javaProject = JavaCore.create(project);
+
+ if(DEBUG) System.out.println("JavaFormatTag.getJavaCoreOptions - using Options from java project " + projectName); //$NON-NLS-1$
+ options = javaProject.getOptions(true);
+ }
+ }
+
+ if(options==null) {
+ if(DEBUG) System.out.println("JavaFormatTag.getJavaCoreOptions - Falling back to generic JavaCore options"); //$NON-NLS-1$
+ options = JavaCore.getOptions();
+ }
+
+ return options;
+ }
+
+ private String invokeJavaFormatterOn(String bodyContent, Map options, int snippetKind, JET2Context context)
+ throws MalformedTreeException, BadLocationException
+ {
+ CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options, ToolFactory.M_FORMAT_EXISTING);
+
+ TextEdit textEdit = codeFormatter.format(snippetKind,
+ bodyContent, /* the whole body of the tag */
+ 0 /*from beginning */,
+ bodyContent.length() /* till the end */,
+ 0 /* initial indentation */,
+ null /* use platform default */);
+
+ if(textEdit==null) {
+ context.logError(Messages.JavaFormatTag_CouldNotFormat);
+ return bodyContent;
+ }
+
+ IDocument document = new Document(bodyContent);
+ textEdit.apply(document);
+
+ String result = document.get();
+ return result;
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/Messages.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/Messages.java
new file mode 100644
index 0000000..4454e29
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/Messages.java
@@ -0,0 +1,38 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2009 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id: Messages.java,v 1.1 2009/04/30 16:27:33 pelder Exp $
+ */
+package org.eclipse.jet.internal.taglib.java;
+
+
+import org.eclipse.osgi.util.NLS;
+
+
+public class Messages extends NLS
+{
+ private static final String BUNDLE_NAME = "org.eclipse.jet.internal.taglib.java.messages"; //$NON-NLS-1$
+
+ public static String JavaFormatTag_CouldNotFormat;
+
+ static
+ {
+ // initialize resource bundle
+ NLS.initializeMessages(BUNDLE_NAME, Messages.class);
+ }
+
+ private Messages()
+ {
+ }
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/TagFactory.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/TagFactory.java
index 8f148d7..166fe4d 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/TagFactory.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/TagFactory.java
@@ -41,15 +41,16 @@ public class TagFactory implements TagInstanceFactory
*/
public TagFactory()
{
- tagOrdinalByName = new HashMap(7);
+ tagOrdinalByName = new HashMap(8);
tagOrdinalByName.put("class",new Integer(1)); //$NON-NLS-1$
- tagOrdinalByName.put("impliedImport",new Integer(2)); //$NON-NLS-1$
- tagOrdinalByName.put("import",new Integer(3)); //$NON-NLS-1$
- tagOrdinalByName.put("importsLocation",new Integer(4)); //$NON-NLS-1$
- tagOrdinalByName.put("merge",new Integer(5)); //$NON-NLS-1$
- tagOrdinalByName.put("package",new Integer(6)); //$NON-NLS-1$
- tagOrdinalByName.put("resource",new Integer(7)); //$NON-NLS-1$
+ tagOrdinalByName.put("format",new Integer(2)); //$NON-NLS-1$
+ tagOrdinalByName.put("impliedImport",new Integer(3)); //$NON-NLS-1$
+ tagOrdinalByName.put("import",new Integer(4)); //$NON-NLS-1$
+ tagOrdinalByName.put("importsLocation",new Integer(5)); //$NON-NLS-1$
+ tagOrdinalByName.put("merge",new Integer(6)); //$NON-NLS-1$
+ tagOrdinalByName.put("package",new Integer(7)); //$NON-NLS-1$
+ tagOrdinalByName.put("resource",new Integer(8)); //$NON-NLS-1$
}
public CustomTag createCustomTag(String name)
@@ -59,17 +60,19 @@ public class TagFactory implements TagInstanceFactory
switch(ordinal == null ? 0 : ordinal.intValue()) {
case 1: // class
return new org.eclipse.jet.internal.taglib.java.JavaClassTag();
- case 2: // impliedImport
+ case 2: // format
+ return new org.eclipse.jet.internal.taglib.java.JavaFormatTag();
+ case 3: // impliedImport
return new org.eclipse.jet.internal.taglib.java.ImpliedImportTag();
- case 3: // import
+ case 4: // import
return new org.eclipse.jet.internal.taglib.java.ImportTag();
- case 4: // importsLocation
+ case 5: // importsLocation
return new org.eclipse.jet.internal.taglib.java.ImportsLocationTag();
- case 5: // merge
+ case 6: // merge
return new org.eclipse.jet.internal.taglib.java.MergeTag();
- case 6: // package
+ case 7: // package
return new org.eclipse.jet.internal.taglib.java.PackageTag();
- case 7: // resource
+ case 8: // resource
return new org.eclipse.jet.internal.taglib.java.JavaResourceTag();
default:
throw new JET2TagException("Unknown Tag: " + name); //$NON-NLS-1$
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/messages.properties b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/messages.properties
new file mode 100644
index 0000000..3febdf3
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/messages.properties
@@ -0,0 +1,13 @@
+###############################################################################
+# Copyright (c) 2009 IBM Corporation and others.
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+# IBM Corporation - initial API and implementation
+###############################################################################
+#
+# NLS_MESSAGEFORMAT_VAR
+JavaFormatTag_CouldNotFormat=The Java content could not be formatted. Check that it is well-formed and that the kind attribute (if specified) is compatible with the actual snippet.
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/AllTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/AllTests.java
index 7cf8f7e..1569e9d 100644
--- a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/AllTests.java
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/AllTests.java
@@ -10,6 +10,7 @@ public class AllTests {
"Test for org.eclipse.jet.tests.taglib.java");
//$JUnit-BEGIN$
suite.addTestSuite(TestImportsLocationTag.class);
+ suite.addTestSuite(JavaFormatTest.class);
//$JUnit-END$
return suite;
}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTest.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTest.java
new file mode 100644
index 0000000..b1b6ec3
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTest.java
@@ -0,0 +1,78 @@
+package org.eclipse.jet.tests.taglib.java;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.StringWriter;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.eclipse.jet.BodyContentWriter;
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.JET2Writer;
+import org.eclipse.jet.internal.core.NewLineUtil;
+import org.eclipse.jet.internal.taglib.java.JavaFormatTag;
+import org.eclipse.jet.taglib.TagInfo;
+
+public class JavaFormatTest extends TestCase {
+
+ private JavaFormatTag tag;
+ private JET2Context context;
+ private JET2Writer out;
+ private TagInfo td;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ context = new JET2Context(null);
+ out = new BodyContentWriter();
+
+ tag = new JavaFormatTag();
+ tag.setContext(context);
+ tag.setOut(out);
+ tag.setParent(null);
+ }
+
+
+ public void testFormat1() throws Exception {
+ doTestFunction("JavaFormatTestInput1.txt", "JavaFormatTestExpectedOutput1.txt");
+ }
+
+ private void doTestFunction(String inputResourceName, String expectedResourceName) throws Exception {
+ // 1) provide attribute values...
+ td = new TagInfo("format", 1, 1, // from a testing point-of-view, these are meaningless
+ new String[] {"kind","project"}, // attribute names...
+ new String[] {"K_UNKNOWN","org.eclipse.jet.tests"}); // attribute values...
+ tag.setTagInfo(td);
+
+ // 2) provide the content the tag will process
+ final String bodyContent = resource2String(inputResourceName);
+
+ final String actualContent = tag.doFunction(td, context, bodyContent);
+
+ final String expectedContent = resource2String(expectedResourceName);
+
+ Assert.assertEquals(expectedContent, actualContent);
+ }
+
+
+
+ private String resource2String(String resourceName) throws Exception{
+ InputStream istream = JavaFormatTest.class.getResourceAsStream(resourceName);
+ InputStreamReader reader = new InputStreamReader(istream);
+ StringWriter writer = new StringWriter();
+
+ char[] carray = new char[1024];
+
+ int read;
+ while ((read = reader.read(carray)) > 0) {
+ writer.write(carray, 0, read);
+ }
+
+ istream.close();
+ String resourceContent = writer.toString();
+ return NewLineUtil.setLineTerminator(resourceContent, System.getProperty("line.separator"));
+ }
+
+}
+
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTestExpectedOutput1.txt b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTestExpectedOutput1.txt
new file mode 100644
index 0000000..f19e6c1
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTestExpectedOutput1.txt
@@ -0,0 +1,11 @@
+void foo(int state) {
+ if (true)
+ return;
+ if (true)
+ return;
+ else if (false)
+ return;
+ else
+ return;
+}
+//} \ No newline at end of file
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTestInput1.txt b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTestInput1.txt
new file mode 100644
index 0000000..aa925db
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/taglib/java/JavaFormatTestInput1.txt
@@ -0,0 +1,11 @@
+ void foo(int state) {
+ if (true) return;
+ if (true)
+ return;
+ else
+ if (false)
+return;
+ else
+ return;
+ }
+ //} \ No newline at end of file

Back to the top