velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henn...@apache.org
Subject svn commit: r595042 - /velocity/texen/trunk/src/java/org/apache/texen/Generator.java
Date Wed, 14 Nov 2007 20:44:33 GMT
Author: henning
Date: Wed Nov 14 12:44:32 2007
New Revision: 595042

URL: http://svn.apache.org/viewvc?rev=595042&view=rev
Log:
Change the setOutputPath method to take a File, fix some minor formatting issues with the
class.

Modified:
    velocity/texen/trunk/src/java/org/apache/texen/Generator.java

Modified: velocity/texen/trunk/src/java/org/apache/texen/Generator.java
URL: http://svn.apache.org/viewvc/velocity/texen/trunk/src/java/org/apache/texen/Generator.java?rev=595042&r1=595041&r2=595042&view=diff
==============================================================================
--- velocity/texen/trunk/src/java/org/apache/texen/Generator.java (original)
+++ velocity/texen/trunk/src/java/org/apache/texen/Generator.java Wed Nov 14 12:44:32 2007
@@ -16,9 +16,27 @@
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
- * under the License.    
+ * under the License.
  */
 
+import java.io.BufferedInputStream;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Properties;
+import java.util.Set;
+
 import org.apache.commons.collections.map.AbstractLinkedMap;
 import org.apache.commons.collections.map.LRUMap;
 import org.apache.velocity.Template;
@@ -27,14 +45,6 @@
 import org.apache.velocity.context.Context;
 import org.apache.velocity.util.ClassUtils;
 
-import java.io.*;
-import java.util.Enumeration;
-import java.util.HashSet;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.Properties;
-import java.util.Set;
-
 /**
  * A text/code generator class
  *
@@ -77,12 +87,12 @@
      * then once then the additional output will be
      * appended to the file instead of overwritting
      * the contents.
-     * 
+     *
      * However, we don't want to keep too many files open,
      * so use this to expire them.
      */
      private LRUMap writers = new FileClosingLRUMap(100);
- 
+
      /**
       * Keep track of every file we ever write to.  If we
       * need to reopen a file we already wrote to, reopen
@@ -118,9 +128,11 @@
      */
     protected Generator()
     {
-				if (instance!=null)
-				    throw new IllegalStateException("Generator.instance!=null");
-	      instance = this;
+        if (Generator.instance != null)
+        {
+            throw new IllegalStateException("Generator.instance is already initialized!");
+        }
+        Generator.instance = this;
         setDefaultProps();
     }
 
@@ -129,11 +141,13 @@
      *
      * @return Generator generator used in the control context.
      */
-    public static Generator getInstance()
+    public synchronized static Generator getInstance()
     {
-	      if (instance==null)
-	          new Generator();
-        return instance;
+        if (Generator.instance == null)
+        {
+            Generator.instance = new Generator();
+        }
+        return Generator.instance;
     }
 
     /**
@@ -155,12 +169,7 @@
      */
     public Generator (String propFile)
     {
-        if (instance!=null)
-        {
-	        throw new IllegalStateException("Generator.instance!=null");
-        }
-        
-        instance = this;
+        this();
         try
         {
             BufferedInputStream bi = null;
@@ -197,12 +206,8 @@
      */
     public Generator (Properties props)
     {
-        if (instance!=null)
-        {
-            throw new IllegalStateException("Generator.instance!=null");
-        }
-		instance = this;
-        this.props = (Properties)props.clone();
+        this();
+        this.props = (Properties) props.clone();
     }
 
     /**
@@ -241,7 +246,7 @@
      *
      * @param templatePath template path for velocity templates.
      */
-    public void setTemplatePath(String templatePath)
+    public void setTemplatePath(final String templatePath)
     {
         props.put(TEMPLATE_PATH, templatePath);
     }
@@ -261,9 +266,10 @@
      * output.
      * @param outputPath
      */
-    public void setOutputPath(String outputPath)
+    public void setOutputPath(final File outputPath)
+        throws IOException
     {
-        props.put(OUTPUT_PATH, outputPath);
+        props.put(OUTPUT_PATH, outputPath.getCanonicalPath());
     }
 
     /**
@@ -303,15 +309,12 @@
      * @return A Writer for this generator.
      * @throws Exception
      */
-    public Writer getWriter(String path, String encoding) throws Exception 
-    {
+    public Writer getWriter(File path, String encoding) throws Exception {
         Writer writer;
+
         boolean seenBefore = writtenFilenames.contains(path);
-        if (    encoding == null || 
-                encoding.length() == 0 || 
-                encoding.equals("8859-1") || 
-                encoding.equals("8859_1")) 
-        {
+
+        if (encoding == null || encoding.length() == 0 || encoding.equals("8859-1") || encoding.equals("8859_1"))
{
             writer = new FileWriter(path, seenBefore);
         }
         else
@@ -425,7 +428,7 @@
                  * a new file writer for it.
                  */
                 writer = getWriter(
-                            getOutputPath() + File.separator + outputFile,
+                            new File(getOutputPath(), outputFile),
                             outputEncoding != null ? outputEncoding : this.outputEncoding
                          );
 
@@ -511,7 +514,7 @@
      */
     protected void fillContextDefaults (Context context)
     {
-        context.put ("generator", instance);
+        context.put ("generator", Generator.instance);
         context.put ("outputDirectory", getOutputPath());
     }
 
@@ -587,7 +590,7 @@
 
 
     private static class FileClosingLRUMap extends LRUMap
-    {        
+    {
         private static final long serialVersionUID = -8944324477458767333L;
 
         public FileClosingLRUMap(int i)
@@ -595,30 +598,31 @@
             super(i);
         }
 
-        protected boolean removeLRU(AbstractLinkedMap.LinkEntry entry) 
+        protected boolean removeLRU(AbstractLinkedMap.LinkEntry entry)
         {
             Writer writer = (Writer) entry.getValue();
-            try
-            {
-                writer.flush();
-            }
-            catch (IOException e)
-            {
-                /* do nothing */
-            }
 
-            try
+            if (writer != null)
             {
-                writer.close();
-            }
-            catch (IOException e)
-            {
-                /* do nothing */
+                try
+                {
+                    writer.flush();
+                }
+                catch (IOException e)
+                {
+                    /* do nothing */
+                }
+
+                try
+                {
+                    writer.close();
+                }
+                catch (IOException e)
+                {
+                    /* do nothing */
+                }
             }
             return true;
         }
     }
-
 }
-
-



Mime
View raw message