diff --git a/src/main/java/org/xmlet/xsdparser/core/XsdParserCore.java b/src/main/java/org/xmlet/xsdparser/core/XsdParserCore.java index 4afd6b1..c2b8a7d 100644 --- a/src/main/java/org/xmlet/xsdparser/core/XsdParserCore.java +++ b/src/main/java/org/xmlet/xsdparser/core/XsdParserCore.java @@ -392,31 +392,12 @@ private void resolveInnerRefs() { for (int i = 0; i < fileNameList.size(); i++) { String fileName = fileNameList.get(i); - if (!doneList.get(i)) { - List includedFiles = - parseElements.get(fileName) - .stream() - .filter(referenceBase -> referenceBase instanceof ConcreteElement && referenceBase.getElement() instanceof XsdInclude) - .map(referenceBase -> (((XsdInclude) referenceBase.getElement()).getSchemaLocation())) - .collect(Collectors.toList()); - - Set transitiveIncludes = new HashSet<>(); - - for (String includedFile : includedFiles) { - parseElements.keySet() - .stream() - .filter(fileNameAux -> fileNameAux.endsWith(includedFile)) - .findFirst() - .ifPresent(fullIncludedFileName -> transitiveIncludes.addAll(parseElements.get(fullIncludedFileName) - .stream() - .filter(referenceBase -> referenceBase instanceof ConcreteElement && referenceBase.getElement() instanceof XsdInclude) - .map(referenceBase -> (((XsdInclude) referenceBase.getElement()).getSchemaLocation())) - .collect(Collectors.toList()))); - } + if (!doneList.get(i)){ + Set includedFiles = new HashSet<>(); + includedFiles.add(fileName); + findTransitiveDependencies(fileName, includedFiles); - includedFiles.addAll((transitiveIncludes)); includedFiles.addAll(getResultXsdSchemas().filter(schema -> schema.getChildrenIncludes().anyMatch(xsdInclude -> xsdInclude.getSchemaLocation().equals(fileName))).map(XsdSchema::getFilePath).distinct().collect(Collectors.toList())); - includedFiles = includedFiles.stream().distinct().collect(Collectors.toList()); List includedElements = new ArrayList<>(parseElements.get(fileName)); @@ -446,8 +427,6 @@ private void resolveInnerRefs() { unsolvedReferenceList = unsolvedReferenceList.stream().filter(u -> parserUnsolvedElementsMap.stream().noneMatch(u1 -> u == u1.getUnsolvedReference())).collect(Collectors.toList()); for (UnsolvedReference unsolvedReference : unsolvedReferenceList) { - doneSomething = true; - replaceUnsolvedReference(concreteElementsMap, unsolvedReference, fileName); } @@ -460,6 +439,8 @@ private void resolveInnerRefs() { if (currentUnsolvedReferenceListSize == startingUnsolvedReferenceListSize) { solveMore = false; + } else { + doneSomething = true; } startingUnsolvedReferenceListSize = currentUnsolvedReferenceListSize; @@ -481,6 +462,29 @@ private void resolveInnerRefs() { } } + private void findTransitiveDependencies(String fileName, Set dependencies) { + List includedFiles = + parseElements.get(fileName) + .stream() + .filter(referenceBase -> referenceBase instanceof ConcreteElement && referenceBase.getElement() instanceof XsdInclude) + .map(referenceBase -> (((XsdInclude) referenceBase.getElement()).getSchemaLocation())) + .map(this::toRealFileName) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toList()); + for (String includedFile : includedFiles) { + if (dependencies.add(includedFile)) { + findTransitiveDependencies(includedFile, dependencies); + } + } + } + private Optional toRealFileName(String fileName) { + return parseElements.keySet() + .stream() + .filter(fileNameAux -> fileNameAux.endsWith(fileName)) + .findFirst(); + } + /** * Replaces a single {@link UnsolvedReference} object, with the respective {@link NamedConcreteElement} object. If * there isn't a {@link NamedConcreteElement} object to replace the {@link UnsolvedReference} object, information diff --git a/src/test/java/org/xmlet/xsdparser/IssuesTest.java b/src/test/java/org/xmlet/xsdparser/IssuesTest.java index 46eabb1..72d6bb6 100644 --- a/src/test/java/org/xmlet/xsdparser/IssuesTest.java +++ b/src/test/java/org/xmlet/xsdparser/IssuesTest.java @@ -777,6 +777,16 @@ public void testIssue53() { Assert.assertEquals("elem2", ((XsdElement) elem2).getName()); } + @Test() + public void testIssue58(){ + XsdParser parser = new XsdParser(getFilePath("issue_58/a.xsd")); + + XsdElement elem = parser.getResultXsdElements().findFirst().get(); + XsdNamedElements type = elem.getTypeAsXsd(); + Assert.assertNotNull(type); + Assert.assertEquals("Foo", type.getName()); + } + @Test public void testIssue55_ResolveUnion() { XsdParser parser = new XsdParser(getFilePath("issue_25_amzn-base.xsd")); diff --git a/src/test/resources/issue_58/a.xsd b/src/test/resources/issue_58/a.xsd new file mode 100644 index 0000000..ca6acb8 --- /dev/null +++ b/src/test/resources/issue_58/a.xsd @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/test/resources/issue_58/b.xsd b/src/test/resources/issue_58/b.xsd new file mode 100644 index 0000000..9e6df07 --- /dev/null +++ b/src/test/resources/issue_58/b.xsd @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/test/resources/issue_58/c.xsd b/src/test/resources/issue_58/c.xsd new file mode 100644 index 0000000..a43f5f7 --- /dev/null +++ b/src/test/resources/issue_58/c.xsd @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/test/resources/issue_58/d.xsd b/src/test/resources/issue_58/d.xsd new file mode 100644 index 0000000..49c6d48 --- /dev/null +++ b/src/test/resources/issue_58/d.xsd @@ -0,0 +1,4 @@ + + + + \ No newline at end of file