diff --git a/src/main/java/com/yame/Graph.java b/src/main/java/com/yame/Graph.java index 7fa3430..4fbeaea 100644 --- a/src/main/java/com/yame/Graph.java +++ b/src/main/java/com/yame/Graph.java @@ -1,12 +1,83 @@ 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 */ 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)); + + } } \ No newline at end of file diff --git a/src/test/java/com/yame/GraphTest.java b/src/test/java/com/yame/GraphTest.java index 5b81bf3..ab8110a 100644 --- a/src/test/java/com/yame/GraphTest.java +++ b/src/test/java/com/yame/GraphTest.java @@ -1,22 +1,12 @@ package com.yame; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; +import static org.junit.Assert.assertTrue; -import com.mxgraph.io.mxCodec; -import com.mxgraph.util.mxXmlUtils; -import com.mxgraph.view.mxGraph; +import java.io.IOException; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.log4j.*; -import org.w3c.dom.Document; -import org.w3c.dom.Node; /** * GraphTest @@ -27,21 +17,28 @@ public class GraphTest { private final Logger log = LoggerFactory.getLogger(GraphTest.class.getName()); @Test - public void TestCase1() { - 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); - } - + public void TestLoad() throws IOException { + + Graph graph = new Graph(); + graph.load(AppTest.testPath + "1.xml"); + assertTrue(graph.dumpModel().startsWith("")); + graph.load(graph.dumpModel().getBytes()); + assertTrue(graph.dumpModel().startsWith("")); + + } + + @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() { + } + } \ No newline at end of file