Skip to content

Commit

Permalink
Grammar generator from XML
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#778

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jun 18, 2020
1 parent f8a0ab1 commit 30abe9e
Show file tree
Hide file tree
Showing 34 changed files with 1,646 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import org.eclipse.lemminx.services.extensions.IXMLExtension;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lsp4j.InitializeParams;

/**
Expand All @@ -24,11 +23,6 @@ public class XMLCatalogPlugin implements IXMLExtension {

private XMLCatalogURIResolverExtension uiResolver;

@Override
public void doSave(ISaveContext context) {

}

@Override
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
uiResolver = new XMLCatalogURIResolverExtension(registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.codelens.ICodeLensParticipant;
import org.eclipse.lemminx.services.extensions.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lsp4j.InitializeParams;

/**
Expand All @@ -49,11 +48,6 @@ public DTDPlugin() {
codeLensParticipant = new DTDCodeLensParticipant();
}

@Override
public void doSave(ISaveContext context) {

}

@Override
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
// register DTD content model provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.lemminx.services.extensions.IHoverParticipant;
import org.eclipse.lemminx.services.extensions.IXMLExtension;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lsp4j.InitializeParams;

/**
Expand Down Expand Up @@ -51,8 +50,4 @@ public void stop(XMLExtensionsRegistry registry) {
registry.unregisterHoverParticipant(hoverParticipant);
}

@Override
public void doSave(ISaveContext context) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.eclipse.lemminx.extensions.general.completion.FilePathCompletionParticipant;
import org.eclipse.lemminx.services.extensions.IXMLExtension;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lsp4j.InitializeParams;

/**
Expand All @@ -38,10 +37,4 @@ public void stop(XMLExtensionsRegistry registry) {
registry.unregisterCompletionParticipant(completionParticipant);
}

@Override
public void doSave(ISaveContext context) {

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*******************************************************************************
* 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.List;

import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lemminx.services.IXMLFullFormatter;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lemminx.utils.StringUtils;
import org.eclipse.lemminx.utils.XMLBuilder;
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, etc) from a given XML source.
*
* <p>
* The process is to build a generic {@link Grammar} 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) {
// Generate the grammar from the XML source document
String newText = doGenerate(document, sharedSettings, generatorSettings);
if (formatter == null) {
return newText;
}
// Format the generated grammar.
return formatter.formatFull(newText, "grammar." + getFileExtension(), sharedSettings);
}

/**
* Returns the grammar file extension (ex : xsd, dtd) to generate.
*
* @return the grammar file extension (ex : xsd, dtd) to generate.
*/
protected abstract String getFileExtension();

private String doGenerate(Document document, SharedSettings sharedSettings, T generatorSettings) {
// Create the generic grammar information from the XML source document.
Grammar grammar = createGrammar(document, isFlat());
XMLBuilder builder = new XMLBuilder(sharedSettings, "", "");
// Generate the grammar content from the grammar information.
generate(grammar, generatorSettings, builder);
return builder.toString();
}

/**
* Returns true if element declaration must be stored as flat mode and false
* otherwise.
*
* <ul>
*
* <li>flat=true: helpful for DTD which declares <!ELEMENT without
* hierarchy.</li>
* <li>flat=false: helpful for XSD which declares xs:element with
* hierarchy.</li>
* </ul>
*
* @return true if element declaration must be stored as flat mode and false
* otherwise.
*/
protected boolean isFlat() {
return false;
}

/**
* 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(Grammar grammar, T grammarSettings, XMLBuilder out);

/**
* Create the grammar from the given XML document.
*
* @param document the XML source document.
* @param flat flat mode
*
* @return the grammar from the given XML document.
*/
private static Grammar createGrammar(Document document, boolean flat) {
Grammar grammar = new Grammar();
// 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, grammar, flat);
return grammar;
}

private static void fillElements(Node node, Grammar grammar, ContainerDeclaration parent, boolean flat) {
if (!node.hasChildNodes()) {
return;
}
NodeList children = node.getChildNodes();
// Parent
if (parent instanceof ElementDeclaration) {
List<String> tags = new ArrayList<>();
ElementDeclaration parentDecl = (ElementDeclaration) parent;
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) child;
String localName = element.getLocalName();
if (!StringUtils.isEmpty(localName)) {
tags.add(localName);
}
}
}
parentDecl.addChildHierarchy(tags);
}

for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) child;
ElementDeclaration elementDecl = getElementDecl(grammar, parent, flat, element);
// Update count occurrences
elementDecl.incrementOccurrences();
// Update has text
if (!elementDecl.hasCharacterContent()) {
elementDecl.setHasCharacterContent(hasCharacterContent(element));
}
// Collect attributes
NamedNodeMap attributes = element.getAttributes();
if (attributes != null) {
for (int j = 0; j < attributes.getLength(); j++) {
Attr attr = (Attr) attributes.item(j);
String name = attr.getName();
// Ignore xmlns attribute
if (!StringUtils.isEmpty(name) && !DOMAttr.isXmlns(name)) {
elementDecl.getAttribute(attr.getName());
}
}
}
fillElements(element, grammar, elementDecl, flat);
}
}
}

private static ElementDeclaration getElementDecl(Grammar grammar, ContainerDeclaration container, boolean flat,
Element element) {
String name = element.getLocalName();
if (flat) {
ElementDeclaration elementDecl = grammar.getElement(name);
container.addElement(elementDecl);
return elementDecl;
}
return container.getElement(name);
}

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;
}
}
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 declaration.
*
*/
public class AttributeDeclaration {

private final String name;

public AttributeDeclaration(String name) {
this.name = name;
}

/**
* Returns the attribute name.
*
* @return the attribute name.
*/
public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.eclipse.lemminx.extensions.generators;

public class Cardinality {

private long min;

private long max;

public void setMin(long min) {
this.min = min;
}

public void setMax(long max) {
this.max = max;
}

public long getMin() {
return min;
}

public long getMax() {
return max;
}

public void updateMin(long min) {
setMin(Math.min(min, getMin()));
}

public void updateMax(long max) {
setMax(Math.max(max, getMax()));
}

@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("[");
result.append(getMin());
result.append("-");
result.append(getMax());
result.append("]");
return result.toString();
}
}
Loading

0 comments on commit 30abe9e

Please sign in to comment.