@@ -29,6 +29,8 @@ class OjpToTriasTransformer {
29
29
"trias_to_ojp2.0_request.xslt"
30
30
);
31
31
32
+ private static final JAXBContext CONTEXT = jaxbContext ();
33
+
32
34
static String transform (OJP ojp ) {
33
35
var writer = new StringWriter ();
34
36
transform (ojp , writer );
@@ -37,8 +39,7 @@ static String transform(OJP ojp) {
37
39
38
40
static void transform (OJP ojp , Writer writer ) {
39
41
try {
40
- var context = JAXBContext .newInstance (OJP .class );
41
- var marshaller = context .createMarshaller ();
42
+ var marshaller = CONTEXT .createMarshaller ();
42
43
43
44
// Convert Java object to XML string
44
45
var outputStream = new ByteArrayOutputStream ();
@@ -53,8 +54,6 @@ static void transform(OJP ojp, Writer writer) {
53
54
}
54
55
55
56
static OJP triasToOjp (String trias ) throws JAXBException , TransformerException {
56
- var context = JAXBContext .newInstance (OJP .class );
57
-
58
57
var xmlSource = new StreamSource (
59
58
new ByteArrayInputStream (trias .getBytes (StandardCharsets .UTF_8 ))
60
59
);
@@ -64,7 +63,7 @@ static OJP triasToOjp(String trias) throws JAXBException, TransformerException {
64
63
transformer .transform (xmlSource , new StreamResult (writer ));
65
64
var transformedXml = writer .toString (StandardCharsets .UTF_8 );
66
65
67
- var unmarshaller = context .createUnmarshaller ();
66
+ var unmarshaller = CONTEXT .createUnmarshaller ();
68
67
return (OJP ) unmarshaller .unmarshal (
69
68
new ByteArrayInputStream (transformedXml .getBytes (StandardCharsets .UTF_8 ))
70
69
);
@@ -87,4 +86,12 @@ static Templates loadTemplate(String name) {
87
86
throw new RuntimeException (e );
88
87
}
89
88
}
89
+
90
+ private static JAXBContext jaxbContext () {
91
+ try {
92
+ return JAXBContext .newInstance (OJP .class );
93
+ } catch (JAXBException e ) {
94
+ throw new RuntimeException (e );
95
+ }
96
+ }
90
97
}
0 commit comments