velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henn...@apache.org
Subject svn commit: r595370 - in /velocity/texen/trunk/src/java/org/apache/texen: Generator.java Texen.java ant/TexenTask.java generator/ generator/GeneratorFactory.java generator/TexenGenerator.java util/PropertiesUtil.java
Date Thu, 15 Nov 2007 17:22:44 GMT
Author: henning
Date: Thu Nov 15 09:22:34 2007
New Revision: 595370

URL: http://svn.apache.org/viewvc?rev=595370&view=rev
Log:
Pull out the whole static generator stuff and replace it with
a factory. You can now set the class of the generator to use right
from the ant task or the Texen class itself. By default it uses the
regular Generator.


Added:
    velocity/texen/trunk/src/java/org/apache/texen/generator/
    velocity/texen/trunk/src/java/org/apache/texen/generator/GeneratorFactory.java   (with
props)
    velocity/texen/trunk/src/java/org/apache/texen/generator/TexenGenerator.java   (with props)
Modified:
    velocity/texen/trunk/src/java/org/apache/texen/Generator.java
    velocity/texen/trunk/src/java/org/apache/texen/Texen.java
    velocity/texen/trunk/src/java/org/apache/texen/ant/TexenTask.java
    velocity/texen/trunk/src/java/org/apache/texen/util/PropertiesUtil.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=595370&r1=595369&r2=595370&view=diff
==============================================================================
--- velocity/texen/trunk/src/java/org/apache/texen/Generator.java (original)
+++ velocity/texen/trunk/src/java/org/apache/texen/Generator.java Thu Nov 15 09:22:34 2007
@@ -39,6 +39,8 @@
 
 import org.apache.commons.collections.map.AbstractLinkedMap;
 import org.apache.commons.collections.map.LRUMap;
+import org.apache.texen.generator.GeneratorFactory;
+import org.apache.texen.generator.TexenGenerator;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
@@ -53,6 +55,7 @@
  * @version $Id$
  */
 public class Generator
+    implements TexenGenerator
 {
     /**
      * Where the texen output will placed.
@@ -101,13 +104,6 @@
      private Set writtenFilenames = new HashSet();
 
     /**
-     * The generator tools used for creating additional
-     * output withing the control template. This could
-     * use some cleaning up.
-     */
-    private static Generator instance;
-
-    /**
      * This is the encoding for the output file(s).
      */
     protected String outputEncoding;
@@ -126,31 +122,12 @@
     /**
      * Default constructor.
      */
-    protected Generator()
+    public Generator()
     {
-        if (Generator.instance != null)
-        {
-            throw new IllegalStateException("Generator.instance is already initialized!");
-        }
-        Generator.instance = this;
         setDefaultProps();
     }
 
     /**
-     * Create a new generator object with default properties.
-     *
-     * @return Generator generator used in the control context.
-     */
-    public synchronized static Generator getInstance()
-    {
-        if (Generator.instance == null)
-        {
-            Generator.instance = new Generator();
-        }
-        return Generator.instance;
-    }
-
-    /**
      * Set the velocity engine.
      * @param ve
      */
@@ -170,6 +147,7 @@
     public Generator (String propFile)
     {
         this();
+
         try
         {
             BufferedInputStream bi = null;
@@ -207,6 +185,7 @@
     public Generator (Properties props)
     {
         this();
+
         this.props = (Properties) props.clone();
     }
 
@@ -514,7 +493,7 @@
      */
     protected void fillContextDefaults (Context context)
     {
-        context.put ("generator", Generator.instance);
+        context.put ("generator", GeneratorFactory.getInstance().getGenerator());
         context.put ("outputDirectory", getOutputPath());
     }
 

Modified: velocity/texen/trunk/src/java/org/apache/texen/Texen.java
URL: http://svn.apache.org/viewvc/velocity/texen/trunk/src/java/org/apache/texen/Texen.java?rev=595370&r1=595369&r2=595370&view=diff
==============================================================================
--- velocity/texen/trunk/src/java/org/apache/texen/Texen.java (original)
+++ velocity/texen/trunk/src/java/org/apache/texen/Texen.java Thu Nov 15 09:22:34 2007
@@ -29,6 +29,8 @@
 
 import org.apache.commons.collections.set.ListOrderedSet;
 import org.apache.commons.lang.StringUtils;
+import org.apache.texen.generator.GeneratorFactory;
+import org.apache.texen.generator.TexenGenerator;
 import org.apache.tools.ant.BuildException;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
@@ -69,6 +71,8 @@
 
     private Set contributors = ListOrderedSet.decorate(new HashSet());
 
+    private String generatorClassName = null;
+
     private final VelocityEngine ve;
 
     /**
@@ -257,11 +261,33 @@
     }
 
     /**
+     * Returns the class name of the generator to use.
+     *
+     * @return The class name of the generator to use.
+     */
+    public String getGeneratorClassName()
+    {
+        return generatorClassName;
+    }
+
+    /**
+     * Set the class name of the generator to use.
+     *
+     * @param generatorClass The name of the generator class to use. Can be null, then the
default generator is used.
+     */
+    public void setGeneratorClassName(final String generatorClassName)
+    {
+        this.generatorClassName = generatorClassName;
+    }
+
+
+    /**
      * Initialize the Texen engine. Can be overridden by
      * subclasses.
      */
     protected void init() throws Exception
     {
+        GeneratorFactory.getInstance().setGeneratorClassName(getGeneratorClassName());
         setVelocityEngineProperties(ve);
         ve.init();
     }
@@ -279,7 +305,7 @@
         init();
 
         // Create the text generator.
-        Generator generator = Generator.getInstance();
+        TexenGenerator generator = GeneratorFactory.getInstance().getGenerator();
         generator.setVelocityEngine(ve);
         generator.setOutputPath(getOutputDirectory());
         generator.setInputEncoding(getInputEncoding());

Modified: velocity/texen/trunk/src/java/org/apache/texen/ant/TexenTask.java
URL: http://svn.apache.org/viewvc/velocity/texen/trunk/src/java/org/apache/texen/ant/TexenTask.java?rev=595370&r1=595369&r2=595370&view=diff
==============================================================================
--- velocity/texen/trunk/src/java/org/apache/texen/ant/TexenTask.java (original)
+++ velocity/texen/trunk/src/java/org/apache/texen/ant/TexenTask.java Thu Nov 15 09:22:34
2007
@@ -92,6 +92,11 @@
     protected String inputEncoding = "ISO-8859-1";
 
     /**
+     * Class to use for the Texen Generator.
+     */
+    protected String generatorClassName = null;
+
+    /**
      * <p>
      * These are properties that are fed into the
      * initial context from a properties file. This
@@ -243,6 +248,8 @@
         this.logFile = logFile;
     }
 
+
+
     /**
      * Set the context properties that will be
      * fed into the initial context be the
@@ -332,6 +339,25 @@
         this.resourceLoaderModificationCheckInterval = resourceLoaderModificationCheckInterval;
     }
 
+    /**
+     * Returns the class name of the generator to use.
+     *
+     * @return The class name of the generator to use.
+     */
+    public String getGeneratorClassName()
+    {
+        return generatorClassName;
+    }
+
+    /**
+     * Set the class name of the generator to use.
+     *
+     * @param generatorClass The name of the generator class to use. Can be null, then the
default generator is used.
+     */
+    public void setGeneratorClassName(final String generatorClassName)
+    {
+        this.generatorClassName = generatorClassName;
+    }
 
     /**
      * Creates a VelocityContext.
@@ -407,6 +433,7 @@
             texen.setLog(this);
             texen.setControlTemplate(controlTemplate);
             texen.addContributors(Collections.singleton(this));
+            texen.setGeneratorClassName(getGeneratorClassName());
 
             texen.execute();
         }

Added: velocity/texen/trunk/src/java/org/apache/texen/generator/GeneratorFactory.java
URL: http://svn.apache.org/viewvc/velocity/texen/trunk/src/java/org/apache/texen/generator/GeneratorFactory.java?rev=595370&view=auto
==============================================================================
--- velocity/texen/trunk/src/java/org/apache/texen/generator/GeneratorFactory.java (added)
+++ velocity/texen/trunk/src/java/org/apache/texen/generator/GeneratorFactory.java Thu Nov
15 09:22:34 2007
@@ -0,0 +1,97 @@
+package org.apache.texen.generator;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "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.
+ */
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.texen.Generator;
+
+/**
+ * This is a factory to create the Generator objects used for Texen rendering.
+ *
+ * @author <a href="mailto:dev@velocity.apache.org">The Velocity developers community</a>
+ * @version $Id$
+ */
+public class GeneratorFactory
+{
+    public static final GeneratorFactory instance = new GeneratorFactory();
+
+    private String generatorClassName = null;
+
+    private TexenGenerator generatorInstance = null;
+
+    public static GeneratorFactory getInstance()
+    {
+        return instance;
+    }
+
+    private GeneratorFactory()
+    {
+    }
+
+    public TexenGenerator getGenerator()
+    {
+        if (generatorInstance == null)
+        {
+            generatorInstance = getGeneratorInstance();
+        }
+        return generatorInstance;
+    }
+
+    /**
+     * Returns the class name of the generator to use.
+     *
+     * @return The class name of the generator to use.
+     */
+    public String getGeneratorClassName()
+    {
+        if (StringUtils.isEmpty(generatorClassName))
+        {
+            return Generator.class.getName();
+        }
+        return generatorClassName;
+    }
+
+    /**
+     * Set the class name of the generator to use.
+     *
+     * @param generatorClass The name of the generator class to use. Can be null, then the
default generator is used.
+     */
+    public void setGeneratorClassName(String generatorClassName)
+    {
+        this.generatorClassName = generatorClassName;
+    }
+
+    private TexenGenerator getGeneratorInstance()
+    {
+        try
+        {
+            Class generatorClass = Class.forName(getGeneratorClassName());
+            return (TexenGenerator) generatorClass.newInstance();
+        }
+        catch (RuntimeException re)
+        {
+            throw re;
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("While instantiating " + generatorClassName + ":"
,e);
+        }
+    }
+}

Propchange: velocity/texen/trunk/src/java/org/apache/texen/generator/GeneratorFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/texen/trunk/src/java/org/apache/texen/generator/GeneratorFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Added: velocity/texen/trunk/src/java/org/apache/texen/generator/TexenGenerator.java
URL: http://svn.apache.org/viewvc/velocity/texen/trunk/src/java/org/apache/texen/generator/TexenGenerator.java?rev=595370&view=auto
==============================================================================
--- velocity/texen/trunk/src/java/org/apache/texen/generator/TexenGenerator.java (added)
+++ velocity/texen/trunk/src/java/org/apache/texen/generator/TexenGenerator.java Thu Nov 15
09:22:34 2007
@@ -0,0 +1,56 @@
+package org.apache.texen.generator;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "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.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Writer;
+
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
+
+/**
+ * This interface describes a generator used by Texen to generate files. Every generator
+ * must implement this interface.
+ *
+ * @author <a href="mailto:dev@velocity.apache.org">The Velocity developers community</a>
+ * @version $Id$
+ */
+
+public interface TexenGenerator
+{
+    void setVelocityEngine(VelocityEngine ve);
+
+    void setOutputPath(final File outputPath) throws IOException;
+
+    void setInputEncoding(String inputEncoding);
+
+    void setOutputEncoding(String outputEncoding);
+
+    String getTemplatePath();
+
+    void setTemplatePath(String templatePath);
+
+    String parse(String inputTemplate, Context context) throws Exception;
+
+    Writer getWriter(File path, String encoding) throws Exception;
+
+    void shutdown();
+}

Propchange: velocity/texen/trunk/src/java/org/apache/texen/generator/TexenGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/texen/trunk/src/java/org/apache/texen/generator/TexenGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Modified: velocity/texen/trunk/src/java/org/apache/texen/util/PropertiesUtil.java
URL: http://svn.apache.org/viewvc/velocity/texen/trunk/src/java/org/apache/texen/util/PropertiesUtil.java?rev=595370&r1=595369&r2=595370&view=diff
==============================================================================
--- velocity/texen/trunk/src/java/org/apache/texen/util/PropertiesUtil.java (original)
+++ velocity/texen/trunk/src/java/org/apache/texen/util/PropertiesUtil.java Thu Nov 15 09:22:34
2007
@@ -16,7 +16,7 @@
  * "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.e
  */
 
 import java.io.FileInputStream;
@@ -24,7 +24,7 @@
 import java.util.Properties;
 import java.util.StringTokenizer;
 
-import org.apache.texen.Generator;
+import org.apache.texen.generator.GeneratorFactory;
 
 /**
  * A property utility class for the texen text/code generator
@@ -36,6 +36,14 @@
  */
 public class PropertiesUtil
 {
+
+    private final String templatePath;
+
+    public PropertiesUtil()
+    {
+        templatePath = GeneratorFactory.getInstance().getGenerator().getTemplatePath();
+    }
+
     /**
      * Load properties from either a file in the templatePath if there
      * is one or the classPath.
@@ -49,7 +57,6 @@
     {
         Properties properties = null;
 
-        String templatePath = Generator.getInstance().getTemplatePath();
         try
         {
             if (templatePath != null)
@@ -88,10 +95,9 @@
      * @throws Exception
      */
     protected Properties loadFromTemplatePath(final String propertiesFile)
-    	throws Exception
+        throws Exception
     {
         Properties properties = new Properties();
-        String templatePath = Generator.getInstance().getTemplatePath();
 
         // We might have something like the following:
         //
@@ -140,10 +146,10 @@
             }
             finally
             {
-        	if (stream != null)
-        	{
-        	    stream.close();
-        	}
+                if (stream != null)
+                {
+                    stream.close();
+                }
             }
         }
         return properties;
@@ -158,7 +164,7 @@
      * @throws Exception
      */
     protected Properties loadFromClassPath(final String propertiesName)
-    	throws Exception
+        throws Exception
     {
         Properties properties = new Properties();
         ClassLoader classLoader = this.getClass().getClassLoader();
@@ -176,17 +182,17 @@
             // JAR files.
 
             String propertiesFile = propertiesName.startsWith("$generator")
-        	    ? propertiesName.substring("$generator.templatePath/".length())
-    		    : propertiesName;
+                ? propertiesName.substring("$generator.templatePath/".length())
+                : propertiesName;
 
-    	    inputStream = classLoader.getResourceAsStream(propertiesFile);
+            inputStream = classLoader.getResourceAsStream(propertiesFile);
             properties.load(inputStream);
         }
         finally
         {
             if (inputStream != null)
             {
-        	inputStream.close();
+                inputStream.close();
             }
         }
         return properties;



Mime
View raw message