Author: max.andersen(a)jboss.com
Date: 2006-11-08 07:00:28 -0500 (Wed, 08 Nov 2006)
New Revision: 10771
Added:
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.core.runtime_3.2.0.v20060603.jar
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.equinox.common_3.2.0.v20060603.jar
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.jdt.core_3.2.0.v_671.jar
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.text_3.2.0.v20060605-1400.jar
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ant/JavaFormatterTask.java
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ide/formatting/JavaFormatter.java
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/JavaFormatterTest.java
branches/Branch_3_2/HibernateExt/tools/src/testsupport/emptyconfig.properties
branches/Branch_3_2/HibernateExt/tools/src/testsupport/formatting/Simple5One.java
branches/Branch_3_2/HibernateExt/tools/src/testsupport/formatting/SimpleOne.java
branches/Branch_3_2/HibernateExt/tools/src/testsupport/javaformattest-build.xml
Removed:
branches/Branch_3_2/HibernateExt/tools/lib/testlibs/org.eclipse.jdt.core_3.1.0.jar
Log:
HBX-811 eclipse code formatter ant task
Added:
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.core.runtime_3.2.0.v20060603.jar
===================================================================
(Binary files differ)
Property changes on:
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.core.runtime_3.2.0.v20060603.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added:
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.equinox.common_3.2.0.v20060603.jar
===================================================================
(Binary files differ)
Property changes on:
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.equinox.common_3.2.0.v20060603.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.jdt.core_3.2.0.v_671.jar
===================================================================
(Binary files differ)
Property changes on:
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.jdt.core_3.2.0.v_671.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added:
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.text_3.2.0.v20060605-1400.jar
===================================================================
(Binary files differ)
Property changes on:
branches/Branch_3_2/HibernateExt/tools/lib/org.eclipse.text_3.2.0.v20060605-1400.jar
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Deleted:
branches/Branch_3_2/HibernateExt/tools/lib/testlibs/org.eclipse.jdt.core_3.1.0.jar
===================================================================
(Binary files differ)
Added:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ant/JavaFormatterTask.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ant/JavaFormatterTask.java 2006-11-08
12:00:23 UTC (rev 10770)
+++
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ant/JavaFormatterTask.java 2006-11-08
12:00:28 UTC (rev 10771)
@@ -0,0 +1,119 @@
+package org.hibernate.tool.ant;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.DirectoryScanner;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.types.FileSet;
+
+import org.eclipse.jdt.internal.core.util.Util;
+import org.hibernate.tool.hbm2x.ExporterException;
+import org.hibernate.tool.ide.formatting.JavaFormatter;
+
+public class JavaFormatterTask extends Task {
+
+ private List fileSets = new ArrayList();
+ private boolean failOnError;
+ private File configurationFile;
+
+ public void addConfiguredFileSet(FileSet fileSet) {
+ fileSets.add(fileSet);
+ }
+
+ public void setConfigurationFile(File configurationFile) {
+ this.configurationFile = configurationFile;
+ }
+
+ private Properties readConfig(File cfgfile) throws IOException {
+ BufferedInputStream stream = null;
+ try {
+ stream = new BufferedInputStream(new FileInputStream(cfgfile));
+ final Properties settings = new Properties();
+ settings.load(stream);
+ return settings;
+ } catch (IOException e) {
+ throw e;
+ } finally {
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+
+ }
+ }
+ }
+ }
+
+
+ public void execute() throws BuildException {
+
+ Map settings = null;
+ if(configurationFile!=null) {
+ try {
+ settings = readConfig( configurationFile );
+ }
+ catch (IOException e) {
+ throw new BuildException("Could not read configurationfile " +
configurationFile, e);
+ }
+ }
+
+ File[] files = getFiles();
+
+ if(files.length>0) {
+
+ JavaFormatter formatter = new JavaFormatter(settings);
+
+ for (int i = 0; i < files.length; i++) {
+ File file = files[i];
+ getProject().log(this, "Formatting " + file, Project.MSG_INFO);
+ try {
+ boolean ok = formatter.formatFile( file );
+ if(!ok) {
+ getProject().log(this, "Formatting failed - skipping " + file,
Project.MSG_WARN);
+ }
+ } catch(ExporterException ee) {
+ if(failOnError) {
+ throw new BuildException("Formatting failed on " + file, ee);
+ } else {
+ getProject().log(this, "Formatting failed on " + file + ", " +
ee.getLocalizedMessage(), Project.MSG_ERR);
+ }
+ }
+ }
+ }
+
+ }
+
+ private File[] getFiles() {
+
+ List files = new LinkedList();
+ for ( Iterator i = fileSets.iterator(); i.hasNext(); ) {
+
+ FileSet fs = (FileSet) i.next();
+ DirectoryScanner ds = fs.getDirectoryScanner( getProject() );
+
+ String[] dsFiles = ds.getIncludedFiles();
+ for (int j = 0; j < dsFiles.length; j++) {
+ File f = new File(dsFiles[j]);
+ if ( !f.isFile() ) {
+ f = new File( ds.getBasedir(), dsFiles[j] );
+ }
+
+ files.add( f );
+ }
+ }
+
+ return (File[]) files.toArray(new File[files.size()]);
+ }
+
+}
Added:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ide/formatting/JavaFormatter.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ide/formatting/JavaFormatter.java 2006-11-08
12:00:23 UTC (rev 10770)
+++
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ide/formatting/JavaFormatter.java 2006-11-08
12:00:28 UTC (rev 10771)
@@ -0,0 +1,90 @@
+package org.hibernate.tool.ide.formatting;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.ToolFactory;
+import org.eclipse.jdt.core.formatter.CodeFormatter;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.text.edits.TextEdit;
+import org.hibernate.tool.hbm2x.ExporterException;
+
+
+public class JavaFormatter {
+
+ private CodeFormatter codeFormatter;
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ //DefaultCodeFormatter.USE_NEW_FORMATTER = true;
+ HashMap hashMap = new HashMap();
+ hashMap.put( JavaCore.COMPILER_SOURCE, "1.5");
+ hashMap.put( JavaCore.COMPILER_COMPLIANCE, "1.5");
+ hashMap.put( JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.5");
+ final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(hashMap);
+
+ //new JavaFormatter().formatFile(new File("c:/temp/SomeJava.java"),
codeFormatter);
+
+ }
+
+ public JavaFormatter(Map settings) {
+ if(settings==null) {
+ // if no settings run with jdk 5 as default
+ settings = new HashMap();
+ settings.put( JavaCore.COMPILER_SOURCE, "1.5");
+ settings.put( JavaCore.COMPILER_COMPLIANCE, "1.5");
+ settings.put( JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.5");
+ }
+
+ this.codeFormatter = ToolFactory.createCodeFormatter(settings);
+ }
+
+ /**
+ * Throws exception if not possible to read or write the file.
+ * Returns true if formatting went ok; returns false if the formatting could not finish
because of errors in the input.
+ *
+ * @param file
+ * @param codeFormatter
+ * @return
+ */
+ public boolean formatFile(File file) throws ExporterException {
+ IDocument doc = new Document();
+ try {
+ String contents = new
String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(file, null));
+ doc.set(contents);
+ TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, contents, 0,
contents.length(), 0, null);
+ if (edit != null) {
+ edit.apply(doc);
+ } else {
+ return false; // most likely syntax errror
+ }
+
+ // write the file
+ final BufferedWriter out = new BufferedWriter(new FileWriter(file));
+ try {
+ out.write(doc.get());
+ out.flush();
+ } finally {
+ try {
+ out.close();
+ } catch (IOException e) {
+ /* ignore */
+ }
+ }
+ return true;
+ } catch (IOException e) {
+ throw new ExporterException("Could not format " + file, e);
+ } catch (BadLocationException e) {
+ throw new ExporterException("Could not format " + file, e);
+ }
+ }
+
+}
Added:
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/JavaFormatterTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/JavaFormatterTest.java 2006-11-08
12:00:23 UTC (rev 10770)
+++
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/JavaFormatterTest.java 2006-11-08
12:00:28 UTC (rev 10771)
@@ -0,0 +1,115 @@
+/*
+ * Created on 13-Feb-2005
+ *
+ */
+package org.hibernate.tool.ant;
+
+import java.io.File;
+import java.util.HashMap;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.eclipse.jdt.core.ToolFactory;
+import org.eclipse.jdt.core.formatter.CodeFormatter;
+import org.hibernate.tool.ide.formatting.JavaFormatter;
+
+/**
+ * @author max
+ *
+ */
+public class JavaFormatterTest extends BuildFileTestCase {
+
+
+
+ public JavaFormatterTest(String name) {
+ super(name);
+ }
+
+ protected void tearDown() throws Exception {
+ executeTarget( "cleanup" );
+ System.out.println(getLog());
+ super.tearDown();
+ }
+ protected void setUp() throws Exception {
+ configureProject("src/testsupport/javaformattest-build.xml");
+ }
+
+ public void testJava() {
+
+ executeTarget("prepare");
+
+ File file = new File(project.getProperty( "build.dir" ),
"formatting/SimpleOne.java");
+ assertFileAndExists( file );
+ long before = file.lastModified();
+
+ JavaFormatter formatter = new JavaFormatter(null);
+ formatter.formatFile( file );
+
+ assertTrue( before!=file.lastModified() );
+
+ }
+
+ public void testJavaJdk5() {
+
+ executeTarget("prepare");
+
+ File file = new File(project.getProperty( "build.dir" ),
"formatting/Simple5One.java");
+ assertFileAndExists( file );
+ long before = file.lastModified();
+
+ JavaFormatter formatter = new JavaFormatter(new HashMap());
+ assertFalse("formatting should fail when using zero settings",
formatter.formatFile( file ));
+
+ assertTrue( before==file.lastModified() );
+
+ executeTarget("prepare");
+ formatter = new JavaFormatter(null);
+ assertTrue("formatting should pass when using default settings",
formatter.formatFile( file ));
+
+ assertTrue( before<file.lastModified() );
+ }
+ public void testAntxDestDir() {
+
+ executeTarget("prepare");
+
+ File file = new File(project.getProperty( "build.dir" ),
"formatting/SimpleOne.java");
+ assertFileAndExists( file );
+ long before = file.lastModified();
+ executeTarget("fileset");
+ System.out.println(getLog());
+ assertTrue( before!=file.lastModified() );
+
+
+ }
+
+ public void testConfig() {
+
+ executeTarget("prepare");
+
+ File jdk5file = new File(project.getProperty( "build.dir" ),
"formatting/Simple5One.java");
+ File jdkfile = new File(project.getProperty( "build.dir" ),
"formatting/SimpleOne.java");
+ assertFileAndExists( jdkfile );
+ long jdk5before = jdk5file.lastModified();
+ long before = jdkfile.lastModified();
+
+ executeTarget("configtest");
+ System.out.println(getLog());
+ assertEquals("jdk5 should fail since config is not specifying
jdk5",jdk5before, jdk5file.lastModified() );
+ assertTrue(before<jdkfile.lastModified());
+
+ executeTarget("noconfigtest");
+ System.out.println(getLog());
+ assertTrue(jdk5before<jdk5file.lastModified() );
+ assertTrue(before<jdk5file.lastModified());
+
+
+ }
+
+
+ public static Test suite() {
+ return new TestSuite(JavaFormatterTest.class);
+ }
+
+
+}
Added: branches/Branch_3_2/HibernateExt/tools/src/testsupport/emptyconfig.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/testsupport/emptyconfig.properties 2006-11-08
12:00:23 UTC (rev 10770)
+++
branches/Branch_3_2/HibernateExt/tools/src/testsupport/emptyconfig.properties 2006-11-08
12:00:28 UTC (rev 10771)
@@ -0,0 +1 @@
+## this file is intentionally left blank
\ No newline at end of file
Added: branches/Branch_3_2/HibernateExt/tools/src/testsupport/formatting/Simple5One.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/testsupport/formatting/Simple5One.java 2006-11-08
12:00:23 UTC (rev 10770)
+++
branches/Branch_3_2/HibernateExt/tools/src/testsupport/formatting/Simple5One.java 2006-11-08
12:00:28 UTC (rev 10771)
@@ -0,0 +1,9 @@
+package formatting;
+
+@Override // intentional that his will give compile error.
+public
+ class
+ Simple5One {
+
+
+}
Added: branches/Branch_3_2/HibernateExt/tools/src/testsupport/formatting/SimpleOne.java
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/testsupport/formatting/SimpleOne.java 2006-11-08
12:00:23 UTC (rev 10770)
+++
branches/Branch_3_2/HibernateExt/tools/src/testsupport/formatting/SimpleOne.java 2006-11-08
12:00:28 UTC (rev 10771)
@@ -0,0 +1,8 @@
+package formatting;
+
+public
+ class
+ SimpleOne {
+
+
+}
Added: branches/Branch_3_2/HibernateExt/tools/src/testsupport/javaformattest-build.xml
===================================================================
---
branches/Branch_3_2/HibernateExt/tools/src/testsupport/javaformattest-build.xml 2006-11-08
12:00:23 UTC (rev 10770)
+++
branches/Branch_3_2/HibernateExt/tools/src/testsupport/javaformattest-build.xml 2006-11-08
12:00:28 UTC (rev 10771)
@@ -0,0 +1,68 @@
+<project name="javaformattest">
+ <property name="hibernate-core.jar"
location="../../../../hibernate-3.1/hibernate3.jar" />
+ <property name="hibernate-core.home"
location="../../../../hibernate3" />
+ <property name="hibernate-core.lib.dir"
location="${hibernate-core.home}/lib" />
+ <property name="build.dir" location="../../build/javaformattest"
/>
+ <property file="../../etc/hibernate.properties" />
+
+
+
+ <path id="tasks.classpath">
+ <pathelement path="../../build/eclipse" />
+ <pathelement path="../../build/classes" />
+
+ <fileset dir="${hibernate-core.lib.dir}">
+ <include name="**/*.jar" />
+ </fileset>
+
+ <pathelement location="${hibernate-core.jar}" />
+ <pathelement path="../../lib/freemarker.jar" />
+
+ <fileset dir="../../lib">
+ <include name="**/*eclipse*.jar" />
+ </fileset>
+ <pathelement path="${hibernate-core.home}\jdbc\hsqldb.jar" />
+ </path>
+
+ <target name="cleanup">
+ <delete dir="${build.dir}"/>
+ </target>
+
+ <target name="prepare">
+ <copy todir="${build.dir}">
+ <fileset dir="." includes="formatting/**/*.java"/>
+ </copy>
+ </target>
+
+ <target name="fileset">
+ <taskdef name="javaformatter"
classname="org.hibernate.tool.ant.JavaFormatterTask"
classpathref="tasks.classpath"/>
+
+ <javaformatter> <!-- config, failonerror, destdir -->
+ <fileset dir="${build.dir}">
+ <include name="formatting/**/*.java"/>
+ </fileset>
+ </javaformatter>
+ </target>
+
+ <target name="configtest">
+ <taskdef name="javaformatter"
classname="org.hibernate.tool.ant.JavaFormatterTask"
classpathref="tasks.classpath"/>
+
+ <javaformatter configurationfile="emptyconfig.properties"> <!--
config, failonerror, destdir -->
+ <fileset dir="${build.dir}">
+ <include name="formatting/**/*.java"/>
+ </fileset>
+ </javaformatter>
+ </target>
+
+ <target name="noconfigtest">
+ <taskdef name="javaformatter"
classname="org.hibernate.tool.ant.JavaFormatterTask"
classpathref="tasks.classpath"/>
+
+ <javaformatter> <!-- config, failonerror, destdir -->
+ <fileset dir="${build.dir}">
+ <include name="formatting/**/*.java"/>
+ </fileset>
+ </javaformatter>
+ </target>
+
+
+</project>