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.
Support for document link DTD entity SYSTEM
Fixes eclipse-lemminx#1165 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
ba438fe
commit 4d85bbd
Showing
6 changed files
with
180 additions
and
18 deletions.
There are no files selected for viewing
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
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
73 changes: 73 additions & 0 deletions
73
...main/java/org/eclipse/lemminx/extensions/dtd/participants/DTDDocumentLinkParticipant.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,73 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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.dtd.participants; | ||
|
||
import static org.eclipse.lemminx.utils.XMLPositionUtility.createDocumentLink; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lemminx.commons.BadLocationException; | ||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.dom.DOMDocumentType; | ||
import org.eclipse.lemminx.dom.DOMRange; | ||
import org.eclipse.lemminx.dom.DTDEntityDecl; | ||
import org.eclipse.lemminx.services.extensions.IDocumentLinkParticipant; | ||
import org.eclipse.lemminx.uriresolver.URIResolverExtensionManager; | ||
import org.eclipse.lsp4j.DocumentLink; | ||
import org.w3c.dom.NamedNodeMap; | ||
|
||
/** | ||
* Document link for DTD entities system inside a DTD: | ||
* | ||
* <code> | ||
* <!ENTITY % xinclude SYSTEM "http://www.docbook.org/xml/4.4/xinclude.mod"> | ||
* </code> | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class DTDDocumentLinkParticipant implements IDocumentLinkParticipant { | ||
|
||
private final URIResolverExtensionManager resolverManager; | ||
|
||
public DTDDocumentLinkParticipant(URIResolverExtensionManager resolverManager) { | ||
this.resolverManager = resolverManager; | ||
} | ||
|
||
@Override | ||
public void findDocumentLinks(DOMDocument document, List<DocumentLink> links) { | ||
// Document link for DTD | ||
DOMDocumentType docType = document.getDoctype(); | ||
if (docType != null) { | ||
// Loop for each entities declaration | ||
NamedNodeMap entities = docType.getEntities(); | ||
for (int i = 0; i < entities.getLength(); i++) { | ||
DTDEntityDecl entity = (DTDEntityDecl) entities.item(i); | ||
String location = resolverManager.resolve(document.getDocumentURI(), entity.getPublicId(), | ||
entity.getSystemId()); | ||
if (location != null) { | ||
try { | ||
// The entity declares a SYSTEM like | ||
// <!ENTITY % xinclude SYSTEM "http://www.docbook.org/xml/4.4/xinclude.mod"> | ||
DOMRange systemIdRange = entity.getSystemIdNode(); | ||
if (systemIdRange != null) { | ||
links.add(createDocumentLink(systemIdRange, location, true)); | ||
} | ||
} catch (BadLocationException e) { | ||
// Do nothing | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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
33 changes: 33 additions & 0 deletions
33
...eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/dtd/DTDDocumentLinkTest.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,33 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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.dtd; | ||
|
||
import static org.eclipse.lemminx.XMLAssert.dl; | ||
import static org.eclipse.lemminx.XMLAssert.r; | ||
|
||
import org.eclipse.lemminx.XMLAssert; | ||
import org.eclipse.lemminx.commons.BadLocationException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* DTD entities document links tests inside a DTD. | ||
* | ||
*/ | ||
public class DTDDocumentLinkTest { | ||
|
||
@Test | ||
public void entity() throws BadLocationException { | ||
String xml = "<!ENTITY % document SYSTEM \"../document.ent\">"; | ||
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/xml/base.dtd", | ||
dl(r(0, 28, 0, 43), "src/test/resources/document.ent")); | ||
} | ||
} |