Skip to content

Commit 9dabcb6

Browse files
authored
Merge pull request #10 from patricklucas/generate_from_repository
Fix generating DD from Repository
2 parents 1833e34 + fe31d68 commit 9dabcb6

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

quickfixj-from-fix-orchestra-repository/quickfixj-from-fix-orchestra-generator/src/main/java/org/quickfixj/orchestra/DataDictionaryGenerator.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@
3232
import javax.xml.bind.JAXBContext;
3333
import javax.xml.bind.JAXBException;
3434
import javax.xml.bind.Unmarshaller;
35+
import javax.xml.bind.util.JAXBSource;
3536
import javax.xml.namespace.NamespaceContext;
3637
import javax.xml.parsers.DocumentBuilder;
3738
import javax.xml.parsers.DocumentBuilderFactory;
3839
import javax.xml.parsers.ParserConfigurationException;
40+
import javax.xml.transform.TransformerException;
41+
import javax.xml.transform.TransformerFactory;
42+
import javax.xml.transform.dom.DOMResult;
3943
import javax.xml.xpath.XPath;
4044
import javax.xml.xpath.XPathConstants;
4145
import javax.xml.xpath.XPathExpressionException;
@@ -67,6 +71,24 @@
6771
*/
6872
public 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() {

quickfixj-from-fix-orchestra-repository/quickfixj-from-fix-orchestra-generator/src/test/java/org/quickfixj/orchestra/DataDictionaryGeneratorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
import java.io.BufferedReader;
55
import java.io.File;
66
import java.io.FileReader;
7+
import java.io.InputStream;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
710
import java.util.regex.Matcher;
811
import java.util.regex.Pattern;
912

13+
import io.fixprotocol._2020.orchestra.repository.Repository;
1014
import org.junit.jupiter.api.BeforeEach;
1115
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.io.TempDir;
17+
18+
import javax.xml.bind.JAXBContext;
19+
import javax.xml.bind.Unmarshaller;
1220

1321
public class DataDictionaryGeneratorTest {
1422

@@ -47,6 +55,27 @@ public void testGenerateFIXLatest() throws Exception {
4755
}
4856
}
4957

58+
@Test
59+
public void testGenerateFIXLatestFromRepository(@TempDir Path tempDir) throws Exception {
60+
JAXBContext jaxbContext = JAXBContext.newInstance(Repository.class);
61+
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
62+
Repository repository;
63+
64+
try (InputStream stream = Thread.currentThread().getContextClassLoader().getResource("trade-latest.xml").openStream()) {
65+
repository = (Repository) unmarshaller.unmarshal(stream);
66+
}
67+
68+
Path outputDir = tempDir.resolve("output");
69+
Files.createDirectory(outputDir);
70+
71+
generator.generate(repository, outputDir.toFile());
72+
try (BufferedReader brTest = Files.newBufferedReader(outputDir.resolve("FIXLatest.xml"))) {
73+
String firstLine = brTest.readLine();
74+
assertEquals("<fix major=\"Latest\" minor=\"0\" servicepack=\"0\" extensionpack=\"269\">",
75+
firstLine);
76+
}
77+
}
78+
5079
@Test
5180
public void testExtract() throws Exception {
5281
assertEquals("0", generator.extractServicePack("FIX.5.0"));

0 commit comments

Comments
 (0)