gexf.js 6.5 KB

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