Subject: [PATCH] 4_jaxb_unmarshal
---
Index: src/main/java/ru/javaops/docjava/xml/jaxb/JaxbUtil.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/ru/javaops/docjava/xml/jaxb/JaxbUtil.java b/src/main/java/ru/javaops/docjava/xml/jaxb/JaxbUtil.java
new file mode 100644
--- /dev/null	(date 1695981330508)
+++ b/src/main/java/ru/javaops/docjava/xml/jaxb/JaxbUtil.java	(date 1695981330508)
@@ -0,0 +1,34 @@
+package ru.javaops.docjava.xml.jaxb;
+
+import jakarta.xml.bind.JAXBContext;
+import jakarta.xml.bind.JAXBException;
+import lombok.experimental.UtilityClass;
+import ru.javaops.docjava.schema.ObjectFactory;
+import ru.javaops.docjava.schema.UsersWithMeals;
+import ru.javaops.docjava.xml.xsd.Schemas;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Reader;
+import java.nio.file.Files;
+
+@UtilityClass
+public class JaxbUtil {
+    private static final JaxbUnmarshaller unmarshaller;
+
+    static {
+        try {
+            JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
+            unmarshaller = new JaxbUnmarshaller(ctx);
+            unmarshaller.setSchema(Schemas.of(new File("in/usersWithMeals.xsd")));
+        } catch (JAXBException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static UsersWithMeals unmarshal(File inputXml) throws IOException, JAXBException {
+        try (Reader reader = Files.newBufferedReader(inputXml.toPath())) {
+            return unmarshaller.unmarshal(reader);
+        }
+    }
+}
Index: src/main/java/ru/javaops/docjava/xml/jaxb/JaxbUnmarshaller.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/ru/javaops/docjava/xml/jaxb/JaxbUnmarshaller.java b/src/main/java/ru/javaops/docjava/xml/jaxb/JaxbUnmarshaller.java
new file mode 100644
--- /dev/null	(date 1695981330504)
+++ b/src/main/java/ru/javaops/docjava/xml/jaxb/JaxbUnmarshaller.java	(date 1695981330504)
@@ -0,0 +1,25 @@
+package ru.javaops.docjava.xml.jaxb;
+
+import jakarta.xml.bind.JAXBContext;
+import jakarta.xml.bind.JAXBException;
+import jakarta.xml.bind.Unmarshaller;
+
+import javax.xml.validation.Schema;
+import java.io.Reader;
+
+public class JaxbUnmarshaller {
+
+    private Unmarshaller unmarshaller;
+
+    public JaxbUnmarshaller(JAXBContext ctx) throws JAXBException {
+        unmarshaller = ctx.createUnmarshaller();
+    }
+
+    public void setSchema(Schema schema) {
+        unmarshaller.setSchema(schema);
+    }
+
+    public <T> T unmarshal(Reader reader) throws JAXBException {
+        return (T) unmarshaller.unmarshal(reader);
+    }
+}
Index: src/main/java/ru/javaops/docjava/Commands.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/ru/javaops/docjava/Commands.java b/src/main/java/ru/javaops/docjava/Commands.java
--- a/src/main/java/ru/javaops/docjava/Commands.java	(revision d525dfcaa3346ce4c1e75b4f0439779307cece6c)
+++ b/src/main/java/ru/javaops/docjava/Commands.java	(date 1695981330514)
@@ -1,9 +1,12 @@
 package ru.javaops.docjava;
 
+import jakarta.xml.bind.JAXBException;
 import org.springframework.shell.standard.ShellComponent;
 import org.springframework.shell.standard.ShellMethod;
 import org.springframework.shell.standard.ShellOption;
 import org.xml.sax.SAXException;
+import ru.javaops.docjava.schema.UsersWithMeals;
+import ru.javaops.docjava.xml.jaxb.JaxbUtil;
 import ru.javaops.docjava.xml.xsd.SchemaUtil;
 
 import java.io.File;
@@ -22,10 +25,9 @@
     public void jaxbProcess(
             @ShellOption(value = {"input", "-i"}, help = "Input file") File inputFile,
             @ShellOption(value = {"filter", "-f"}, help = "Filter params", defaultValue = "") List<String> params,
-            @ShellOption(value = {"output", "-o"}, help = "Output file") File outputFile) {
-        System.out.println("\nInput file: " + inputFile.getAbsolutePath());
-        System.out.println("Filter params: " + params.toString());
-        System.out.println("Output file: " + outputFile.getAbsolutePath());
+            @ShellOption(value = {"output", "-o"}, help = "Output file") File outputFile) throws JAXBException, IOException {
+        UsersWithMeals users = JaxbUtil.unmarshal(inputFile);
+        System.out.println(users);
     }
 
     @ShellMethod(key = "stax", value = "Process XML file via StAX")
