Initial commit

This commit is contained in:
Erik C. Thauvin 2023-03-13 19:03:26 -07:00
commit ffe05f5b2e
23 changed files with 1116 additions and 0 deletions

51
lib/build.gradle.kts Normal file
View file

@ -0,0 +1,51 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
plugins {
`java-library`
id("com.github.ben-manes.versions") version "0.46.0"
}
version = "0.9.0-SNAPSHOT"
repositories {
mavenLocal()
mavenCentral()
maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots") } // only needed for SNAPSHOT
}
dependencies {
implementation("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT") {
this.isChanging = true
}
runtimeOnly("com.uwyn.rife2:rife2:1.5.0-SNAPSHOT:agent") {
this.isChanging = true
}
testImplementation(platform("org.junit:junit-bom:5.9.2"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
configurations {
all {
resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS);
}
}
java {
withJavadocJar()
withSourcesJar()
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
tasks {
test {
useJUnitPlatform()
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
}
}

View file

@ -0,0 +1,52 @@
/*
* 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;
import java.util.regex.Pattern;
/**
* <p>Capitalizes a template value.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.Capitalize:valueId/--&gt;
* {{v render:rife.render.Capitalize:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class Capitalize implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
var value = template.getValue(differentiator);
return value.substring(0, 1).toUpperCase(Locale.getDefault()) + value.substring(1);
} 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 java.util.Locale;
/**
* <p>Converts a template value to lowercase.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.Lowercase:valueId/--&gt;
* {{v render:rife.render.Lowercase:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class Lowercase implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return template.getValue(differentiator).toLowerCase(Locale.getDefault());
} else {
return "";
}
}
}

View file

@ -0,0 +1,80 @@
/*
* 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>Swap case of a template value.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.SwapCase:valueId/--&gt;
* {{v render:rife.render.SwapCase:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class SwapCase implements ValueRenderer {
/**
* Swaps the case of a String.
* @param s the String to swap the case of
* @return the modified String or null
*/
public static String swapCase(String s) {
if (s == null || s.isEmpty()) {
return s;
}
var buffer = s.toCharArray();
var whitespace = true;
for (var i = 0; i < buffer.length; i++) {
var ch = buffer[i];
if (Character.isUpperCase(ch) || Character.isTitleCase(ch)) {
buffer[i] = Character.toLowerCase(ch);
whitespace = false;
} else if (Character.isLowerCase(ch)) {
if (whitespace) {
buffer[i] = Character.toTitleCase(ch);
whitespace = false;
} else {
buffer[i] = Character.toUpperCase(ch);
}
} else {
whitespace = Character.isWhitespace(ch);
}
}
return new String(buffer);
}
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return swapCase(template.getValue(differentiator));
} else {
return "";
}
}
}

View file

@ -0,0 +1,48 @@
/*
* 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>Removes leading and trailing whitespace from a template value.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.Trim:valueId/--&gt;
* {{v render:rife.render.Trim:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class Trim implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return template.getValue(differentiator).trim();
} else {
return "";
}
}
}

View file

@ -0,0 +1,51 @@
/*
* 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>Un-capitalizes a template value.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.Uncapitalize:valueId/--&gt;
* {{v render:rife.render.Uncapitalize:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class Uncapitalize implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
var value = template.getValue(differentiator);
return value.substring(0, 1).toLowerCase(Locale.getDefault()) + value.substring(1);
} 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 java.util.Locale;
/**
* <p>Convert a template value to uppercase.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.Uppercase:valueId/--&gt;
* {{v render:rife.render.Uppercase:valueId}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public class Uppercase implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
if (differentiator != null && !differentiator.isBlank() && template.hasValueId(differentiator)) {
return template.getValue(differentiator).toUpperCase(Locale.getDefault());
} else {
return "";
}
}
}