Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<showDeprecation>true</showDeprecation>
<showWarning>true</showWarning>
Expand Down
62 changes: 62 additions & 0 deletions velocity-engine-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
```
+----------+ |
| | nowContext +---------+ |
| | -----------> | Context | <--+ render
init | | +---------+ |
----->| Velocity | v
| | getTemplate +------------------+
| | ------------> | Template |
| | +------------------+
+----------+
```

```java
import java.io.StringReader;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.spi.Translator;
import org.apache.velocity.runtime.parser.Parser;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.ResourceManager;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;

public class View {

/**
* render the template
*
* @param name template name
* @param writer out
*/
public void render(String name, Writer writer) {
// 1. get template
Template template = Velocity.getTemplate(name, "utf-8");
// 2. get current context
Context context = Velocity.nowContext();
// 3. render template
template.render(context, writer);
}
}

public class Velocity {

private ResourceManager resourceManager;
private Parser parser;
private Translator translator;
private ResourceLoader loader;

public Template getTemplate(String name, String encoding) {
String path = toPath(name);
Resource resource = resourceManager.getResource(path, ResourceManager.RESOURCE_TEMPLATE, encoding);
SimpleNode node = parser.parse(new StringReader((String) resource.getData()), new Template());
return translator.translate(resource, node);
}

private String toPath(String name) {
return "suffix/" + name;
}

}

```
6 changes: 6 additions & 0 deletions velocity-engine-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@
</build>

<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.28.0-GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.apache.velocity.api;

import org.apache.velocity.runtime.visitor.BaseVisitor;

public interface Node {

void accept(BaseVisitor visitor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.apache.velocity.api;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Locale;
import org.apache.velocity.runtime.RuntimeInstance;

public interface Resource {

/**
* Get the resource name.
*
* @return name
*/
String getName();

/**
* Get the resource encoding.
*
* @return encoding
*/
String getEncoding();

/**
* Get the resource locale.
*
* @return locale
*/
Locale getLocale();

/**
* Get the resource last modified time.
*
* @return last modified time
*/
long getLastModified();

/**
* Get the resource length.
*
* @return source length
*/
long getLength();

/**
* Get the template source.
*
* @return source
* @throws IOException - If an I/O error occurs
*/
String getSource() throws IOException;

/**
* Get the template source reader.
* <p/>
* NOTE: Don't forget close the reader.
* <p/>
* <pre>
* Reader reader = resource.openReader();
* try {
* // do something ...
* } finally {
* reader.close();
* }
* </pre>
*
* @return source reader
* @throws IOException - If an I/O error occurs
*/
Reader openReader() throws IOException;

/**
* Get the template source input stream.
* <p/>
* NOTE: Don't forget close the input stream.
* <p/>
* <pre>
* InputStream stream = resource.openStream();
* try {
* // do something ...
* } finally {
* stream.close();
* }
* </pre>
*
* @return source input stream
* @throws IOException - If an I/O error occurs
*/
InputStream openStream() throws IOException;

/**
* Get the template engine.
*
* @return engine
*/
RuntimeInstance getEngine();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.apache.velocity.api;

import java.io.IOException;
import java.io.Writer;
import java.text.ParseException;
import org.apache.velocity.context.Context;

public interface Template extends Resource, Node {

void render(Context context, Writer out) throws IOException, ParseException;

Object evaluate(Context context) throws ParseException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
* under the License.
*/

import java.io.Reader;
import java.io.Writer;
import java.util.Objects;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.MethodInvocationException;
Expand All @@ -28,10 +32,6 @@
import org.apache.velocity.runtime.RuntimeInstance;
import org.slf4j.Logger;

import java.io.Reader;
import java.io.Writer;
import java.util.Properties;

/**
* <p>
* This class provides a separate new-able instance of the
Expand All @@ -51,14 +51,19 @@
*/
public class VelocityEngine implements RuntimeConstants
{
private RuntimeInstance ri = new RuntimeInstance();
private final RuntimeInstance ri;

/**
* Init-less CTOR
*/
public VelocityEngine()
{
// do nothing
this(new RuntimeInstance());
}

public VelocityEngine(RuntimeInstance ri) // for testable
{
this.ri = Objects.requireNonNull(ri, "RuntimeInstance is null");
}

/**
Expand All @@ -68,6 +73,7 @@ public VelocityEngine()
*/
public VelocityEngine(String propsFilename)
{
this();
ri.setProperties(propsFilename);
}

Expand All @@ -77,6 +83,7 @@ public VelocityEngine(String propsFilename)
*/
public VelocityEngine(Properties p)
{
this();
ri.setProperties(p);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.apache.velocity.spi;

/*
* 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.util.concurrent.ConcurrentMap;

public interface Cache<K, V> extends ConcurrentMap<K, V> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.apache.velocity.spi;

/*
* 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.velocity.exception.VelocityException;

public interface Compiler {

Class<?> compile(String source) throws VelocityException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.apache.velocity.spi;

/*
* 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.
*/

public interface Converter {

String convert(String key, String value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.apache.velocity.spi;

/*
* 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.IOException;
import java.util.Locale;
import org.apache.velocity.api.Resource;

public interface Loader {

boolean exists(String name, Locale locale);

Resource load(String name, Locale locale, String encoding) throws IOException;
}
Loading