You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2017/05/14 10:52:54 UTC
[11/51] [partial] incubator-freemarker git commit: Migrated from Ant
to Gradle, and modularized the project. This is an incomplete migration;
there are some TODO-s in the build scripts, and release related tasks are
still missing. What works: Building th
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/util/_StringUtil.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/util/_StringUtil.java b/freemarker-core/src/main/java/org/apache/freemarker/core/util/_StringUtil.java
new file mode 100644
index 0000000..b53aae8
--- /dev/null
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/util/_StringUtil.java
@@ -0,0 +1,1675 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.freemarker.core.util;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.regex.Pattern;
+
+import org.apache.freemarker.core.Environment;
+import org.apache.freemarker.core.ParsingConfiguration;
+import org.apache.freemarker.core.Template;
+import org.apache.freemarker.core.Version;
+
+/** Don't use this; used internally by FreeMarker, might changes without notice. */
+public class _StringUtil {
+
+ private static final char[] LT = new char[] { '&', 'l', 't', ';' };
+ private static final char[] GT = new char[] { '&', 'g', 't', ';' };
+ private static final char[] AMP = new char[] { '&', 'a', 'm', 'p', ';' };
+ private static final char[] QUOT = new char[] { '&', 'q', 'u', 'o', 't', ';' };
+ private static final char[] HTML_APOS = new char[] { '&', '#', '3', '9', ';' };
+ private static final char[] XML_APOS = new char[] { '&', 'a', 'p', 'o', 's', ';' };
+
+ /**
+ * XML Encoding.
+ * Replaces all '>' '<' '&', "'" and '"' with entity reference
+ */
+ public static String XMLEnc(String s) {
+ return XMLOrHTMLEnc(s, true, true, XML_APOS);
+ }
+
+ /**
+ * Like {@link #XMLEnc(String)}, but writes the result into a {@link Writer}.
+ *
+ * @since 2.3.24
+ */
+ public static void XMLEnc(String s, Writer out) throws IOException {
+ XMLOrHTMLEnc(s, XML_APOS, out);
+ }
+
+ /**
+ * XHTML Encoding.
+ * Replaces all '>' '<' '&', "'" and '"' with entity reference
+ * suitable for XHTML decoding in common user agents (including legacy
+ * user agents, which do not decode "&apos;" to "'", so "&#39;" is used
+ * instead [see http://www.w3.org/TR/xhtml1/#C_16])
+ */
+ public static String XHTMLEnc(String s) {
+ return XMLOrHTMLEnc(s, true, true, HTML_APOS);
+ }
+
+ /**
+ * Like {@link #XHTMLEnc(String)}, but writes the result into a {@link Writer}.
+ *
+ * @since 2.3.24
+ */
+ public static void XHTMLEnc(String s, Writer out) throws IOException {
+ XMLOrHTMLEnc(s, HTML_APOS, out);
+ }
+
+ private static String XMLOrHTMLEnc(String s, boolean escGT, boolean escQuot, char[] apos) {
+ final int ln = s.length();
+
+ // First we find out if we need to escape, and if so, what the length of the output will be:
+ int firstEscIdx = -1;
+ int lastEscIdx = 0;
+ int plusOutLn = 0;
+ for (int i = 0; i < ln; i++) {
+ escape: do {
+ final char c = s.charAt(i);
+ switch (c) {
+ case '<':
+ plusOutLn += LT.length - 1;
+ break;
+ case '>':
+ if (!(escGT || maybeCDataEndGT(s, i))) {
+ break escape;
+ }
+ plusOutLn += GT.length - 1;
+ break;
+ case '&':
+ plusOutLn += AMP.length - 1;
+ break;
+ case '"':
+ if (!escQuot) {
+ break escape;
+ }
+ plusOutLn += QUOT.length - 1;
+ break;
+ case '\'': // apos
+ if (apos == null) {
+ break escape;
+ }
+ plusOutLn += apos.length - 1;
+ break;
+ default:
+ break escape;
+ }
+
+ if (firstEscIdx == -1) {
+ firstEscIdx = i;
+ }
+ lastEscIdx = i;
+ } while (false);
+ }
+
+ if (firstEscIdx == -1) {
+ return s; // Nothing to escape
+ } else {
+ final char[] esced = new char[ln + plusOutLn];
+ if (firstEscIdx != 0) {
+ s.getChars(0, firstEscIdx, esced, 0);
+ }
+ int dst = firstEscIdx;
+ scan: for (int i = firstEscIdx; i <= lastEscIdx; i++) {
+ final char c = s.charAt(i);
+ switch (c) {
+ case '<':
+ dst = shortArrayCopy(LT, esced, dst);
+ continue scan;
+ case '>':
+ if (!(escGT || maybeCDataEndGT(s, i))) {
+ break;
+ }
+ dst = shortArrayCopy(GT, esced, dst);
+ continue scan;
+ case '&':
+ dst = shortArrayCopy(AMP, esced, dst);
+ continue scan;
+ case '"':
+ if (!escQuot) {
+ break;
+ }
+ dst = shortArrayCopy(QUOT, esced, dst);
+ continue scan;
+ case '\'': // apos
+ if (apos == null) {
+ break;
+ }
+ dst = shortArrayCopy(apos, esced, dst);
+ continue scan;
+ }
+ esced[dst++] = c;
+ }
+ if (lastEscIdx != ln - 1) {
+ s.getChars(lastEscIdx + 1, ln, esced, dst);
+ }
+
+ return String.valueOf(esced);
+ }
+ }
+
+ private static boolean maybeCDataEndGT(String s, int i) {
+ if (i == 0) return true;
+ if (s.charAt(i - 1) != ']') return false;
+ return i == 1 || s.charAt(i - 2) == ']';
+ }
+
+ private static void XMLOrHTMLEnc(String s, char[] apos, Writer out) throws IOException {
+ int writtenEnd = 0; // exclusive end
+ int ln = s.length();
+ for (int i = 0; i < ln; i++) {
+ char c = s.charAt(i);
+ if (c == '<' || c == '>' || c == '&' || c == '"' || c == '\'') {
+ int flushLn = i - writtenEnd;
+ if (flushLn != 0) {
+ out.write(s, writtenEnd, flushLn);
+ }
+ writtenEnd = i + 1;
+
+ switch (c) {
+ case '<': out.write(LT); break;
+ case '>': out.write(GT); break;
+ case '&': out.write(AMP); break;
+ case '"': out.write(QUOT); break;
+ default: out.write(apos); break;
+ }
+ }
+ }
+ if (writtenEnd < ln) {
+ out.write(s, writtenEnd, ln - writtenEnd);
+ }
+ }
+
+ /**
+ * For efficiently copying very short char arrays.
+ */
+ private static int shortArrayCopy(char[] src, char[] dst, int dstOffset) {
+ for (char aSrc : src) {
+ dst[dstOffset++] = aSrc;
+ }
+ return dstOffset;
+ }
+
+ /**
+ * XML encoding without replacing apostrophes.
+ * @see #XMLEnc(String)
+ */
+ public static String XMLEncNA(String s) {
+ return XMLOrHTMLEnc(s, true, true, null);
+ }
+
+ /**
+ * XML encoding for attribute values quoted with <tt>"</tt> (not with <tt>'</tt>!).
+ * Also can be used for HTML attributes that are quoted with <tt>"</tt>.
+ * @see #XMLEnc(String)
+ */
+ public static String XMLEncQAttr(String s) {
+ return XMLOrHTMLEnc(s, false, true, null);
+ }
+
+ /**
+ * XML encoding without replacing apostrophes and quotation marks and
+ * greater-thans (except in {@code ]]>}).
+ * @see #XMLEnc(String)
+ */
+ public static String XMLEncNQG(String s) {
+ return XMLOrHTMLEnc(s, false, false, null);
+ }
+
+ /**
+ * Rich Text Format encoding (does not replace line breaks).
+ * Escapes all '\' '{' '}'.
+ */
+ public static String RTFEnc(String s) {
+ int ln = s.length();
+
+ // First we find out if we need to escape, and if so, what the length of the output will be:
+ int firstEscIdx = -1;
+ int lastEscIdx = 0;
+ int plusOutLn = 0;
+ for (int i = 0; i < ln; i++) {
+ char c = s.charAt(i);
+ if (c == '{' || c == '}' || c == '\\') {
+ if (firstEscIdx == -1) {
+ firstEscIdx = i;
+ }
+ lastEscIdx = i;
+ plusOutLn++;
+ }
+ }
+
+ if (firstEscIdx == -1) {
+ return s; // Nothing to escape
+ } else {
+ char[] esced = new char[ln + plusOutLn];
+ if (firstEscIdx != 0) {
+ s.getChars(0, firstEscIdx, esced, 0);
+ }
+ int dst = firstEscIdx;
+ for (int i = firstEscIdx; i <= lastEscIdx; i++) {
+ char c = s.charAt(i);
+ if (c == '{' || c == '}' || c == '\\') {
+ esced[dst++] = '\\';
+ }
+ esced[dst++] = c;
+ }
+ if (lastEscIdx != ln - 1) {
+ s.getChars(lastEscIdx + 1, ln, esced, dst);
+ }
+
+ return String.valueOf(esced);
+ }
+ }
+
+ /**
+ * Like {@link #RTFEnc(String)}, but writes the result into a {@link Writer}.
+ *
+ * @since 2.3.24
+ */
+ public static void RTFEnc(String s, Writer out) throws IOException {
+ int writtenEnd = 0; // exclusive end
+ int ln = s.length();
+ for (int i = 0; i < ln; i++) {
+ char c = s.charAt(i);
+ if (c == '{' || c == '}' || c == '\\') {
+ int flushLn = i - writtenEnd;
+ if (flushLn != 0) {
+ out.write(s, writtenEnd, flushLn);
+ }
+ out.write('\\');
+ writtenEnd = i; // Not i + 1, so c will be written out later
+ }
+ }
+ if (writtenEnd < ln) {
+ out.write(s, writtenEnd, ln - writtenEnd);
+ }
+ }
+
+
+ /**
+ * URL encoding (like%20this) for query parameter values, path <em>segments</em>, fragments; this encodes all
+ * characters that are reserved anywhere.
+ */
+ public static String URLEnc(String s, Charset charset) throws UnsupportedEncodingException {
+ return URLEnc(s, charset, false);
+ }
+
+ /**
+ * Like {@link #URLEnc(String, Charset)} but doesn't escape the slash character ({@code /}).
+ * This can be used to encode a path only if you know that no folder or file name will contain {@code /}
+ * character (not in the path, but in the name itself), which usually stands, as the commonly used OS-es don't
+ * allow that.
+ *
+ * @since 2.3.21
+ */
+ public static String URLPathEnc(String s, Charset charset) throws UnsupportedEncodingException {
+ return URLEnc(s, charset, true);
+ }
+
+ private static String URLEnc(String s, Charset charset, boolean keepSlash)
+ throws UnsupportedEncodingException {
+ int ln = s.length();
+ int i;
+ for (i = 0; i < ln; i++) {
+ char c = s.charAt(i);
+ if (!safeInURL(c, keepSlash)) {
+ break;
+ }
+ }
+ if (i == ln) {
+ // Nothing to escape
+ return s;
+ }
+
+ StringBuilder b = new StringBuilder(ln + ln / 3 + 2);
+ b.append(s.substring(0, i));
+
+ int encStart = i;
+ for (i++; i < ln; i++) {
+ char c = s.charAt(i);
+ if (safeInURL(c, keepSlash)) {
+ if (encStart != -1) {
+ byte[] o = s.substring(encStart, i).getBytes(charset);
+ for (byte bc : o) {
+ b.append('%');
+ int c1 = bc & 0x0F;
+ int c2 = (bc >> 4) & 0x0F;
+ b.append((char) (c2 < 10 ? c2 + '0' : c2 - 10 + 'A'));
+ b.append((char) (c1 < 10 ? c1 + '0' : c1 - 10 + 'A'));
+ }
+ encStart = -1;
+ }
+ b.append(c);
+ } else {
+ if (encStart == -1) {
+ encStart = i;
+ }
+ }
+ }
+ if (encStart != -1) {
+ byte[] o = s.substring(encStart, i).getBytes(charset);
+ for (byte bc : o) {
+ b.append('%');
+ int c1 = bc & 0x0F;
+ int c2 = (bc >> 4) & 0x0F;
+ b.append((char) (c2 < 10 ? c2 + '0' : c2 - 10 + 'A'));
+ b.append((char) (c1 < 10 ? c1 + '0' : c1 - 10 + 'A'));
+ }
+ }
+
+ return b.toString();
+ }
+
+ private static boolean safeInURL(char c, boolean keepSlash) {
+ return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'
+ || c >= '0' && c <= '9'
+ || c == '_' || c == '-' || c == '.' || c == '!' || c == '~'
+ || c >= '\'' && c <= '*'
+ || keepSlash && c == '/';
+ }
+
+ public static Locale deduceLocale(String input) {
+ if (input == null) return null;
+ Locale locale = Locale.getDefault();
+ if (input.length() > 0 && input.charAt(0) == '"') input = input.substring(1, input.length() - 1);
+ StringTokenizer st = new StringTokenizer(input, ",_ ");
+ String lang = "", country = "";
+ if (st.hasMoreTokens()) {
+ lang = st.nextToken();
+ }
+ if (st.hasMoreTokens()) {
+ country = st.nextToken();
+ }
+ if (!st.hasMoreTokens()) {
+ locale = new Locale(lang, country);
+ } else {
+ locale = new Locale(lang, country, st.nextToken());
+ }
+ return locale;
+ }
+
+ public static String capitalize(String s) {
+ StringTokenizer st = new StringTokenizer(s, " \t\r\n", true);
+ StringBuilder buf = new StringBuilder(s.length());
+ while (st.hasMoreTokens()) {
+ String tok = st.nextToken();
+ buf.append(tok.substring(0, 1).toUpperCase());
+ buf.append(tok.substring(1).toLowerCase());
+ }
+ return buf.toString();
+ }
+
+ public static boolean getYesNo(String s) {
+ if (s.startsWith("\"")) {
+ s = s.substring(1, s.length() - 1);
+
+ }
+ if (s.equalsIgnoreCase("n")
+ || s.equalsIgnoreCase("no")
+ || s.equalsIgnoreCase("f")
+ || s.equalsIgnoreCase("false")) {
+ return false;
+ } else if (s.equalsIgnoreCase("y")
+ || s.equalsIgnoreCase("yes")
+ || s.equalsIgnoreCase("t")
+ || s.equalsIgnoreCase("true")) {
+ return true;
+ }
+ throw new IllegalArgumentException("Illegal boolean value: " + s);
+ }
+
+ /**
+ * Splits a string at the specified character.
+ */
+ public static String[] split(String s, char c) {
+ int i, b, e;
+ int cnt;
+ String res[];
+ int ln = s.length();
+
+ i = 0;
+ cnt = 1;
+ while ((i = s.indexOf(c, i)) != -1) {
+ cnt++;
+ i++;
+ }
+ res = new String[cnt];
+
+ i = 0;
+ b = 0;
+ while (b <= ln) {
+ e = s.indexOf(c, b);
+ if (e == -1) e = ln;
+ res[i++] = s.substring(b, e);
+ b = e + 1;
+ }
+ return res;
+ }
+
+ /**
+ * Splits a string at the specified string.
+ */
+ public static String[] split(String s, String sep, boolean caseInsensitive) {
+ String splitString = caseInsensitive ? sep.toLowerCase() : sep;
+ String input = caseInsensitive ? s.toLowerCase() : s;
+ int i, b, e;
+ int cnt;
+ String res[];
+ int ln = s.length();
+ int sln = sep.length();
+
+ if (sln == 0) throw new IllegalArgumentException(
+ "The separator string has 0 length");
+
+ i = 0;
+ cnt = 1;
+ while ((i = input.indexOf(splitString, i)) != -1) {
+ cnt++;
+ i += sln;
+ }
+ res = new String[cnt];
+
+ i = 0;
+ b = 0;
+ while (b <= ln) {
+ e = input.indexOf(splitString, b);
+ if (e == -1) e = ln;
+ res[i++] = s.substring(b, e);
+ b = e + sln;
+ }
+ return res;
+ }
+
+ /**
+ * Same as {@link #replace(String, String, String, boolean, boolean)} with two {@code false} parameters.
+ * @since 2.3.20
+ */
+ public static String replace(String text, String oldSub, String newSub) {
+ return replace(text, oldSub, newSub, false, false);
+ }
+
+ /**
+ * Replaces all occurrences of a sub-string in a string.
+ * @param text The string where it will replace <code>oldSub</code> with
+ * <code>newSub</code>.
+ * @return String The string after the replacements.
+ */
+ public static String replace(String text,
+ String oldSub,
+ String newSub,
+ boolean caseInsensitive,
+ boolean firstOnly) {
+ StringBuilder buf;
+ int tln;
+ int oln = oldSub.length();
+
+ if (oln == 0) {
+ int nln = newSub.length();
+ if (nln == 0) {
+ return text;
+ } else {
+ if (firstOnly) {
+ return newSub + text;
+ } else {
+ tln = text.length();
+ buf = new StringBuilder(tln + (tln + 1) * nln);
+ buf.append(newSub);
+ for (int i = 0; i < tln; i++) {
+ buf.append(text.charAt(i));
+ buf.append(newSub);
+ }
+ return buf.toString();
+ }
+ }
+ } else {
+ oldSub = caseInsensitive ? oldSub.toLowerCase() : oldSub;
+ String input = caseInsensitive ? text.toLowerCase() : text;
+ int e = input.indexOf(oldSub);
+ if (e == -1) {
+ return text;
+ }
+ int b = 0;
+ tln = text.length();
+ buf = new StringBuilder(
+ tln + Math.max(newSub.length() - oln, 0) * 3);
+ do {
+ buf.append(text.substring(b, e));
+ buf.append(newSub);
+ b = e + oln;
+ e = input.indexOf(oldSub, b);
+ } while (e != -1 && !firstOnly);
+ buf.append(text.substring(b));
+ return buf.toString();
+ }
+ }
+
+ /**
+ * Removes a line-break from the end of the string (if there's any).
+ */
+ public static String chomp(String s) {
+ if (s.endsWith("\r\n")) return s.substring(0, s.length() - 2);
+ if (s.endsWith("\r") || s.endsWith("\n"))
+ return s.substring(0, s.length() - 1);
+ return s;
+ }
+
+ /**
+ * Converts a 0-length string to null, leaves the string as is otherwise.
+ * @param s maybe {@code null}.
+ */
+ public static String emptyToNull(String s) {
+ if (s == null) return null;
+ return s.length() == 0 ? null : s;
+ }
+
+ /**
+ * Converts the parameter with <code>toString</code> (if it's not <code>null</code>) and passes it to
+ * {@link #jQuote(String)}.
+ */
+ public static String jQuote(Object obj) {
+ return jQuote(obj != null ? obj.toString() : null);
+ }
+
+ /**
+ * Quotes string as Java Language string literal.
+ * Returns string <code>"null"</code> if <code>s</code>
+ * is <code>null</code>.
+ */
+ public static String jQuote(String s) {
+ if (s == null) {
+ return "null";
+ }
+ int ln = s.length();
+ StringBuilder b = new StringBuilder(ln + 4);
+ b.append('"');
+ for (int i = 0; i < ln; i++) {
+ char c = s.charAt(i);
+ if (c == '"') {
+ b.append("\\\"");
+ } else if (c == '\\') {
+ b.append("\\\\");
+ } else if (c < 0x20) {
+ if (c == '\n') {
+ b.append("\\n");
+ } else if (c == '\r') {
+ b.append("\\r");
+ } else if (c == '\f') {
+ b.append("\\f");
+ } else if (c == '\b') {
+ b.append("\\b");
+ } else if (c == '\t') {
+ b.append("\\t");
+ } else {
+ b.append("\\u00");
+ int x = c / 0x10;
+ b.append(toHexDigit(x));
+ x = c & 0xF;
+ b.append(toHexDigit(x));
+ }
+ } else {
+ b.append(c);
+ }
+ } // for each characters
+ b.append('"');
+ return b.toString();
+ }
+
+ /**
+ * Converts the parameter with <code>toString</code> (if not
+ * <code>null</code>)and passes it to {@link #jQuoteNoXSS(String)}.
+ */
+ public static String jQuoteNoXSS(Object obj) {
+ return jQuoteNoXSS(obj != null ? obj.toString() : null);
+ }
+
+ /**
+ * Same as {@link #jQuote(String)} but also escapes <code>'<'</code>
+ * as <code>\</code><code>u003C</code>. This is used for log messages to prevent XSS
+ * on poorly written Web-based log viewers.
+ */
+ public static String jQuoteNoXSS(String s) {
+ if (s == null) {
+ return "null";
+ }
+ int ln = s.length();
+ StringBuilder b = new StringBuilder(ln + 4);
+ b.append('"');
+ for (int i = 0; i < ln; i++) {
+ char c = s.charAt(i);
+ if (c == '"') {
+ b.append("\\\"");
+ } else if (c == '\\') {
+ b.append("\\\\");
+ } else if (c == '<') {
+ b.append("\\u003C");
+ } else if (c < 0x20) {
+ if (c == '\n') {
+ b.append("\\n");
+ } else if (c == '\r') {
+ b.append("\\r");
+ } else if (c == '\f') {
+ b.append("\\f");
+ } else if (c == '\b') {
+ b.append("\\b");
+ } else if (c == '\t') {
+ b.append("\\t");
+ } else {
+ b.append("\\u00");
+ int x = c / 0x10;
+ b.append(toHexDigit(x));
+ x = c & 0xF;
+ b.append(toHexDigit(x));
+ }
+ } else {
+ b.append(c);
+ }
+ } // for each characters
+ b.append('"');
+ return b.toString();
+ }
+
+ /**
+ * Escapes the <code>String</code> with the escaping rules of Java language
+ * string literals, so it's safe to insert the value into a string literal.
+ * The resulting string will not be quoted.
+ *
+ * <p>All characters under UCS code point 0x20 will be escaped.
+ * Where they have no dedicated escape sequence in Java, they will
+ * be replaced with hexadecimal escape (<tt>\</tt><tt>u<i>XXXX</i></tt>).
+ *
+ * @see #jQuote(String)
+ */
+ public static String javaStringEnc(String s) {
+ int ln = s.length();
+ for (int i = 0; i < ln; i++) {
+ char c = s.charAt(i);
+ if (c == '"' || c == '\\' || c < 0x20) {
+ StringBuilder b = new StringBuilder(ln + 4);
+ b.append(s.substring(0, i));
+ while (true) {
+ if (c == '"') {
+ b.append("\\\"");
+ } else if (c == '\\') {
+ b.append("\\\\");
+ } else if (c < 0x20) {
+ if (c == '\n') {
+ b.append("\\n");
+ } else if (c == '\r') {
+ b.append("\\r");
+ } else if (c == '\f') {
+ b.append("\\f");
+ } else if (c == '\b') {
+ b.append("\\b");
+ } else if (c == '\t') {
+ b.append("\\t");
+ } else {
+ b.append("\\u00");
+ int x = c / 0x10;
+ b.append((char)
+ (x < 0xA ? x + '0' : x - 0xA + 'a'));
+ x = c & 0xF;
+ b.append((char)
+ (x < 0xA ? x + '0' : x - 0xA + 'a'));
+ }
+ } else {
+ b.append(c);
+ }
+ i++;
+ if (i >= ln) {
+ return b.toString();
+ }
+ c = s.charAt(i);
+ }
+ } // if has to be escaped
+ } // for each characters
+ return s;
+ }
+
+ /**
+ * Escapes a {@link String} to be safely insertable into a JavaScript string literal; for more see
+ * {@link #jsStringEnc(String, boolean) jsStringEnc(s, false)}.
+ */
+ public static String javaScriptStringEnc(String s) {
+ return jsStringEnc(s, false);
+ }
+
+ /**
+ * Escapes a {@link String} to be safely insertable into a JSON string literal; for more see
+ * {@link #jsStringEnc(String, boolean) jsStringEnc(s, true)}.
+ */
+ public static String jsonStringEnc(String s) {
+ return jsStringEnc(s, true);
+ }
+
+ private static final int NO_ESC = 0;
+ private static final int ESC_HEXA = 1;
+ private static final int ESC_BACKSLASH = 3;
+
+ /**
+ * Escapes a {@link String} to be safely insertable into a JavaScript or a JSON string literal.
+ * The resulting string will <em>not</em> be quoted; the caller must ensure that they are there in the final
+ * output. Note that for JSON, the quotation marks must be {@code "}, not {@code '}, because JSON doesn't escape
+ * {@code '}.
+ *
+ * <p>The escaping rules guarantee that if the inside of the JavaScript/JSON string literal is from one or more
+ * touching pieces that were escaped with this, no character sequence can occur that closes the
+ * JavaScript/JSON string literal, or has a meaning in HTML/XML that causes the HTML script section to be closed.
+ * (If, however, the escaped section is preceded by or followed by strings from other sources, this can't be
+ * guaranteed in some rare cases. Like <tt>x = "</${a?js_string}"</tt> might closes the "script"
+ * element if {@code a} is {@code "script>"}.)
+ *
+ * The escaped characters are:
+ *
+ * <table style="width: auto; border-collapse: collapse" border="1" summary="Characters escaped by jsStringEnc">
+ * <tr>
+ * <th>Input
+ * <th>Output
+ * <tr>
+ * <td><tt>"</tt>
+ * <td><tt>\"</tt>
+ * <tr>
+ * <td><tt>'</tt> if not in JSON-mode
+ * <td><tt>\'</tt>
+ * <tr>
+ * <td><tt>\</tt>
+ * <td><tt>\\</tt>
+ * <tr>
+ * <td><tt>/</tt> if the method can't know that it won't be directly after <tt><</tt>
+ * <td><tt>\/</tt>
+ * <tr>
+ * <td><tt>></tt> if the method can't know that it won't be directly after <tt>]]</tt> or <tt>--</tt>
+ * <td>JavaScript: <tt>\></tt>; JSON: <tt>\</tt><tt>u003E</tt>
+ * <tr>
+ * <td><tt><</tt> if the method can't know that it won't be directly followed by <tt>!</tt> or <tt>?</tt>
+ * <td><tt><tt>\</tt>u003C</tt>
+ * <tr>
+ * <td>
+ * u0000-u001f (UNICODE control characters - disallowed by JSON)<br>
+ * u007f-u009f (UNICODE control characters - disallowed by JSON)
+ * <td><tt>\n</tt>, <tt>\r</tt> and such, or if there's no such dedicated escape:
+ * JavaScript: <tt>\x<i>XX</i></tt>, JSON: <tt>\<tt>u</tt><i>XXXX</i></tt>
+ * <tr>
+ * <td>
+ * u2028 (Line separator - source code line-break in ECMAScript)<br>
+ * u2029 (Paragraph separator - source code line-break in ECMAScript)<br>
+ * <td><tt>\<tt>u</tt><i>XXXX</i></tt>
+ * </table>
+ *
+ * @since 2.3.20
+ */
+ public static String jsStringEnc(String s, boolean json) {
+ _NullArgumentException.check("s", s);
+
+ int ln = s.length();
+ StringBuilder sb = null;
+ for (int i = 0; i < ln; i++) {
+ final char c = s.charAt(i);
+ final int escapeType; //
+ if (!(c > '>' && c < 0x7F && c != '\\') && c != ' ' && !(c >= 0xA0 && c < 0x2028)) { // skip common chars
+ if (c <= 0x1F) { // control chars range 1
+ if (c == '\n') {
+ escapeType = 'n';
+ } else if (c == '\r') {
+ escapeType = 'r';
+ } else if (c == '\f') {
+ escapeType = 'f';
+ } else if (c == '\b') {
+ escapeType = 'b';
+ } else if (c == '\t') {
+ escapeType = 't';
+ } else {
+ escapeType = ESC_HEXA;
+ }
+ } else if (c == '"') {
+ escapeType = ESC_BACKSLASH;
+ } else if (c == '\'') {
+ escapeType = json ? NO_ESC : ESC_BACKSLASH;
+ } else if (c == '\\') {
+ escapeType = ESC_BACKSLASH;
+ } else if (c == '/' && (i == 0 || s.charAt(i - 1) == '<')) { // against closing elements
+ escapeType = ESC_BACKSLASH;
+ } else if (c == '>') { // against "]]> and "-->"
+ final boolean dangerous;
+ if (i == 0) {
+ dangerous = true;
+ } else {
+ final char prevC = s.charAt(i - 1);
+ if (prevC == ']' || prevC == '-') {
+ if (i == 1) {
+ dangerous = true;
+ } else {
+ final char prevPrevC = s.charAt(i - 2);
+ dangerous = prevPrevC == prevC;
+ }
+ } else {
+ dangerous = false;
+ }
+ }
+ escapeType = dangerous ? (json ? ESC_HEXA : ESC_BACKSLASH) : NO_ESC;
+ } else if (c == '<') { // against "<!"
+ final boolean dangerous;
+ if (i == ln - 1) {
+ dangerous = true;
+ } else {
+ char nextC = s.charAt(i + 1);
+ dangerous = nextC == '!' || nextC == '?';
+ }
+ escapeType = dangerous ? ESC_HEXA : NO_ESC;
+ } else if ((c >= 0x7F && c <= 0x9F) // control chars range 2
+ || (c == 0x2028 || c == 0x2029) // UNICODE line terminators
+ ) {
+ escapeType = ESC_HEXA;
+ } else {
+ escapeType = NO_ESC;
+ }
+
+ if (escapeType != NO_ESC) { // If needs escaping
+ if (sb == null) {
+ sb = new StringBuilder(ln + 6);
+ sb.append(s.substring(0, i));
+ }
+
+ sb.append('\\');
+ if (escapeType > 0x20) {
+ sb.append((char) escapeType);
+ } else if (escapeType == ESC_HEXA) {
+ if (!json && c < 0x100) {
+ sb.append('x');
+ sb.append(toHexDigit(c >> 4));
+ sb.append(toHexDigit(c & 0xF));
+ } else {
+ sb.append('u');
+ sb.append(toHexDigit((c >> 12) & 0xF));
+ sb.append(toHexDigit((c >> 8) & 0xF));
+ sb.append(toHexDigit((c >> 4) & 0xF));
+ sb.append(toHexDigit(c & 0xF));
+ }
+ } else { // escapeType == ESC_BACKSLASH
+ sb.append(c);
+ }
+ continue;
+ }
+ // Falls through when escapeType == NO_ESC
+ }
+ // Needs no escaping
+
+ if (sb != null) sb.append(c);
+ } // for each characters
+
+ return sb == null ? s : sb.toString();
+ }
+
+ private static char toHexDigit(int d) {
+ return (char) (d < 0xA ? d + '0' : d - 0xA + 'A');
+ }
+
+ /**
+ * Parses a name-value pair list, where the pairs are separated with comma,
+ * and the name and value is separated with colon.
+ * The keys and values can contain only letters, digits and <tt>_</tt>. They
+ * can't be quoted. White-space around the keys and values are ignored. The
+ * value can be omitted if <code>defaultValue</code> is not null. When a
+ * value is omitted, then the colon after the key must be omitted as well.
+ * The same key can't be used for multiple times.
+ *
+ * @param s the string to parse.
+ * For example: <code>"strong:100, soft:900"</code>.
+ * @param defaultValue the value used when the value is omitted in a
+ * key-value pair.
+ *
+ * @return the map that contains the name-value pairs.
+ *
+ * @throws java.text.ParseException if the string is not a valid name-value
+ * pair list.
+ */
+ public static Map parseNameValuePairList(String s, String defaultValue)
+ throws java.text.ParseException {
+ Map map = new HashMap();
+
+ char c = ' ';
+ int ln = s.length();
+ int p = 0;
+ int keyStart;
+ int valueStart;
+ String key;
+ String value;
+
+ fetchLoop: while (true) {
+ // skip ws
+ while (p < ln) {
+ c = s.charAt(p);
+ if (!Character.isWhitespace(c)) {
+ break;
+ }
+ p++;
+ }
+ if (p == ln) {
+ break fetchLoop;
+ }
+ keyStart = p;
+
+ // seek key end
+ while (p < ln) {
+ c = s.charAt(p);
+ if (!(Character.isLetterOrDigit(c) || c == '_')) {
+ break;
+ }
+ p++;
+ }
+ if (keyStart == p) {
+ throw new java.text.ParseException(
+ "Expecting letter, digit or \"_\" "
+ + "here, (the first character of the key) but found "
+ + jQuote(String.valueOf(c))
+ + " at position " + p + ".",
+ p);
+ }
+ key = s.substring(keyStart, p);
+
+ // skip ws
+ while (p < ln) {
+ c = s.charAt(p);
+ if (!Character.isWhitespace(c)) {
+ break;
+ }
+ p++;
+ }
+ if (p == ln) {
+ if (defaultValue == null) {
+ throw new java.text.ParseException(
+ "Expecting \":\", but reached "
+ + "the end of the string "
+ + " at position " + p + ".",
+ p);
+ }
+ value = defaultValue;
+ } else if (c != ':') {
+ if (defaultValue == null || c != ',') {
+ throw new java.text.ParseException(
+ "Expecting \":\" here, but found "
+ + jQuote(String.valueOf(c))
+ + " at position " + p + ".",
+ p);
+ }
+
+ // skip ","
+ p++;
+
+ value = defaultValue;
+ } else {
+ // skip ":"
+ p++;
+
+ // skip ws
+ while (p < ln) {
+ c = s.charAt(p);
+ if (!Character.isWhitespace(c)) {
+ break;
+ }
+ p++;
+ }
+ if (p == ln) {
+ throw new java.text.ParseException(
+ "Expecting the value of the key "
+ + "here, but reached the end of the string "
+ + " at position " + p + ".",
+ p);
+ }
+ valueStart = p;
+
+ // seek value end
+ while (p < ln) {
+ c = s.charAt(p);
+ if (!(Character.isLetterOrDigit(c) || c == '_')) {
+ break;
+ }
+ p++;
+ }
+ if (valueStart == p) {
+ throw new java.text.ParseException(
+ "Expecting letter, digit or \"_\" "
+ + "here, (the first character of the value) "
+ + "but found "
+ + jQuote(String.valueOf(c))
+ + " at position " + p + ".",
+ p);
+ }
+ value = s.substring(valueStart, p);
+
+ // skip ws
+ while (p < ln) {
+ c = s.charAt(p);
+ if (!Character.isWhitespace(c)) {
+ break;
+ }
+ p++;
+ }
+
+ // skip ","
+ if (p < ln) {
+ if (c != ',') {
+ throw new java.text.ParseException(
+ "Excpecting \",\" or the end "
+ + "of the string here, but found "
+ + jQuote(String.valueOf(c))
+ + " at position " + p + ".",
+ p);
+ } else {
+ p++;
+ }
+ }
+ }
+
+ // store the key-value pair
+ if (map.put(key, value) != null) {
+ throw new java.text.ParseException(
+ "Dublicated key: "
+ + jQuote(key), keyStart);
+ }
+ }
+
+ return map;
+ }
+
+ /**
+ * @return whether the qname matches the combination of nodeName, nsURI, and environment prefix settings.
+ */
+ static public boolean matchesQName(String qname, String nodeName, String nsURI, Environment env) {
+ String defaultNS = env.getDefaultNS();
+ if ((defaultNS != null) && defaultNS.equals(nsURI)) {
+ return qname.equals(nodeName)
+ || qname.equals(Template.DEFAULT_NAMESPACE_PREFIX + ":" + nodeName);
+ }
+ if ("".equals(nsURI)) {
+ if (defaultNS != null) {
+ return qname.equals(Template.NO_NS_PREFIX + ":" + nodeName);
+ } else {
+ return qname.equals(nodeName) || qname.equals(Template.NO_NS_PREFIX + ":" + nodeName);
+ }
+ }
+ String prefix = env.getPrefixForNamespace(nsURI);
+ if (prefix == null) {
+ return false; // Is this the right thing here???
+ }
+ return qname.equals(prefix + ":" + nodeName);
+ }
+
+ /**
+ * Pads the string at the left with spaces until it reaches the desired
+ * length. If the string is longer than this length, then it returns the
+ * unchanged string.
+ *
+ * @param s the string that will be padded.
+ * @param minLength the length to reach.
+ */
+ public static String leftPad(String s, int minLength) {
+ return leftPad(s, minLength, ' ');
+ }
+
+ /**
+ * Pads the string at the left with the specified character until it reaches
+ * the desired length. If the string is longer than this length, then it
+ * returns the unchanged string.
+ *
+ * @param s the string that will be padded.
+ * @param minLength the length to reach.
+ * @param filling the filling pattern.
+ */
+ public static String leftPad(String s, int minLength, char filling) {
+ int ln = s.length();
+ if (minLength <= ln) {
+ return s;
+ }
+
+ StringBuilder res = new StringBuilder(minLength);
+
+ int dif = minLength - ln;
+ for (int i = 0; i < dif; i++) {
+ res.append(filling);
+ }
+
+ res.append(s);
+
+ return res.toString();
+ }
+
+ /**
+ * Pads the string at the left with a filling pattern until it reaches the
+ * desired length. If the string is longer than this length, then it returns
+ * the unchanged string. For example: <code>leftPad('ABC', 9, '1234')</code>
+ * returns <code>"123412ABC"</code>.
+ *
+ * @param s the string that will be padded.
+ * @param minLength the length to reach.
+ * @param filling the filling pattern. Must be at least 1 characters long.
+ * Can't be <code>null</code>.
+ */
+ public static String leftPad(String s, int minLength, String filling) {
+ int ln = s.length();
+ if (minLength <= ln) {
+ return s;
+ }
+
+ StringBuilder res = new StringBuilder(minLength);
+
+ int dif = minLength - ln;
+ int fln = filling.length();
+ if (fln == 0) {
+ throw new IllegalArgumentException(
+ "The \"filling\" argument can't be 0 length string.");
+ }
+ int cnt = dif / fln;
+ for (int i = 0; i < cnt; i++) {
+ res.append(filling);
+ }
+ cnt = dif % fln;
+ for (int i = 0; i < cnt; i++) {
+ res.append(filling.charAt(i));
+ }
+
+ res.append(s);
+
+ return res.toString();
+ }
+
+ /**
+ * Pads the string at the right with spaces until it reaches the desired
+ * length. If the string is longer than this length, then it returns the
+ * unchanged string.
+ *
+ * @param s the string that will be padded.
+ * @param minLength the length to reach.
+ */
+ public static String rightPad(String s, int minLength) {
+ return rightPad(s, minLength, ' ');
+ }
+
+ /**
+ * Pads the string at the right with the specified character until it
+ * reaches the desired length. If the string is longer than this length,
+ * then it returns the unchanged string.
+ *
+ * @param s the string that will be padded.
+ * @param minLength the length to reach.
+ * @param filling the filling pattern.
+ */
+ public static String rightPad(String s, int minLength, char filling) {
+ int ln = s.length();
+ if (minLength <= ln) {
+ return s;
+ }
+
+ StringBuilder res = new StringBuilder(minLength);
+
+ res.append(s);
+
+ int dif = minLength - ln;
+ for (int i = 0; i < dif; i++) {
+ res.append(filling);
+ }
+
+ return res.toString();
+ }
+
+ /**
+ * Pads the string at the right with a filling pattern until it reaches the
+ * desired length. If the string is longer than this length, then it returns
+ * the unchanged string. For example: <code>rightPad('ABC', 9, '1234')</code>
+ * returns <code>"ABC412341"</code>. Note that the filling pattern is
+ * started as if you overlay <code>"123412341"</code> with the left-aligned
+ * <code>"ABC"</code>, so it starts with <code>"4"</code>.
+ *
+ * @param s the string that will be padded.
+ * @param minLength the length to reach.
+ * @param filling the filling pattern. Must be at least 1 characters long.
+ * Can't be <code>null</code>.
+ */
+ public static String rightPad(String s, int minLength, String filling) {
+ int ln = s.length();
+ if (minLength <= ln) {
+ return s;
+ }
+
+ StringBuilder res = new StringBuilder(minLength);
+
+ res.append(s);
+
+ int dif = minLength - ln;
+ int fln = filling.length();
+ if (fln == 0) {
+ throw new IllegalArgumentException(
+ "The \"filling\" argument can't be 0 length string.");
+ }
+ int start = ln % fln;
+ int end = fln - start <= dif
+ ? fln
+ : start + dif;
+ for (int i = start; i < end; i++) {
+ res.append(filling.charAt(i));
+ }
+ dif -= end - start;
+ int cnt = dif / fln;
+ for (int i = 0; i < cnt; i++) {
+ res.append(filling);
+ }
+ cnt = dif % fln;
+ for (int i = 0; i < cnt; i++) {
+ res.append(filling.charAt(i));
+ }
+
+ return res.toString();
+ }
+
+ /**
+ * Converts a version number string to an integer for easy comparison.
+ * The version number must start with numbers separated with
+ * dots. There can be any number of such dot-separated numbers, but only
+ * the first three will be considered. After the numbers arbitrary text can
+ * follow, and will be ignored.
+ *
+ * The string will be trimmed before interpretation.
+ *
+ * @return major * 1000000 + minor * 1000 + micro
+ */
+ public static int versionStringToInt(String version) {
+ return new Version(version).intValue();
+ }
+
+ /**
+ * Tries to run {@code toString()}, but if that fails, returns a
+ * {@code "[com.example.SomeClass.toString() failed: " + e + "]"} instead. Also, it returns {@code null} for
+ * {@code null} parameter.
+ *
+ * @since 2.3.20
+ */
+ public static String tryToString(Object object) {
+ if (object == null) return null;
+
+ try {
+ return object.toString();
+ } catch (Throwable e) {
+ return failedToStringSubstitute(object, e);
+ }
+ }
+
+ private static String failedToStringSubstitute(Object object, Throwable e) {
+ String eStr;
+ try {
+ eStr = e.toString();
+ } catch (Throwable e2) {
+ eStr = _ClassUtil.getShortClassNameOfObject(e);
+ }
+ return "[" + _ClassUtil.getShortClassNameOfObject(object) + ".toString() failed: " + eStr + "]";
+ }
+
+ /**
+ * Converts {@code 1}, {@code 2}, {@code 3} and so forth to {@code "A"}, {@code "B"}, {@code "C"} and so fort. When
+ * reaching {@code "Z"}, it continues like {@code "AA"}, {@code "AB"}, etc. The lowest supported number is 1, but
+ * there's no upper limit.
+ *
+ * @throws IllegalArgumentException
+ * If the argument is 0 or less.
+ *
+ * @since 2.3.22
+ */
+ public static String toUpperABC(int n) {
+ return toABC(n, 'A');
+ }
+
+ /**
+ * Same as {@link #toUpperABC(int)}, but produces lower case result, like {@code "ab"}.
+ *
+ * @since 2.3.22
+ */
+ public static String toLowerABC(int n) {
+ return toABC(n, 'a');
+ }
+
+ /**
+ * @param oneDigit
+ * The character that stands for the value 1.
+ */
+ private static String toABC(final int n, char oneDigit) {
+ if (n < 1) {
+ throw new IllegalArgumentException("Can't convert 0 or negative "
+ + "numbers to latin-number: " + n);
+ }
+
+ // First find out how many "digits" will we need. We start from A, then
+ // try AA, then AAA, etc. (Note that the smallest digit is "A", which is
+ // 1, not 0. Hence this isn't like a usual 26-based number-system):
+ int reached = 1;
+ int weight = 1;
+ while (true) {
+ int nextWeight = weight * 26;
+ int nextReached = reached + nextWeight;
+ if (nextReached <= n) {
+ // So we will have one more digit
+ weight = nextWeight;
+ reached = nextReached;
+ } else {
+ // No more digits
+ break;
+ }
+ }
+
+ // Increase the digits of the place values until we get as close
+ // to n as possible (but don't step over it).
+ StringBuilder sb = new StringBuilder();
+ while (weight != 0) {
+ // digitIncrease: how many we increase the digit which is already 1
+ final int digitIncrease = (n - reached) / weight;
+ sb.append((char) (oneDigit + digitIncrease));
+ reached += digitIncrease * weight;
+
+ weight /= 26;
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Behaves exactly like {@link String#trim()}, but works on arrays. If the resulting array would have the same
+ * content after trimming, it returns the original array instance. Otherwise it returns a new array instance (or
+ * {@link _CollectionUtil#EMPTY_CHAR_ARRAY}).
+ *
+ * @since 2.3.22
+ */
+ public static char[] trim(final char[] cs) {
+ if (cs.length == 0) {
+ return cs;
+ }
+
+ int start = 0;
+ int end = cs.length;
+ while (start < end && cs[start] <= ' ') {
+ start++;
+ }
+ while (start < end && cs[end - 1] <= ' ') {
+ end--;
+ }
+
+ if (start == 0 && end == cs.length) {
+ return cs;
+ }
+ if (start == end) {
+ return _CollectionUtil.EMPTY_CHAR_ARRAY;
+ }
+
+ char[] newCs = new char[end - start];
+ System.arraycopy(cs, start, newCs, 0, end - start);
+ return newCs;
+ }
+
+ /**
+ * Tells if {@link String#trim()} will return a 0-length string for the {@link String} equivalent of the argument.
+ *
+ * @since 2.3.22
+ */
+ public static boolean isTrimmableToEmpty(char[] text) {
+ return isTrimmableToEmpty(text, 0, text.length);
+ }
+
+ /**
+ * Like {@link #isTrimmableToEmpty(char[])}, but acts on a sub-array that starts at {@code start} (inclusive index).
+ *
+ * @since 2.3.23
+ */
+ public static boolean isTrimmableToEmpty(char[] text, int start) {
+ return isTrimmableToEmpty(text, start, text.length);
+ }
+
+ /**
+ * Like {@link #isTrimmableToEmpty(char[])}, but acts on a sub-array that starts at {@code start} (inclusive index)
+ * and ends at {@code end} (exclusive index).
+ *
+ * @since 2.3.23
+ */
+ public static boolean isTrimmableToEmpty(char[] text, int start, int end) {
+ for (int i = start; i < end; i++) {
+ // We follow Java's String.trim() here, which simply states that c <= ' ' is whitespace.
+ if (text[i] > ' ') {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Same as {@link #globToRegularExpression(String, boolean)} with {@code caseInsensitive} argument {@code false}.
+ *
+ * @since 2.3.24
+ */
+ public static Pattern globToRegularExpression(String glob) {
+ return globToRegularExpression(glob, false);
+ }
+
+ /**
+ * Creates a regular expression from a glob. The glob must use {@code /} for as file separator, not {@code \}
+ * (backslash), and is always case sensitive.
+ *
+ * <p>This glob implementation recognizes these special characters:
+ * <ul>
+ * <li>{@code ?}: Wildcard that matches exactly one character, other than {@code /}
+ * <li>{@code *}: Wildcard that matches zero, one or multiple characters, other than {@code /}
+ * <li>{@code **}: Wildcard that matches zero, one or multiple directories. For example, {@code **}{@code /head.ftl}
+ * matches {@code foo/bar/head.ftl}, {@code foo/head.ftl} and {@code head.ftl} too. {@code **} must be either
+ * preceded by {@code /} or be at the beginning of the glob. {@code **} must be either followed by {@code /} or be
+ * at the end of the glob. When {@code **} is at the end of the glob, it also matches file names, like
+ * {@code a/**} matches {@code a/b/c.ftl}. If the glob only consist of a {@code **}, it will be a match for
+ * everything.
+ * <li>{@code \} (backslash): Makes the next character non-special (a literal). For example {@code How\?.ftl} will
+ * match {@code How?.ftl}, but not {@code HowX.ftl}. Naturally, two backslashes produce one literal backslash.
+ * <li>{@code [}: Reserved for future purposes; can't be used
+ * <li><code>{</code>: Reserved for future purposes; can't be used
+ * </ul>
+ *
+ * @since 2.3.24
+ */
+ public static Pattern globToRegularExpression(String glob, boolean caseInsensitive) {
+ StringBuilder regex = new StringBuilder();
+
+ int nextStart = 0;
+ boolean escaped = false;
+ int ln = glob.length();
+ for (int idx = 0; idx < ln; idx++) {
+ char c = glob.charAt(idx);
+ if (!escaped) {
+ if (c == '?') {
+ appendLiteralGlobSection(regex, glob, nextStart, idx);
+ regex.append("[^/]");
+ nextStart = idx + 1;
+ } else if (c == '*') {
+ appendLiteralGlobSection(regex, glob, nextStart, idx);
+ if (idx + 1 < ln && glob.charAt(idx + 1) == '*') {
+ if (!(idx == 0 || glob.charAt(idx - 1) == '/')) {
+ throw new IllegalArgumentException(
+ "The \"**\" wildcard must be directly after a \"/\" or it must be at the "
+ + "beginning, in this glob: " + glob);
+ }
+
+ if (idx + 2 == ln) { // trailing "**"
+ regex.append(".*");
+ idx++;
+ } else { // "**/"
+ if (!(idx + 2 < ln && glob.charAt(idx + 2) == '/')) {
+ throw new IllegalArgumentException(
+ "The \"**\" wildcard must be followed by \"/\", or must be at tehe end, "
+ + "in this glob: " + glob);
+ }
+ regex.append("(.*?/)*");
+ idx += 2; // "*/".length()
+ }
+ } else {
+ regex.append("[^/]*");
+ }
+ nextStart = idx + 1;
+ } else if (c == '\\') {
+ escaped = true;
+ } else if (c == '[' || c == '{') {
+ throw new IllegalArgumentException(
+ "The \"" + c + "\" glob operator is currently unsupported "
+ + "(precede it with \\ for literal matching), "
+ + "in this glob: " + glob);
+ }
+ } else {
+ escaped = false;
+ }
+ }
+ appendLiteralGlobSection(regex, glob, nextStart, glob.length());
+
+ return Pattern.compile(regex.toString(), caseInsensitive ? Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE : 0);
+ }
+
+ private static void appendLiteralGlobSection(StringBuilder regex, String glob, int start, int end) {
+ if (start == end) return;
+ String part = unescapeLiteralGlobSection(glob.substring(start, end));
+ regex.append(Pattern.quote(part));
+ }
+
+ private static String unescapeLiteralGlobSection(String s) {
+ int backslashIdx = s.indexOf('\\');
+ if (backslashIdx == -1) {
+ return s;
+ }
+ int ln = s.length();
+ StringBuilder sb = new StringBuilder(ln - 1);
+ int nextStart = 0;
+ do {
+ sb.append(s, nextStart, backslashIdx);
+ nextStart = backslashIdx + 1;
+ } while ((backslashIdx = s.indexOf('\\', nextStart + 1)) != -1);
+ if (nextStart < ln) {
+ sb.append(s, nextStart, ln);
+ }
+ return sb.toString();
+ }
+
+ public static String toFTLIdentifierReferenceAfterDot(String name) {
+ return FTLUtil.escapeIdentifier(name);
+ }
+
+ public static String toFTLTopLevelIdentifierReference(String name) {
+ return FTLUtil.escapeIdentifier(name);
+ }
+
+ public static String toFTLTopLevelTragetIdentifier(final String name) {
+ char quotationType = 0;
+ scanForQuotationType: for (int i = 0; i < name.length(); i++) {
+ final char c = name.charAt(i);
+ if (!(i == 0 ? FTLUtil.isNonEscapedIdentifierStart(c) : FTLUtil.isNonEscapedIdentifierPart(c)) && c != '@') {
+ if ((quotationType == 0 || quotationType == '\\') && (c == '-' || c == '.' || c == ':')) {
+ quotationType = '\\';
+ } else {
+ quotationType = '"';
+ break scanForQuotationType;
+ }
+ }
+ }
+ switch (quotationType) {
+ case 0:
+ return name;
+ case '"':
+ return FTLUtil.toStringLiteral(name);
+ case '\\':
+ return FTLUtil.escapeIdentifier(name);
+ default:
+ throw new BugException();
+ }
+ }
+
+ /**
+ * @return {@link ParsingConfiguration#CAMEL_CASE_NAMING_CONVENTION}, or {@link ParsingConfiguration#LEGACY_NAMING_CONVENTION}
+ * or, {@link ParsingConfiguration#AUTO_DETECT_NAMING_CONVENTION} when undecidable.
+ */
+ public static int getIdentifierNamingConvention(String name) {
+ final int ln = name.length();
+ for (int i = 0; i < ln; i++) {
+ final char c = name.charAt(i);
+ if (c == '_') {
+ return ParsingConfiguration.LEGACY_NAMING_CONVENTION;
+ }
+ if (_StringUtil.isUpperUSASCII(c)) {
+ return ParsingConfiguration.CAMEL_CASE_NAMING_CONVENTION;
+ }
+ }
+ return ParsingConfiguration.AUTO_DETECT_NAMING_CONVENTION;
+ }
+
+ // [2.4] Won't be needed anymore
+ /**
+ * A deliberately very inflexible camel case to underscored converter; it must not convert improper camel case
+ * names to a proper underscored name.
+ */
+ public static String camelCaseToUnderscored(String camelCaseName) {
+ int i = 0;
+ while (i < camelCaseName.length() && Character.isLowerCase(camelCaseName.charAt(i))) {
+ i++;
+ }
+ if (i == camelCaseName.length()) {
+ // No conversion needed
+ return camelCaseName;
+ }
+
+ StringBuilder sb = new StringBuilder();
+ sb.append(camelCaseName.substring(0, i));
+ while (i < camelCaseName.length()) {
+ final char c = camelCaseName.charAt(i);
+ if (_StringUtil.isUpperUSASCII(c)) {
+ sb.append('_');
+ sb.append(Character.toLowerCase(c));
+ } else {
+ sb.append(c);
+ }
+ i++;
+ }
+ return sb.toString();
+ }
+
+ public static boolean isASCIIDigit(char c) {
+ return c >= '0' && c <= '9';
+ }
+
+ public static boolean isUpperUSASCII(char c) {
+ return c >= 'A' && c <= 'Z';
+ }
+
+ private static final Pattern NORMALIZE_EOLS_REGEXP = Pattern.compile("\\r\\n?+");
+
+ /**
+ * Converts all non UN*X End-Of-Line character sequences (CR and CRLF) to UN*X format (LF).
+ * Returns {@code null} for {@code null} input.
+ */
+ public static String normalizeEOLs(String s) {
+ if (s == null) {
+ return null;
+ }
+ return NORMALIZE_EOLS_REGEXP.matcher(s).replaceAll("\n");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/util/_UnmodifiableCompositeSet.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/util/_UnmodifiableCompositeSet.java b/freemarker-core/src/main/java/org/apache/freemarker/core/util/_UnmodifiableCompositeSet.java
new file mode 100644
index 0000000..ab88f57
--- /dev/null
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/util/_UnmodifiableCompositeSet.java
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.freemarker.core.util;
+
+import java.util.Iterator;
+import java.util.Set;
+
+/** Don't use this; used internally by FreeMarker, might changes without notice. */
+public class _UnmodifiableCompositeSet<E> extends _UnmodifiableSet<E> {
+
+ private final Set<E> set1, set2;
+
+ public _UnmodifiableCompositeSet(Set<E> set1, Set<E> set2) {
+ this.set1 = set1;
+ this.set2 = set2;
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return new CompositeIterator();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return set1.contains(o) || set2.contains(o);
+ }
+
+ @Override
+ public int size() {
+ return set1.size() + set2.size();
+ }
+
+ private class CompositeIterator implements Iterator<E> {
+
+ private Iterator<E> it1, it2;
+ private boolean it1Deplected;
+
+ @Override
+ public boolean hasNext() {
+ if (!it1Deplected) {
+ if (it1 == null) {
+ it1 = set1.iterator();
+ }
+ if (it1.hasNext()) {
+ return true;
+ }
+
+ it2 = set2.iterator();
+ it1 = null;
+ it1Deplected = true;
+ // Falls through
+ }
+ return it2.hasNext();
+ }
+
+ @Override
+ public E next() {
+ if (!it1Deplected) {
+ if (it1 == null) {
+ it1 = set1.iterator();
+ }
+ if (it1.hasNext()) {
+ return it1.next();
+ }
+
+ it2 = set2.iterator();
+ it1 = null;
+ it1Deplected = true;
+ // Falls through
+ }
+ return it2.next();
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/util/_UnmodifiableSet.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/util/_UnmodifiableSet.java b/freemarker-core/src/main/java/org/apache/freemarker/core/util/_UnmodifiableSet.java
new file mode 100644
index 0000000..7e08815
--- /dev/null
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/util/_UnmodifiableSet.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.freemarker.core.util;
+
+import java.util.AbstractSet;
+
+/** Don't use this; used internally by FreeMarker, might changes without notice. */
+public abstract class _UnmodifiableSet<E> extends AbstractSet<E> {
+
+ @Override
+ public boolean add(E o) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ if (contains(o)) {
+ throw new UnsupportedOperationException();
+ }
+ return false;
+ }
+
+ @Override
+ public void clear() {
+ if (!isEmpty()) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/util/package.html
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/util/package.html b/freemarker-core/src/main/java/org/apache/freemarker/core/util/package.html
new file mode 100644
index 0000000..e90df61
--- /dev/null
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/util/package.html
@@ -0,0 +1,25 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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
+
+ http://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.
+-->
+<html>
+<head>
+</head>
+<body bgcolor="white">
+<p>Various classes used by core FreeMarker code but might be useful outside of it too.</p>
+</body>
+</html>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/InvalidFormatParametersException.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/InvalidFormatParametersException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/InvalidFormatParametersException.java
new file mode 100644
index 0000000..ba09574
--- /dev/null
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/InvalidFormatParametersException.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.freemarker.core.valueformat;
+
+/**
+ * Used when creating {@link TemplateDateFormat}-s and {@link TemplateNumberFormat}-s to indicate that the parameters
+ * part of the format string (like some kind of pattern) is malformed.
+ *
+ * @since 2.3.24
+ */
+public final class InvalidFormatParametersException extends InvalidFormatStringException {
+
+ public InvalidFormatParametersException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public InvalidFormatParametersException(String message) {
+ this(message, null);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/InvalidFormatStringException.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/InvalidFormatStringException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/InvalidFormatStringException.java
new file mode 100644
index 0000000..05c7ed1
--- /dev/null
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/InvalidFormatStringException.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.freemarker.core.valueformat;
+
+/**
+ * Used when creating {@link TemplateDateFormat}-s and {@link TemplateNumberFormat}-s to indicate that the format
+ * string (like the value of the {@code dateFormat} setting) is malformed.
+ *
+ * @since 2.3.24
+ */
+public abstract class InvalidFormatStringException extends TemplateValueFormatException {
+
+ public InvalidFormatStringException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public InvalidFormatStringException(String message) {
+ this(message, null);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/ParsingNotSupportedException.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/ParsingNotSupportedException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/ParsingNotSupportedException.java
new file mode 100644
index 0000000..08eba37
--- /dev/null
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/ParsingNotSupportedException.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.freemarker.core.valueformat;
+
+/**
+ * Thrown when the {@link TemplateValueFormat} doesn't support parsing, and parsing was invoked.
+ *
+ * @since 2.3.24
+ */
+public class ParsingNotSupportedException extends TemplateValueFormatException {
+
+ public ParsingNotSupportedException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public ParsingNotSupportedException(String message) {
+ this(message, null);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/TemplateDateFormat.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/TemplateDateFormat.java b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/TemplateDateFormat.java
new file mode 100644
index 0000000..7626d90
--- /dev/null
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/TemplateDateFormat.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.freemarker.core.valueformat;
+
+import java.text.DateFormat;
+import java.util.Date;
+
+import org.apache.freemarker.core.Environment;
+import org.apache.freemarker.core.model.TemplateDateModel;
+import org.apache.freemarker.core.model.TemplateMarkupOutputModel;
+import org.apache.freemarker.core.model.TemplateModelException;
+
+/**
+ * Represents a date/time/dateTime format; used in templates for formatting and parsing with that format. This is
+ * similar to Java's {@link DateFormat}, but made to fit the requirements of FreeMarker. Also, it makes easier to define
+ * formats that can't be represented with Java's existing {@link DateFormat} implementations.
+ *
+ * <p>
+ * Implementations need not be thread-safe if the {@link TemplateNumberFormatFactory} doesn't recycle them among
+ * different {@link Environment}-s. As far as FreeMarker's concerned, instances are bound to a single
+ * {@link Environment}, and {@link Environment}-s are thread-local objects.
+ *
+ * @since 2.3.24
+ */
+public abstract class TemplateDateFormat extends TemplateValueFormat {
+
+ /**
+ * @param dateModel
+ * The date/time/dateTime to format; not {@code null}. Most implementations will just work with the return value of
+ * {@link TemplateDateModel#getAsDate()}, but some may format differently depending on the properties of
+ * a custom {@link TemplateDateModel} implementation.
+ *
+ * @return The date/time/dateTime as text, with no escaping (like no HTML escaping); can't be {@code null}.
+ *
+ * @throws TemplateValueFormatException
+ * When a problem occurs during the formatting of the value. Notable subclass:
+ * {@link UnknownDateTypeFormattingUnsupportedException}
+ * @throws TemplateModelException
+ * Exception thrown by the {@code dateModel} object when calling its methods.
+ */
+ public abstract String formatToPlainText(TemplateDateModel dateModel)
+ throws TemplateValueFormatException, TemplateModelException;
+
+ /**
+ * Formats the model to markup instead of to plain text if the result markup will be more than just plain text
+ * escaped, otherwise falls back to formatting to plain text. If the markup result would be just the result of
+ * {@link #formatToPlainText(TemplateDateModel)} escaped, it must return the {@link String} that
+ * {@link #formatToPlainText(TemplateDateModel)} does.
+ *
+ * <p>The implementation in {@link TemplateDateFormat} simply calls {@link #formatToPlainText(TemplateDateModel)}.
+ *
+ * @return A {@link String} or a {@link TemplateMarkupOutputModel}; not {@code null}.
+ */
+ public Object format(TemplateDateModel dateModel) throws TemplateValueFormatException, TemplateModelException {
+ return formatToPlainText(dateModel);
+ }
+
+ /**
+ * Parsers a string to date/time/datetime, according to this format. Some format implementations may throw
+ * {@link ParsingNotSupportedException} here.
+ *
+ * @param s
+ * The string to parse
+ * @param dateType
+ * The expected date type of the result. Not all {@link TemplateDateFormat}-s will care about this;
+ * though those who return a {@link TemplateDateModel} instead of {@link Date} often will. When strings
+ * are parsed via {@code ?date}, {@code ?time}, or {@code ?datetime}, then this parameter is
+ * {@link TemplateDateModel#DATE}, {@link TemplateDateModel#TIME}, or {@link TemplateDateModel#DATETIME},
+ * respectively. This parameter rarely if ever {@link TemplateDateModel#UNKNOWN}, but the implementation
+ * that cares about this parameter should be prepared for that. If nothing else, it should throw
+ * {@link UnknownDateTypeParsingUnsupportedException} then.
+ *
+ * @return The interpretation of the text either as a {@link Date} or {@link TemplateDateModel}. Typically, a
+ * {@link Date}. {@link TemplateDateModel} is used if you have to attach some application-specific
+ * meta-information thats also extracted during {@link #formatToPlainText(TemplateDateModel)} (so if you format
+ * something and then parse it, you get back an equivalent result). It can't be {@code null}. Known issue
+ * (at least in FTL 2): {@code ?date}/{@code ?time}/{@code ?datetime}, when not invoked as a method, can't
+ * return the {@link TemplateDateModel}, only the {@link Date} from inside it, hence the additional
+ * application-specific meta-info will be lost.
+ */
+ public abstract Object parse(String s, int dateType) throws TemplateValueFormatException;
+
+ /**
+ * Tells if this formatter should be re-created if the locale changes.
+ */
+ public abstract boolean isLocaleBound();
+
+ /**
+ * Tells if this formatter should be re-created if the time zone changes. Currently always {@code true}.
+ */
+ public abstract boolean isTimeZoneBound();
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/TemplateDateFormatFactory.java
----------------------------------------------------------------------
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/TemplateDateFormatFactory.java b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/TemplateDateFormatFactory.java
new file mode 100644
index 0000000..ca3b4bd
--- /dev/null
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/valueformat/TemplateDateFormatFactory.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.freemarker.core.valueformat;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import org.apache.freemarker.core.CustomStateKey;
+import org.apache.freemarker.core.MutableProcessingConfiguration;
+import org.apache.freemarker.core.Configuration;
+import org.apache.freemarker.core.Environment;
+import org.apache.freemarker.core.model.TemplateDateModel;
+
+/**
+ * Factory for a certain kind of date/time/dateTime formatting ({@link TemplateDateFormat}). Usually a singleton
+ * (one-per-VM or one-per-{@link Configuration}), and so must be thread-safe.
+ *
+ * @see MutableProcessingConfiguration#setCustomDateFormats(java.util.Map)
+ *
+ * @since 2.3.24
+ */
+public abstract class TemplateDateFormatFactory extends TemplateValueFormatFactory {
+
+ /**
+ * Returns a formatter for the given parameters.
+ *
+ * <p>
+ * The returned formatter can be a new instance or a reused (cached) instance. Note that {@link Environment} itself
+ * caches the returned instances, though that cache is lost with the {@link Environment} (i.e., when the top-level
+ * template execution ends), also it might flushes lot of entries if the locale or time zone is changed during
+ * template execution. So caching on the factory level is still useful, unless creating the formatters is
+ * sufficiently cheap.
+ *
+ * @param params
+ * The string that further describes how the format should look. For example, when the
+ * {@link MutableProcessingConfiguration#getDateFormat() dateFormat} is {@code "@fooBar 1, 2"}, then it will be
+ * {@code "1, 2"} (and {@code "@fooBar"} selects the factory). The format of this string is up to the
+ * {@link TemplateDateFormatFactory} implementation. Not {@code null}, often an empty string.
+ * @param dateType
+ * {@link TemplateDateModel#DATE}, {@link TemplateDateModel#TIME}, {@link TemplateDateModel#DATETIME} or
+ * {@link TemplateDateModel#UNKNOWN}. Supporting {@link TemplateDateModel#UNKNOWN} is not necessary, in
+ * which case the method should throw an {@link UnknownDateTypeFormattingUnsupportedException} exception.
+ * @param locale
+ * The locale to format for. Not {@code null}. The resulting format should be bound to this locale
+ * forever (i.e. locale changes in the {@link Environment} must not be followed).
+ * @param timeZone
+ * The time zone to format for. Not {@code null}. The resulting format must be bound to this time zone
+ * forever (i.e. time zone changes in the {@link Environment} must not be followed).
+ * @param zonelessInput
+ * Indicates that the input Java {@link Date} is not from a time zone aware source. When this is
+ * {@code true}, the formatters shouldn't override the time zone provided to its constructor (most
+ * formatters don't do that anyway), and it shouldn't show the time zone, if it can hide it (like a
+ * {@link SimpleDateFormat} pattern-based formatter may can't do that, as the pattern prescribes what to
+ * show).
+ * <p>
+ * As of FreeMarker 2.3.21, this is {@code true} exactly when the date is an SQL "date without time of
+ * the day" (i.e., a {@link java.sql.Date java.sql.Date}) or an SQL "time of the day" value (i.e., a
+ * {@link java.sql.Time java.sql.Time}, although this rule can change in future, depending on
+ * configuration settings and such, so you shouldn't rely on this rule, just accept what this parameter
+ * says.
+ * @param env
+ * The runtime environment from which the formatting was called. This is mostly meant to be used for
+ * {@link Environment#getCustomState(CustomStateKey)}.
+ *
+ * @throws TemplateValueFormatException
+ * If any problem occurs while parsing/getting the format. Notable subclasses:
+ * {@link InvalidFormatParametersException} if {@code params} is malformed;
+ * {@link UnknownDateTypeFormattingUnsupportedException} if {@code dateType} is
+ * {@link TemplateDateModel#UNKNOWN} and that's unsupported by this factory.
+ */
+ public abstract TemplateDateFormat get(
+ String params,
+ int dateType, Locale locale, TimeZone timeZone, boolean zonelessInput,
+ Environment env)
+ throws TemplateValueFormatException;
+
+}