Enterprise Tookit for Acrobat Products > Mobile > (Android | iOS)

JavaScript for Acrobat Reader Mobile API Reference (iOS)

File: Util.js

  1. /*************************************************************************
  2. *
  3. * ADOBE CONFIDENTIAL
  4. * ___________________
  5. *
  6. * Copyright 2012 Adobe Systems Incorporated
  7. * All Rights Reserved.
  8. *
  9. * NOTICE: All information contained herein is, and remains
  10. * the property of Adobe Systems Incorporated and its suppliers,
  11. * if any. The intellectual and technical concepts contained
  12. * herein are proprietary to Adobe Systems Incorporated and its
  13. * suppliers and are protected by trade secret or copyright law.
  14. * Dissemination of this information or reproduction of this material
  15. * is strictly forbidden unless prior written permission is obtained
  16. * from Adobe Systems Incorporated.
  17. *
  18. **************************************************************************/
  19. /**
  20. * A static JavaScript object that defines a number of utility methods and convenience
  21. * functions for string and date formatting and parsing.
  22. *
  23. * @class util
  24. */
  25. function util()
  26. {
  27. };
  28. /**
  29. * Formats one or more arguments as a string according to a format string. It is similar
  30. * to the C function of the same name.
  31. *
  32. * @method printf
  33. * @param cFormat {String} The format string to use.
  34. * @param [arguments] {Any} The optional arguments(s) that contain the data to be inserted
  35. * in place of the % tags specified in the first parameter, the format string. The number of
  36. * optional arguments must be the same as the number of % tags.
  37. * @return {String} A result string formatted as specified.
  38. */
  39. util.printf = function()
  40. {
  41. var ret = "";
  42. try
  43. {
  44. // limit number of argument to less than 50 for now
  45. if (arguments.length < 50)
  46. {
  47. ret = sprintf.apply(this, arguments);
  48. }
  49. }
  50. catch(e) {}
  51. return ret;
  52. };
  53. /**
  54. * Returns a date using a specified format.
  55. *
  56. * @method printd
  57. * @param cFormat {String} A string that is a pattern of supported substrings that are place-holders
  58. * for date and time data.
  59. * @param oDate {Object} A Date object to format.
  60. * @return {String} The formatted date string.
  61. */
  62. util.printd = function()
  63. {
  64. var formattedDate = "";
  65. if (validateInput.apply(this, arguments))
  66. {
  67. var cFormat = arguments[0].toString();
  68. var date = arguments[1];
  69. if ((cFormat.length == 1) && isNumber(cFormat.charAt(0)))
  70. {
  71. // Note: All the cases below behave different from what is specified in Acrobat JS Reference
  72. // but they are in sync with the behaviour on desktop Acrobat
  73. switch (cFormat.charAt(0))
  74. {
  75. case "0":
  76. formattedDate = util.printd("D:yyyymmddHHMMss", date);
  77. break;
  78. case "1":
  79. formattedDate = util.printd("yyyy.mm.dd HH:MM:ss", date);
  80. break;
  81. case "2":
  82. formattedDate = util.printd("m/d/yy h:MM:ss tt", date);
  83. break;
  84. }
  85. }
  86. else
  87. {
  88. var pattern = /m+|d+|y+|H+|h+|M+|s+|t+|j+|\\./g;
  89. var matches = cFormat.match(pattern);
  90. if (matches)
  91. {
  92. formattedDate += cFormat.substring(0, search(cFormat, matches[0]));
  93. for (var i = 0; i < matches.length; i++)
  94. {
  95. var count = matches[i].length;
  96. formattedDate += getFormattedComponentValue(date, matches[i]);
  97. if ((i + 1) < matches.length)
  98. {
  99. var separatorBeginIndex = search(cFormat, matches[i]) + count;
  100. var separatorEndIndex = search(cFormat.slice(separatorBeginIndex), matches[i+1]) + separatorBeginIndex;
  101. formattedDate += cFormat.substring(separatorBeginIndex, separatorEndIndex);
  102. cFormat = cFormat.slice(separatorEndIndex);
  103. }
  104. else
  105. {
  106. var separatorBeginIndex = search(cFormat, matches[i]) + count;
  107. formattedDate += cFormat.slice(separatorBeginIndex);
  108. }
  109. }
  110. }
  111. else
  112. {
  113. formattedDate = cFormat;
  114. }
  115. }
  116. }
  117. return formattedDate;
  118. /**
  119. * Returns formatted date component.
  120. *
  121. * @private
  122. * @method getFormattedComponentValue
  123. * @param date {Object} The date object.
  124. * @param match {String} The component match found in the format string.
  125. * @return {String} Formatted date component.
  126. */
  127. function getFormattedComponentValue(date, match)
  128. {
  129. var component = match.charAt(0);
  130. var count = match.length;
  131. var formattedComponentValue = "";
  132. switch (component)
  133. {
  134. case "m":
  135. if (count == 1)
  136. {
  137. formattedComponentValue = (date.getMonth() + 1).toString();
  138. }
  139. else if (count == 2)
  140. {
  141. formattedComponentValue = lpad(date.getMonth() + 1, 2);
  142. }
  143. else if (count == 3)
  144. {
  145. formattedComponentValue = getMonthString((date.getMonth() + 1), true);
  146. }
  147. else if (count == 4)
  148. {
  149. formattedComponentValue = getMonthString((date.getMonth() + 1), false);
  150. }
  151. break;
  152. case "d":
  153. if (count == 1)
  154. {
  155. formattedComponentValue = date.getDate().toString();
  156. }
  157. else if (count == 2)
  158. {
  159. formattedComponentValue = lpad(date.getDate(), 2);
  160. }
  161. else if (count == 3)
  162. {
  163. formattedComponentValue = getDayString((date.getDay() + 1), true);
  164. }
  165. else if (count == 4)
  166. {
  167. formattedComponentValue = getDayString((date.getDay() + 1), false);
  168. }
  169. break;
  170. case "y":
  171. if (count == 2)
  172. {
  173. var year = lpad(date.getFullYear(), 4);
  174. formattedComponentValue = year.charAt(2) + year.charAt(3);
  175. }
  176. else if (count == 4)
  177. {
  178. formattedComponentValue = lpad(date.getFullYear(), 4);
  179. }
  180. break;
  181. case "H":
  182. if (count == 1)
  183. {
  184. formattedComponentValue = date.getHours().toString();
  185. }
  186. else if (count == 2)
  187. {
  188. formattedComponentValue = lpad(date.getHours(), 2);
  189. }
  190. break;
  191. case "h":
  192. var hours = date.getHours() % 12; // converting to 12 hour format
  193. if (hours == 0)
  194. hours = 12;
  195. if (count == 1)
  196. {
  197. formattedComponentValue = hours.toString();
  198. }
  199. else if (count == 2)
  200. {
  201. formattedComponentValue = lpad(hours, 2);
  202. }
  203. break;
  204. case "M":
  205. if (count == 1)
  206. {
  207. formattedComponentValue = date.getMinutes().toString();
  208. }
  209. else if (count == 2)
  210. {
  211. formattedComponentValue = lpad(date.getMinutes(), 2);
  212. }
  213. break;
  214. case "s":
  215. if (count == 1)
  216. {
  217. formattedComponentValue = date.getSeconds().toString();
  218. }
  219. else if (count == 2)
  220. {
  221. formattedComponentValue = lpad(date.getSeconds(), 2);
  222. }
  223. break;
  224. case "t":
  225. var tValue = "";
  226. if (date.getHours() < 12)
  227. tValue = EScriptString.get("IDS_AM");
  228. else
  229. tValue = EScriptString.get("IDS_PM");
  230. if (count == 1)
  231. {
  232. formattedComponentValue = tValue.chatAt(0);
  233. }
  234. else if (count == 2)
  235. {
  236. formattedComponentValue = tValue;
  237. }
  238. break;
  239. case "\\":
  240. formattedComponentValue = match[1];
  241. break;
  242. }
  243. return formattedComponentValue;
  244. }
  245. /**
  246. * Returns the day string for the given index.
  247. *
  248. * @private
  249. * @method getDayString
  250. * @param index {Number} The index of the day. For e.g. 1 for Sunday.
  251. * @param abbreviated {Boolean} Specifies if the abbreviated string is to be returned or the entire string.
  252. * @return {String} The day string for the index.
  253. */
  254. function getDayString(index, abbreviated)
  255. {
  256. // returns the string (abbreviated or full) corresponding to the given day or a string that
  257. // is indicative of the fact that the index was invalid
  258. var IDS_DAY_INFO = EScriptString.get("IDS_DAY_INFO");
  259. var IDS_INVALID_MONTH = EScriptString.get("IDS_INVALID_MONTH");
  260. var result = "";
  261. var datre = new RegExp("(\\w+)\\[" + index + "\\]", "g");
  262. var matches = IDS_DAY_INFO.match(monthre);
  263. if (matches.length == 2)
  264. {
  265. if (abbreviated)
  266. result = matches[1].slice(0, (matches[1].indexOf("[")));
  267. else
  268. result = matches[0].slice(0, (matches[1].indexOf("[")));
  269. return result;
  270. }
  271. return IDS_INVALID_MONTH;
  272. }
  273. /**
  274. * Returns the month string for the given index.
  275. *
  276. * @private
  277. * @method getMonthString
  278. * @param index {Number} The index of the day. For e.g. 1 for January.
  279. * @param abbreviated {Boolean} Specifies if the abbreviated string is to be returned or the entire string.
  280. * @return {String} The month string for the index.
  281. */
  282. function getMonthString(index, abbreviated)
  283. {
  284. // returns the string (abbreviated or full) corresponding to the given month or a string that
  285. // is indicative of the fact that the index was invalid
  286. // TODO_READER_MOBILE<pvyas>: modify the IDS_MONTH_INFO string so that it includes abbreviated May.
  287. // this will prevent special handling, and then getDateString and getMonthString can share code.
  288. if (abbreviated)
  289. {
  290. var IDS_MONTH_INFO = EScriptString.get("IDS_MONTH_INFO");
  291. var IDS_INVALID_MONTH = EScriptString.get("IDS_INVALID_MONTH");
  292. var monthre = new RegExp("(\\w+)\\[" + index + "\\]", "g");
  293. var matches = IDS_MONTH_INFO.match(monthre);
  294. if (matches.length == 2)
  295. return matches[1].slice(0, (matches[1].indexOf("[")));
  296. return AFGetMonthString(index); // if 2 matches are not found we try and default to full name
  297. }
  298. else
  299. {
  300. return AFGetMonthString(index);
  301. }
  302. }
  303. /**
  304. * Does input validation for printd method.
  305. *
  306. * @private
  307. * @method validateInput
  308. * @return {Boolean} True if input is valid, otherwise false.
  309. */
  310. function validateInput()
  311. {
  312. var result = true;
  313. // check if number of arguments are valid
  314. if (!((arguments.length > 1) && (arguments.length < 4)))
  315. {
  316. result = false;
  317. }
  318. else
  319. {
  320. // check if argument 1 is valid
  321. if (!((typeof(arguments[0]) == "string") || (typeof(arguments[0]) == "number")))
  322. {
  323. result = false;
  324. }
  325. else
  326. {
  327. if (typeof(arguments[0]) == "number")
  328. {
  329. if (!((arguments[0] >= 0) && (arguments[0] <= 2)))
  330. result = false;
  331. }
  332. }
  333. // check if argument 2 is valid
  334. if (!(arguments[1] instanceof Date))
  335. {
  336. result = false;
  337. }
  338. }
  339. return result;
  340. }
  341. };
  342. /**
  343. * Formats a source string, cSource, according to a formatting string, cFormat.
  344. *
  345. * @method printx
  346. * @param cFormat {String} The formatting string to use.
  347. * @param cSource {String} The source string to use.
  348. * @return {String} The formatted string.
  349. */
  350. util.printx = function()
  351. {
  352. var formattedValue = "";
  353. if (validateInput.apply(this, arguments))
  354. {
  355. var cFormat = arguments[0].toString();
  356. var cSource = arguments[1].toString();
  357. var sourceIndex = 0;
  358. var currCase = "";
  359. var pattern = /\?+|X+|A+|9+|\*|\\.|>|<|\=/g;
  360. var matches = cFormat.match(pattern);
  361. if (matches)
  362. {
  363. formattedValue += cFormat.substring(0, search(cFormat, matches[0]));
  364. for (var i = 0; i < matches.length; i++)
  365. {
  366. var count = matches[i].length;
  367. formattedValue += getFormattedComponentValue(cSource, matches[i]);
  368. if ((i + 1) < matches.length)
  369. {
  370. var separatorBeginIndex = search(cFormat, matches[i]) + count;
  371. var separatorEndIndex = search(cFormat.slice(separatorBeginIndex), matches[i+1]) + separatorBeginIndex;
  372. formattedValue += cFormat.substring(separatorBeginIndex, separatorEndIndex);
  373. cFormat = cFormat.slice(separatorEndIndex);
  374. }
  375. else
  376. {
  377. var separatorBeginIndex = search(cFormat, matches[i]) + count;
  378. formattedValue += cFormat.slice(separatorBeginIndex);
  379. }
  380. }
  381. }
  382. else
  383. {
  384. formattedValue = cFormat;
  385. }
  386. }
  387. return formattedValue;
  388. /**
  389. * Returns formatted component value.
  390. *
  391. * @private
  392. * @method getFormattedComponentValue
  393. * @param cSource {String} The source string.
  394. * @param match {String} The component match found in the format string.
  395. * @return {String} Formatted component value.
  396. */
  397. function getFormattedComponentValue(cSource, match)
  398. {
  399. var component = match.charAt(0);
  400. var count = match.length;
  401. var formattedComponentValue = "";
  402. switch (component)
  403. {
  404. case "?":
  405. for (var i = 0; (i < count) && (sourceIndex < cSource.length); i++)
  406. {
  407. if (currCase == "upper")
  408. formattedComponentValue += cSource.charAt(sourceIndex).toUpperCase();
  409. else if (currCase == "lower")
  410. formattedComponentValue += cSource.charAt(sourceIndex).toLowerCase();
  411. else
  412. formattedComponentValue += cSource.charAt(sourceIndex);
  413. sourceIndex++;
  414. }
  415. break;
  416. case "X":
  417. for (var i = 0; (i < count) && (sourceIndex < cSource.length);)
  418. {
  419. if (isAlphaNumeric(cSource.charAt(sourceIndex)))
  420. {
  421. formattedComponentValue += cSource.charAt(sourceIndex);
  422. i++;
  423. }
  424. sourceIndex++;
  425. }
  426. break;
  427. case "A":
  428. for (var i = 0; (i < count) && (sourceIndex < cSource.length);)
  429. {
  430. if (isAlphabetic(cSource.charAt(sourceIndex)))
  431. {
  432. formattedComponentValue += cSource.charAt(sourceIndex);
  433. i++;
  434. }
  435. sourceIndex++;
  436. }
  437. break;
  438. case "9":
  439. for (var i = 0; (i < count) && (sourceIndex < cSource.length);)
  440. {
  441. if (isNumber(cSource.charAt(sourceIndex)))
  442. {
  443. formattedComponentValue += cSource.charAt(sourceIndex);
  444. i++;
  445. }
  446. sourceIndex++;
  447. }
  448. break;
  449. case "*":
  450. formattedComponentValue = cSource.slice(sourceIndex);
  451. break;
  452. case "\\":
  453. formattedComponentValue = match[1];
  454. break;
  455. case ">":
  456. currCase = "upper";
  457. break;
  458. case "<":
  459. currCase = "lower";
  460. break;
  461. case "=":
  462. currCase = "";
  463. break;
  464. }
  465. return formattedComponentValue;
  466. }
  467. /**
  468. * Does input validation for printx method.
  469. *
  470. * @private
  471. * @method validateInput
  472. * @return {Boolean} True if input is valid, otherwise false.
  473. */
  474. function validateInput()
  475. {
  476. var result = true;
  477. // check if number of arguments are valid
  478. if (!(arguments.length == 2))
  479. {
  480. result = false;
  481. }
  482. else
  483. {
  484. // check if argument 1 is valid
  485. if (isNullOrUndefined(arguments[0]))
  486. {
  487. result = false;
  488. }
  489. // check if argument 2 is valid
  490. if (isNullOrUndefined(arguments[1]))
  491. {
  492. result = false;
  493. }
  494. }
  495. return result;
  496. }
  497. }
  498. /**
  499. * Adds left pad to value.
  500. *
  501. * @private
  502. * @method lpad
  503. * @param value {Number} The value to be left padded.
  504. * @param width {Number} The width in characters of the final string.
  505. * @return {String} Left padded value.
  506. */
  507. function lpad(value, width)
  508. {
  509. result = value.toString();
  510. while (result.length < width)
  511. result = "0" + result;
  512. return result;
  513. }
  514. /**
  515. * Searches for a string in another string.
  516. *
  517. * @private
  518. * @method search
  519. * @param string {String} The string in which the searchString is to be found.
  520. * @param searchString {String} The search string.
  521. * @return {Number} The index of where the searchString was found in the string.
  522. */
  523. function search(string, searchString)
  524. {
  525. return string.search(searchString.replace(/\\/g,"\\\\").replace(/\?/g,"\\?").replace(/\*/g,"\\*"));
  526. }
  527. /**
  528. * Checks if the value is either null or undefined.
  529. *
  530. * @private
  531. * @method isNullOrUndefined
  532. * @param value {Any} The value which is to be tested.
  533. * @return {Boolean} True if value is null or undefined, otherwise false.
  534. */
  535. function isNullOrUndefined(value)
  536. {
  537. if ((value == null) || (value == undefined))
  538. return true;
  539. return false;
  540. }

© 2013-15 Adobe Systems, Inc. All rights reserved. Use of these APIs including, the download of software, submission of comments, ideas, feature requests and techniques, and Adobe's rights to use such submitted materials, is governed by the Adobe.com Terms of Use and the Adobe Privacy Policy.