-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[xml-model] XML Completion based on DTD/XML Schema by using xml-model
Fixes #698 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
4fe9cfb
commit 5e1dfe8
Showing
31 changed files
with
881 additions
and
336 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
86 changes: 86 additions & 0 deletions
86
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/dom/XMLModel.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,86 @@ | ||
/******************************************************************************* | ||
* 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.dom; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.lemminx.extensions.xerces.xmlmodel.XMLModelDeclaration; | ||
import org.w3c.dom.ProcessingInstruction; | ||
|
||
/** | ||
* XML model processing instruction. | ||
* | ||
* <pre> | ||
* <?xml-model href="http://www.docbook.org/xml/5.0/xsd/docbook.xsd"?> | ||
* </pre> | ||
* | ||
* | ||
* @see https://www.w3.org/TR/xml-model/ | ||
*/ | ||
public class XMLModel { | ||
|
||
private static final String XML_MODEL_PI = "xml-model"; | ||
|
||
private final DOMProcessingInstruction processingInstruction; | ||
private XMLModelDeclaration declaration; | ||
|
||
public XMLModel(DOMProcessingInstruction processingInstruction) { | ||
this.processingInstruction = processingInstruction; | ||
} | ||
|
||
/** | ||
* Returns the location of the referenced schema | ||
* | ||
* @return the location of the referenced schema | ||
*/ | ||
public String getHref() { | ||
String data = processingInstruction.getData(); | ||
if (data == null) { | ||
return null; | ||
} | ||
if (declaration == null) { | ||
declaration = XMLModelDeclaration.parse(data.toCharArray(), 0, data.length()); | ||
} | ||
return declaration.getHref(); | ||
} | ||
|
||
/** | ||
* Returns the declared xml-model list. | ||
* | ||
* @param document the DOM document. | ||
* | ||
* @return the declared xml-model list. | ||
*/ | ||
static List<XMLModel> createXMLModels(DOMDocument document) { | ||
List<DOMNode> children = document.getChildren(); | ||
if (children != null && !children.isEmpty()) { | ||
return children.stream().filter(XMLModel::isXMLModel) | ||
.map(node -> new XMLModel((DOMProcessingInstruction) node)).collect(Collectors.toList()); | ||
} | ||
return Collections.emptyList(); | ||
} | ||
|
||
/** | ||
* Returns true if the given node is a xml-model processing instruction and | ||
* false otherwise. | ||
* | ||
* @param node the DOM node. | ||
* @return true if the given node is a xml-model processing instruction and | ||
* false otherwise. | ||
*/ | ||
public static boolean isXMLModel(DOMNode node) { | ||
return node.isProcessingInstruction() && XML_MODEL_PI.equals(((ProcessingInstruction) node).getTarget()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.