You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configXmlParser.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Parser for config.xml file. Read plugin-specific preferences (from <universal-links> tag) as JSON object.
  3. */
  4. var path = require('path');
  5. var ConfigXmlHelper = require('./configXmlHelper.js');
  6. var DEFAULT_SCHEME = 'http';
  7. module.exports = {
  8. readPreferences: readPreferences
  9. };
  10. // region Public API
  11. /**
  12. * Read plugin preferences from the config.xml file.
  13. *
  14. * @param {Object} cordovaContext - cordova context object
  15. * @return {Array} list of host objects
  16. */
  17. function readPreferences(cordovaContext) {
  18. // read data from projects root config.xml file
  19. var configXml = new ConfigXmlHelper(cordovaContext).read();
  20. if (configXml == null) {
  21. console.warn('config.xml not found! Please, check that it exist\'s in your project\'s root directory.');
  22. return null;
  23. }
  24. // look for data from the <universal-links> tag
  25. var ulXmlPreferences = configXml.widget['universal-links'];
  26. if (ulXmlPreferences == null || ulXmlPreferences.length == 0) {
  27. console.warn('<universal-links> tag is not set in the config.xml. Universal Links plugin is not going to work.');
  28. return null;
  29. }
  30. var xmlPreferences = ulXmlPreferences[0];
  31. // read hosts
  32. var hosts = constructHostsList(xmlPreferences);
  33. // read ios team ID
  34. var iosTeamId = getTeamIdPreference(xmlPreferences);
  35. return {
  36. 'hosts': hosts,
  37. 'iosTeamId': iosTeamId
  38. };
  39. }
  40. // endregion
  41. // region Private API
  42. function getTeamIdPreference(xmlPreferences) {
  43. if (xmlPreferences.hasOwnProperty('ios-team-id')) {
  44. return xmlPreferences['ios-team-id'][0]['$']['value'];
  45. }
  46. return null;
  47. }
  48. /**
  49. * Construct list of host objects, defined in xml file.
  50. *
  51. * @param {Object} xmlPreferences - plugin preferences from config.xml as JSON object
  52. * @return {Array} array of JSON objects, where each entry defines host data from config.xml.
  53. */
  54. function constructHostsList(xmlPreferences) {
  55. var hostsList = [];
  56. // look for defined hosts
  57. var xmlHostList = xmlPreferences['host'];
  58. if (xmlHostList == null || xmlHostList.length == 0) {
  59. return [];
  60. }
  61. xmlHostList.forEach(function(xmlElement) {
  62. var host = constructHostEntry(xmlElement);
  63. if (host) {
  64. hostsList.push(host);
  65. }
  66. });
  67. return hostsList;
  68. }
  69. /**
  70. * Construct host object from xml data.
  71. *
  72. * @param {Object} xmlElement - xml data to process.
  73. * @return {Object} host entry as JSON object
  74. */
  75. function constructHostEntry(xmlElement) {
  76. var host = {
  77. scheme: DEFAULT_SCHEME,
  78. name: '',
  79. paths: []
  80. };
  81. var hostProperties = xmlElement['$'];
  82. if (hostProperties == null || hostProperties.length == 0) {
  83. return null;
  84. }
  85. // read host name
  86. host.name = hostProperties.name;
  87. // read scheme if defined
  88. if (hostProperties['scheme'] != null) {
  89. host.scheme = hostProperties.scheme;
  90. }
  91. // construct paths list, defined for the given host
  92. host.paths = constructPaths(xmlElement);
  93. return host;
  94. }
  95. /**
  96. * Construct list of path objects from the xml data.
  97. *
  98. * @param {Object} xmlElement - xml data to process
  99. * @return {Array} list of path entries, each on is a JSON object
  100. */
  101. function constructPaths(xmlElement) {
  102. if (xmlElement['path'] == null) {
  103. return ['*'];
  104. }
  105. var paths = [];
  106. xmlElement.path.some(function(pathElement) {
  107. var url = pathElement['$']['url'];
  108. // Ignore explicit paths if '*' is defined
  109. if (url === '*') {
  110. paths = ['*'];
  111. return true;
  112. }
  113. paths.push(url);
  114. });
  115. return paths;
  116. }
  117. // endregion