|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2020 Pivotal, Inc. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Pivotal, Inc. - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package org.springframework.ide.vscode.boot.xml; |
| 12 | + |
| 13 | +import static org.springframework.ide.vscode.boot.xml.XmlConfigConstants.BEANS_NAMESPACE; |
| 14 | +import static org.springframework.ide.vscode.boot.xml.XmlConfigConstants.BEAN_ELEMENT; |
| 15 | +import static org.springframework.ide.vscode.boot.xml.XmlConfigConstants.VALUE_ATTRIBUTE; |
| 16 | +import static org.springframework.ide.vscode.boot.xml.XmlConfigConstants.PROPERTY_ELEMENT; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | + |
| 20 | +import org.eclipse.lemminx.dom.DOMDocument; |
| 21 | +import org.eclipse.lemminx.dom.DOMNode; |
| 22 | +import org.eclipse.lemminx.dom.DOMParser; |
| 23 | +import org.eclipse.lsp4j.TextDocumentIdentifier; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | +import org.springframework.ide.vscode.boot.java.handlers.SpelExpressionReconciler; |
| 27 | +import org.springframework.ide.vscode.commons.java.IJavaProject; |
| 28 | +import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; |
| 29 | +import org.springframework.ide.vscode.commons.languageserver.reconcile.IProblemCollector; |
| 30 | +import org.springframework.ide.vscode.commons.languageserver.reconcile.IReconcileEngine; |
| 31 | +import org.springframework.ide.vscode.commons.util.text.IDocument; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Martin Lippert |
| 35 | + */ |
| 36 | +public class SpringXMLReconcileEngine implements IReconcileEngine { |
| 37 | + |
| 38 | + private static final Logger log = LoggerFactory.getLogger(SpringXMLReconcileEngine.class); |
| 39 | + |
| 40 | + private final JavaProjectFinder projectFinder; |
| 41 | + private final SpelExpressionReconciler spelExpressionReconciler; |
| 42 | + private final XMLElementReconciler[] reconcilers; |
| 43 | + |
| 44 | + public SpringXMLReconcileEngine(JavaProjectFinder projectFinder) { |
| 45 | + this.projectFinder = projectFinder; |
| 46 | + |
| 47 | + this.spelExpressionReconciler = new SpelExpressionReconciler(); |
| 48 | + |
| 49 | + this.reconcilers = new XMLElementReconciler[] { |
| 50 | + |
| 51 | + new XMLElementReconciler(new XMLElementKey(BEANS_NAMESPACE, BEAN_ELEMENT, PROPERTY_ELEMENT, VALUE_ATTRIBUTE), "#{", "}", spelExpressionReconciler) |
| 52 | + |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + public void setSpelExpressionSyntaxValidationEnabled(boolean spelExpressionValidationEnabled) { |
| 57 | + this.spelExpressionReconciler.setEnabled(spelExpressionValidationEnabled); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void reconcile(final IDocument doc, final IProblemCollector problemCollector) { |
| 62 | + IJavaProject project = projectFinder.find(new TextDocumentIdentifier(doc.getUri())).orElse(null); |
| 63 | + URI uri = URI.create(doc.getUri()); |
| 64 | + |
| 65 | + if (project != null) { |
| 66 | + |
| 67 | + try { |
| 68 | + problemCollector.beginCollecting(); |
| 69 | + reconcileXML(doc, problemCollector); |
| 70 | + } |
| 71 | + finally { |
| 72 | + problemCollector.endCollecting(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void reconcileXML(final IDocument doc, final IProblemCollector problemCollector) { |
| 78 | + String content = doc.get(); |
| 79 | + |
| 80 | + DOMParser parser = DOMParser.getInstance(); |
| 81 | + DOMDocument dom = parser.parse(content, "", null); |
| 82 | + reconcileNode(dom, problemCollector); |
| 83 | + } |
| 84 | + |
| 85 | + private void reconcileNode(DOMNode node, IProblemCollector problemCollector) { |
| 86 | + reconcile(node, problemCollector); |
| 87 | + |
| 88 | + for (DOMNode domNode : node.getChildren()) { |
| 89 | + reconcileNode(domNode, problemCollector); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void reconcile(DOMNode node, IProblemCollector problemCollector) { |
| 94 | + for (int i = 0; i < reconcilers.length; i++) { |
| 95 | + reconcilers[i].visit(node, problemCollector); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments