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.
Fixes eclipse-lemminx#778 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
427367b
commit 5288c13
Showing
18 changed files
with
959 additions
and
14 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...src/main/java/org/eclipse/lemminx/extensions/generators/AbstractXML2GrammarGenerator.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,124 @@ | ||
/******************************************************************************* | ||
* 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.generators; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lemminx.commons.TextDocument; | ||
import org.eclipse.lemminx.dom.DOMAttr; | ||
import org.eclipse.lemminx.services.IXMLFullFormatter; | ||
import org.eclipse.lemminx.settings.SharedSettings; | ||
import org.eclipse.lemminx.utils.XMLBuilder; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4j.TextEdit; | ||
import org.w3c.dom.Attr; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.NamedNodeMap; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
|
||
/** | ||
* Abstract class to generate a grammar (XSD, DTD, etcc) from a given XML | ||
* source. | ||
* | ||
* <p> | ||
* The process is to build a generic {@link GrammarInfo} instance from the XML | ||
* source and each implementation uses this grammar information to generate the | ||
* proper grammar. | ||
* </p> | ||
* | ||
* @param <T> the grammar generator settings. | ||
*/ | ||
public abstract class AbstractXML2GrammarGenerator<T extends FileContentGeneratorSettings> | ||
implements IFileContentGenerator<Document, T> { | ||
|
||
@Override | ||
public String generate(Document document, SharedSettings sharedSettings, T generatorSettings, | ||
IXMLFullFormatter formatter) { | ||
String newText = doGenerate(document, sharedSettings, generatorSettings); | ||
if (formatter == null) { | ||
return newText; | ||
} | ||
return formatter.formatFull(new TextDocument(newText, "grammar." + getFileExtension()), | ||
sharedSettings); | ||
} | ||
|
||
protected abstract String getFileExtension(); | ||
|
||
private String doGenerate(Document document, SharedSettings sharedSettings, T generatorSettings) { | ||
// Create the generic grammar information from the XML source document. | ||
GrammarInfo grammar = createGrammar(document); | ||
XMLBuilder builder = new XMLBuilder(sharedSettings, "", ""); | ||
// Generate the grammar content from the grammar information. | ||
generate(grammar, generatorSettings, builder); | ||
return builder.toString(); | ||
} | ||
|
||
/** | ||
* Generate the grammar content from the given grammr information into the given | ||
* builder. | ||
* | ||
* @param grammar the grammar information. | ||
* @param grammarSettings the grammar settings | ||
* @param out the XML builder to update. | ||
*/ | ||
protected abstract void generate(GrammarInfo grammar, T grammarSettings, XMLBuilder out); | ||
|
||
private GrammarInfo createGrammar(Document document) { | ||
GrammarInfo grammar = new GrammarInfo(); | ||
// Update default namespace | ||
String defaultNamespace = null; | ||
Element documentElement = document.getDocumentElement(); | ||
if (documentElement != null) { | ||
defaultNamespace = document.getDocumentElement().getAttribute(DOMAttr.XMLNS_ATTR); | ||
} | ||
grammar.setDefaultNamespace(defaultNamespace); | ||
// Update elements information | ||
fillElements(document, grammar); | ||
return grammar; | ||
} | ||
|
||
private static void fillElements(Node node, ChildrenInfo grammar) { | ||
NodeList children = node.getChildNodes(); | ||
for (int i = 0; i < children.getLength(); i++) { | ||
Node child = children.item(i); | ||
if (child.getNodeType() == Node.ELEMENT_NODE) { | ||
Element element = (Element) child; | ||
ElementInfo grammarElt = grammar.getElement(element.getLocalName()); | ||
// Has text | ||
grammarElt.setHasCharacterContent(hasCharacterContent(element)); | ||
// Attributes | ||
NamedNodeMap attributes = element.getAttributes(); | ||
if (attributes != null) { | ||
for (int j = 0; j < attributes.getLength(); j++) { | ||
Attr attr = (Attr) attributes.item(i); | ||
grammarElt.getAttribute(attr.getName()); | ||
} | ||
} | ||
fillElements(element, grammarElt); | ||
} | ||
} | ||
} | ||
|
||
private static boolean hasCharacterContent(Element element) { | ||
NodeList children = element.getChildNodes(); | ||
for (int i = 0; i < children.getLength(); i++) { | ||
Node child = children.item(i); | ||
if (child.getNodeType() == Node.TEXT_NODE) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...clipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/AttributeInfo.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,34 @@ | ||
/******************************************************************************* | ||
* 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.generators; | ||
|
||
/** | ||
* Attribute information. | ||
* | ||
*/ | ||
public class AttributeInfo { | ||
|
||
private final String name; | ||
|
||
public AttributeInfo(String name) { | ||
this.name = name; | ||
} | ||
|
||
/** | ||
* Returns the attribute name. | ||
* | ||
* @return the attribute name. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ChildrenInfo.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,78 @@ | ||
/******************************************************************************* | ||
* 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.generators; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Children information. | ||
* | ||
*/ | ||
public class ChildrenInfo { | ||
|
||
private final Map<String, ElementInfo> children; | ||
|
||
public ChildrenInfo() { | ||
this.children = new LinkedHashMap<>(); | ||
} | ||
|
||
/** | ||
* Returns the element information for the given name and create it if not | ||
* found. | ||
* | ||
* @param name the element name | ||
* @return the element information for the given name and create it if not | ||
* found. | ||
*/ | ||
public ElementInfo getElement(String name) { | ||
ElementInfo element = children.get(name); | ||
if (element != null) { | ||
return element; | ||
} | ||
element = new ElementInfo(name); | ||
children.put(name, element); | ||
return element; | ||
} | ||
|
||
/** | ||
* Returns the elements information of the node. | ||
* | ||
* @return the elements information of the node. | ||
*/ | ||
public Collection<ElementInfo> getElements() { | ||
return children.values(); | ||
} | ||
|
||
/** | ||
* Returns the all elements information from the node and children. | ||
* | ||
* @return the all elements information from the node and children. | ||
*/ | ||
public Collection<ElementInfo> getAllElements() { | ||
List<ElementInfo> all = new ArrayList<>(); | ||
fillWithAllElements(all); | ||
return all; | ||
} | ||
|
||
void fillWithAllElements(List<ElementInfo> allElements) { | ||
for (ElementInfo element : getElements()) { | ||
if (!allElements.contains(element)) { | ||
allElements.add(element); | ||
element.fillWithAllElements(allElements); | ||
} | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ElementInfo.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,78 @@ | ||
/******************************************************************************* | ||
* 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.generators; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Element information. | ||
* | ||
*/ | ||
public class ElementInfo extends ChildrenInfo { | ||
|
||
private final String name; | ||
|
||
private final Map<String, AttributeInfo> attributes; | ||
|
||
private boolean hasCharacterContent; | ||
|
||
public ElementInfo(String name) { | ||
this.name = name; | ||
this.attributes = new HashMap<>(); | ||
} | ||
|
||
/** | ||
* Returns the element name. | ||
* | ||
* @return the element name. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Returns the attribute information for the given name and create it if not | ||
* found. | ||
* | ||
* @param name the attribute name | ||
* @return the attribute information for the given name and create it if not | ||
* found. | ||
*/ | ||
public AttributeInfo getAttribute(String name) { | ||
AttributeInfo attribute = attributes.get(name); | ||
if (attribute != null) { | ||
return attribute; | ||
} | ||
attribute = new AttributeInfo(name); | ||
attributes.put(name, attribute); | ||
return attribute; | ||
} | ||
|
||
public Collection<AttributeInfo> getAttributes() { | ||
return attributes.values(); | ||
} | ||
|
||
/** | ||
* Returns true if element has character content and false otherwise. | ||
* | ||
* @return true if element has character content and false otherwise. | ||
*/ | ||
public boolean hasCharacterContent() { | ||
return hasCharacterContent; | ||
} | ||
|
||
void setHasCharacterContent(boolean hasCharacterContent) { | ||
this.hasCharacterContent = hasCharacterContent; | ||
} | ||
} |
Oops, something went wrong.