Skip to content

Commit

Permalink
#20: Rename Resource to Template as its more clear/understandable
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroup committed Oct 29, 2018
1 parent 12159d5 commit c56cfb1
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 131 deletions.
24 changes: 12 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {
}
```
## Get started
**Generate the text/sql/xml/markdown/json/etc based on velocity [resource](/src/main/java/com/github/dgroup/velocity/Resource.java).**
**Generate the text/sql/xml/markdown/json/etc based on velocity [template](/src/main/java/com/github/dgroup/velocity/Template.java).**
1. Define velocity template `query.sql`
```sql
select 1 from dual
Expand Down Expand Up @@ -65,12 +65,12 @@ dependencies {
```
2. Define instance of velocity template using
- full path to resource
- full path to template
```java
@Test
public void transformSql() throws RsException {
public void transformSql() throws TemplateException {
MatcherAssert.assertThat(
new RsText("query.sql", "src/test/resources/velocity")
new Text("query.sql", "src/test/resources/velocity")
.compose(
new ArgOf("flag", true)
),
Expand All @@ -80,13 +80,13 @@ dependencies {
);
}
```
See [more](/src/test/java/com/github/dgroup/velocity/rs/RsTextTest.java).
See [more](/src/test/java/com/github/dgroup/velocity/template/TextTest.java).
- hierarchical search
```java
@Test
public void hierarchical() throws RsException {
public void hierarchical() throws TemplateException {
MatcherAssert.assertThat(
new RsText("query.sql", "src/test/resources"))
new Text("query.sql", "src/test/resources"))
.compose(
new ArgOf("flag", true)
),
Expand All @@ -96,13 +96,13 @@ dependencies {
);
}
```
You can also specify the multiple roots ([more](/src/main/java/com/github/dgroup/velocity/rs/RsText.java#L64)).
- classpath resource
You can also specify the multiple roots ([more](/src/main/java/com/github/dgroup/velocity/template/Text.java#L64)).
- classpath template
```java
@Test
public void classpath() throws RsException {
public void classpath() throws TemplateException {
MatcherAssert.assertThat(
new RsClasspath(new RelativePath("velocity{0}query.sql"))
new Classpath(new RelativePath("velocity{0}query.sql"))
.compose(
new ArgOf("flag", true)
),
Expand All @@ -112,4 +112,4 @@ dependencies {
);
}
```
See [more](/src/test/java/com/github/dgroup/velocity/rs/RsClasspathTest.java).
See [more](/src/test/java/com/github/dgroup/velocity/template/ClasspathTest.java).
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,45 @@
*/
package com.github.dgroup.velocity;

import com.github.dgroup.velocity.rs.RsException;
import com.github.dgroup.velocity.template.TemplateException;
import org.apache.velocity.VelocityContext;
import org.cactoos.Scalar;

/**
* Velocity resource for text generation (HTML,SQL,XML,etc).
* Velocity template for text generation (HTML,SQL,XML,etc).
*
* Reed more about Apache Velocity at
* http://velocity.apache.org/engine/1.7/user-guide.html.
*
* @param <T> Type of resource.
* @param <T> Type of template.
* @since 0.1.0
*/
public interface Resource<T> {
public interface Template<T> {

/**
* Transform the velocity template to HTML/SQL/etc using velocity variables.
* @param args The velocity variables for template.
* @return Text, XML, JSON, SQL, HTML, etc
* @throws RsException in case template format error.
* @throws TemplateException in case template format error.
*/
T compose(Arg... args) throws RsException;
T compose(Arg... args) throws TemplateException;

/**
* Transform the velocity template to HTML/SQL/etc using velocity variables.
*
* @param args The velocity variables for template.
* @return Text, XML, JSON, SQL, HTML, etc
* @throws RsException in case template format error.
* @throws TemplateException in case template format error.
*/
T compose(Iterable<Arg> args) throws RsException;
T compose(Iterable<Arg> args) throws TemplateException;

/**
* Transform the velocity template to HTML/SQL/etc using velocity variables.
*
* @param ctx The velocity context with variables.
* @return Text, XML, JSON, SQL, HTML, etc
* @throws RsException in case template format error.
* @throws TemplateException in case template format error.
*/
T compose(Scalar<VelocityContext> ctx) throws RsException;
T compose(Scalar<VelocityContext> ctx) throws TemplateException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@
*/
package com.github.dgroup.velocity;

import com.github.dgroup.velocity.rs.RsException;
import com.github.dgroup.velocity.template.TemplateException;

/**
* Repository of velocity resources.
* Repository of velocity templates.
*
* @param <T> The type of resource.
* @since 0.2.0
*/
public interface Resources<T> {
public interface Templates<T> {

/**
* Find the velocity resource by filename in repository.
* Find the velocity template by filename in repository.
*
* @param fname The file name of velocity resource
* @param fname The file name of velocity template
* @return The resource.
* @throws RsException in case if resource not found.
* @throws TemplateException in case if resource not found.
*/
Resource<T> find(String fname) throws RsException;
Template<T> find(String fname) throws TemplateException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* The path(s) to velocity resources.
* The path(s) to velocity templates.
*/
package com.github.dgroup.velocity.path;
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.dgroup.velocity.rs;
package com.github.dgroup.velocity.template;

import com.github.dgroup.velocity.path.RelativePath;
import java.text.MessageFormat;
import org.cactoos.Scalar;
import org.cactoos.io.InputOf;

/**
* Velocity resource from the classpath.
* Velocity template from the classpath.
*
* @since 0.2.0
* @todo #/DEV:30min Add constructor RsClasspath(String) with relative path.
* Ensure that getResourceAsStream works on WinOs with '/' symbol.
*/
public final class RsClasspath extends RsEnvelope {
public final class Classpath extends TemplateEnvelope {

/**
* Build relative path to the resource from the classpath.
Expand All @@ -45,7 +45,7 @@ public final class RsClasspath extends RsEnvelope {
* @param pattern The pattern for {@link MessageFormat#format}.
* @param args The arguments for {@link MessageFormat#format}.
*/
public RsClasspath(final String pattern, final Object... args) {
public Classpath(final String pattern, final Object... args) {
this(new RelativePath(pattern, args));
}

Expand All @@ -56,7 +56,7 @@ public RsClasspath(final String pattern, final Object... args) {
* {@code src/main/resources/com/xxx/rs.txt} then relative path is
* {@code com/xxx/rs.txt}.
*/
public RsClasspath(final Scalar<String> rscr) {
public Classpath(final Scalar<String> rscr) {
super(
rscr,
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.dgroup.velocity.rs;
package com.github.dgroup.velocity.template;

import com.github.dgroup.velocity.Arg;
import com.github.dgroup.velocity.Resource;
import com.github.dgroup.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.cactoos.Scalar;

Expand All @@ -34,7 +34,7 @@
* @param <T> Type of fake resource.
* @since 0.1.0
*/
public final class RsFake<T> implements Resource<T> {
public final class FakeTemplate<T> implements Template<T> {

/**
* The fake resource.
Expand All @@ -45,7 +45,7 @@ public final class RsFake<T> implements Resource<T> {
* Ctor.
* @param val The fake resource.
*/
public RsFake(final T val) {
public FakeTemplate(final T val) {
this.val = val;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.dgroup.velocity.rs;
package com.github.dgroup.velocity.template;

import com.github.dgroup.velocity.Arg;
import com.github.dgroup.velocity.Resource;
import com.github.dgroup.velocity.Template;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.RuntimeSingleton;
Expand All @@ -39,13 +38,13 @@
import org.cactoos.text.TextOf;

/**
* The envelope for {@link Resource}.
* The envelope for {@link Template}.
*
* @since 0.1.0
* @todo #/DEV:30min Add RsJoined child which allows to join multiple templates
* in singe resource and compose them.
*/
public class RsEnvelope implements Resource<String> {
public class TemplateEnvelope implements Template<String> {

/**
* The func to evaluate the entity.
Expand All @@ -56,10 +55,13 @@ public class RsEnvelope implements Resource<String> {
* Ctor.
* @param tname The name of Velocity template.
* @param src The Velocity template as {@link org.cactoos.Input}.
* The input stream will be closed automatically by {@link RsEnvelope}.
* The input stream will be closed automatically by
* {@link TemplateEnvelope}.
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public RsEnvelope(final Scalar<String> tname, final Scalar<Input> src) {
public TemplateEnvelope(
final Scalar<String> tname, final Scalar<Input> src
) {
this(
ctx -> {
// @checkstyle IllegalCatchCheck (20 lines)
Expand All @@ -69,15 +71,16 @@ public RsEnvelope(final Scalar<String> tname, final Scalar<Input> src) {
final StringReader srdr = new StringReader(
new TextOf(inp).asString()
);
final Template template = new Template();
final org.apache.velocity.Template template =
new org.apache.velocity.Template();
template.setRuntimeServices(rsrv);
template.setData(rsrv.parse(srdr, tname.value()));
template.initDocument();
final StringWriter writer = new StringWriter();
template.merge(ctx, writer);
return writer.toString();
} catch (final Exception cause) {
throw new RsException(cause);
throw new TemplateException(cause);
}
}
);
Expand All @@ -87,18 +90,18 @@ public RsEnvelope(final Scalar<String> tname, final Scalar<Input> src) {
* Ctor.
* @param fnc The function to map the {@link VelocityContext} to entity.
*/
public RsEnvelope(final Func<VelocityContext, String> fnc) {
public TemplateEnvelope(final Func<VelocityContext, String> fnc) {
this.fnc = fnc;
}

@Override
public final String compose(final Arg... args) throws RsException {
public final String compose(final Arg... args) throws TemplateException {
return this.compose(new ListOf<>(args));
}

@Override
public final String compose(final Iterable<Arg> args)
throws RsException {
throws TemplateException {
return this.compose(
() -> {
final VelocityContext ctx = new VelocityContext();
Expand All @@ -114,11 +117,11 @@ public final String compose(final Iterable<Arg> args)
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public final String compose(final Scalar<VelocityContext> ctx)
// @checkstyle IllegalCatchCheck (5 lines)
throws RsException {
throws TemplateException {
try {
return this.fnc.apply(ctx.value());
} catch (final Exception cause) {
throw new RsException(cause);
throw new TemplateException(cause);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.dgroup.velocity.rs;
package com.github.dgroup.velocity.template;

/**
* For exceptions occurred during transformation of Apache Velocity templates.
*
* @since 0.1.0
*/
public final class RsException extends Exception {
public final class TemplateException extends Exception {

/**
* Ctor.
* @param cause The root cause.
*/
public RsException(final Exception cause) {
public TemplateException(final Exception cause) {
super(cause);
}

Expand All @@ -43,7 +43,7 @@ public RsException(final Exception cause) {
* @param msg The exception message.
* @param cause The root cause.
*/
public RsException(final String msg, final Exception cause) {
public TemplateException(final String msg, final Exception cause) {
super(msg, cause);
}

Expand Down
Loading

0 comments on commit c56cfb1

Please sign in to comment.