Skip to content

Commit

Permalink
Fix XML catalog with XSD files that have <xs:include />
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#914

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Oct 23, 2020
1 parent 6c048fd commit e08f0b9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.eclipse.lemminx.extensions.contentmodel.uriresolver;

import java.io.IOException;

import org.apache.xerces.impl.xs.XSDDescription;
import org.apache.xerces.util.XMLCatalogResolver;
import org.apache.xerces.xni.XMLResourceIdentifier;
import org.apache.xerces.xni.XNIException;

class LSPXMLCatalogResolver extends XMLCatalogResolver {

public LSPXMLCatalogResolver(String[] catalogs) {
super(catalogs);
}

@Override
public String resolveIdentifier(XMLResourceIdentifier resourceIdentifier) throws IOException, XNIException {
if (isXSDInclude(resourceIdentifier)) {
return resourceIdentifier.getExpandedSystemId();
}
return super.resolveIdentifier(resourceIdentifier);
}

private boolean isXSDInclude(XMLResourceIdentifier resourceIdentifier) {
if (resourceIdentifier != null && resourceIdentifier instanceof XSDDescription
&& resourceIdentifier.getNamespace() != null) {
XSDDescription description = (XSDDescription) resourceIdentifier;
int contextType = description.getContextType();
return contextType == XSDDescription.CONTEXT_INCLUDE || contextType == XSDDescription.CONTEXT_IMPORT;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public boolean setCatalogs(String[] catalogs) {
}
}
if (xmlCatalogFiles.size() > 0) {
XMLCatalogResolver catalogResolver = new XMLCatalogResolver(xmlCatalogFiles.toArray(new String[0]));
XMLCatalogResolver catalogResolver = new LSPXMLCatalogResolver(xmlCatalogFiles.toArray(new String[0]));
setCatalogResolver(catalogResolver);
} else {
setCatalogResolver(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,15 @@ public void diagnosticRelatedInformationWithSchemaLocationSyntaxProblem() throws
diagnostic);
}

@Test
public void testWithCatalogAndXSDInclude() throws BadLocationException {
String xml = "<document xmlns=\"http://foobar.com/test\">\r\n" + //
" <page></page>\r\n" + //
"</document>";
Diagnostic diagnostic = d(1, 2, 1, 6, XMLSchemaErrorCode.cvc_complex_type_2_4_b);
XMLAssert.testDiagnosticsFor(xml, "src/test/resources/catalogs/include/catalog-include.xml", diagnostic);
}

private static void testDiagnosticsFor(String xml, Diagnostic... expected) {
XMLAssert.testDiagnosticsFor(xml, "src/test/resources/catalogs/catalog.xml", expected);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<uri
name="http://foobar.com/test"
uri="./test.xsd" />
</catalog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://foobar.com/test" xmlns:test="http://foobar.com/test">
<xs:element name="document">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="test:page"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="page" type="test:page-content"/>
<xs:complexType name="page-content">
<xs:sequence>
<xs:element ref="test:title"/>
<xs:element ref="test:content"/>
</xs:sequence>
</xs:complexType>
<xs:element name="title" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:schema>
13 changes: 13 additions & 0 deletions org.eclipse.lemminx/src/test/resources/catalogs/include/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="http://foobar.com/test">
<page>
<title></title><content></content></page>
<page>
<title>Page 1</title>
<content>Content for page 1</content>
</page>
<page>
<title>Page 2</title>
<content>Content for page 2</content>
</page>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://foobar.com/test" xmlns:test="http://foobar.com/test">
<xs:include schemaLocation="test-include.xsd"/>
</xs:schema>

0 comments on commit e08f0b9

Please sign in to comment.