gexf.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. // @ts-nocheck
  41. /**
  42. * This is a parse of GEXF.
  43. *
  44. * The spec of GEXF:
  45. * https://gephi.org/gexf/1.2draft/gexf-12draft-primer.pdf
  46. */
  47. import * as zrUtil from 'zrender/lib/core/util.js';
  48. export function parse(xml) {
  49. var doc;
  50. if (typeof xml === 'string') {
  51. var parser = new DOMParser();
  52. doc = parser.parseFromString(xml, 'text/xml');
  53. } else {
  54. doc = xml;
  55. }
  56. if (!doc || doc.getElementsByTagName('parsererror').length) {
  57. return null;
  58. }
  59. var gexfRoot = getChildByTagName(doc, 'gexf');
  60. if (!gexfRoot) {
  61. return null;
  62. }
  63. var graphRoot = getChildByTagName(gexfRoot, 'graph');
  64. var attributes = parseAttributes(getChildByTagName(graphRoot, 'attributes'));
  65. var attributesMap = {};
  66. for (var i = 0; i < attributes.length; i++) {
  67. attributesMap[attributes[i].id] = attributes[i];
  68. }
  69. return {
  70. nodes: parseNodes(getChildByTagName(graphRoot, 'nodes'), attributesMap),
  71. links: parseEdges(getChildByTagName(graphRoot, 'edges'))
  72. };
  73. }
  74. function parseAttributes(parent) {
  75. return parent ? zrUtil.map(getChildrenByTagName(parent, 'attribute'), function (attribDom) {
  76. return {
  77. id: getAttr(attribDom, 'id'),
  78. title: getAttr(attribDom, 'title'),
  79. type: getAttr(attribDom, 'type')
  80. };
  81. }) : [];
  82. }
  83. function parseNodes(parent, attributesMap) {
  84. return parent ? zrUtil.map(getChildrenByTagName(parent, 'node'), function (nodeDom) {
  85. var id = getAttr(nodeDom, 'id');
  86. var label = getAttr(nodeDom, 'label');
  87. var node = {
  88. id: id,
  89. name: label,
  90. itemStyle: {
  91. normal: {}
  92. }
  93. };
  94. var vizSizeDom = getChildByTagName(nodeDom, 'viz:size');
  95. var vizPosDom = getChildByTagName(nodeDom, 'viz:position');
  96. var vizColorDom = getChildByTagName(nodeDom, 'viz:color'); // let vizShapeDom = getChildByTagName(nodeDom, 'viz:shape');
  97. var attvaluesDom = getChildByTagName(nodeDom, 'attvalues');
  98. if (vizSizeDom) {
  99. node.symbolSize = parseFloat(getAttr(vizSizeDom, 'value'));
  100. }
  101. if (vizPosDom) {
  102. node.x = parseFloat(getAttr(vizPosDom, 'x'));
  103. node.y = parseFloat(getAttr(vizPosDom, 'y')); // z
  104. }
  105. if (vizColorDom) {
  106. node.itemStyle.normal.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  107. } // if (vizShapeDom) {
  108. // node.shape = getAttr(vizShapeDom, 'shape');
  109. // }
  110. if (attvaluesDom) {
  111. var attvalueDomList = getChildrenByTagName(attvaluesDom, 'attvalue');
  112. node.attributes = {};
  113. for (var j = 0; j < attvalueDomList.length; j++) {
  114. var attvalueDom = attvalueDomList[j];
  115. var attId = getAttr(attvalueDom, 'for');
  116. var attValue = getAttr(attvalueDom, 'value');
  117. var attribute = attributesMap[attId];
  118. if (attribute) {
  119. switch (attribute.type) {
  120. case 'integer':
  121. case 'long':
  122. attValue = parseInt(attValue, 10);
  123. break;
  124. case 'float':
  125. case 'double':
  126. attValue = parseFloat(attValue);
  127. break;
  128. case 'boolean':
  129. attValue = attValue.toLowerCase() === 'true';
  130. break;
  131. default:
  132. }
  133. node.attributes[attId] = attValue;
  134. }
  135. }
  136. }
  137. return node;
  138. }) : [];
  139. }
  140. function parseEdges(parent) {
  141. return parent ? zrUtil.map(getChildrenByTagName(parent, 'edge'), function (edgeDom) {
  142. var id = getAttr(edgeDom, 'id');
  143. var label = getAttr(edgeDom, 'label');
  144. var sourceId = getAttr(edgeDom, 'source');
  145. var targetId = getAttr(edgeDom, 'target');
  146. var edge = {
  147. id: id,
  148. name: label,
  149. source: sourceId,
  150. target: targetId,
  151. lineStyle: {
  152. normal: {}
  153. }
  154. };
  155. var lineStyle = edge.lineStyle.normal;
  156. var vizThicknessDom = getChildByTagName(edgeDom, 'viz:thickness');
  157. var vizColorDom = getChildByTagName(edgeDom, 'viz:color'); // let vizShapeDom = getChildByTagName(edgeDom, 'viz:shape');
  158. if (vizThicknessDom) {
  159. lineStyle.width = parseFloat(vizThicknessDom.getAttribute('value'));
  160. }
  161. if (vizColorDom) {
  162. lineStyle.color = 'rgb(' + [getAttr(vizColorDom, 'r') | 0, getAttr(vizColorDom, 'g') | 0, getAttr(vizColorDom, 'b') | 0].join(',') + ')';
  163. } // if (vizShapeDom) {
  164. // edge.shape = vizShapeDom.getAttribute('shape');
  165. // }
  166. return edge;
  167. }) : [];
  168. }
  169. function getAttr(el, attrName) {
  170. return el.getAttribute(attrName);
  171. }
  172. function getChildByTagName(parent, tagName) {
  173. var node = parent.firstChild;
  174. while (node) {
  175. if (node.nodeType !== 1 || node.nodeName.toLowerCase() !== tagName.toLowerCase()) {
  176. node = node.nextSibling;
  177. } else {
  178. return node;
  179. }
  180. }
  181. return null;
  182. }
  183. function getChildrenByTagName(parent, tagName) {
  184. var node = parent.firstChild;
  185. var children = [];
  186. while (node) {
  187. if (node.nodeName.toLowerCase() === tagName.toLowerCase()) {
  188. children.push(node);
  189. }
  190. node = node.nextSibling;
  191. }
  192. return children;
  193. }