Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #58 #59

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions src/main/java/org/xmlet/xsdparser/core/XsdParserCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> includedFiles =
parseElements.get(fileName)
.stream()
.filter(referenceBase -> referenceBase instanceof ConcreteElement && referenceBase.getElement() instanceof XsdInclude)
.map(referenceBase -> (((XsdInclude) referenceBase.getElement()).getSchemaLocation()))
.collect(Collectors.toList());

Set<String> 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<String> 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<ReferenceBase> includedElements = new ArrayList<>(parseElements.get(fileName));

Expand Down Expand Up @@ -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);
}

Expand All @@ -460,6 +439,8 @@ private void resolveInnerRefs() {

if (currentUnsolvedReferenceListSize == startingUnsolvedReferenceListSize) {
solveMore = false;
} else {
doneSomething = true;
}

startingUnsolvedReferenceListSize = currentUnsolvedReferenceListSize;
Expand All @@ -481,6 +462,29 @@ private void resolveInnerRefs() {
}
}

private void findTransitiveDependencies(String fileName, Set<String> dependencies) {
List<String> 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<String> 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
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/xmlet/xsdparser/IssuesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/issue_58/a.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://mytestns.org/issue58" targetNamespace="http://mytestns.org/issue58">
<xsd:include schemaLocation="b.xsd"/>
<xsd:element name="elem" type="Foo"/>
</xsd:schema>
3 changes: 3 additions & 0 deletions src/test/resources/issue_58/b.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://mytestns.org/issue58" targetNamespace="http://mytestns.org/issue58">
<xsd:include schemaLocation="c.xsd"/>
</xsd:schema>
3 changes: 3 additions & 0 deletions src/test/resources/issue_58/c.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://mytestns.org/issue58" targetNamespace="http://mytestns.org/issue58">
<xsd:include schemaLocation="d.xsd"/>
</xsd:schema>
4 changes: 4 additions & 0 deletions src/test/resources/issue_58/d.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://mytestns.org/issue58" targetNamespace="http://mytestns.org/issue58">
<xsd:complexType name="Foo">
</xsd:complexType>
</xsd:schema>