TODO: clone mxcodec and new Graph with mxcodec

This commit is contained in:
huangsimin 2019-12-13 18:23:06 +08:00
parent 1bb33e4e15
commit 009a5e37e2
2 changed files with 97 additions and 29 deletions

View File

@ -1,12 +1,83 @@
package com.yame; package com.yame;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.mxgraph.io.mxCodec;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.util.mxXmlUtils;
import com.mxgraph.view.mxGraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
/** /**
* Graph * Graph
*/ */
public class Graph { public class Graph {
public void load() { private final Logger log = LoggerFactory.getLogger(Graph.class.getName());
mxGraph graph;
mxIGraphModel imodel;
mxCodec codec;
public static Graph Merge(Graph g1, Graph g2) {
Graph graph = new Graph();
mxGraph mg1 = g1.getMXGraph();
mxGraph mg2 = g2.getMXGraph();
for( Object cell: mg1.getChildCells(mg1.getDefaultParent())) {
graph.getMXGraph().addCell(cell);
}
for( Object cell: mg2.getChildCells(mg2.getDefaultParent())) {
graph.getMXGraph().addCell(cell);
}
return graph;
}
private void loadString(String xmlString) {
codec = null;
imodel = new mxGraphModel();
graph = new mxGraph(imodel);
Document doc = mxXmlUtils.parseXml(xmlString);
codec = new mxCodec(doc);
Node dmodel = doc.getElementsByTagName("mxGraphModel").item(0);
codec.decode(dmodel, imodel);
}
public void load(String pathStr) throws IOException {
Path path = Paths.get(pathStr);
String xmlString = Files.readString(path);
loadString(xmlString);
}
public void load(byte[] xmlBytes) {
loadString(new String(xmlBytes));
}
public mxGraph getMXGraph() {
return this.graph;
}
public String dumpModel() {
return mxXmlUtils.getXml(codec.encode(imodel));
} }
} }

View File

@ -1,22 +1,12 @@
package com.yame; package com.yame;
import java.io.File; import static org.junit.Assert.assertTrue;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.mxgraph.io.mxCodec; import java.io.IOException;
import com.mxgraph.util.mxXmlUtils;
import com.mxgraph.view.mxGraph;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.apache.log4j.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
/** /**
* GraphTest * GraphTest
@ -27,21 +17,28 @@ public class GraphTest {
private final Logger log = LoggerFactory.getLogger(GraphTest.class.getName()); private final Logger log = LoggerFactory.getLogger(GraphTest.class.getName());
@Test @Test
public void TestCase1() { public void TestLoad() throws IOException {
mxGraph graph = new mxGraph();
Path path = Paths.get(AppTest.testPath + "1.xml");
try {
String xmlString = Files.readString( path );
Document doc = mxXmlUtils.parseXml(xmlString);
mxCodec code = new mxCodec(doc);
Object a = code.decode(doc.getParentNode());
log.debug(a.toString());
// log.debug(mxXmlUtils.getXml( a));
} catch (Exception e) {
// e.printStackTrace();
log.error("Case1", e);
}
Graph graph = new Graph();
graph.load(AppTest.testPath + "1.xml");
assertTrue(graph.dumpModel().startsWith("<mxGraphModel>"));
graph.load(graph.dumpModel().getBytes());
assertTrue(graph.dumpModel().startsWith("<mxGraphModel>"));
} }
@Test
public void TestMegre() throws IOException {
Graph graph = new Graph();
graph.load(AppTest.testPath + "1.xml");
Graph g = Graph.Merge(graph, graph);
log.debug(g.dumpModel());
}
@Test
public void TestCase1() {
}
} }