@@ -29,32 +29,31 @@ class OjpToTriasTransformer {
29
29
"trias_to_ojp2.0_request.xslt"
30
30
);
31
31
32
- static String transform (OJP ojp ) {
32
+ private static final JAXBContext CONTEXT = jaxbContext ();
33
+
34
+ static String ojpToTrias (OJP ojp ) {
33
35
var writer = new StringWriter ();
34
- transform (ojp , writer );
36
+ ojpToTrias (ojp , writer );
35
37
return writer .toString ();
36
38
}
37
39
38
- static void transform (OJP ojp , Writer writer ) {
40
+ static void ojpToTrias (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 ();
45
46
marshaller .marshal (ojp , outputStream );
46
47
47
48
var xmlSource = new StreamSource (new ByteArrayInputStream (outputStream .toByteArray ()));
48
49
49
- transform (writer , xmlSource );
50
+ ojpToTrias (writer , xmlSource );
50
51
} catch (IOException | JAXBException | TransformerException e ) {
51
52
throw new RuntimeException (e );
52
53
}
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,13 +63,13 @@ 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
);
71
70
}
72
71
73
- static void transform (Writer writer , StreamSource xmlSource )
72
+ static void ojpToTrias (Writer writer , StreamSource xmlSource )
74
73
throws IOException , TransformerException {
75
74
var transformer = OJP_TO_TRIAS_TEMPLATE .newTransformer ();
76
75
transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
@@ -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