From f4fcd5080ce916a8d87620d33033f800b8de0c7b Mon Sep 17 00:00:00 2001 From: rusefi Date: Sat, 16 May 2020 18:52:08 -0400 Subject: [PATCH] refactoring --- .../inifile/src/com/rusefi/xml/XmlUtil.java | 35 ++++++++++++++++++ .../screen/src/com/rusefi/MdGenerator.java | 2 +- .../src/com/rusefi/ScreenGenerator.java | 3 +- .../screen/src/com/rusefi/xml/XmlUtil.java | 37 ------------------- 4 files changed, 38 insertions(+), 39 deletions(-) create mode 100644 java_console/inifile/src/com/rusefi/xml/XmlUtil.java delete mode 100644 java_tools/ts_screenshots/screen/src/com/rusefi/xml/XmlUtil.java diff --git a/java_console/inifile/src/com/rusefi/xml/XmlUtil.java b/java_console/inifile/src/com/rusefi/xml/XmlUtil.java new file mode 100644 index 0000000000..ed62f3240a --- /dev/null +++ b/java_console/inifile/src/com/rusefi/xml/XmlUtil.java @@ -0,0 +1,35 @@ +package com.rusefi.xml; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; + +public class XmlUtil { + + public static void writeXml(Object instance, Class modelClass, String fileName) throws JAXBException, IOException { + JAXBContext jaxbContext = JAXBContext.newInstance(modelClass); + + Marshaller marshaller = jaxbContext.createMarshaller(); + + StringWriter xmlWriter = new StringWriter(); + marshaller.marshal(instance, xmlWriter); + System.out.println(xmlWriter.toString()); + + System.out.println("Writing " + fileName); + marshaller.marshal(instance, new FileWriter(fileName)); + System.out.println("Done " + fileName); + } + + public static T readModel(Class modelClass, String fileName) throws Exception { + File xmlFile = new File(fileName); + JAXBContext jaxbContext; + jaxbContext = JAXBContext.newInstance(modelClass); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + return (T) jaxbUnmarshaller.unmarshal(xmlFile); + } +} diff --git a/java_tools/ts_screenshots/screen/src/com/rusefi/MdGenerator.java b/java_tools/ts_screenshots/screen/src/com/rusefi/MdGenerator.java index 7b98910fb8..459e5030a1 100644 --- a/java_tools/ts_screenshots/screen/src/com/rusefi/MdGenerator.java +++ b/java_tools/ts_screenshots/screen/src/com/rusefi/MdGenerator.java @@ -16,7 +16,7 @@ public class MdGenerator { //FOLDER = "images/"; FOLDER = "overview/TS_generated/"; - ContentModel contentModel = XmlUtil.readModel(); + ContentModel contentModel = XmlUtil.readModel(ContentModel.class, ScreenGenerator.FILE_NAME); generateTopLevel(contentModel); diff --git a/java_tools/ts_screenshots/screen/src/com/rusefi/ScreenGenerator.java b/java_tools/ts_screenshots/screen/src/com/rusefi/ScreenGenerator.java index 70fc3f11c3..2ecabc518b 100644 --- a/java_tools/ts_screenshots/screen/src/com/rusefi/ScreenGenerator.java +++ b/java_tools/ts_screenshots/screen/src/com/rusefi/ScreenGenerator.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicReference; public class ScreenGenerator { + public static final String FILE_NAME = "output.xml"; private static final String PNG = "png"; private static ArrayList topLevelButtons = new ArrayList<>(); @@ -61,7 +62,7 @@ public class ScreenGenerator { handleTopLevelButtons(mainFrame, topLevelButtons); - XmlUtil.writeXml(contentModel); + XmlUtil.writeXml(contentModel, ContentModel.class, FILE_NAME); } private static void waitForMainFrame(Frame mainFrame) throws InterruptedException { diff --git a/java_tools/ts_screenshots/screen/src/com/rusefi/xml/XmlUtil.java b/java_tools/ts_screenshots/screen/src/com/rusefi/xml/XmlUtil.java deleted file mode 100644 index 7b0dfd7dab..0000000000 --- a/java_tools/ts_screenshots/screen/src/com/rusefi/xml/XmlUtil.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.rusefi.xml; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.StringWriter; - -public class XmlUtil { - - public static final String FILE_NAME = "output.xml"; - - public static void writeXml(ContentModel contentModel) throws JAXBException, IOException { - JAXBContext jaxbContext = JAXBContext.newInstance(ContentModel.class); - - Marshaller marshaller = jaxbContext.createMarshaller(); - - StringWriter xmlWriter = new StringWriter(); - marshaller.marshal(contentModel, xmlWriter); - System.out.println(xmlWriter.toString()); - - System.out.println("Writing " + FILE_NAME); - marshaller.marshal(contentModel, new FileWriter(FILE_NAME)); - System.out.println("Done " + FILE_NAME); - } - - public static ContentModel readModel() throws Exception { - File xmlFile = new File(FILE_NAME); - JAXBContext jaxbContext; - jaxbContext = JAXBContext.newInstance(ContentModel.class); - Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); - return (ContentModel) jaxbUnmarshaller.unmarshal(xmlFile); - } -}