Codebase list plexus-velocity / bc0f211
Merge tag 'upstream/1.2' Upstream version 1.2 Emmanuel Bourg 7 years ago
13 changed file(s) with 687 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 target/
1 .project
2 .classpath
3 .settings/
4 bin
5 *.iml
6 .idea
0 <!--
1 Copyright 2001-2006 Codehaus Foundation.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 -->
15
16 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
17 <modelVersion>4.0.0</modelVersion>
18
19 <parent>
20 <artifactId>plexus-components</artifactId>
21 <groupId>org.codehaus.plexus</groupId>
22 <version>4.0</version>
23 </parent>
24
25 <artifactId>plexus-velocity</artifactId>
26 <version>1.2</version>
27
28 <name>Plexus Velocity Component</name>
29
30 <scm>
31 <connection>scm:git:git@github.com:codehaus-plexus/plexus-velocity.git</connection>
32 <developerConnection>scm:git:git@github.com:codehaus-plexus/plexus-velocity.git</developerConnection>
33 <url>http://github.com/codehaus-plexus/plexus-velocity</url>
34 <tag>plexus-velocity-1.2</tag>
35 </scm>
36 <issueManagement>
37 <system>github</system>
38 <url>http://github.com/codehaus-plexus/plexus-velocity/issues</url>
39 </issueManagement>
40 <distributionManagement>
41 <site>
42 <id>github:gh-pages</id>
43 <url>${project.scm.developerConnection}</url>
44 </site>
45 </distributionManagement>
46
47 <dependencies>
48 <dependency>
49 <groupId>org.codehaus.plexus</groupId>
50 <artifactId>plexus-container-default</artifactId>
51 </dependency>
52 <dependency>
53 <groupId>commons-collections</groupId>
54 <artifactId>commons-collections</artifactId>
55 <version>3.1</version>
56 </dependency>
57 <dependency>
58 <groupId>org.apache.velocity</groupId>
59 <artifactId>velocity</artifactId>
60 <version>1.7</version>
61 </dependency>
62 </dependencies>
63
64 <build>
65 <plugins>
66 <plugin>
67 <groupId>org.apache.maven.plugins</groupId>
68 <artifactId>maven-scm-publish-plugin</artifactId>
69 <configuration>
70 <content>${project.reporting.outputDirectory}</content><!-- mono-module doesn't require site:stage -->
71 </configuration>
72 <executions>
73 <execution>
74 <id>scm-publish</id>
75 <phase>site-deploy</phase><!-- deploy site with maven-scm-publish-plugin -->
76 <goals>
77 <goal>publish-scm</goal>
78 </goals>
79 </execution>
80 </executions>
81 </plugin>
82 </plugins>
83 </build>
84 </project>
0 package org.codehaus.plexus.velocity;
1
2 /*
3 * Copyright 2001-2016 Codehaus Foundation.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 import java.io.InputStream;
19
20 import org.apache.velocity.runtime.resource.Resource;
21 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
22 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
23 import org.apache.velocity.exception.ResourceNotFoundException;
24
25 import org.apache.commons.collections.ExtendedProperties;
26
27 /**
28 *
29 * @deprecated use {@link ClasspathResourceLoader}
30 *
31 */
32 @Deprecated
33 public class ContextClassLoaderResourceLoader
34 extends ResourceLoader
35 {
36 public void init( ExtendedProperties configuration)
37 {
38 }
39
40 public synchronized InputStream getResourceStream( String name )
41 throws ResourceNotFoundException
42 {
43 InputStream result = null;
44
45 if (name == null || name.length() == 0)
46 {
47 throw new ResourceNotFoundException ("No template name provided");
48 }
49
50 try
51 {
52 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
53
54 if ( name.startsWith( "/" ) )
55 {
56 name = name.substring( 1 );
57 }
58
59 result = classLoader.getResourceAsStream( name );
60 }
61 catch( Exception fnfe )
62 {
63 throw new ResourceNotFoundException( fnfe.getMessage() );
64 }
65
66 return result;
67 }
68
69 public boolean isSourceModified(Resource resource)
70 {
71 return false;
72 }
73
74 public long getLastModified(Resource resource)
75 {
76 return 0;
77 }
78 }
79
0 package org.codehaus.plexus.velocity;
1
2 /*
3 * Copyright 2001-2016 Codehaus Foundation.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 import java.util.Enumeration;
19 import java.util.Properties;
20
21 import org.apache.velocity.app.VelocityEngine;
22 import org.apache.velocity.runtime.RuntimeServices;
23 import org.apache.velocity.runtime.RuntimeConstants;
24 import org.apache.velocity.runtime.log.LogChute;
25 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
26 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
27 import org.codehaus.plexus.logging.AbstractLogEnabled;
28
29 /**
30 * A simple velocity component implementation.
31 * <p/>
32 * A typical configuration will look like this:
33 * <pre>
34 * <configuration>
35 * <properties>
36 * <property>
37 * <name>resource.loader</name>
38 * <value>classpath</value>
39 * </property>
40 * <property>
41 * <name>classpath.resource.loader.class</name>
42 * <value>org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value>
43 * </property>
44 * </properties>
45 * </configuration>
46 * </pre>
47 */
48 public class DefaultVelocityComponent
49 extends AbstractLogEnabled
50 implements VelocityComponent, Initializable, LogChute
51 {
52 private VelocityEngine engine;
53
54 private Properties properties;
55
56 // ----------------------------------------------------------------------
57 //
58 // ----------------------------------------------------------------------
59
60 public void initialize()
61 throws InitializationException
62 {
63 engine = new VelocityEngine();
64
65 // avoid "unable to find resource 'VM_global_library.vm' in any resource loader."
66 engine.setProperty( RuntimeConstants.VM_LIBRARY, "" );
67
68 engine.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this );
69
70 if ( properties != null )
71 {
72 for ( Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); )
73 {
74 String key = e.nextElement().toString();
75
76 String value = properties.getProperty( key );
77
78 engine.setProperty( key, value );
79
80 getLogger().debug( "Setting property: " + key + " => '" + value + "'." );
81 }
82 }
83
84 try
85 {
86 engine.init();
87 }
88 catch ( Exception e )
89 {
90 throw new InitializationException( "Cannot start the Velocity engine", e );
91 }
92 }
93
94 // ----------------------------------------------------------------------
95 //
96 // ----------------------------------------------------------------------
97
98 public VelocityEngine getEngine()
99 {
100 return engine;
101 }
102 // ----------------------------------------------------------------------
103 //
104 // ----------------------------------------------------------------------
105
106 private RuntimeServices runtimeServices;
107
108 public void init( RuntimeServices runtimeServices )
109 {
110 this.runtimeServices = runtimeServices;
111 }
112
113 public void log(int level, String message)
114 {
115 switch ( level )
116 {
117 case LogChute.WARN_ID:
118 getLogger().warn( message );
119 break;
120 case LogChute.INFO_ID:
121 getLogger().info( message );
122 break;
123 case LogChute.DEBUG_ID:
124 case LogChute.TRACE_ID:
125 getLogger().debug( message );
126 break;
127 case LogChute.ERROR_ID:
128 getLogger().error( message );
129 break;
130 default:
131 getLogger().debug( message );
132 break;
133 }
134 }
135
136 public void log(int level, String message, Throwable t)
137 {
138 switch ( level )
139 {
140 case LogChute.WARN_ID:
141 getLogger().warn( message, t );
142 break;
143 case LogChute.INFO_ID:
144 getLogger().info( message, t );
145 break;
146 case LogChute.DEBUG_ID:
147 case LogChute.TRACE_ID:
148 getLogger().debug( message, t );
149 break;
150 case LogChute.ERROR_ID:
151 getLogger().error( message, t );
152 break;
153 default:
154 getLogger().debug( message, t );
155 break;
156 }
157 }
158
159 public boolean isLevelEnabled( int level )
160 {
161 switch ( level )
162 {
163 case LogChute.WARN_ID:
164 return getLogger().isWarnEnabled();
165 case LogChute.INFO_ID:
166 return getLogger().isInfoEnabled();
167 case LogChute.DEBUG_ID:
168 case LogChute.TRACE_ID:
169 return getLogger().isDebugEnabled();
170 case LogChute.ERROR_ID:
171 return getLogger().isErrorEnabled();
172 default:
173 return getLogger().isDebugEnabled();
174 }
175 }
176 }
0 package org.codehaus.plexus.velocity;
1
2 /*
3 * Copyright 2001-2016 Codehaus Foundation.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 import org.apache.commons.collections.ExtendedProperties;
19 import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
20 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
21 import org.apache.velocity.exception.ResourceNotFoundException;
22
23 import java.io.InputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27
28 /**
29 * Allows you to dynamically add resources that you want to be processed by Velocity. For
30 * example if you want to generate a site and pull in some random files to be interpolated
31 * by Velocity you can use this resource loader.
32 *
33 * @author Jason van Zyl
34 * @deprecated use {@link FileResourceLoader}
35 */
36 @Deprecated
37 public class SiteResourceLoader
38 extends ResourceLoader
39 {
40 private static String resource;
41
42 private static File resourceDir;
43
44 public static void setResource( String staticResource )
45 {
46 resource = staticResource;
47 resourceDir = new File( resource ).getParentFile();
48 }
49
50 public void init( ExtendedProperties p )
51 {
52 }
53
54 public InputStream getResourceStream( String name )
55 throws ResourceNotFoundException
56 {
57 if ( resource != null )
58 {
59 try
60 {
61 File f = new File( resourceDir, name );
62 if ( f.exists() )
63 {
64 return new FileInputStream( f );
65 }
66 else
67 {
68 return new FileInputStream( resource );
69 }
70 }
71 catch ( FileNotFoundException e )
72 {
73 throw new ResourceNotFoundException( "Cannot find resource, make sure you set the right resource." );
74 }
75 }
76
77 return null;
78 }
79
80 public boolean isSourceModified( org.apache.velocity.runtime.resource.Resource resource )
81 {
82 return false; //To change body of implemented methods use File | Settings | File Templates.
83 }
84
85 public long getLastModified( org.apache.velocity.runtime.resource.Resource resource )
86 {
87 return 0;
88 }
89 }
90
0 package org.codehaus.plexus.velocity;
1
2 /*
3 * Copyright 2001-2016 Codehaus Foundation.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 import org.apache.velocity.app.VelocityEngine;
19
20 /**
21 * The VelocityComponent API to access <a href="http://velocity.apache.org/engine/">Apache Velocity Engine</a>
22 * configured through Plexus.
23 */
24 public interface VelocityComponent
25 {
26 public final static String ROLE = VelocityComponent.class.getName();
27
28 VelocityEngine getEngine();
29 }
0 <component-set>
1 <components>
2 <component>
3 <role>org.codehaus.plexus.velocity.VelocityComponent</role>
4 <role-hint>default</role-hint>
5 <implementation>org.codehaus.plexus.velocity.DefaultVelocityComponent</implementation>
6 <configuration>
7 <properties>
8 <property>
9 <name>resource.loader</name>
10 <value>classpath,file</value>
11 </property>
12 <property>
13 <name>classpath.resource.loader.class</name>
14 <value>org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value>
15 </property>
16 <property>
17 <name>file.resource.loader.class</name>
18 <value>org.apache.velocity.runtime.resource.loader.FileResourceLoader</value>
19 </property>
20 <property>
21 <name>file.resource.loader.path</name>
22 <!-- This space intended! Actually Velocity expects an empty string but
23 Plexus/XStream choke on it and skip the remaining configuration.
24 Luckily, Plexus/XStream trim strings and we achieve the desired effect. -->
25 <value> </value>
26 </property>
27 <property>
28 <name>runtime.log.invalid.references</name>
29 <value>false</value>
30 </property>
31 <property>
32 <name>velocimacro.messages.on</name>
33 <value>false</value>
34 </property>
35 <property>
36 <name>resource.manager.logwhenfound</name>
37 <value>false</value>
38 </property>
39 <property>
40 <name>eventhandler.include.class</name>
41 <value>org.apache.velocity.app.event.implement.IncludeRelativePath</value>
42 </property>
43 <property>
44 <name>velocimacro.permissions.allow.inline.to.replace.global</name>
45 <value>true</value>
46 </property>
47 </properties>
48 </configuration>
49 </component>
50 </components>
51 </component-set>
0 <?xml version="1.0" encoding="UTF-8"?>
1
2 <project xmlns="http://maven.apache.org/DECORATION/1.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/DECORATION/1.0.0 http://maven.apache.org/xsd/decoration-1.0.0.xsd">
5 <body>
6 <menu name="Overview">
7 <item name="Introduction" href="index.html"/>
8 <item name="JavaDocs" href="apidocs/index.html"/>
9 <item name="Source Xref" href="xref/index.html"/>
10 <!--item name="FAQ" href="faq.html"/-->
11 </menu>
12
13 <menu ref="parent"/>
14 <menu ref="reports"/>
15 </body>
16 </project>
0 <?xml version="1.0"?>
1
2 <document xmlns="http://maven.apache.org/XDOC/2.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 https://maven.apache.org/xsd/xdoc-2.0.xsd">
5 <properties>
6 <title>Plexus Velocity Component</title>
7 <author email="olamy@codehaus.org">Olivier Lamy</author>
8 </properties>
9 <body>
10 <section name="Plexus Velocity Component">
11 <p>A component which provides <a href="http://velocity.apache.org/">Apache Velocity</a> templating engine
12 integration.</p>
13
14 <subsection name="Typical use">
15 <p>A typical use:
16 <source>
17 VelocityContext context = new VelocityContext();
18 VelocityComponent velocityComponent = (DefaultVelocityComponent) lookup( VelocityComponent.ROLE );
19 Template template = velocityComponent.getEngine().getTemplate( "path to your template" );
20 StringWriter writer = new StringWriter();
21 template.merge( context, writer );</source>
22 </p>
23 </subsection>
24 <subsection name="Typical Configuration">
25 <p>Plexus Velocity Component comes with a default configuration: it is
26 expected to be customized to match local specific need. This is done by hand writing a
27 <code>META-INF/plexus/components.xml</code> Plexus descriptor.</p>
28 <p>A typical component configuration:
29 <source>
30 <![CDATA[ <component>
31 <role>org.codehaus.plexus.velocity.VelocityComponent</role>
32 <implementation>org.codehaus.plexus.velocity.DefaultVelocityComponent</implementation>
33 <configuration>
34 <properties>
35 <property>
36 <name>resource.loader</name>
37 <value>classpath</value>
38 </property>
39 <property>
40 <name>classpath.resource.loader.class</name>
41 <value>org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value>
42 </property>
43 </properties>
44 </configuration>
45 </component>]]></source>
46 </p>
47 <p>See <a href="http://velocity.apache.org/engine/releases/velocity-1.7/developer-guide.html#Velocity_Configuration_Keys_and_Values">Velocity Configuration Keys and Values</a>
48 reference documentation for details on available configurations.</p>
49 </subsection>
50 </section>
51 </body>
52 </document>
0 package org.codehaus.plexus.velocity;
1
2 /*
3 * Copyright 2001-2016 Codehaus Foundation.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 import java.io.StringWriter;
19
20 import org.apache.velocity.Template;
21 import org.apache.velocity.VelocityContext;
22 import org.codehaus.plexus.PlexusTestCase;
23
24 public class DefaultVelocityComponentTest
25 extends PlexusTestCase
26 {
27 public void testBasic()
28 throws Exception
29 {
30 DefaultVelocityComponent velocity;
31
32 VelocityContext context;
33
34 String value;
35
36 velocity = (DefaultVelocityComponent) lookup( VelocityComponent.ROLE );
37
38 // test the properties
39 value = (String) velocity.getEngine().getProperty( "hello" );
40
41 assertNotNull( value );
42
43 assertEquals( "world", value );
44
45 // test the rendering
46 context = new VelocityContext();
47
48 context.put( "variable", "Value from context" );
49
50 Template template = velocity.getEngine().getTemplate("org/codehaus/plexus/velocity/DefaultVelocityComponentTest.vm" );
51
52 StringWriter writer = new StringWriter();
53
54 template.merge( context, writer );
55
56 assertEquals( "Static text -- Value from context -- More static text", writer.toString() );
57 }
58 }
0 log4j.rootCategory=DEBUG,testlog
1
2 # testlog is set to be a File appender using a PatternLayout.
3 log4j.appender.testlog = org.apache.log4j.FileAppender
4 log4j.appender.testlog.File = test.log
5 log4j.appender.testlog.Append = false
6 log4j.appender.testlog.Threshold = DEBUG
7 log4j.appender.testlog.layout = org.apache.log4j.PatternLayout
8 log4j.appender.testlog.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
9
0 <plexus>
1 <components>
2 <component>
3 <role>org.codehaus.plexus.velocity.VelocityComponent</role>
4 <role-hint>default</role-hint>
5 <implementation>org.codehaus.plexus.velocity.DefaultVelocityComponent</implementation>
6 <configuration>
7 <properties>
8 <property>
9 <name>resource.loader</name>
10 <value>classpath</value>
11 </property>
12 <property>
13 <name>classpath.resource.loader.class</name>
14 <value>org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</value>
15 </property>
16 <property>
17 <name>hello</name>
18 <value>world</value>
19 </property>
20 </properties>
21 </configuration>
22 </component>
23 </components>
24 </plexus>