/src/com/emitrom/touch4j/client/utils/Format.java

https://github.com/tony-mowers/touch4j · Java · 436 lines · 78 code · 29 blank · 329 comment · 1 complexity · ff6e3b54543601376c5feb822db9156f MD5 · raw file

  1. /**************************************************************************
  2. * Format.java is part of Touch4j 4.0. Copyright 2012 Emitrom LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. **************************************************************************/
  16. package com.emitrom.touch4j.client.utils;
  17. import com.google.gwt.core.client.JavaScriptObject;
  18. /**
  19. * Reusable data formatting functions.
  20. *
  21. * @author alain ekambi
  22. *
  23. */
  24. public class Format {
  25. private Format() {
  26. }
  27. public static native JavaScriptObject getNumberRender(String format)/*-{
  28. return $wnd.Ext.util.Format.numberRenderer(format);
  29. }-*/;
  30. /**
  31. * Parse a value into a formatted date using the specified format pattern.
  32. * Format defaults to 'm/d/Y'.
  33. *
  34. * @param value
  35. * the date string
  36. * @return the formatted date string
  37. */
  38. public static native String date(String value)/*-{
  39. return $wnd.Ext.util.Format.date(value);
  40. }-*/;
  41. /**
  42. * Parse a value into a formatted date using the specified format pattern.
  43. *
  44. * @param value
  45. * the value to format
  46. * @param format
  47. * Any valid date format string (defaults to 'm/d/Y')
  48. * @return the formatted date string
  49. */
  50. public static native String date(String value, String format)/*-{
  51. return $wnd.Ext.util.Format.date(value, format);
  52. }-*/;
  53. /**
  54. * Truncate a string and add an ellipsis ('...') to the end if it exceeds
  55. * the specified length.
  56. *
  57. * @param value
  58. * the string to truncate
  59. * @param length
  60. * the maximum length to allow before truncating
  61. * @return the converted text
  62. */
  63. public static native String ellipsis(String value, int length)/*-{
  64. return $wnd.Ext.util.Format.ellipsis(value, length);
  65. }-*/;
  66. /**
  67. * Convert certain characters (&, <, >, and ') from their HTML character
  68. * equivalents.
  69. *
  70. * @param value
  71. * the string to decode
  72. * @return the decoded text
  73. */
  74. public static native String htmlDecode(String value)/*-{
  75. return $wnd.Ext.util.Format.htmlDecode(value);
  76. }-*/;
  77. /**
  78. * Convert certain characters (&, <, >, and ') to their HTML character
  79. * equivalents for literal display in web pages.
  80. *
  81. * @param value
  82. * the string to encode
  83. * @return the encoded text
  84. */
  85. public static native String htmlEncode(String value)/*-{
  86. return $wnd.Ext.util.Format.htmlEncode(value);
  87. }-*/;
  88. /**
  89. * Strips all HTML tags.
  90. *
  91. * @param value
  92. * the text from which to strip tags
  93. * @return the stripped text
  94. */
  95. public static native String stripTags(String value)/*-{
  96. return $wnd.Ext.util.Format.stripTags(value);
  97. }-*/;
  98. /**
  99. * Strips all script tags.
  100. *
  101. * @param text
  102. * the text from which to strip script tags
  103. * @return the stripped text
  104. */
  105. public static native String stripScripts(String text)/*-{
  106. return $wnd.Ext.util.Format.stripScripts(text);
  107. }-*/;
  108. /**
  109. * Simple format for a file size (xxx bytes, xxx KB, xxx MB).
  110. *
  111. * @param size
  112. * the numeric value to format
  113. * @return the formatted file size
  114. */
  115. public static native String fileSize(double size)/*-{
  116. return $wnd.Ext.util.Format.fileSize(size);
  117. }-*/;
  118. /**
  119. * Format a number as US currency.
  120. *
  121. * @param value
  122. * the value value to format
  123. * @return the formatted currency string
  124. */
  125. public static native String usMoney(String value)/*-{
  126. return $wnd.Ext.util.Format.usMoney(value);
  127. }-*/;
  128. /**
  129. * Format a number as US currency.
  130. *
  131. * @param value
  132. * the numeric value to format
  133. * @return the formatted currency string
  134. */
  135. public static native String usMoney(double value)/*-{
  136. return $wnd.Ext.util.Format.usMoney(value);
  137. }-*/;
  138. /**
  139. * Allows you to define a tokenized string and pass an arbitrary number of
  140. * arguments to replace the tokens. Each token must be unique, and must
  141. * increment in the format {0}, {1}, etc.
  142. *
  143. * @param format
  144. * the tokenized string to be formatted
  145. * @param value
  146. * the value to replace token {0}
  147. * @return the formatted string
  148. */
  149. public static String format(String format, int value) {
  150. return format(format, value + "");
  151. }
  152. /**
  153. * Allows you to define a tokenized string and pass an arbitrary number of
  154. * arguments to replace the tokens. Each token must be unique, and must
  155. * increment in the format {0}, {1}, etc.
  156. *
  157. * @param format
  158. * the tokenized string to be formatted
  159. * @param value
  160. * the value to replace token {0}
  161. * @return the formatted string
  162. */
  163. public static native String format(String format, String value) /*-{
  164. return $wnd.String.format(format, value);
  165. }-*/;
  166. // ext should be taking array of strings as secord argument like Template
  167. // instead of varargs
  168. // need hack becuase of varargs
  169. /**
  170. * Allows you to define a tokenized string and pass an arbitrary number of
  171. * arguments to replace the tokens. Each token must be unique, and must
  172. * increment in the format {0}, {1}, etc.
  173. *
  174. * @param format
  175. * the tokenized string to be formatted
  176. * @param values
  177. * the value to replace token {0}, {1}, ...
  178. * @return the formatted string
  179. */
  180. public static String format(String format, String[] values) {
  181. switch (values.length) {
  182. case 1:
  183. return format(format, values[0]);
  184. case 2:
  185. return format(format, values[0], values[1]);
  186. case 3:
  187. return format(format, values[0], values[1], values[2]);
  188. case 4:
  189. return format(format, values[0], values[1], values[2], values[3]);
  190. case 5:
  191. return format(format, values[0], values[1], values[2], values[3], values[4]);
  192. case 6:
  193. return format(format, values[0], values[1], values[2], values[3], values[4], values[5]);
  194. case 7:
  195. return format(format, values[0], values[1], values[2], values[3], values[4], values[5], values[6]);
  196. default:
  197. return format(format, values[0], values[1], values[2], values[3], values[4]);
  198. }
  199. }
  200. /**
  201. * Allows you to define a tokenized string and pass an arbitrary number of
  202. * arguments to replace the tokens. Each token must be unique, and must
  203. * increment in the format {0}, {1}, etc.
  204. *
  205. * @param format
  206. * the tokenized string to be formatted
  207. * @param value1
  208. * the value to replace token {0}
  209. * @param value2
  210. * the value to replace token {1}
  211. * @return the formatted string
  212. */
  213. public static String format(String format, int value1, int value2) {
  214. return format(format, value1 + "", value2 + "");
  215. }
  216. /**
  217. * Allows you to define a tokenized string and pass an arbitrary number of
  218. * arguments to replace the tokens. Each token must be unique, and must
  219. * increment in the format {0}, {1}, etc.
  220. *
  221. * @param format
  222. * the tokenized string to be formatted
  223. * @param value1
  224. * the value to replace token {0}
  225. * @param value2
  226. * the value to replace token {1}
  227. * @return the formatted string
  228. */
  229. public static native String format(String format, String value1, String value2) /*-{
  230. return $wnd.String.format(format, value1, value2);
  231. }-*/;
  232. /**
  233. * Allows you to define a tokenized string and pass an arbitrary number of
  234. * arguments to replace the tokens. Each token must be unique, and must
  235. * increment in the format {0}, {1}, etc.
  236. *
  237. * @param format
  238. * the tokenized string to be formatted
  239. * @param value1
  240. * the value to replace token {0}
  241. * @param value2
  242. * the value to replace token {1}
  243. * @param value3
  244. * the value to replace token {2}
  245. * @return the formatted string
  246. */
  247. public static String format(String format, int value1, int value2, int value3) {
  248. return format(format, value1 + "", value2 + "", value3 + "");
  249. }
  250. /**
  251. * Allows you to define a tokenized string and pass an arbitrary number of
  252. * arguments to replace the tokens. Each token must be unique, and must
  253. * increment in the format {0}, {1}, etc.
  254. *
  255. * @param format
  256. * the tokenized string to be formatted
  257. * @param value1
  258. * the value to replace token {0}
  259. * @param value2
  260. * the value to replace token {1}
  261. * @param value3
  262. * the value to replace token {2}
  263. * @return the formatted string
  264. */
  265. public static native String format(String format, String value1, String value2, String value3) /*-{
  266. return $wnd.String.format(format, value1, value2, value3);
  267. }-*/;
  268. /**
  269. * Allows you to define a tokenized string and pass an arbitrary number of
  270. * arguments to replace the tokens. Each token must be unique, and must
  271. * increment in the format {0}, {1}, etc.
  272. *
  273. * @param format
  274. * the tokenized string to be formatted
  275. * @param value1
  276. * the value to replace token {0}
  277. * @param value2
  278. * the value to replace token {1}
  279. * @param value3
  280. * the value to replace token {2}
  281. * @param value4
  282. * the value to replace token {3}
  283. * @return the formatted string
  284. */
  285. public static native String format(String format, String value1, String value2, String value3, String value4) /*-{
  286. return $wnd.String.format(format, value1, value2, value3, value4);
  287. }-*/;
  288. /**
  289. * Allows you to define a tokenized string and pass an arbitrary number of
  290. * arguments to replace the tokens. Each token must be unique, and must
  291. * increment in the format {0}, {1}, etc.
  292. *
  293. * @param format
  294. * the tokenized string to be formatted
  295. * @param value1
  296. * the value to replace token {0}
  297. * @param value2
  298. * the value to replace token {1}
  299. * @param value3
  300. * the value to replace token {2}
  301. * @param value4
  302. * the value to replace token {3}
  303. * @param value5
  304. * the value to replace token {4}
  305. * @return the formatted string
  306. */
  307. public static native String format(String format, String value1, String value2, String value3, String value4,
  308. String value5) /*-{
  309. return $wnd.String.format(format, value1, value2, value3, value4,
  310. value5);
  311. }-*/;
  312. /**
  313. * Allows you to define a tokenized string and pass an arbitrary number of
  314. * arguments to replace the tokens. Each token must be unique, and must
  315. * increment in the format {0}, {1}, etc.
  316. *
  317. * @param format
  318. * the tokenized string to be formatted
  319. * @param value1
  320. * the value to replace token {0}
  321. * @param value2
  322. * the value to replace token {1}
  323. * @param value3
  324. * the value to replace token {2}
  325. * @param value4
  326. * the value to replace token {3}
  327. * @param value5
  328. * the value to replace token {4}
  329. * @param value6
  330. * the value to replace token {5}
  331. * @return the formatted string
  332. */
  333. public static native String format(String format, String value1, String value2, String value3, String value4,
  334. String value5, String value6) /*-{
  335. return $wnd.String.format(format, value1, value2, value3, value4,
  336. value5, value6);
  337. }-*/;
  338. /**
  339. * Allows you to define a tokenized string and pass an arbitrary number of
  340. * arguments to replace the tokens. Each token must be unique, and must
  341. * increment in the format {0}, {1}, etc.
  342. *
  343. * @param format
  344. * the tokenized string to be formatted
  345. * @param value1
  346. * the value to replace token {0}
  347. * @param value2
  348. * the value to replace token {1}
  349. * @param value3
  350. * the value to replace token {2}
  351. * @param value4
  352. * the value to replace token {3}
  353. * @param value5
  354. * the value to replace token {4}
  355. * @param value6
  356. * the value to replace token {5}
  357. * @param value7
  358. * the value to replace token {6}
  359. * @return the formatted string
  360. */
  361. public static native String format(String format, String value1, String value2, String value3, String value4,
  362. String value5, String value6, String value7) /*-{
  363. return $wnd.String.format(format, value1, value2, value3, value4,
  364. value5, value6, value7);
  365. }-*/;
  366. /**
  367. * Pads the left side of a string with a specified character. This is
  368. * especially useful for normalizing number and date strings.
  369. * <p/>
  370. *
  371. * <pre>
  372. * String val = Format.leftPad(&quot;123&quot;, 5, &quot;0&quot;);
  373. * // val now containts the String &quot;00123&quot;
  374. * </pre>
  375. *
  376. * @param string
  377. * the original string
  378. * @param size
  379. * the total length of the output string
  380. * @return the padded string
  381. */
  382. public static native String leftPad(String string, int size) /*-{
  383. return $wnd.String.leftPad(string, size);
  384. }-*/;
  385. /**
  386. * Pads the left side of a string with a specified character. This is
  387. * especially useful for normalizing number and date strings.
  388. * <p/>
  389. *
  390. * <pre>
  391. * String val = Format.leftPad(&quot;123&quot;, 5, &quot;0&quot;);
  392. * // val now containts the String &quot;00123&quot;
  393. * </pre>
  394. *
  395. * @param string
  396. * the original string
  397. * @param size
  398. * the total length of the output string
  399. * @param character
  400. * he character with which to pad the original string (defaults
  401. * to empty string " ")
  402. * @return the padded string
  403. */
  404. public static native String leftPad(String string, int size, String character) /*-{
  405. return $wnd.String.leftPad(string, size, character);
  406. }-*/;
  407. }