Skip to content

Commit

Permalink
Fix completion & hover based on XSD with
Browse files Browse the repository at this point in the history
elementFormDefault="unqualified"

See redhat-developer/vscode-xml#311

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Aug 25, 2020
1 parent 1e3f131 commit 32b05d8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public ContentModelManager(URIResolverExtensionManager resolverManager) {
/**
* Returns the owner document of the declared element which matches the given
* XML element and null otherwise.
*
*
* @param element the XML element
*
*
* @return the owner document of the declared element which matches the given
* XML element and null otherwise.
*/
Expand All @@ -81,9 +81,9 @@ public Collection<CMDocument> findCMDocument(DOMElement element) {
/**
* Returns the owner document of the declared element which matches the given
* XML element and null otherwise.
*
*
* @param element the XML element
*
*
* @return the owner document of the declared element which matches the given
* XML element and null otherwise.
*/
Expand All @@ -97,12 +97,30 @@ public Collection<CMDocument> findCMDocument(DOMDocument xmlDocument, String nam

/**
* Returns the declared documents which match the given DOM document.
*
*
* @param xmlDocument the DOM document.
* @param namespaceURI the namespace URI
* @return the declared documents which match the given DOM document.
*/
public Collection<CMDocument> findCMDocument(DOMDocument xmlDocument, String namespaceURI, boolean withInternal) {
if (namespaceURI == null) {
// This case comes from when an element has no namespace and XML Schema defines
// elementFormDefault="unqualified"
// --> we use the namespace from the DOM document

// ex: XSD:
// <xs:schema targetNamespace="urn:reports/itops"
// elementFormDefault="unqualified"

// ex : XML
// <i:ITOpsReport xmlns:i="urn:reports/itops"
// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
// xsi:schemaLocation="urn:reports/itops ../schema/reports/itops.xsd">
// <templates> --> here template has a null namespace and the DOM document has
// "urn:reports/itops" namespace
// --> we use urn:reports/itops as namespace to get the DOM Document
namespaceURI = xmlDocument.getNamespaceURI();
}
Collection<CMDocument> documents = new ArrayList<>();
for (ContentModelProvider modelProvider : modelProviders) {
// internal grammar
Expand Down Expand Up @@ -142,7 +160,7 @@ public Collection<CMDocument> findCMDocument(DOMDocument xmlDocument, String nam
/**
* Returns true if the given document is linked to the given grammar URI (XML
* Schema, DTD) and false otherwise.
*
*
* @param document the DOM document
* @param grammarURI the grammar URI
* @return true if the given document is linked to the given grammar URI (XML
Expand Down Expand Up @@ -172,7 +190,7 @@ public boolean dependsOnGrammar(DOMDocument document, String grammarURI) {
/**
* Returns the content model document loaded by the given uri and null
* otherwise.
*
*
* @param publicId the public identifier.
* @param systemId the expanded system identifier.
* @param modelProvider
Expand Down Expand Up @@ -248,9 +266,9 @@ private void cache(String key, CMDocument cmDocument) {

/**
* Returns the model provider by the given uri and null otherwise.
*
*
* @param uri the grammar URI
*
*
* @return the model provider by the given uri and null otherwise.
*/
public ContentModelProvider getModelProviderByURI(String uri) {
Expand All @@ -264,7 +282,7 @@ public ContentModelProvider getModelProviderByURI(String uri) {

/**
* Set up XML catalogs.
*
*
* @param catalogs list of XML catalog files.
* @return true if catalogs changed and false otherwise
*/
Expand All @@ -281,7 +299,7 @@ public void refreshCatalogs() {

/**
* Set file associations.
*
*
* @param fileAssociations
* @return true if file associations changed and false otherwise
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,19 @@ public void generateOnlyStartElementOnText() throws BadLocationException {
c("employee", te(3, 0, 3, 0, "<employee>$1$0"), "employee"));
}

@Test
public void completionWithUnqualifiedElementFormDefault() throws BadLocationException {
String xml = "<f:foo xmlns:f=\"http://foo\"\r\n" + //
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
" xsi:schemaLocation=\"\r\n" + //
" http://foo xsd/foo-unqualified.xsd\">\r\n" + //
" <bar>\r\n" + //
" |";
XMLAssert.testCompletionFor(xml, null, "src/test/resources/foo-unqualified.xml", //
null, //
c("item", te(5, 2, 5, 2, "<item></item>"), "item", null, null));
}

@Test
public void generateOnlyStartElementOnElement() throws BadLocationException {
// </employee> already exists, completion must generate only <employee>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<f:foo xmlns:f="http://foo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://foo ../xsd/foo-unqualified.xsd">
<bar>
<item></item>
</bar>
</f:foo>
19 changes: 19 additions & 0 deletions org.eclipse.lemminx/src/test/resources/xsd/foo-unqualified.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="unqualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://foo">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="bar">
<xs:complexType>
<xs:sequence>
<xs:element name="item">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

0 comments on commit 32b05d8

Please sign in to comment.