Skip to content

Commit 42eca55

Browse files
author
xiaoma20082008
committed
<refactor>: refactor the velocity-engine to be more scalable
1. define new and interfaces. APIs for users, SPIs for developers. 2. implements some builtin spi interfaces
1 parent bb5a9d6 commit 42eca55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3462
-3
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
<plugin>
138138
<groupId>org.apache.maven.plugins</groupId>
139139
<artifactId>maven-compiler-plugin</artifactId>
140+
<version>3.8.1</version>
140141
<configuration>
141142
<showDeprecation>true</showDeprecation>
142143
<showWarning>true</showWarning>

velocity-engine-core/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
```
2+
+----------+ |
3+
| | nowContext +---------+ |
4+
| | -----------> | Context | <--+ run
5+
init | | +---------+ |
6+
----->| Velocity | v
7+
| | getTemplate +------------------+
8+
| | ------------> | Template |
9+
| | +------------------+
10+
+----------+
11+
```
12+
13+
```java
14+
import java.io.StringReader;
15+
import java.io.Writer;
16+
import org.apache.velocity.Template;
17+
import org.apache.velocity.spi.Translator;
18+
import org.apache.velocity.runtime.parser.Parser;
19+
import org.apache.velocity.runtime.parser.node.SimpleNode;
20+
import org.apache.velocity.runtime.resource.Resource;
21+
import org.apache.velocity.runtime.resource.ResourceManager;
22+
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
23+
24+
public class View {
25+
26+
/**
27+
* render the template
28+
*
29+
* @param name template name
30+
* @param writer out
31+
*/
32+
public void render(String name, Writer writer) {
33+
// 1. get template
34+
Template template = Velocity.getTemplate(name, "utf-8");
35+
// 2. get current context
36+
Context context = Velocity.nowContext();
37+
// 3. render template
38+
template.render(context, writer);
39+
}
40+
}
41+
42+
public class Velocity {
43+
44+
private ResourceManager resourceManager;
45+
private Parser parser;
46+
private Translator translator;
47+
private ResourceLoader loader;
48+
49+
public Template getTemplate(String name, String encoding) {
50+
String path = toPath(name);
51+
Resource resource = resourceManager.getResource(path, ResourceManager.RESOURCE_TEMPLATE, encoding);
52+
SimpleNode node = parser.parse(new StringReader((String) resource.getData()), new Template());
53+
return translator.translate(resource, node);
54+
}
55+
56+
private String toPath(String name) {
57+
return "suffix/" + name;
58+
}
59+
60+
}
61+
62+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.apache.velocity.api;
2+
3+
import org.apache.velocity.runtime.visitor.BaseVisitor;
4+
5+
public interface Node {
6+
7+
void accept(BaseVisitor visitor);
8+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.apache.velocity.api;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.Reader;
6+
import java.util.Locale;
7+
import org.apache.velocity.runtime.RuntimeInstance;
8+
9+
public interface Resource {
10+
11+
/**
12+
* Get the resource name.
13+
*
14+
* @return name
15+
*/
16+
String getName();
17+
18+
/**
19+
* Get the resource encoding.
20+
*
21+
* @return encoding
22+
*/
23+
String getEncoding();
24+
25+
/**
26+
* Get the resource locale.
27+
*
28+
* @return locale
29+
*/
30+
Locale getLocale();
31+
32+
/**
33+
* Get the resource last modified time.
34+
*
35+
* @return last modified time
36+
*/
37+
long getLastModified();
38+
39+
/**
40+
* Get the resource length.
41+
*
42+
* @return source length
43+
*/
44+
long getLength();
45+
46+
/**
47+
* Get the template source.
48+
*
49+
* @return source
50+
* @throws IOException - If an I/O error occurs
51+
*/
52+
String getSource() throws IOException;
53+
54+
/**
55+
* Get the template source reader.
56+
* <p/>
57+
* NOTE: Don't forget close the reader.
58+
* <p/>
59+
* <pre>
60+
* Reader reader = resource.openReader();
61+
* try {
62+
* // do something ...
63+
* } finally {
64+
* reader.close();
65+
* }
66+
* </pre>
67+
*
68+
* @return source reader
69+
* @throws IOException - If an I/O error occurs
70+
*/
71+
Reader openReader() throws IOException;
72+
73+
/**
74+
* Get the template source input stream.
75+
* <p/>
76+
* NOTE: Don't forget close the input stream.
77+
* <p/>
78+
* <pre>
79+
* InputStream stream = resource.openStream();
80+
* try {
81+
* // do something ...
82+
* } finally {
83+
* stream.close();
84+
* }
85+
* </pre>
86+
*
87+
* @return source input stream
88+
* @throws IOException - If an I/O error occurs
89+
*/
90+
InputStream openStream() throws IOException;
91+
92+
/**
93+
* Get the template engine.
94+
*
95+
* @return engine
96+
*/
97+
RuntimeInstance getEngine();
98+
99+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.apache.velocity.api;
2+
3+
import java.io.IOException;
4+
import java.io.Writer;
5+
import java.text.ParseException;
6+
import org.apache.velocity.context.Context;
7+
8+
public interface Template extends Resource, Node {
9+
10+
void render(Context context, Writer out) throws IOException, ParseException;
11+
12+
Object evaluate(Context context) throws ParseException;
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.apache.velocity.spi;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.util.concurrent.ConcurrentMap;
23+
24+
public interface Cache<K, V> extends ConcurrentMap<K, V> {
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.apache.velocity.spi;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.apache.velocity.exception.VelocityException;
23+
24+
public interface Compiler {
25+
26+
Class<?> compile(String source) throws VelocityException;
27+
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.apache.velocity.spi;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
public interface Converter {
23+
24+
String convert(String key, String value);
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.apache.velocity.spi;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.io.IOException;
23+
import java.util.List;
24+
import java.util.Locale;
25+
import org.apache.velocity.api.Resource;
26+
27+
public interface Loader {
28+
29+
/**
30+
* @param suffix resource suffix
31+
* @return resource names.
32+
*/
33+
List<String> list(String suffix) throws IOException;
34+
35+
boolean exists(String name, Locale locale);
36+
37+
Resource load(String name, Locale locale, String encoding) throws IOException;
38+
}

0 commit comments

Comments
 (0)