Skip to content

Commit

Permalink
Implement XML completion based on DTD for element (see #106)
Browse files Browse the repository at this point in the history
Signed-off-by: angelozerr <[email protected]>
  • Loading branch information
angelozerr committed Nov 20, 2018
1 parent 4d9361d commit 03183c2
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lsp4xml.extensions.contentmodel.model.ContentModelProvider;
import org.eclipse.lsp4xml.extensions.dtd.contentmodel.DTDContentModelProvider;
import org.eclipse.lsp4xml.extensions.dtd.contentmodel.CMDTDContentModelProvider;
import org.eclipse.lsp4xml.extensions.dtd.diagnostics.DTDDiagnosticsParticipant;
import org.eclipse.lsp4xml.services.extensions.IXMLExtension;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
Expand All @@ -39,7 +39,7 @@ public void doSave(ISaveContext context) {
@Override
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
// register DTD content model provider
ContentModelProvider modelProvider = new DTDContentModelProvider(registry.getResolverExtensionManager());
ContentModelProvider modelProvider = new CMDTDContentModelProvider(registry.getResolverExtensionManager());
ContentModelManager modelManager = registry.getComponent(ContentModelManager.class);
modelManager.registerModelProvider(modelProvider);
// register diagnostic participant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
*/
package org.eclipse.lsp4xml.extensions.dtd.contentmodel;

import org.apache.xerces.impl.dtd.DTDGrammar;
import org.apache.xerces.impl.dtd.XMLDTDLoader;
import org.apache.xerces.xni.grammars.Grammar;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.eclipse.lsp4xml.dom.DOMDocumentType;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DOMDocumentType;
import org.eclipse.lsp4xml.extensions.contentmodel.model.CMDocument;
import org.eclipse.lsp4xml.extensions.contentmodel.model.ContentModelProvider;
import org.eclipse.lsp4xml.uriresolver.URIResolverExtensionManager;
Expand All @@ -23,13 +22,11 @@
/**
* DTD content model provider.
*/
public class DTDContentModelProvider implements ContentModelProvider {
public class CMDTDContentModelProvider implements ContentModelProvider {

private final URIResolverExtensionManager resolverExtensionManager;

private XMLDTDLoader loader;

public DTDContentModelProvider(URIResolverExtensionManager resolverExtensionManager) {
public CMDTDContentModelProvider(URIResolverExtensionManager resolverExtensionManager) {
this.resolverExtensionManager = resolverExtensionManager;
}

Expand All @@ -56,42 +53,18 @@ public String getSystemId(DOMDocument xmlDocument, String namespaceURI) {

@Override
public CMDocument createCMDocument(String key) {
DTDGrammar model;
try {
model = (DTDGrammar) getLoader().loadGrammar(new XMLInputSource(null, key, null));
CMDTDDocument document = new CMDTDDocument();
document.setEntityResolver(resolverExtensionManager);
Grammar grammar = document.loadGrammar(new XMLInputSource(null, key, null));
if (grammar != null) {
// DTD can be loaded
return document;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
if (model != null) {
//DTD can be loaded
return new DTDDocument(model);
}
return null;
}

public XMLDTDLoader getLoader() {
if (loader == null) {
loader = getSynchLoader();
}
return loader;
}

private synchronized XMLDTDLoader getSynchLoader() {
if (loader != null) {
return loader;
}
XMLDTDLoader loader = new XMLDTDLoader();
loader.setEntityResolver(resolverExtensionManager);
/*
* loader.setErrorHandler(new DOMErrorHandler() {
*
* @Override public boolean handleError(DOMError error) { if
* (error.getRelatedException() instanceof CacheResourceDownloadingException) {
* throw ((CacheResourceDownloadingException) error.getRelatedException()); }
* return false; } });
*/
return loader;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,49 @@
*/
package org.eclipse.lsp4xml.extensions.dtd.contentmodel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.xerces.impl.dtd.DTDGrammar;
import org.apache.xerces.impl.dtd.XMLDTDLoader;
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.grammars.Grammar;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.eclipse.lsp4xml.dom.DOMElement;
import org.eclipse.lsp4xml.extensions.contentmodel.model.CMAttributeDeclaration;
import org.eclipse.lsp4xml.extensions.contentmodel.model.CMDocument;
import org.eclipse.lsp4xml.extensions.contentmodel.model.CMElementDeclaration;

/**
* DTD document.
*
* @author azerr
*
*/
public class DTDDocument implements CMDocument {
public class CMDTDDocument extends XMLDTDLoader implements CMDocument {

private final DTDGrammar grammar;
private Map<String, List<String>> hierachiesMap;
private List<CMElementDeclaration> elements;

public DTDDocument(DTDGrammar grammar) {
this.grammar = grammar;
}
private DTDGrammar grammar;
private List<String> hierachies;

@Override
public Collection<CMElementDeclaration> getElements() {
elements = null;
if (elements == null) {
elements = new ArrayList<>();
int index = grammar.getFirstElementDeclIndex();
while (index != -1) {
DTDElementDeclaration elementDecl = new DTDElementDeclaration();
CMDTDElementDeclaration elementDecl = new CMDTDElementDeclaration(this);
grammar.getElementDecl(index, elementDecl);
elements.add(elementDecl);
index = grammar.getNextElementDeclIndex(index);
}

}
return elements;
}
Expand Down Expand Up @@ -80,5 +88,52 @@ private CMElementDeclaration findElementDeclaration(String tag, String namespace
return null;
}

@Override
public void startContentModel(String elementName, Augmentations augs) throws XNIException {
if (hierachiesMap == null) {
hierachiesMap = new HashMap<>();
}
hierachies = new ArrayList<>();
hierachiesMap.put(elementName, hierachies);
super.startContentModel(elementName, augs);
}

@Override
public void element(String elementName, Augmentations augs) throws XNIException {
hierachies.add(elementName);
super.element(elementName, augs);
}

@Override
public void endContentModel(Augmentations augs) throws XNIException {
hierachies = null;
super.endContentModel(augs);
}

@Override
public Grammar loadGrammar(XMLInputSource source) throws IOException, XNIException {
grammar = (DTDGrammar) super.loadGrammar(source);
return grammar;
}

void collectElementsDeclaration(String elementName, List<CMElementDeclaration> elements) {
if (hierachiesMap == null) {
return;
}
List<String> children = hierachiesMap.get(elementName);
if (children == null) {
return;
}
children.stream().forEach(name -> {
CMElementDeclaration element = findElementDeclaration(name, null);
if (element != null) {
elements.add(element);
}
});
}

void collectAttributesDeclaration(String name, List<CMAttributeDeclaration> attributes) {
// TODO Auto-generated method stub

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
/**
* DTD element declaration.
*
* @author azerr
*
*/
public class DTDElementDeclaration extends XMLElementDecl implements CMElementDeclaration {
public class CMDTDElementDeclaration extends XMLElementDecl implements CMElementDeclaration {

private final CMDTDDocument document;
private List<CMElementDeclaration> elements;
private List<CMAttributeDeclaration> attributes;

public CMDTDElementDeclaration(CMDTDDocument document) {
this.document = document;
}

@Override
public String getName() {
Expand All @@ -40,28 +45,34 @@ public String getNamespace() {

@Override
public Collection<CMAttributeDeclaration> getAttributes() {
// TODO Auto-generated method stub
return null;
if (attributes == null) {
attributes = new ArrayList<>();
document.collectAttributesDeclaration(getName(), attributes);
}
return attributes;
}

@Override
public Collection<CMElementDeclaration> getElements() {
if (elements == null) {
elements = new ArrayList<>();
// collectElementsDeclaration(elementDeclaration, elements);
document.collectElementsDeclaration(getName(), elements);
}
return elements;
}

@Override
public CMElementDeclaration findCMElement(String tag, String namespace) {
// TODO Auto-generated method stub
for (CMElementDeclaration cmElement : getElements()) {
if (cmElement.getName().equals(tag)) {
return cmElement;
}
}
return null;
}

@Override
public CMAttributeDeclaration findCMAttribute(String attributeName) {
// TODO Auto-generated method stub
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lsp4xml.extensions.contentmodel.model.ContentModelProvider;
import org.eclipse.lsp4xml.extensions.xsd.contentmodel.XSDContentModelProvider;
import org.eclipse.lsp4xml.extensions.xsd.contentmodel.CMXSDContentModelProvider;
import org.eclipse.lsp4xml.extensions.xsd.participants.XSDCompletionParticipant;
import org.eclipse.lsp4xml.extensions.xsd.participants.diagnostics.XSDDiagnosticsParticipant;
import org.eclipse.lsp4xml.services.extensions.ICompletionParticipant;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) {
uiResolver = new XSDURIResolverExtension(registry.getDocumentProvider());
registry.getResolverExtensionManager().registerResolver(uiResolver);
// register XSD content model provider
ContentModelProvider modelProvider = new XSDContentModelProvider(registry.getResolverExtensionManager());
ContentModelProvider modelProvider = new CMXSDContentModelProvider(registry.getResolverExtensionManager());
ContentModelManager modelManager = registry.getComponent(ContentModelManager.class);
modelManager.registerModelProvider(modelProvider);
// register completion, diagnostic particpant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
* XSD attribute declaration implementation.
*
*/
public class XSDAttributeDeclaration implements CMAttributeDeclaration {
public class CMXSDAttributeDeclaration implements CMAttributeDeclaration {

private final XSAttributeUse attributeUse;
private String documentation;

public XSDAttributeDeclaration(XSAttributeUse attributeUse) {
public CMXSDAttributeDeclaration(XSAttributeUse attributeUse) {
this.attributeUse = attributeUse;
}

Expand All @@ -45,7 +45,7 @@ public String getName() {
public String getDefaultValue() {
XSValue xsValue = attributeUse.getValueConstraintValue();
if (xsValue == null) {
if (XSDDocument.isBooleanType(getAttrDeclaration().getTypeDefinition())) {
if (CMXSDDocument.isBooleanType(getAttrDeclaration().getTypeDefinition())) {
return "false";
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public Collection<String> getEnumerationValues() {
XSAttributeDeclaration attributeDeclaration = getAttrDeclaration();
if (attributeDeclaration != null) {
XSSimpleTypeDefinition typeDefinition = attributeDeclaration.getTypeDefinition();
return XSDDocument.getEnumerationValues(typeDefinition);
return CMXSDDocument.getEnumerationValues(typeDefinition);
}
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
/**
* XSD content model provider.
*/
public class XSDContentModelProvider implements ContentModelProvider {
public class CMXSDContentModelProvider implements ContentModelProvider {

private final URIResolverExtensionManager resolverExtensionManager;

private XSLoaderImpl loader;

public XSDContentModelProvider(URIResolverExtensionManager resolverExtensionManager) {
public CMXSDContentModelProvider(URIResolverExtensionManager resolverExtensionManager) {
this.resolverExtensionManager = resolverExtensionManager;
}

Expand Down Expand Up @@ -70,7 +70,7 @@ public CMDocument createCMDocument(String key) {
XSModel model = getLoader().loadURI(key);
if (model != null) {
// XML Schema can be loaded
return new XSDDocument(model);
return new CMXSDDocument(model);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
* XSD document implementation.
*
*/
public class XSDDocument implements CMDocument {
public class CMXSDDocument implements CMDocument {

private final XSModel model;

private final Map<XSElementDeclaration, XSDElementDeclaration> elementMappings;
private final Map<XSElementDeclaration, CMXSDElementDeclaration> elementMappings;

private Collection<CMElementDeclaration> elements;

public XSDDocument(XSModel model) {
public CMXSDDocument(XSModel model) {
this.model = model;
this.elementMappings = new HashMap<>();
}
Expand Down Expand Up @@ -125,9 +125,9 @@ private CMElementDeclaration findElementDeclaration(String tag, String namespace
}

CMElementDeclaration getXSDElement(XSElementDeclaration elementDeclaration) {
XSDElementDeclaration element = elementMappings.get(elementDeclaration);
CMXSDElementDeclaration element = elementMappings.get(elementDeclaration);
if (element == null) {
element = new XSDElementDeclaration(this, elementDeclaration);
element = new CMXSDElementDeclaration(this, elementDeclaration);
elementMappings.put(elementDeclaration, element);
}
return element;
Expand Down
Loading

0 comments on commit 03183c2

Please sign in to comment.