gexf.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // @ts-nocheck
  20. /**
  21. * This is a parse of GEXF.
  22. *
  23. * The spec of GEXF:
  24. * https://gephi.org/gexf/1.2draft/gexf-12draft-primer.pdf
  25. */
  26. import * as zrUtil from 'zrender/src/core/util';
  27. export function parse(xml) {
  28. let doc;
  29. if (typeof xml === 'string') {
  30. const parser = new DOMParser();
  31. doc = parser.parseFromString(xml, 'text/xml');
  32. }
  33. else {
  34. doc = xml;
  35. }
  36. if (!doc || doc.getElementsByTagName('parsererror').length) {
  37. return null;
  38. }
  39. const gexfRoot = getChildByTagName(doc, 'gexf');
  40. if (!gexfRoot) {
  41. return null;
  42. }
  43. const graphRoot = getChildByTagName(gexfRoot, 'graph');
  44. const attributes = parseAttributes(getChildByTagName(graphRoot, 'attributes'));
  45. const attributesMap = {};
  46. for (let i = 0; i < attributes.length; i++) {
  47. attributesMap[attributes[i].id] = attributes[i];
  48. }
  49. return {
  50. nodes: parseNodes(getChildByTagName(graphRoot, 'nodes'), attributesMap),
  51. links: parseEdges(getChildByTagName(graphRoot, 'edges'))
  52. };
  53. }
  54. function parseAttributes(parent) {
  55. return parent ? zrUtil.map(getChildrenByTagName(parent, 'attribute'), function (attribDom) {
  56. return {
  57. id: getAttr(attribDom, 'id'),
  58. title: getAttr(attribDom, 'title'),
  59. type: getAttr(attribDom, 'type')
  60. };
  61. }) : [];
  62. }
  63. function parseNodes(parent, attributesMap) {
  64. return parent ? zrUtil.map(getChildrenByTagName(parent, 'node'), function (nodeDom) {
  65. const id = getAttr(nodeDom, 'id');
  66. const label = getAttr(nodeDom, 'label');
  67. const node = {
  68. id: id,
  69. name: label,
  70. itemStyle: {
  71. normal: {}
  72. }
  73. };
  74. const vizSizeDom = getChildByTagName(nodeDom, 'viz:size');
  75. const vizPosDom = getChildByTagName(nodeDom, 'viz:position');
  76. const vizColorDom = getChildByTagName(nodeDom, 'viz:color');
  77. // let vizShapeDom = getChildByTagName(nodeDom, 'viz:shape');
  78. const attvaluesDom = getChildByTagName(nodeDom, 'attvalues');
  79. if (vizSizeDom) {
  80. node.symbolSize = parseFloat(getAttr(vizSizeDom, 'value'));
  81. }
  82. if (vizPosDom) {
  83. node.x = parseFloat(getAttr(vizPosDom, 'x'));
  84. node.y = parseFloat(getAttr(vizPosDom, 'y'));
  85. // z
  86. }
  87. if (vizColorDom) {
  88. node.itemStyle.normal.color = 'rgb(' + [
  89. getAttr(vizColorDom, 'r') | 0,
  90. getAttr(vizColorDom, 'g') | 0,
  91. getAttr(vizColorDom, 'b') | 0
  92. ].join(',') + ')';
  93. }
  94. // if (vizShapeDom) {
  95. // node.shape = getAttr(vizShapeDom, 'shape');
  96. // }
  97. if (attvaluesDom) {
  98. const attvalueDomList = getChildrenByTagName(attvaluesDom, 'attvalue');
  99. node.attributes = {};
  100. for (let j = 0; j < attvalueDomList.length; j++) {
  101. const attvalueDom = attvalueDomList[j];
  102. const attId = getAttr(attvalueDom, 'for');
  103. let attValue = getAttr(attvalueDom, 'value');
  104. const attribute = attributesMap[attId];
  105. if (attribute) {
  106. switch (attribute.type) {
  107. case 'integer':
  108. case 'long':
  109. attValue = parseInt(attValue, 10);
  110. break;
  111. case 'float':
  112. case 'double':
  113. attValue = parseFloat(attValue);
  114. break;
  115. case 'boolean':
  116. attValue = attValue.toLowerCase() === 'true';
  117. break;
  118. default:
  119. }
  120. node.attributes[attId] = attValue;
  121. }
  122. }
  123. }
  124. return node;
  125. }) : [];
  126. }
  127. function parseEdges(parent) {
  128. return parent ? zrUtil.map(getChildrenByTagName(parent, 'edge'), function (edgeDom) {
  129. const id = getAttr(edgeDom, 'id');
  130. const label = getAttr(edgeDom, 'label');
  131. const sourceId = getAttr(edgeDom, 'source');
  132. const targetId = getAttr(edgeDom, 'target');
  133. const edge = {
  134. id: id,
  135. name: label,
  136. source: sourceId,
  137. target: targetId,
  138. lineStyle: {
  139. normal: {}
  140. }
  141. };
  142. const lineStyle = edge.lineStyle.normal;
  143. const vizThicknessDom = getChildByTagName(edgeDom, 'viz:thickness');
  144. const vizColorDom = getChildByTagName(edgeDom, 'viz:color');
  145. // let vizShapeDom = getChildByTagName(edgeDom, 'viz:shape');
  146. if (vizThicknessDom) {
  147. lineStyle.width = parseFloat(vizThicknessDom.getAttribute('value'));
  148. }
  149. if (vizColorDom) {
  150. lineStyle.color = 'rgb(' + [
  151. getAttr(vizColorDom, 'r') | 0,
  152. getAttr(vizColorDom, 'g') | 0,
  153. getAttr(vizColorDom, 'b') | 0
  154. ].join(',') + ')';
  155. }
  156. // if (vizShapeDom) {
  157. // edge.shape = vizShapeDom.getAttribute('shape');
  158. // }
  159. return edge;
  160. }) : [];
  161. }
  162. function getAttr(el, attrName) {
  163. return el.getAttribute(attrName);
  164. }
  165. function getChildByTagName(parent, tagName) {
  166. let node = parent.firstChild;
  167. while (node) {
  168. if (
  169. node.nodeType !== 1
  170. || node.nodeName.toLowerCase() !== tagName.toLowerCase()
  171. ) {
  172. node = node.nextSibling;
  173. }
  174. else {
  175. return node;
  176. }
  177. }
  178. return null;
  179. }
  180. function getChildrenByTagName(parent, tagName) {
  181. let node = parent.firstChild;
  182. const children = [];
  183. while (node) {
  184. if (node.nodeName.toLowerCase() === tagName.toLowerCase()) {
  185. children.push(node);
  186. }
  187. node = node.nextSibling;
  188. }
  189. return children;
  190. }