forked from eclipse-lemminx/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix XML catalog with XSD files that have <xs:include />
Fixes eclipse-lemminx#914 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
6c048fd
commit 307f0f0
Showing
7 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...n/java/org/eclipse/lemminx/extensions/contentmodel/uriresolver/LSPXMLCatalogResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
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; | ||
|
||
/** | ||
* Extension of Xerces XML catalog resolver to support include of XSD. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
class LSPXMLCatalogResolver extends XMLCatalogResolver { | ||
|
||
public LSPXMLCatalogResolver(String[] catalogs) { | ||
super(catalogs); | ||
} | ||
|
||
@Override | ||
public String resolveIdentifier(XMLResourceIdentifier resourceIdentifier) throws IOException, XNIException { | ||
// The default Xerces XML catalog implementation promote the namespace | ||
// information froml the resource identifier | ||
// See | ||
// https://github.com/apache/xerces2-j/blob/cf0c517a41b31b0242b96ab1af9627a3ab07fcd2/src/org/apache/xerces/util/XMLCatalogResolver.java#L418 | ||
// For XSD include the resolve identifier is not computed correctly. | ||
if (isXSDIncludeWithNamespace(resourceIdentifier)) { | ||
return resourceIdentifier.getExpandedSystemId(); | ||
} | ||
return super.resolveIdentifier(resourceIdentifier); | ||
} | ||
|
||
/** | ||
* Returns true if it's an XSD include/import with namespace and false | ||
* otherwise. | ||
* | ||
* @param resourceIdentifier the resource indetifier. | ||
* | ||
* @return true if it's an XSD include/import with namespace and false | ||
* otherwise. | ||
*/ | ||
private boolean isXSDIncludeWithNamespace(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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
org.eclipse.lemminx/src/test/resources/catalogs/include/catalog-include.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
19 changes: 19 additions & 0 deletions
19
org.eclipse.lemminx/src/test/resources/catalogs/include/test-include.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
org.eclipse.lemminx/src/test/resources/catalogs/include/test.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
4 changes: 4 additions & 0 deletions
4
org.eclipse.lemminx/src/test/resources/catalogs/include/test.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |