3232import javax .xml .bind .JAXBContext ;
3333import javax .xml .bind .JAXBException ;
3434import javax .xml .bind .Unmarshaller ;
35+ import javax .xml .bind .util .JAXBSource ;
3536import javax .xml .namespace .NamespaceContext ;
3637import javax .xml .parsers .DocumentBuilder ;
3738import javax .xml .parsers .DocumentBuilderFactory ;
3839import javax .xml .parsers .ParserConfigurationException ;
40+ import javax .xml .transform .TransformerException ;
41+ import javax .xml .transform .TransformerFactory ;
42+ import javax .xml .transform .dom .DOMResult ;
3943import javax .xml .xpath .XPath ;
4044import javax .xml .xpath .XPathConstants ;
4145import javax .xml .xpath .XPathExpressionException ;
6771 */
6872public class DataDictionaryGenerator {
6973
74+ private static class UnmarshalResult {
75+ private final Repository repository ;
76+ private final Node repositoryNode ;
77+
78+ public UnmarshalResult (Repository repository , Node repositoryNode ) {
79+ this .repository = repository ;
80+ this .repositoryNode = repositoryNode ;
81+ }
82+
83+ public Repository getRepository () {
84+ return repository ;
85+ }
86+
87+ public Node getRepositoryNode () {
88+ return repositoryNode ;
89+ }
90+ }
91+
7092 private static class KeyValue <T > {
7193 final String key ;
7294 final T value ;
@@ -112,19 +134,30 @@ public static void main(String[] args) {
112134 private final Map <Integer , ComponentType > components = new HashMap <>();
113135 private final Map <Integer , GroupType > groups = new HashMap <>();
114136 private final Map <Integer , FieldType > fields = new HashMap <>();
115- private Document xmlDocument ;
116137 private static final String FIX_LATEST = "FIX.Latest" ;
117138
118139 public void generate (InputStream inputFile , File outputDir ) throws JAXBException , IOException ,
119140 ParserConfigurationException , SAXException , XPathExpressionException {
120- final Repository repository = unmarshal (inputFile );
121- generate (repository , outputDir );
141+ final UnmarshalResult result = unmarshal (inputFile );
142+ generate (result . getRepository () , outputDir , result . getRepositoryNode () );
122143 }
123144
124145 public void generate (Repository repository , File outputDir )
125146 throws IOException , XPathExpressionException {
147+ try {
148+ JAXBContext jaxbContext = JAXBContext .newInstance (Repository .class );
149+ DOMResult domResult = new DOMResult ();
150+ TransformerFactory .newInstance ().newTransformer ().transform (new JAXBSource (jaxbContext , repository ), domResult );
151+ Node repositoryNode = domResult .getNode ();
152+ generate (repository , outputDir , repositoryNode );
153+ } catch (JAXBException | TransformerException e ) {
154+ throw new RuntimeException ("Failed to transform repository to document" , e );
155+ }
156+ }
126157
127- Set <Integer > requiredGroupIds = getRequiredGroups ();
158+ private void generate (Repository repository , File outputDir , Node repositoryNode )
159+ throws IOException , XPathExpressionException {
160+ Set <Integer > requiredGroupIds = getRequiredGroups (repositoryNode );
128161
129162 final List <CodeSetType > codeSetList = repository .getCodeSets ().getCodeSet ();
130163 for (final CodeSetType codeSet : codeSetList ) {
@@ -252,8 +285,9 @@ private File getSpecFilePath(File outputDir, String versionPath, String extensio
252285 return new File (outputDir , sb .toString ());
253286 }
254287
255- private Set <Integer > getRequiredGroups () throws XPathExpressionException {
288+ private Set <Integer > getRequiredGroups (Node repositoryNode ) throws XPathExpressionException {
256289 Set <Integer > groupIds = new HashSet <>();
290+
257291 XPath xPath = XPathFactory .newInstance ().newXPath ();
258292 xPath .setNamespaceContext (new NamespaceContext () {
259293
@@ -280,7 +314,7 @@ public Iterator<String> getPrefixes(String namespaceURI) {
280314 });
281315 String expression = "//fixr:groupRef[@presence='required']" ;
282316 NodeList nodeList = (NodeList ) xPath .compile (expression )
283- .evaluate (xmlDocument . getDocumentElement () , XPathConstants .NODESET );
317+ .evaluate (repositoryNode , XPathConstants .NODESET );
284318 for (int i = 0 ; i < nodeList .getLength (); i ++) {
285319 if (nodeList .item (i ).getNodeType () == Node .ELEMENT_NODE ) {
286320 Element element = (Element ) nodeList .item (i );
@@ -311,15 +345,16 @@ private String toConstantName(String symbolicName) {
311345 return sb .toString ().toUpperCase ();
312346 }
313347
314- private Repository unmarshal (InputStream inputFile )
348+ private UnmarshalResult unmarshal (InputStream inputFile )
315349 throws JAXBException , ParserConfigurationException , SAXException , IOException {
316350 DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance ();
317351 builderFactory .setNamespaceAware (true );
318352 DocumentBuilder builder = builderFactory .newDocumentBuilder ();
319- xmlDocument = builder .parse (inputFile );
353+ Document document = builder .parse (inputFile );
320354 final JAXBContext jaxbContext = JAXBContext .newInstance (Repository .class );
321355 final Unmarshaller jaxbUnmarshaller = jaxbContext .createUnmarshaller ();
322- return (Repository ) jaxbUnmarshaller .unmarshal (xmlDocument );
356+ Repository repository = (Repository ) jaxbUnmarshaller .unmarshal (document );
357+ return new UnmarshalResult (repository , document .getDocumentElement ());
323358 }
324359
325360 private void usage () {
0 commit comments