-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlBenefitsApplication.java
87 lines (69 loc) · 2.58 KB
/
XmlBenefitsApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.sweeneyb.xmlBenefits;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.*;
@SpringBootApplication
public class XmlBenefitsApplication implements CommandLineRunner {
private static Logger LOG = LoggerFactory
.getLogger(XmlBenefitsApplication.class);
public static void main(String[] args) {
SpringApplication.run(XmlBenefitsApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
LOG.info("EXECUTING : command line runner");
SchemaFactory sFactory = SchemaFactory.newInstance(
XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema commonSchema = sFactory.newSchema(new Source[] {
new StreamSource(new FileReader(new File("public/common.xsd")))
});
Schema carrierSchema = sFactory.newSchema(new Source[] {
new StreamSource(new FileReader(new File("public/carrier.xsd")))
});
StreamSource source = new StreamSource(new File("public/basicResponse.xml"));
Validator validator = commonSchema.newValidator();
// System.out.println(xml);
try {
validator.validate(source);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
StreamSource stylesource = new StreamSource(new File("public/commonToQuestions.xslt"));
Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
Validator carrierValidator = carrierSchema.newValidator();
try {
carrierValidator.validate(new StreamSource(new StringReader(writer.toString())));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
LOG.info(writer.toString());
LOG.info("something");
for (int i = 0; i < args.length; ++i) {
LOG.info("args[{}]: {}", i, args[i]);
}
System.exit(0);
}
}