Logo Search packages:      
Sourcecode: velocity version File versions  Download package

void org::apache::velocity::texen::ant::TexenTask::execute (  )  throws BuildException [inline]

Execute the input script with Velocity

Exceptions:
BuildException BuildExceptions are thrown when required attributes are missing. Exceptions thrown by Velocity are rethrown as BuildExceptions.

Definition at line 358 of file TexenTask.java.

References org::apache::velocity::app::VelocityEngine::addProperty(), cleanup(), contextProperties, controlTemplate, ERR_MSG_FRAGMENT, org::apache::velocity::runtime::RuntimeConstants::FILE_RESOURCE_LOADER_PATH, org::apache::velocity::exception::MethodInvocationException::getMethodName(), org::apache::velocity::exception::MethodInvocationException::getReferenceName(), org::apache::velocity::exception::MethodInvocationException::getWrappedThrowable(), org::apache::velocity::texen::Generator::getWriter(), org::apache::velocity::app::VelocityEngine::init(), initControlContext(), inputEncoding, outputDirectory, outputEncoding, outputFile, org::apache::velocity::texen::Generator::parse(), populateInitialContext(), org::apache::velocity::context::Context::put(), org::apache::velocity::texen::Generator::setInputEncoding(), org::apache::velocity::texen::Generator::setOutputEncoding(), org::apache::velocity::texen::Generator::setOutputPath(), org::apache::velocity::app::VelocityEngine::setProperty(), org::apache::velocity::texen::Generator::setTemplatePath(), org::apache::velocity::texen::Generator::setVelocityEngine(), org::apache::velocity::texen::Generator::shutdown(), templatePath, and useClasspath.

    {
        // Make sure the template path is set.
        if (templatePath == null && useClasspath == false)
        {
            throw new BuildException(
                "The template path needs to be defined if you are not using " +
                "the classpath for locating templates!");
        }            
    
        // Make sure the control template is set.
        if (controlTemplate == null)
        {
            throw new BuildException("The control template needs to be defined!");
        }            

        // Make sure the output directory is set.
        if (outputDirectory == null)
        {
            throw new BuildException("The output directory needs to be defined!");
        }            
        
        // Make sure there is an output file.
        if (outputFile == null)
        {
            throw new BuildException("The output file needs to be defined!");
        }            
        
        VelocityEngine ve = new VelocityEngine();
        
        try
        {
            // Setup the Velocity Runtime.
            if (templatePath != null)
            {
                  log("Using templatePath: " + templatePath, project.MSG_VERBOSE);
                ve.setProperty(
                    ve.FILE_RESOURCE_LOADER_PATH, templatePath);
            }
            
            if (useClasspath)
            {
                  log("Using classpath");
                ve.addProperty(
                    VelocityEngine.RESOURCE_LOADER, "classpath");
            
                ve.setProperty(
                    "classpath." + VelocityEngine.RESOURCE_LOADER + ".class",
                        "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

                ve.setProperty(
                    "classpath." + VelocityEngine.RESOURCE_LOADER + 
                        ".cache", "false");

                ve.setProperty(
                    "classpath." + VelocityEngine.RESOURCE_LOADER + 
                        ".modificationCheckInterval", "2");
            }
            
            ve.init();

            // Create the text generator.
            Generator generator = Generator.getInstance();
            generator.setVelocityEngine(ve);
            generator.setOutputPath(outputDirectory);
            generator.setInputEncoding(inputEncoding);
            generator.setOutputEncoding(outputEncoding);

            if (templatePath != null)
            {
                generator.setTemplatePath(templatePath);
            }
            
            // Make sure the output directory exists, if it doesn't
            // then create it.
            File file = new File(outputDirectory);
            if (! file.exists())
            {
                file.mkdirs();
            }
            
            String path = outputDirectory + File.separator + outputFile;
            log("Generating to file " + path, project.MSG_INFO);
            Writer writer = generator.getWriter(path, outputEncoding);
            
            // The generator and the output path should
            // be placed in the init context here and
            // not in the generator class itself.
            Context c = initControlContext();
            
            // Everything in the generator class should be
            // pulled out and placed in here. What the generator
            // class does can probably be added to the Velocity
            // class and the generator class can probably
            // be removed all together.
            populateInitialContext(c);
            
            // Feed all the options into the initial
            // control context so they are available
            // in the control/worker templates.
            if (contextProperties != null)
            {
                Iterator i = contextProperties.getKeys();
        
                while (i.hasNext())
                {
                    String property = (String) i.next();
                    String value = contextProperties.getString(property);
                    
                    // Now lets quickly check to see if what
                    // we have is numeric and try to put it
                    // into the context as an Integer.
                    try
                    {
                        c.put(property, new Integer(value)); 
                    }
                    catch (NumberFormatException nfe)
                    {
                        // Now we will try to place the value into
                        // the context as a boolean value if it
                        // maps to a valid boolean value.
                        String booleanString = 
                            contextProperties.testBoolean(value);
                        
                        if (booleanString != null)
                        {    
                            c.put(property, new Boolean(booleanString));
                        }
                        else
                        {
                            // We are going to do something special
                            // for properties that have a "file.contents"
                            // suffix: for these properties will pull
                            // in the contents of the file and make
                            // them available in the context. So for
                            // a line like the following in a properties file:
                            //
                            // license.file.contents = license.txt
                            //
                            // We will pull in the contents of license.txt
                            // and make it available in the context as
                            // $license. This should make texen a little
                            // more flexible.
                            if (property.endsWith("file.contents"))
                            {
                                // We need to turn the license file from relative to
                                // absolute, and let Ant help :)
                                value = StringUtils.fileContentsToString(   
                                    project.resolveFile(value).getCanonicalPath());
                            
                                property = property.substring(
                                    0, property.indexOf("file.contents") - 1);
                            }
                        
                            c.put(property, value);
                        }
                    }
                }
            }
            
            writer.write(generator.parse(controlTemplate, c));
            writer.flush();
            writer.close();
            generator.shutdown();
            cleanup();
        }
        catch( BuildException e)
        {
            throw e;
        }
        catch( MethodInvocationException e )
        {
            throw new BuildException(
                "Exception thrown by '" + e.getReferenceName() + "." + 
                    e.getMethodName() +"'" + ERR_MSG_FRAGMENT,
                        e.getWrappedThrowable());
        }       
        catch( ParseErrorException e )
        {
            throw new BuildException("Velocity syntax error" + ERR_MSG_FRAGMENT ,e);
        }        
        catch( ResourceNotFoundException e )
        {
            throw new BuildException("Resource not found" + ERR_MSG_FRAGMENT,e);
        }
        catch( Exception e )
        {
            throw new BuildException("Generation failed" + ERR_MSG_FRAGMENT ,e);
        }
    }


Generated by  Doxygen 1.6.0   Back to index