Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Added custom namespace definition for (spring-template-1.2.xsd) the…

… associated entry in NamespaceParserRegistry

- Added documentation for the spring template nvelocity implementation
  • Loading branch information...
commit c75ee591401cbcbf32ea7839e5beaef7d67b8706 1 parent 3c7d8ea
@ezbz ezbz authored
View
24 Spring.Net.2008.sln
@@ -63,6 +63,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.Data.NHibernate21.Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.Data.NHibernate21.2008", "src\Spring\Spring.Data.NHibernate21\Spring.Data.NHibernate21.2008.csproj", "{E57B4652-0231-49CB-B058-87E10EFE540D}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.Template.Velocity.2008", "src\Spring\Spring.Template.Velocity\Spring.Template.Velocity.2008.csproj", "{BF3AB954-8375-407C-9E98-4C51D8072784}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spring.Template.Velocity.Tests", "test\Spring\Spring.Template.Velocity.Tests\Spring.Template.Velocity.Tests.csproj", "{10405837-CB67-40D8-9326-84C9383983E2}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|.NET = Debug|.NET
@@ -377,6 +381,26 @@ Global
{E57B4652-0231-49CB-B058-87E10EFE540D}.Release|Any CPU.Build.0 = Release|Any CPU
{E57B4652-0231-49CB-B058-87E10EFE540D}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{E57B4652-0231-49CB-B058-87E10EFE540D}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Debug|.NET.ActiveCfg = Debug|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Release|.NET.ActiveCfg = Release|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Release|Any CPU.Build.0 = Release|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {BF3AB954-8375-407C-9E98-4C51D8072784}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Debug|.NET.ActiveCfg = Debug|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Release|.NET.ActiveCfg = Release|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Release|Any CPU.Build.0 = Release|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {10405837-CB67-40D8-9326-84C9383983E2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
View
15 doc/reference/src/index.xml
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE book [
<!ENTITY aop SYSTEM "aop.xml">
<!ENTITY aop-aspect-library SYSTEM "aop-aspect-library.xml">
@@ -17,6 +17,7 @@
<!ENTITY messaging SYSTEM "messaging.xml">
<!ENTITY msmq SYSTEM "msmq.xml">
<!ENTITY scheduling SYSTEM "scheduling.xml">
+ <!ENTITY templating SYSTEM "templating.xml">
<!ENTITY web SYSTEM "web.xml">
<!ENTITY ajax SYSTEM "ajax.xml">
<!ENTITY services SYSTEM "services.xml">
@@ -62,7 +63,7 @@
<title>The Spring.NET Framework</title>
<subtitle>Reference Documentation</subtitle>
<releaseinfo>Version 1.2.1</releaseinfo>
- <pubdate>Last Updated April xx, 2009 <ulink url="http://www.springframework.net/doc-latest/reference/html/index.html">(Latest documentation)</ulink></pubdate>
+ <pubdate>Last Updated July xx, 2009 <ulink url="http://www.springframework.net/doc-latest/reference/html/index.html">(Latest documentation)</ulink></pubdate>
<authorgroup>
<author>
<firstname>Mark</firstname>
@@ -105,6 +106,10 @@
<surname>Rim</surname>
</author>
<author>
+ <firstname>Erez</firstname>
+ <surname>Mazor</surname>
+ </author>
+ <author>
<firstname>The Spring</firstname>
<surname>Java Team</surname>
</author>
@@ -328,11 +333,17 @@
<xref linkend="scheduling"/>
</para>
</listitem>
+ <listitem>
+ <para>
+ <xref linkend="templating"/>
+ </para>
+ </listitem>
</itemizedlist>
</partintro>
&messaging;
&msmq;
&scheduling;
+ &templating;
</part>
<part xml:id="spring-vsnet">
<title>VS.NET Integration</title>
View
223 doc/reference/src/templating.xml
@@ -0,0 +1,223 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+/*
+ * Copyright 2002-2008 the original author or authors.
+ *
+ * Licensed 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.
+ */
+-->
+<chapter version="5" xml:id="templating" xmlns="http://docbook.org/ns/docbook"
+ xmlns:ns5="http://www.w3.org/1999/xhtml"
+ xmlns:ns42="http://www.w3.org/2000/svg"
+ xmlns:ns4="http://www.w3.org/1999/xlink"
+ xmlns:ns3="http://www.w3.org/1998/Math/MathML"
+ xmlns:ns="http://docbook.org/ns/docbook">
+ <title>Template Engine Support</title>
+
+ <section xml:id="templating-introduction">
+ <title>Introduction</title>
+
+ <para>The Spring Framework features integration classes for templating
+ engine support. Currently, Spring supports the <ulink
+ url="http://www.castleproject.org/others/nvelocity/index.html">NVelocity</ulink>
+ templating engine.</para>
+ </section>
+
+ <section xml:id="templating-nvelocity-dependencies">
+ <title>Dependencies</title>
+
+ <para>The Spring NVelocity support depends on the Castle project's
+ NVelocity implementation which is provided in the lib directory of the
+ spring release.</para>
+ </section>
+
+ <section xml:id="templating-nvelocity-factory">
+ <title>Using the NVelocity Factory Object</title>
+
+ <para>The NVelocity template engine is set up using a
+ <literal>IFactoryObject</literal> with optional configuration parameters
+ to define where templates reside, define logging and more.</para>
+
+ <section xml:id="templating-nvelocity-file">
+ <title>Simple file based template engine definition</title>
+
+ <para>A simple definition of the template engine:</para>
+
+ <programlisting language="myxml">&lt;!-- Simple no arg file based configuration use's NVeclocity default file resource loader --&gt;
+&lt;object id="velocityEngine" type="Spring.Template.Velocity.VelocityEngineFactoryObject, Spring.Template.Velocity" /&gt;</programlisting>
+
+ <para>The velocity engine could then be used to load and merge a local
+ template using a simple relative path:</para>
+
+ <programlisting language="csharp">StringWriter stringWriter = new StringWriter();
+Hashtable modelTable = new Hashtable();
+modelTable.Add("var1", TEST_VALUE);
+VelocityContext velocityContext = new VelocityContext(modelTable);
+velocityEngine.MergeTemplate("Template/Velocity/MyTemplate.vm", Encoding.UTF8.WebName, velocityContext, stringWriter);
+string mergedContent = stringWriter.ToString();</programlisting>
+ </section>
+
+ <section xml:id="templating-nvelocity-assembly">
+ <title>Assembly based template loading</title>
+
+ <para>When templates are packaged in an assembly, NVelocity's assembly
+ resource loader can be used to define where templates reside:</para>
+
+ <programlisting language="myxml">&lt;!-- Assembly based template loading with NVelocity assembly resource loader --&gt;
+&lt;object id="assemblyBasedVelocityEngine" type="Spring.Template.Velocity.VelocityEngineFactoryObject, Spring.Template.Velocity"&gt;
+ &lt;property name="VelocityProperties"&gt;
+ &lt;dictionary key-type="string" value-type="object"&gt;
+ &lt;entry key="resource.loader" value="assembly"/&gt;
+ &lt;entry key="assembly.resource.loader.class" value="NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader"/&gt;
+ &lt;entry key="assembly.resource.loader.assembly" value="MyAssembly"/&gt;
+ &lt;/dictionary&gt;
+ &lt;/property&gt;
+&lt;/object&gt;</programlisting>
+
+ <para>Using the example above the template would be loaded using a
+ namespace syntax for the template resource:</para>
+
+ <programlisting language="csharp">velocityEngine.MergeTemplate("MyAssembly.MyNamespace.MyTemplate.vm", Encoding.UTF8.WebName, velocityContext, stringWriter);</programlisting>
+
+ <para>Using the custom namespace the same definition could be
+ simplified:</para>
+
+ <programlisting language="myxml">&lt;template:nvelocity id="velocityEngine" &gt;
+ &lt;template:resource-loader&gt;
+ &lt;template:assembly name="MyAssembly" /&gt;
+ &lt;/template:resource-loader&gt;
+&lt;/template:nvelocity&gt;</programlisting>
+ </section>
+
+ <section xml:id="templating-nvelocity-resource-loader">
+ <title>Using Spring's <literal>IResourceLoader</literal> to load
+ templates</title>
+
+ <para>In some cases Spring's resource abstraction can be beneficial to
+ load templates from a variety of resources. A spring resource loader
+ extension to the NVelocity resource loader implementation is provided
+ for this use case.</para>
+
+ <programlisting language="myxml">&lt;object id="velocityEngine" type="Spring.Template.Velocity.VelocityEngineFactoryObject, Spring.Template.Velocity" &gt;
+ &lt;property name="ResourceLoaderPath" value="file://MyTemplateFolder/AnotherFolder/" /&gt;
+&lt;/object&gt;
+</programlisting>
+
+ <para>Or with multiple locations</para>
+
+ <programlisting language="myxml">&lt;object id="velocityEngine" type="Spring.Template.Velocity.VelocityEngineFactoryObject, Spring.Template.Velocity" &gt;
+ &lt;property name="ResourceLoaderPaths" &gt;
+ &lt;list&gt;
+ &lt;value&gt;file://MyTemplateFolder/&lt;/value&gt;
+ &lt;value&gt;file://MyOtherTemplateFolder/&lt;/value&gt;
+ &lt;/list&gt;
+ &lt;/property&gt;
+&lt;/object&gt;</programlisting>
+
+ <note>
+ <para>By default spring will attempt to load resources using file
+ based template loading (useful for detection of template changes at
+ runtime). If this is not desirable you set the
+ <literal>preferFileSystemAccess</literal> property of the factory
+ object to <literal>false</literal>
+ (<literal>prefer-file-system-access="false"</literal> for custom
+ namespace use)</para>
+ </note>
+
+ <para>Using the example above when resource loader paths are defined
+ templates can be loaded using their name:</para>
+
+ <programlisting language="csharp">string mergedTemplate = VelocityEngineUtils.MergeTemplateIntostring(velocityEngine, "MyTemplate.vm", Encoding.UTF8.WebName, model);</programlisting>
+ </section>
+
+ <section xml:id="templating-nvelocity-resource-config">
+ <title>Using a custom configuration file</title>
+
+ <para>If so desired one could provide a custom configuration resource to
+ customize the NVelocity configuration:</para>
+
+ <programlisting language="myxml">&lt;object id="velocityEngine" type="Spring.Template.Velocity.VelocityEngineFactoryObject, Spring.Template.Velocity" &gt;
+ &lt;property name="ConfigLocation " value="file://Template/Velocity/config.properties" /&gt;
+&lt;/object&gt;</programlisting>
+
+ <para>
+ <note>
+ <para>You can override specific properties by providing the <literal>VelocityProperties</literal> property to the NVelocity factory object (shown above)</para>
+ </note>
+ </para>
+ </section>
+
+ <section xml:id="templating-nvelocity-resource-logging">
+
+
+ <title>Logging</title>
+
+
+
+ <para>By default Spring will override NVelocity's default
+ <literal>ILogSystem</literal> implementation with its own
+ <literal>CommonsLoggingLogSystem</literal> implementation. If this is
+ not desirable, you can specify the following property of the NVelocity
+ factory object:</para>
+
+
+
+ <programlisting language="myxml">&lt;object id="velocityEngine" type="Spring.Template.Velocity.VelocityEngineFactoryObject, Spring.Template.Velocity" &gt;
+ &lt;property name="OverrideLogging" value="false" /&gt;
+&lt;/object&gt;</programlisting>
+
+ or
+
+ <programlisting language="myxml">&lt;template:nvelocity id="velocityEngine" override-logging="false" /&gt;
+</programlisting>
+
+
+
+ <note>
+ <para>You can override specific NVelocity properties locally by
+ providing a dictionary as the <literal>VelocityProperties</literal>
+ property of the NVelocity factory object (shown above)</para>
+ </note>
+
+
+ </section>
+ </section>
+
+ <section xml:id="templating-nvelocity-resource-merging">
+ <title>Merging a template</title>
+
+ <para>Spring provides the <literal>VelocityEngineUtils</literal> utility
+ for merging templates using an engine instance:</para>
+
+ <programlisting language="myxml">string mergedTemplate = VelocityEngineUtils.MergeTemplateIntostring(velocityEngine, "MyTemplate.vm", Encoding.UTF8.WebName, model);</programlisting>
+ </section>
+
+ <section xml:id="templating-nvelocity-resource-namespace">
+ <title>Namespace</title>
+
+ <para>For convinience in defining NVelocity engine instances a custom
+ namespace is provided, for example the resource loader definition could be
+ done this way:</para>
+
+ <programlisting language="myxml">
+&lt;objects xmlns="http://www.springframework.net" xmlns:template="http://www.springframework.net/template"&gt;
+
+&lt;template:nvelocity id="customNamespaceVelocityTemplate" &gt;
+ &lt;template:resource-loader&gt;
+ &lt;template:file path="Template/Velocity/" /&gt;
+ &lt;/template:resource-loader&gt;
+&lt;/template:nvelocity&gt;
+
+&lt;/objects&gt;</programlisting>
+ </section>
+</chapter>
View
1  src/Spring/Spring.Core/Objects/Factory/Xml/NamespaceParserRegistry.cs
@@ -102,6 +102,7 @@ static NamespaceParserRegistry()
wellknownNamespaceParserTypeNames["http://www.springframework.net/nms"] = "Spring.Messaging.Nms.Config.NmsNamespaceParser, Spring.Messaging.Nms";
wellknownNamespaceParserTypeNames["http://www.springframework.net/ems"] = "Spring.Messaging.Ems.Config.EmsNamespaceParser, Spring.Messaging.Ems";
wellknownNamespaceParserTypeNames["http://www.springframework.net/validation"] = "Spring.Validation.Config.ValidationNamespaceParser, Spring.Core";
+ wellknownNamespaceParserTypeNames["http://www.springframework.net/template"] = "Spring.Template.Config.TemplateNamespaceParser, Spring.Template.Velocity";
Reset();
}
View
5 src/Spring/Spring.Template.Velocity/Spring.Template.Velocity.2008.csproj
@@ -58,6 +58,7 @@
<HintPath>..\..\..\lib\Net\2.0\NVelocity.dll</HintPath>
</Reference>
<Reference Include="System" />
+ <Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Spring.Core\Spring.Core.2008.csproj">
@@ -66,6 +67,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
+ <Compile Include="Template\Config\TemplateNamespaceParser.cs" />
<Compile Include="Template\Velocity\CommonsLoggingLogSystem.cs" />
<Compile Include="Template\Velocity\SpringResourceLoader.cs" />
<Compile Include="Template\Velocity\VelocityEngineFactory.cs" />
@@ -99,6 +101,9 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
+ <ItemGroup>
+ <EmbeddedResource Include="Template\Config\spring-template-1.2.xsd" />
+ </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
View
192 src/Spring/Spring.Template.Velocity/Template/Config/TemplateNamespaceParser.cs
@@ -0,0 +1,192 @@
+#region License
+
+/*
+ * Copyright 2002-2004 the original author or authors.
+ *
+ * Licensed 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.
+ */
+
+#endregion
+
+#region Imports
+
+using System;
+using System.Collections.Generic;
+using System.Xml;
+using NVelocity.Runtime;
+using Spring.Core.TypeResolution;
+using Spring.Objects.Factory;
+using Spring.Objects.Factory.Support;
+using Spring.Objects.Factory.Xml;
+using Spring.Template.Velocity;
+using Spring.Util;
+
+#endregion
+
+namespace Spring.Template.Config {
+ /// <summary>
+ /// Implementation of the custom configuration parser for template configurations
+ /// </summary>
+ /// <author>Erez Mazor</author>
+ [
+ NamespaceParser(
+ Namespace = "http://www.springframework.net/template",
+ SchemaLocationAssemblyHint = typeof(TemplateNamespaceParser),
+ SchemaLocation = "/Spring.Template.Config/spring-template-1.2.xsd")
+ ]
+ public sealed class TemplateNamespaceParser : AbstractSingleObjectDefinitionParser {
+ private const string TemplateTypePrefix = "template: ";
+
+ static TemplateNamespaceParser() {
+ TypeRegistry.RegisterType(TemplateTypePrefix + TemplateDefinitionConstants.NVelocityElement, typeof(VelocityEngineFactoryObject));
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="TemplateNamespaceParser"/> class.
+ /// </summary>
+ public TemplateNamespaceParser() {
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="element"></param>
+ /// <param name="parserContext"></param>
+ /// <param name="builder"></param>
+ protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder) {
+ switch (element.LocalName) {
+ case TemplateDefinitionConstants.NVelocityElement:
+ ParseNVelocityTemplate(element, parserContext, builder);
+ return;
+ default:
+ throw new ArgumentException(string.Format("undefined element for templating namespace: {0}", element.LocalName));
+ }
+ }
+
+ private void ParseNVelocityTemplate(XmlElement element, ParserContext parseContext, ObjectDefinitionBuilder builder) {
+ string preferFileSystemAccess = GetAttributeValue(element, TemplateDefinitionConstants.AttributePreferFileSystemAccess);
+ string overrideLogging = GetAttributeValue(element, TemplateDefinitionConstants.AttributeOverrideLogging);
+ string configFile = GetAttributeValue(element, TemplateDefinitionConstants.AttributeConfigFile);
+
+ if (StringUtils.HasText(preferFileSystemAccess)) {
+ builder.AddPropertyValue(NVelocityEngineFactoryProperties.PropertyPreferFileSystemAccess, preferFileSystemAccess);
+ }
+
+ if (StringUtils.HasText(overrideLogging)) {
+ builder.AddPropertyValue(NVelocityEngineFactoryProperties.PropertyOverrideLogging, overrideLogging);
+ }
+
+ if (StringUtils.HasText(configFile)) {
+ builder.AddPropertyValue(NVelocityEngineFactoryProperties.PropertyConfigFile, configFile);
+ }
+
+ XmlNodeList childElements = element.ChildNodes;
+ if (childElements.Count > 0) {
+ ParseChildDefinitions(childElements, parseContext, builder);
+ }
+ }
+
+ private void ParseChildDefinitions(XmlNodeList childElements, ParserContext context, ObjectDefinitionBuilder builder) {
+ IDictionary<string, object> properties = new Dictionary<string, object>();
+
+ foreach (XmlElement element in childElements) {
+ switch (element.LocalName) {
+ case TemplateDefinitionConstants.ElementResourceLoader:
+ ParseResourceLoader(element, context, builder, properties);
+ break;
+ case TemplateDefinitionConstants.ElementNVelocityProperties:
+ ParseNVelocityProperties(element, properties);
+ break;
+ }
+ }
+
+ if (properties.Count > 0) {
+ builder.AddPropertyValue(NVelocityEngineFactoryProperties.PropertyVelocityProperties, properties);
+ }
+ }
+
+ private void ParseResourceLoader(XmlElement element, ParserContext context, ObjectDefinitionBuilder builder, IDictionary<string, object> properties) {
+ string loaderTypeDefinition = GetAttributeValue(element, TemplateDefinitionConstants.AttributeLoaderType);
+ XmlNodeList loaderElements = element.ChildNodes;
+ foreach (XmlElement loaderElement in loaderElements) {
+ switch (loaderElement.LocalName) {
+ case TemplateDefinitionConstants.ElementResourceLoaderFile:
+ AppendResourceLoaderPaths(loaderElements, builder);
+ break;
+ case TemplateDefinitionConstants.ElementResourceLoaderAssembly:
+ AppendAssemblyLoaderProperties((XmlElement)loaderElements.Item(0), properties, loaderTypeDefinition);
+ break;
+ }
+ }
+
+ }
+
+ private void AppendAssemblyLoaderProperties(XmlElement element, IDictionary<string, object> properties, string loaderTypeDefinition) {
+ string assemblyName = GetAttributeValue(element, TemplateDefinitionConstants.AttributeName);
+ string loaderType = (StringUtils.HasText(loaderTypeDefinition))
+ ? loaderTypeDefinition
+ : "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader";
+ properties.Add(RuntimeConstants.RESOURCE_LOADER, element.LocalName);
+ properties.Add(element.LocalName + "." + RuntimeConstants.RESOURCE_LOADER + ".class", loaderType);
+ properties.Add(element.LocalName + "." + RuntimeConstants.RESOURCE_LOADER + ".assembly", assemblyName);
+ }
+
+ private void AppendResourceLoaderPaths(XmlNodeList elements, ObjectDefinitionBuilder builder) {
+ IList<string> paths = new List<string>();
+ foreach (XmlElement element in elements) {
+ string path = GetAttributeValue(element, TemplateDefinitionConstants.AttributePath);
+ paths.Add(path);
+ }
+ builder.AddPropertyValue(NVelocityEngineFactoryProperties.PropertyResourceLoaderPaths, paths);
+ }
+
+
+ private void ParseNVelocityProperties(XmlElement element, IDictionary<string, object> dictionary) {
+ // todo add impl
+ }
+
+
+ protected override string GetObjectTypeName(XmlElement element) {
+ string typeName = GetAttributeValue(element, ObjectDefinitionConstants.TypeAttribute);
+ if (StringUtils.IsNullOrEmpty(typeName)) {
+ return TemplateTypePrefix + element.LocalName;
+ }
+ return typeName;
+ }
+
+ #region Element & Attribute Name Constants
+
+ private class TemplateDefinitionConstants {
+ public const string NVelocityElement = "nvelocity";
+ public const string AttributePreferFileSystemAccess = "prefer-file-system-access";
+ public const string AttributeConfigFile = "config-file";
+ public const string AttributeOverrideLogging = "override-logging";
+ public const string AttributeLoaderType = "loader-type";
+ public const string ElementResourceLoader = "resource-loader";
+ public const string ElementResourceLoaderFile = "file";
+ public const string ElementResourceLoaderAssembly = "assembly";
+ public const string ElementNVelocityProperties = "nvelocity-properties";
+ public const string AttributeName = "name";
+ public const string AttributePath = "path";
+ }
+
+ private class NVelocityEngineFactoryProperties {
+ public const string PropertyPreferFileSystemAccess = "PreferFileSystemAccess";
+ public const string PropertyOverrideLogging = "OverrideLogging";
+ public const string PropertyConfigFile = "ConfigLocation";
+ public const string PropertyResourceLoaderPaths = "ResourceLoaderPaths";
+ public const string PropertyVelocityProperties = "VelocityProperties";
+ }
+ #endregion
+ }
+}
View
117 src/Spring/Spring.Template.Velocity/Template/Config/spring-template-1.2.xsd
@@ -0,0 +1,117 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<xsd:schema xmlns="http://www.springframework.net/template"
+ xmlns:objects="http://www.springframework.net"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:vs="http://schemas.microsoft.com/Visual-Studio-Intellisense"
+ targetNamespace="http://www.springframework.net/template"
+ elementFormDefault="qualified"
+ attributeFormDefault="unqualified"
+ vs:friendlyname="Spring.NET Templating Framework Configuration"
+ vs:ishtmlschema="false"
+ vs:iscasesensitive="true"
+ vs:requireattributequotes="true"
+ vs:defaultnamespacequalifier=""
+ vs:defaultnsprefix="">
+
+ <xsd:import namespace="http://www.springframework.net"/>
+ <xsd:import namespace="http://www.springframework.net/tool"/>
+
+ <xsd:annotation>
+ <xsd:documentation>
+ <![CDATA[
+ Spring.NET Template Framework Config Schema Definition
+
+ Author: Erez Mazor
+
+ This file defines a configuration schema for the template framework
+ object definitions. Using elements from this schema instead of the
+ standard object definitions can greatly simplify templating engine configuration.
+ ]]>
+ </xsd:documentation>
+ </xsd:annotation>
+
+
+ <xsd:complexType name="nvelocityTemplate">
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[Defines an NVelocity template configuration]]></xsd:documentation>
+ </xsd:annotation>
+ <xsd:sequence>
+ <xsd:element name="nvelocity-properties" type="objects:objectMap" minOccurs="0" maxOccurs="1"/>
+ <xsd:element name="resource-loader" type="resourceLoader" minOccurs="0" maxOccurs="1" />
+ </xsd:sequence>
+ <xsd:attribute name="id" type="objects:nonNullString" use="required">
+ <xsd:annotation>
+ <xsd:documentation>
+ <![CDATA[The id of the NVelocity template instance to be referenced.]]>
+ </xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
+ <xsd:attribute name="config-file" use="optional">
+ <xsd:simpleType>
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[Optional location of a properties file defining the NVelocity configuration.]]></xsd:documentation>
+ </xsd:annotation>
+ <xsd:restriction base="objects:nonNullString"/>
+ </xsd:simpleType>
+ </xsd:attribute>
+ <xsd:attribute name="prefer-file-system-access" use="optional" default="true">
+ <xsd:simpleType>
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[Optional. Instructs the NVelocity engine factory to attempt use NVelocity's file loader with hot tracking.]]></xsd:documentation>
+ </xsd:annotation>
+ <xsd:restriction base="xsd:boolean"/>
+ </xsd:simpleType>
+ </xsd:attribute>
+ <xsd:attribute name="override-logging" use="optional" default="true">
+ <xsd:simpleType>
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[Optional. Instructs the NVelocity engine factory to use the provided spring commons logging based logging system.]]></xsd:documentation>
+ </xsd:annotation>
+ <xsd:restriction base="xsd:boolean"/>
+ </xsd:simpleType>
+ </xsd:attribute>
+ </xsd:complexType>
+
+
+ <xsd:complexType name="resourceLoader">
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[Defines Assembly based template loader.]]></xsd:documentation>
+ </xsd:annotation>
+ <xsd:choice>
+ <xsd:element name="assembly" minOccurs="1" maxOccurs="1">
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[The assembly where templates reside.]]></xsd:documentation>
+ </xsd:annotation>
+ <xsd:complexType>
+ <xsd:attribute name="name" type="objects:nonNullString" use="required">
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[The name of the assembly.]]></xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
+ </xsd:complexType>
+
+ </xsd:element>
+ <xsd:element name="file" minOccurs="1" maxOccurs="unbounded">
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[The location of the template files.]]></xsd:documentation>
+ </xsd:annotation>
+ <xsd:complexType>
+ <xsd:attribute name="path" type="objects:nonNullString" use="required">
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[The qualified path where the template files exists.]]></xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:choice>
+ <xsd:attribute name="loader-type" type="objects:nonNullString" use="optional">
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[Optional. The NVelocity.Runtime.Resource.Loader.ResourceLoader extension class used to load the files. By default a file based resource loader is used (unless prefer-file-system-access is set to false)]]></xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
+ </xsd:complexType>
+
+
+ <xsd:element name="nvelocity" type="nvelocityTemplate" />
+</xsd:schema>
View
13 ...ring/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.cs
@@ -81,6 +81,19 @@ public class VelocityEngineFactoryObjectTests {
}
/// <summary>
+ /// Test the fileBasedVelocityEngine bean configuration from VelocityEngineFactoryObjectTests.xml
+ /// </summary>
+ [Test]
+ public void TestMergeUsingCustomNamespaceDefinition() {
+ VelocityEngine velocityEngine =
+ appContext.GetObject("customNamespaceVelocityTemplate") as VelocityEngine;
+ Assert.IsNotNull(velocityEngine, "velocityEngine is null");
+ string mergedTemplate = VelocityEngineUtils.MergeTemplateIntostring(velocityEngine, "SimpleTemplate.vm",
+ Encoding.UTF8.WebName, model);
+ Assert.AreEqual(string.Format("value={0}", "TEST_VALUE"), mergedTemplate);
+ }
+
+ /// <summary>
/// Test using definition of ResourceLoaderPath (file-based configuration) referencing just the template name
/// </summary>
[Test]
View
10 ...ing/Spring.Template.Velocity.Tests/Template/Velocity/VelocityEngineFactoryObjectTests.xml
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
-<objects xmlns="http://www.springframework.net">
+<objects xmlns="http://www.springframework.net" xmlns:template="http://www.springframework.net/template">
+
+ <template:nvelocity id="customNamespaceVelocityTemplate" >
+ <template:resource-loader>
+ <template:file path="Template/Velocity/" />
+ </template:resource-loader>
+ </template:nvelocity>
+
<!-- Does file system access with hot tracking with NVelocity assembly resource loader -->
<object id="assemblyBasedVelocityEngine" type="Spring.Template.Velocity.VelocityEngineFactoryObject, Spring.Template.Velocity">
@@ -39,5 +46,4 @@
<property name="ResourceLoaderPath " value="Template/Velocity/" />
<property name="PreferFileSystemAccess " value="false" />
</object>
-
</objects>
Please sign in to comment.
Something went wrong with that request. Please try again.