Compare commits

...

2 commits

11 changed files with 516 additions and 10 deletions

8
.idea/misc.xml generated
View file

@ -7,6 +7,14 @@
<pattern value="rife2.render.Trim" /> <pattern value="rife2.render.Trim" />
<pattern value="rife2.render.Uncapitalize" /> <pattern value="rife2.render.Uncapitalize" />
<pattern value="rife2.render.Uppercase" /> <pattern value="rife2.render.Uppercase" />
<pattern value="rife2.render.EncodeHtml" />
<pattern value="rife2.render.EncodeJson" />
<pattern value="rife2.render.EncodeUrl" />
<pattern value="rife2.render.EncodeXml" />
<pattern value="rife2.render.EncodeQp" />
<pattern value="rife2.render.EncodeHtmlEntities" />
<pattern value="rife2.render.Rot13" />
<pattern value="rife2.render.Year" />
</component> </component>
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="PDMPlugin"> <component name="PDMPlugin">

View file

@ -7,8 +7,20 @@ This project provides a set of template renderers.
- **rife.render.Capitalize** - **rife.render.Capitalize**
- Capitalizes a template value. - Capitalizes a template value.
- **rife.render.EncodeHtml**
- Encodes a template value to HTML.
- **rife.render.EncodeJson**
- Encodes a template value to JSON.
- **rife.render.EncodeQp**
- Converts a template value to a quoted-printable string.
- **rife.render.EncodeUrl**
- URL-encodes a template value.
- **rife.render.EncodeXml**
- Encodes a template value to XML.
- **rife.render.Lowercase** - **rife.render.Lowercase**
- Converts a template value to lowercase. - Converts a template value to lowercase.
- **rife.render.Rot13**
- Translates a template value to/from ROT13.
- **rife.render.SwapCase** - **rife.render.SwapCase**
- Swap case of a template value. - Swap case of a template value.
- **rife.render.Trim** - **rife.render.Trim**
@ -17,29 +29,31 @@ This project provides a set of template renderers.
- Un-capitalizes a template value. - Un-capitalizes a template value.
- **rife.render.Uppercase** - **rife.render.Uppercase**
- Convert a template value to uppercase. - Convert a template value to uppercase.
- **rife.render.Year**
- Renders the current year.
## Usage in Templates ## Usage in Templates
All rendered can be used at follows: All rendered can be used at follows:
```html ```
<!--v render:rife.render.RendererName:valueId/--> <!--v render:rife.render.RendererName:valueId/-->
``` ```
or or
```json ```
{{v render:rife.render.RendererName:valueId}} {{v render:rife.render.RendererName:valueId}}
``` ```
For example, to capitalize a template `foo` value: For example, to capitalize a template `foo` value:
```html ```
<!--v render:rife.render.Capitalize:foo/--> <!--v render:rife.render.Capitalize:foo/-->
``` ```
or or
```json ```
{{v render:rife.render.Capitalize:foo}} {{v render:rife.render.Capitalize:foo}}
``` ```

View file

@ -29,7 +29,7 @@ dependencies {
configurations { configurations {
all { all {
resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS); resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS)
} }
} }

View file

@ -0,0 +1,50 @@
/*
* Copyright 2023 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
*
* https://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.
*
*/
package rife2.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import rife.tools.StringUtils;
/**
* <p>Encodes a template value to HTML.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.EncodeHtml:valueId/--&gt;
* {{v render:rife.render.EncodeHtml:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @see StringUtils#encodeHtml(String)
* @since 1.0
*/
public class EncodeHtml implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return StringUtils.encodeHtml(template.getValue(differentiator));
} else {
return "";
}
}
}

View file

@ -0,0 +1,68 @@
/*
* Copyright 2023 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
*
* https://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.
*
*/
package rife2.render;
import rife.template.Template;
import rife.template.ValueRenderer;
/**
* <p>Encodes a template value to HTML decimal entities</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.EncodeHtmlEntities:valueId/--&gt;
* {{v render:rife.render.EncodeHtmlEntities:valueId}}
* </pre>
*
* <p>For example {@code john@doe.com} would be encoded to:</p>
*
* <pre>&amp;#106;&amp;#111;&amp;#104;&amp;#110;&amp;#64;&amp;#100;&amp;#111;&amp;#101;&amp;#46;&amp;#99;&amp;#111;&amp;#109;</pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class EncodeHtmlEntities implements ValueRenderer {
/**
* Converts a text string to HTML decimal entities.
*
* @param text the String to convert.
* @return the converted string.
*/
public static String toHtmlEntities(String text) {
var buff = new StringBuilder(text.length() * 6);
for (var i = 0; i < text.length(); i++) {
buff.append("&#").append((int) text.charAt(i)).append(';');
}
return buff.toString();
}
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return toHtmlEntities(template.getValue(differentiator));
} else {
return "";
}
}
}

View file

@ -0,0 +1,50 @@
/*
* Copyright 2023 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
*
* https://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.
*
*/
package rife2.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import rife.tools.StringUtils;
/**
* <p>Encodes a template value to JSON.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.EncodeJson:valueId/--&gt;
* {{v render:rife.render.EncodeJson:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @see StringUtils#encodeJson(String)
* @since 1.0
*/
public class EncodeJson implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return StringUtils.encodeJson(template.getValue(differentiator));
} else {
return "";
}
}
}

View file

@ -0,0 +1,86 @@
/*
* Copyright 2023 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
*
* https://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.
*
*/
package rife2.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import java.util.Locale;
/**
* <p>Converts a template value to a quoted-printable string.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.EncodeQp:valueId/--&gt;
* {{v render:rife.render.EncodeQp:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class EncodeQp implements ValueRenderer {
/**
* Converts the given String to a quoted-printable string.
*
* @param src the source String
* @return the quoted-printable String
*/
public static String toQuotedPrintable(String src) {
if (src == null || src.isEmpty()) {
return src;
}
char c;
var buff = new StringBuilder(src.length());
String hex;
for (var i = 0; i < src.length(); i++) {
c = src.charAt(i);
if (((c > 47) && (c < 58)) || ((c > 64) && (c < 91)) || ((c > 96) && (c < 123))) {
buff.append(c);
} else {
hex = Integer.toString(c, 16);
buff.append('=');
if (hex.length() == 1) {
buff.append('0');
}
buff.append(hex.toUpperCase(Locale.getDefault()));
}
}
return buff.toString();
}
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return toQuotedPrintable(template.getValue(differentiator));
} else {
return "";
}
}
}

View file

@ -0,0 +1,50 @@
/*
* Copyright 2023 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
*
* https://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.
*
*/
package rife2.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import rife.tools.StringUtils;
/**
* <p>URL-encodes a template value.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.EncodeUrl:valueId/--&gt;
* {{v render:rife.render.EncodeUrl:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @see StringUtils#encodeUrl(String)
* @since 1.0
*/
public class EncodeUrl implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return StringUtils.encodeUrl(template.getValue(differentiator));
} else {
return "";
}
}
}

View file

@ -0,0 +1,50 @@
/*
* Copyright 2023 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
*
* https://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.
*
*/
package rife2.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import rife.tools.StringUtils;
/**
* <p>Encodes a template value to XML.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.EncodeXml:valueId/--&gt;
* {{v render:rife.render.EncodeXml:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @see StringUtils#encodeXml(String)
* @since 1.0
*/
public class EncodeXml implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return StringUtils.encodeXml(template.getValue(differentiator));
} else {
return "";
}
}
}

View file

@ -0,0 +1,86 @@
/*
* Copyright 2023 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
*
* https://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.
*
*/
package rife2.render;
import rife.template.Template;
import rife.template.ValueRenderer;
/**
* <p>Translates a template value to/from ROT13.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.Rot13:valueId/--&gt;
* {{v render:rife.render.Rot13:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class Rot13 implements ValueRenderer {
/**
* Translates a String to/from ROT13.
*
* @param src The source String.
* @return The translated String.
*/
public static String rot13(String src) {
if (src == null || src.isEmpty()) {
return "";
} else {
var output = new StringBuilder(src.length());
for (var i = 0; i < src.length(); i++) {
var inChar = src.charAt(i);
if ((inChar >= 'A') && (inChar <= 'Z')) {
inChar += (char) 13;
if (inChar > 'Z') {
inChar -= (char) 26;
}
}
if ((inChar >= 'a') && (inChar <= 'z')) {
inChar += (char) 13;
if (inChar > 'z') {
inChar -= (char) 26;
}
}
output.append(inChar);
}
return output.toString();
}
}
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return rot13(template.getValue(differentiator));
} else {
return "";
}
}
}

View file

@ -0,0 +1,44 @@
/*
* Copyright 2023 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
*
* https://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.
*
*/
package rife2.render;
import rife.template.Template;
import rife.template.ValueRenderer;
/**
* <p>Renders the current year.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.Year/--&gt;
* {{v render:rife.render.Year}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class Year implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
return java.time.Year.now().toString();
}
}