diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/AbstractXML2GrammarGenerator.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/AbstractXML2GrammarGenerator.java
new file mode 100644
index 0000000000..dbf2c1f841
--- /dev/null
+++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/AbstractXML2GrammarGenerator.java
@@ -0,0 +1,164 @@
+/*******************************************************************************
+* 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.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.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, etcc) from a given XML
+ * source.
+ *
+ *
+ * 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.
+ *
+ *
+ * @param the grammar generator settings.
+ */
+public abstract class AbstractXML2GrammarGenerator
+ implements IFileContentGenerator {
+
+ @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.
+ 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();
+ }
+
+ 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);
+
+ private 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 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);
+
+ // ((ElementDeclaration) container).addTagName(element.getTagName());
+ }
+
+ 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));
+ }
+ // Update attributes
+ NamedNodeMap attributes = element.getAttributes();
+ if (attributes != null) {
+ for (int j = 0; j < attributes.getLength(); j++) {
+ Attr attr = (Attr) attributes.item(i);
+ 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(element.getLocalName());
+ }
+
+ 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;
+ }
+}
diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/AttributeDeclaration.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/AttributeDeclaration.java
new file mode 100644
index 0000000000..502ccd8eb6
--- /dev/null
+++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/AttributeDeclaration.java
@@ -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;
+ }
+}
diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/Cardinality.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/Cardinality.java
new file mode 100644
index 0000000000..18f1d5ecb4
--- /dev/null
+++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/Cardinality.java
@@ -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();
+ }
+}
\ No newline at end of file
diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ChildrenProperties.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ChildrenProperties.java
new file mode 100644
index 0000000000..d5717c4795
--- /dev/null
+++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ChildrenProperties.java
@@ -0,0 +1,140 @@
+/*******************************************************************************
+* 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.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * Children properties of an element declaration:
+ *
+ *
+ *
sequenced if all children have the same order.
+ *
cardinalities for each child.
+ *
+ *
+ */
+public class ChildrenProperties {
+
+ /**
+ * Current ordered tags.
+ */
+ private List orderedTags;
+
+ /**
+ * True if tags for each child hierarchy have the same order and false
+ * otherwise.
+ */
+ private boolean sequenced;
+
+ /**
+ * Map which stores cardinality per tag.
+ */
+ private Map cardinalities;
+
+ public ChildrenProperties() {
+ this.cardinalities = new LinkedHashMap<>();
+ this.sequenced = true;
+ }
+
+ public void addChildHierarchy(List tags) {
+ boolean first = orderedTags == null;
+
+ // Group by tags (by keeping the order) to count it
+ // For the given tags "a", "b", "c", "b", we compute this map:
+ // - "a" : 1
+ // - "b" : 2
+ // - "c": 1
+ Map tagsCount = tags.stream().collect( //
+ Collectors.groupingBy( //
+ Function.identity(), LinkedHashMap::new, Collectors.counting() //
+ ));
+ // Get ordered distinct tags (ex : "a", "b", "c")
+ List newOrderedTags = new LinkedList<>(tagsCount.keySet());
+
+ // Update cardinality for existing tags
+ for (Map.Entry entry : cardinalities.entrySet()) {
+ String tag = entry.getKey();
+ Cardinality cardinality = entry.getValue();
+ // Get the count of the existing tag from the new tags by removing it
+ Long count = tagsCount.remove(tag);
+ if (count != null) {
+ cardinality.updateMin(count);
+ cardinality.updateMax(count);
+ } else {
+ // the current tag doesn't exist in the existings tags, set min to 0
+ cardinality.setMin(0);
+ }
+ }
+
+ // Update cardinality for the new tags
+ for (Map.Entry entry : tagsCount.entrySet()) {
+ String tag = entry.getKey();
+ Cardinality cardinality = cardinalities.get(tag);
+ if (cardinality == null) {
+ cardinality = new Cardinality();
+ cardinalities.put(tag, cardinality);
+ }
+ Long count = tagsCount.get(tag);
+ if (first) {
+ cardinality.setMin(count);
+ cardinality.setMax(count);
+ } else {
+ cardinality.updateMin(count);
+ cardinality.updateMax(count);
+ }
+ }
+
+ // Update ordered tags by removing tag which have 0 as min cardinality (optional
+ // tag)
+ for (Map.Entry entry : cardinalities.entrySet()) {
+ String tag = entry.getKey();
+ Cardinality cardinality = entry.getValue();
+ if (cardinality.getMin() == 0) {
+ if (this.orderedTags != null) {
+ this.orderedTags.remove(tag);
+ }
+ newOrderedTags.remove(tag);
+ }
+ }
+
+ // Compute sequenced
+ if (sequenced && this.orderedTags != null) {
+ sequenced = this.orderedTags.equals(newOrderedTags);
+ }
+ this.orderedTags = newOrderedTags;
+ }
+
+ /**
+ * Returns map which stores cardinality per tag.
+ *
+ * @return map which stores cardinality per tag
+ */
+ public Map getCardinalities() {
+ return cardinalities;
+ }
+
+ /**
+ * Returns true if tags for each child hierarchy have the same order and false
+ * otherwise.
+ *
+ * @return true if tags for each child hierarchy have the same order and false
+ * otherwise.
+ */
+ public boolean isSequenced() {
+ return sequenced;
+ }
+}
diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ContainerDeclaration.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ContainerDeclaration.java
new file mode 100644
index 0000000000..9c2cd3350c
--- /dev/null
+++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ContainerDeclaration.java
@@ -0,0 +1,61 @@
+/*******************************************************************************
+* 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.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * Children information.
+ *
+ */
+public class ContainerDeclaration {
+
+ private final Map container;
+
+ public ContainerDeclaration() {
+ this.container = 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 ElementDeclaration getElement(String name) {
+ ElementDeclaration element = container.get(name);
+ if (element != null) {
+ return element;
+ }
+ element = new ElementDeclaration(name);
+ addElement(element);
+ return element;
+ }
+
+ public void addElement(ElementDeclaration element) {
+ container.put(element.getName(), element);
+ }
+
+ /**
+ * Returns the elements information of the node.
+ *
+ * @return the elements information of the node.
+ */
+ public Collection getElements() {
+ return container.values();
+ }
+
+}
diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ElementDeclaration.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ElementDeclaration.java
new file mode 100644
index 0000000000..5a0072d035
--- /dev/null
+++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/ElementDeclaration.java
@@ -0,0 +1,120 @@
+/*******************************************************************************
+* 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.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Element information.
+ *
+ */
+public class ElementDeclaration extends ContainerDeclaration {
+
+ private final String name;
+
+ private ElementDeclaration parent;
+
+ private final Map attributes;
+
+ private boolean hasCharacterContent;
+
+ private int occurrences;
+
+ // Children information
+
+ private ChildrenProperties childrenProperties;
+
+ public ElementDeclaration(String name) {
+ this.name = name;
+ this.attributes = new HashMap<>();
+ this.occurrences = 0;
+ this.childrenProperties = new ChildrenProperties();
+ }
+
+ /**
+ * 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 AttributeDeclaration getAttribute(String name) {
+ AttributeDeclaration attribute = attributes.get(name);
+ if (attribute != null) {
+ return attribute;
+ }
+ attribute = new AttributeDeclaration(name);
+ attributes.put(name, attribute);
+ return attribute;
+ }
+
+ public Collection 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;
+ }
+
+ public void incrementOccurrences() {
+ occurrences++;
+ }
+
+ public int getOccurrences() {
+ return occurrences;
+ }
+
+ @Override
+ public void addElement(ElementDeclaration element) {
+ super.addElement(element);
+ element.setParent(this);
+ }
+
+ public ElementDeclaration getParent() {
+ return parent;
+ }
+
+ void setParent(ElementDeclaration parent) {
+ this.parent = parent;
+ }
+
+ public void addChildHierarchy(List tags) {
+ childrenProperties.addChildHierarchy(tags);
+ }
+
+ public ChildrenProperties getChildrenProperties() {
+ return childrenProperties;
+ }
+}
diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/FileContentGeneratorManager.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/FileContentGeneratorManager.java
new file mode 100644
index 0000000000..ab94683139
--- /dev/null
+++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/generators/FileContentGeneratorManager.java
@@ -0,0 +1,92 @@
+/*******************************************************************************
+* 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.HashMap;
+import java.util.Map;
+
+import org.eclipse.lemminx.extensions.generators.xml2dtd.DTDGeneratorSettings;
+import org.eclipse.lemminx.extensions.generators.xml2dtd.XML2DTDGenerator;
+import org.eclipse.lemminx.extensions.generators.xml2xsd.XML2XMLSchemaGenerator;
+import org.eclipse.lemminx.extensions.generators.xml2xsd.XMLSchemaGeneratorSettings;
+import org.eclipse.lemminx.services.IXMLFullFormatter;
+import org.eclipse.lemminx.settings.SharedSettings;
+
+/**
+ * Generic file generator.
+ *
+ *
+ * By default the manager is able to generate :
+ *
+ *
+ *
XML Schema from a given XML.
+ *
DTD from a given XML.
+ *
+ *
+ */
+public class FileContentGeneratorManager {
+
+ private static final FileContentGeneratorManager INSTANCE = new FileContentGeneratorManager();
+ private final Map, IFileContentGenerator, ?>> generators;
+
+ private FileContentGeneratorManager() {
+ generators = new HashMap<>();
+ registerDefaultGenerators();
+ }
+
+ /**
+ * Register default generators.
+ */
+ private void registerDefaultGenerators() {
+ registerGenerator(new XML2DTDGenerator(), DTDGeneratorSettings.class);
+ registerGenerator(new XML2XMLSchemaGenerator(), XMLSchemaGeneratorSettings.class);
+ }
+
+ public static FileContentGeneratorManager getInstance() {
+ return INSTANCE;
+ }
+
+ /**
+ * Register the given generator by using settings class as key generator.
+ *
+ * @param generator the generator.
+ * @param generatorKey the key of the generator identified by the settings
+ * class.
+ */
+ public void registerGenerator(IFileContentGenerator, ?> generator,
+ Class extends FileContentGeneratorSettings> generatorKey) {
+ generators.put(generatorKey, generator);
+ }
+
+ /**
+ * Generates a file content (ex : XSD, DTD) from the given document source
+ * document (ex: XML) by using the given settings
+ * generatorSettings.
+ *
+ * @param document the document source (ex : XML).
+ * @param sharedSettings the shared settings.
+ * @param generatorSettings the generator settings.
+ * @param formatter the formatter.
+ * @return the result of the generation of the file content (ex : XSD, DTD) from
+ * the given document source document (ex: XML) by using
+ * the given settings generatorSettings.
+ */
+ public