sirius.web.templates.ContentHelper Maven / Gradle / Ivy
Show all versions of sirius-web Show documentation
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.web.templates;
import sirius.kernel.commons.Strings;
import sirius.kernel.di.std.Part;
import sirius.web.resources.Resource;
import sirius.web.resources.Resources;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Optional;
/**
* Provides helper methods used in Velocity Macros.
*
* The instance of this class is made available as helper
* via {@link DefaultGlobalContextExtender}.
*/
public class ContentHelper {
/**
* Contains the instance which is passed into the velocity context as "helper".
*/
public static final ContentHelper INSTANCE = new ContentHelper();
private static final String STRIP_XML_REGEX = "\\s*?[a-zA-Z0-9]+[^>]*>\\s*";
@Part
private static Resources resources;
private ContentHelper() {
}
/**
* Replaces new line with <br> tags.
*
* @param content the content to parse
* @return the value of content where all line breaks are replaced by <br> tags.
*/
public String nl2br(String content) {
if (content == null) {
return null;
}
return content.replace("\n", "
");
}
/**
* Removes all XML characters (<, >, ", ', &) from the input and replaces it with the well known
* replacement characters.
*
* @param aText the text to replace
* @return a string which can safely output in XML or HTML. Returns an empty string if the input was null.
*/
@Nonnull
public static String escapeXML(@Nullable Object aText) {
if (Strings.isEmpty(aText)) {
return "";
}
final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(aText.toString());
char character = iterator.current();
while (character != CharacterIterator.DONE) {
if (character == '<') {
result.append("<");
} else if (character == '>') {
result.append(">");
} else if (character == '\"') {
result.append(""");
} else if (character == '\'') {
result.append("'");
} else if (character == '&') {
result.append("&");
} else {
// the char is not a special one
// add it to the result as is
result.append(character);
}
character = iterator.next();
}
return result.toString();
}
/**
* Removes all XML tags from a given content.
*
* @param content content to strip XML of
* @return content without XML tags
*/
public String stripXML(String content) {
if (Strings.isEmpty(content)) {
return content;
}
String alreadyStrippedContent = content;
String contentToStrip;
do {
contentToStrip = alreadyStrippedContent;
alreadyStrippedContent = contentToStrip.replaceFirst(STRIP_XML_REGEX, " ");
} while (!Strings.areEqual(contentToStrip, alreadyStrippedContent));
return alreadyStrippedContent;
}
/**
* Returns the contents of the given template as a single line string which can be embedded into a string
* enclosed by ' (e.g. a JavaScript string).
*
* @param resource the template to fetch
* @return the contents of the template without line breaks and with escaped ticks ('). If the template
* cannot be found an empty string is returned
*/
@Nonnull
public String getResourceAsInlineString(String resource) {
Optional res = resources.resolve(resource);
return res.map(r -> r.getContentAsString().replaceAll("\\r?\\n", " ").replace("'", "\\'")).orElse("");
}
}