|
|
|
|
|
Re: How to access Velocity templates in a plugin [message #525024 is a reply to message #524591] |
Mon, 05 April 2010 12:56 |
Charlie Kelly Messages: 276 Registered: July 2009 |
Senior Member |
|
|
Hi Dave,
See the snippet below.
Remember to include the directory that contains your templates in your
build path (via MANIFEST.MF).
HTH
Charlie
private void initializeVelocity() throws Exception
{
try
{
Plugin sageGeneratorActivator = SageGeneratorActivator.getDefault();
if (null == sageGeneratorActivator)
throw new Exception ("generatorActivator is null");
Bundle uiGeneratorBundle = sageGeneratorActivator.getBundle();
//////////////////////////////////////////////////////////// ////
// retrieve "static" properties for initializing velocity engine
//////////////////////////////////////////////////////////// ////
String propertiesFileName = "properties/velocity.properties";
Path propertiesPath = new Path(propertiesFileName);
URL propertiesURL = FileLocator.find(uiGeneratorBundle,
propertiesPath, null);
if (null == propertiesURL)
throw new Exception ("URL is null for: " + propertiesFileName);
boolean substituteArgumentsFlag = false;
InputStream propertiesInputStream =
FileLocator.openStream(uiGeneratorBundle, propertiesPath,
substituteArgumentsFlag);
Properties velocityProperties = new Properties();
velocityProperties.load(propertiesInputStream);
//////////////////////////////////////////////////////////// ////
// find the location on local file system where Bundle is located
// add this to velocity properties
//////////////////////////////////////////////////////////// ////
String velocityPathKey = "file.resource.loader.path";
Path templatesPath = new Path ("templates");
URL templatesURL = FileLocator.find(uiGeneratorBundle,
templatesPath, null);
if (null == templatesURL)
throw new Exception ("templatesURL is null for: " + "templates", null);
URL tempURL = FileLocator.toFileURL(templatesURL);
String tempValue = tempURL.toExternalForm();
String velocityPathValue = null;
if (tempValue.startsWith("file:/"))
{
velocityPathValue = tempValue.substring(6);
}// if (tempValue.startsWith("file:/"))
else
velocityPathValue = tempValue;
//////////////////////////////////////////////////////////// ////
// use the properties to initialize velocity
//////////////////////////////////////////////////////////// ////
velocityProperties.put(velocityPathKey, velocityPathValue);
velocityEngine = new VelocityEngine();
velocityEngine.init(velocityProperties);
// tempTest();
}// try
catch (Exception e)
{
throw new Exception(e); // trap for debugging
}// catch (Exception e)
}// private void initializeVelocity() throws Exception
//////////////////////////////////////////////////////////// ////////////////
Dave Derry wrote:
> I'm creating a small RCP app to gain some understanding of the
> architecture and functionality. The application provides a way to easily
> generate a configuration file for parsing a specialized flat file used
> in one of our enterprise applications. After generating the
> configuration, a standalone application is executed against it to
> generate a java module consisting of the java entity classes and a
> driver class for parsing the file. This step uses Velocity for
> generating the necessary files.
>
> I have modified the generation application to build an OSGi bundle, and
> have added a Command to the main menu for executing the generation
> class. It all works fine until it actually attempts to populate a
> template to create a file. Then I get the "
> org.apache.velocity.exception.ResourceNotFoundException: Unable to find
> resource" error. I have tried everything I can think of; making sure
> that the templates are in a package that is Imported; placing the
> templates into a separate bundle which is listed as a Required-Bundle,
> etc. But I continue to get this error.
>
> What am I doing wrong?
>
> TIA,
> Dave
|
|
|
Re: How to access Velocity templates in a plugin [message #525025 is a reply to message #525024] |
Mon, 05 April 2010 13:01 |
Charlie Kelly Messages: 276 Registered: July 2009 |
Senior Member |
|
|
Hi Dave,
You might also need the following:
Charlie
private void initialize(String templateString) throws Exception
{
initializeVelocity();
velocityContext = new VelocityContext();
template = velocityEngine.getTemplate(templateString);
}// private void initialize(String templateString) throws Exception
//////////////////////////////////////////////////////////// ////////////////
exquisitus wrote:
> Hi Dave,
>
> See the snippet below.
> Remember to include the directory that contains your templates in your
> build path (via MANIFEST.MF).
>
> HTH
>
> Charlie
>
>
>
> private void initializeVelocity() throws Exception
> {
> try
> {
> Plugin sageGeneratorActivator =
> SageGeneratorActivator.getDefault();
> if (null == sageGeneratorActivator)
> throw new Exception ("generatorActivator is null");
> Bundle uiGeneratorBundle = sageGeneratorActivator.getBundle();
>
> //////////////////////////////////////////////////////////// ////
> // retrieve "static" properties for initializing
> velocity engine
>
> //////////////////////////////////////////////////////////// ////
> String propertiesFileName = "properties/velocity.properties";
> Path propertiesPath = new Path(propertiesFileName);
> URL propertiesURL = FileLocator.find(uiGeneratorBundle,
> propertiesPath, null);
> if (null == propertiesURL)
> throw new Exception ("URL is null for: " +
> propertiesFileName);
> boolean substituteArgumentsFlag = false;
> InputStream propertiesInputStream =
> FileLocator.openStream(uiGeneratorBundle,
> propertiesPath, substituteArgumentsFlag);
> Properties velocityProperties = new Properties();
> velocityProperties.load(propertiesInputStream);
>
> //////////////////////////////////////////////////////////// ////
> // find the location on local file system where Bundle
> is located
> // add this to velocity properties
>
> //////////////////////////////////////////////////////////// ////
> String velocityPathKey = "file.resource.loader.path";
> Path templatesPath = new Path ("templates");
> URL templatesURL = FileLocator.find(uiGeneratorBundle,
> templatesPath, null);
> if (null == templatesURL)
> throw new Exception ("templatesURL is null for: " +
> "templates", null);
>
> URL tempURL = FileLocator.toFileURL(templatesURL);
> String tempValue = tempURL.toExternalForm();
> String velocityPathValue = null;
> if (tempValue.startsWith("file:/"))
> {
> velocityPathValue = tempValue.substring(6);
> }// if (tempValue.startsWith("file:/"))
> else
> velocityPathValue = tempValue;
>
> //////////////////////////////////////////////////////////// ////
> // use the properties to initialize velocity
>
> //////////////////////////////////////////////////////////// ////
> velocityProperties.put(velocityPathKey, velocityPathValue);
> velocityEngine = new VelocityEngine();
> velocityEngine.init(velocityProperties);
>
> // tempTest();
> }// try
> catch (Exception e)
> {
> throw new Exception(e); // trap for debugging
> }// catch (Exception e)
> }// private void initializeVelocity() throws Exception
> //////////////////////////////////////////////////////////// ////////////////
>
>
>
> Dave Derry wrote:
>> I'm creating a small RCP app to gain some understanding of the
>> architecture and functionality. The application provides a way to
>> easily generate a configuration file for parsing a specialized flat
>> file used in one of our enterprise applications. After generating the
>> configuration, a standalone application is executed against it to
>> generate a java module consisting of the java entity classes and a
>> driver class for parsing the file. This step uses Velocity for
>> generating the necessary files.
>>
>> I have modified the generation application to build an OSGi bundle,
>> and have added a Command to the main menu for executing the generation
>> class. It all works fine until it actually attempts to populate a
>> template to create a file. Then I get the "
>> org.apache.velocity.exception.ResourceNotFoundException: Unable to
>> find resource" error. I have tried everything I can think of; making
>> sure that the templates are in a package that is Imported; placing the
>> templates into a separate bundle which is listed as a Required-Bundle,
>> etc. But I continue to get this error.
>>
>> What am I doing wrong?
>>
>> TIA,
>> Dave
|
|
|
|
Powered by
FUDForum. Page generated in 0.03745 seconds