package net.bootsfaces.utils; import java.util.HashMap; import java.util.Map; public class MdfToJdf { private Map javaDateFormats; private Map momentDateFormats; private Map javaFormatMapping; private Map momentFormatMapping; public MdfToJdf() { javaDateFormats = new HashMap(); momentDateFormats = new HashMap(); initialiseJavaFormatMapping(); initialiseMomentFormatMapping(); } private void initialiseJavaFormatMapping() { javaFormatMapping = new HashMap(); javaFormatMapping.put("d", "D"); javaFormatMapping.put("dd", "DD"); javaFormatMapping.put("y", "YYYY"); javaFormatMapping.put("yy", "YY"); javaFormatMapping.put("yyy", "YYYY"); javaFormatMapping.put("yyyy", "YYYY"); javaFormatMapping.put("a", "a"); javaFormatMapping.put("A", "A"); javaFormatMapping.put("M", "M"); javaFormatMapping.put("MM", "MM"); javaFormatMapping.put("MMM", "MMM"); javaFormatMapping.put("MMMM", "MMMM"); javaFormatMapping.put("h", "h"); javaFormatMapping.put("hh", "hh"); javaFormatMapping.put("H", "H"); javaFormatMapping.put("HH", "HH"); javaFormatMapping.put("m", "m"); javaFormatMapping.put("mm", "mm"); javaFormatMapping.put("s", "s"); javaFormatMapping.put("ss", "ss"); javaFormatMapping.put("S", "SSS"); javaFormatMapping.put("SS", "SSS"); javaFormatMapping.put("SSS", "SSS"); javaFormatMapping.put("E", "ddd"); javaFormatMapping.put("EE", "ddd"); javaFormatMapping.put("EEE", "ddd"); javaFormatMapping.put("EEEE", "dddd"); javaFormatMapping.put("EEEEE", "dddd"); javaFormatMapping.put("EEEEEE", "dddd"); javaFormatMapping.put("D", "DDD"); javaFormatMapping.put("w", "W"); javaFormatMapping.put("ww", "WW"); javaFormatMapping.put("z", "ZZ"); javaFormatMapping.put("zzzz", "Z"); javaFormatMapping.put("Z", "ZZ"); javaFormatMapping.put("X", "ZZ"); javaFormatMapping.put("XX", "ZZ"); javaFormatMapping.put("XXX", "Z"); javaFormatMapping.put("u", "e"); } private void initialiseMomentFormatMapping() { momentFormatMapping = new HashMap(); momentFormatMapping.put("D", "d"); momentFormatMapping.put("DD", "dd"); momentFormatMapping.put("YY", "yy"); momentFormatMapping.put("YYY", "yyyy"); momentFormatMapping.put("YYYY", "yyyy"); momentFormatMapping.put("a", "a"); momentFormatMapping.put("A", "A"); momentFormatMapping.put("M", "M"); momentFormatMapping.put("MM", "MM"); momentFormatMapping.put("MMM", "MMM"); momentFormatMapping.put("MMMM", "MMMM"); momentFormatMapping.put("h", "h"); momentFormatMapping.put("hh", "hh"); momentFormatMapping.put("H", "H"); momentFormatMapping.put("HH", "HH"); momentFormatMapping.put("m", "m"); momentFormatMapping.put("mm", "mm"); momentFormatMapping.put("s", "s"); momentFormatMapping.put("ss", "ss"); momentFormatMapping.put("S", "S"); momentFormatMapping.put("SS", "S"); momentFormatMapping.put("SSS", "S"); momentFormatMapping.put("ddd", "E"); momentFormatMapping.put("dddd", "EEEE"); momentFormatMapping.put("DDD", "D"); momentFormatMapping.put("W", "w"); momentFormatMapping.put("WW", "ww"); momentFormatMapping.put("ZZ", "z"); momentFormatMapping.put("Z", "XXX"); momentFormatMapping.put("E", "u"); } /** * Translates the momentjs format String to a java date format String. * * @function toJDFString * @param {String} * formatString The format String to be translated. * @returns {String} */ public String toMomentFormatString(String formatString) { if (!javaDateFormats.containsKey(formatString)) { javaDateFormats.put(formatString, translateFormat(formatString, javaFormatMapping)); } return javaDateFormats.get(formatString); } /** * Format the moment with the given java date format String. * * @function formatWithJDF * @param {String} * formatString The format String to be translated. * @returns {String} */ // public String formatWithJDF(String formatString) { // return format(toMomentFormatString(formatString)); // }; /** * Translates the momentjs format string to a java date format string * * @function toJDFString * @param {String} * formatString The format String to be translated. * @returns {String} */ public String toJDFString(String formatString) { if (!momentDateFormats.containsKey(formatString)) { momentDateFormats.put(formatString, translateFormat(formatString, momentFormatMapping)); } return momentDateFormats.get(formatString); }; /** * Translates the java date format String to a momentjs format String. * * @function translateFormat * @param {String} * formatString The unmodified format string * @param {Object} * mapping The date format mapping object * @returns {String} */ private String translateFormat(String formatString, Map mapping) { int len = formatString.length(), i = 0, beginIndex = -1; char lastChar = '\u0000', currentChar = '\u0000'; String resultString = ""; for (; i < len; i++) { currentChar = formatString.charAt(i); if (lastChar == '\u0000' || lastChar != currentChar) { // change detected resultString = appendMappedString(formatString, mapping, beginIndex, i, resultString); beginIndex = i; } lastChar = currentChar; } System.out.println("FORMAT, formatString: " + formatString + ", resultString:" + resultString); return appendMappedString(formatString, mapping, beginIndex, i, resultString); }; /** * Checks if the substring is a mapped date format pattern and adds it to * the result format String. * * @function _appendMappedString * @param {String} * formatString The unmodified format String. * @param {Object} * mapping The date format mapping Object. * @param {Number} * beginIndex The begin index of the continuous format * characters. * @param {Number} * currentIndex The last index of the continuous format * characters. * @param {String} * resultString The result format String. * @returns {String} * @private */ private String appendMappedString(String formatString, Map mapping, int beginIndex, int currentIndex, String resultString) { String tempString; if (beginIndex != -1) { tempString = formatString.substring(beginIndex, currentIndex); // check if the temporary string has a known mapping if (mapping.containsKey(tempString)) { tempString = mapping.get(tempString); } resultString = resultString.concat(tempString); } return resultString; }; }