Skip to content

Commit

Permalink
Checking base complex type for annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Bowersox <[email protected]>
  • Loading branch information
mattbsox committed Aug 3, 2022
1 parent f1d7ca2 commit 20e0fa9
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.xerces.impl.xs.models.CMBuilder;
import org.apache.xerces.impl.xs.models.CMNodeFactory;
import org.apache.xerces.impl.xs.models.XSCMValidator;
import org.apache.xerces.impl.xs.util.XSObjectListImpl;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xs.XSAttributeUse;
import org.apache.xerces.xs.XSComplexTypeDefinition;
Expand Down Expand Up @@ -370,8 +371,32 @@ private XSObjectList getElementAnnotations() {
if (typeDefinition == null) {
return null;
}
return getElementAnnotations(typeDefinition);
}

private XSObjectList getElementAnnotations(XSTypeDefinition typeDefinition) {
if (typeDefinition.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
return ((XSComplexTypeDecl) typeDefinition).getAnnotations();
XSObjectList annotation = ((XSComplexTypeDecl) typeDefinition).getAnnotations();
// Get annotations for types derived with extension:base
if (((XSComplexTypeDecl) typeDefinition).getDerivationMethod() == XSConstants.DERIVATION_EXTENSION) {
XSObjectListImpl allAnnotations = new XSObjectListImpl();
XSTypeDefinition baseType = ((XSComplexTypeDecl) typeDefinition).getBaseType();
//Get annotations for current type
for (Object xsObject : annotation.toArray()) {
if (((XSObject) xsObject) != null) {
allAnnotations.addXSObject((XSObject) xsObject);
}
}
//Get annotations for base type
for (Object xsObject : getElementAnnotations(baseType).toArray()) {
if (((XSObject) xsObject) != null) {
allAnnotations.addXSObject((XSObject) xsObject);
}
}
return allAnnotations;
} else {
return annotation;
}
} else if (typeDefinition.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {
return ((XSSimpleTypeDecl) typeDefinition).getAnnotations();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*******************************************************************************
* Copyright (c) 2022 IBM Corporation 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.contentmodel;

import static org.eclipse.lemminx.XMLAssert.r;

import java.util.Arrays;

import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.util.URI.MalformedURIException;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.services.XMLLanguageService;
import org.eclipse.lemminx.settings.SchemaDocumentationType;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lsp4j.HoverCapabilities;
import org.eclipse.lsp4j.MarkupKind;
import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.Test;

public class XMLSchemaHoverExtendedComplexTypeTest {

@Test
public void testHoverComplexTypeDocumentation() throws BadLocationException, MalformedURIException {
String schemaURI = getXMLSchemaFileURI("extendedComplexType.xsd");

String xml = "<t|estType\n" +
" xmlns=\"http://extendedComplexType\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://extendedComplexType xsd/extendedComplexType.xsd\">\n" +
"</testType>\n";

String expected = "base type documentation value" + //
System.lineSeparator() + //
System.lineSeparator() + //
"Source: [extendedComplexType.xsd](" + schemaURI + ")";

assertHover(xml, expected,r(0, 1, 0, 9));
}

@Test
public void testHoverExtendedComplexTypeDocumentation() throws BadLocationException, MalformedURIException {
String schemaURI = getXMLSchemaFileURI("extendedComplexType.xsd");

String xml = "<e|xtendedTestType\n" +
" xmlns=\"http://extendedComplexType\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://extendedComplexType xsd/extendedComplexType.xsd\">\n" +
"</extendedTestType>\n";

String expected = "extending type documentation value" + //
System.lineSeparator() + //
System.lineSeparator() + //
"base type documentation value" + //
System.lineSeparator() + //
System.lineSeparator() + //
"Source: [extendedComplexType.xsd](" + schemaURI + ")";

assertHover(xml, expected, r(0, 1, 0, 17));
}

private SharedSettings createSharedSettings(SchemaDocumentationType docSource, boolean markdownSupported) {
SharedSettings settings = new SharedSettings();
if (markdownSupported) {
HoverCapabilities capabilities = new HoverCapabilities(Arrays.asList(MarkupKind.MARKDOWN), false);
settings.getHoverSettings().setCapabilities(capabilities);
}
settings.getPreferences()
.setShowSchemaDocumentationType(docSource);
return settings;
}

private static String getXMLSchemaFileURI(String schemaURI) throws MalformedURIException {
return XMLEntityManager.expandSystemId("xsd/" + schemaURI, "src/test/resources/test.xml", true).replace("///",
"/");
}

private void assertHover(String xml, String expected, Range range) throws BadLocationException, MalformedURIException {
XMLAssert.assertHover(new XMLLanguageService(), xml, null, "src/test/resources/extendedComplexType.xml", expected, range, //
createSharedSettings(SchemaDocumentationType.documentation, true));
}
}
21 changes: 21 additions & 0 deletions org.eclipse.lemminx/src/test/resources/xsd/extendedComplexType.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:complexType name='baseType'>
<xs:annotation>
<xs:documentation>base type documentation value</xs:documentation>
<xs:appinfo>base type appinfo value</xs:appinfo>
</xs:annotation>
</xs:complexType>
<xs:complexType name="extendingType">
<xs:complexContent>
<xs:extension base="baseType">
<xs:annotation>
<xs:documentation>extending type documentation value</xs:documentation>
<xs:appinfo>extending type appinfo value</xs:appinfo>
</xs:annotation>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name='testType' type='baseType'/>
<xs:element name='extendedTestType' type='extendingType'/>
</xs:schema>

0 comments on commit 20e0fa9

Please sign in to comment.