|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ide.vscode.boot.java.commands; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.BiConsumer; |
| 24 | +import java.util.function.Consumer; |
| 25 | + |
| 26 | +import org.eclipse.lsp4j.Location; |
| 27 | +import org.eclipse.lsp4j.Range; |
| 28 | +import org.jmolecules.stereotype.api.Stereotype; |
| 29 | +import org.jmolecules.stereotype.tooling.LabelProvider; |
| 30 | +import org.jmolecules.stereotype.tooling.MethodNodeContext; |
| 31 | +import org.jmolecules.stereotype.tooling.NodeContext; |
| 32 | +import org.jmolecules.stereotype.tooling.NodeHandler; |
| 33 | +import org.springframework.ide.vscode.boot.java.stereotypes.StereotypeClassElement; |
| 34 | +import org.springframework.ide.vscode.boot.java.stereotypes.StereotypeMethodElement; |
| 35 | +import org.springframework.ide.vscode.boot.java.stereotypes.StereotypePackageElement; |
| 36 | + |
| 37 | +import com.google.gson.Gson; |
| 38 | +import com.google.gson.GsonBuilder; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Oliver Drotbohm |
| 42 | + * @author Martin Lippert |
| 43 | + */ |
| 44 | +class ToolsJsonNodeHandler implements NodeHandler<StereotypePackageElement, StereotypePackageElement, StereotypeClassElement, StereotypeMethodElement, Object> { |
| 45 | + |
| 46 | + public static final String ICON = "icon"; |
| 47 | + public static final String TEXT = "text"; |
| 48 | + |
| 49 | + private final Node root; |
| 50 | + private final LabelProvider<StereotypePackageElement, StereotypePackageElement, StereotypeClassElement, StereotypeMethodElement, Object> labels; |
| 51 | + private final BiConsumer<Node, Object> customHandler; |
| 52 | + private Node current; |
| 53 | + |
| 54 | + public ToolsJsonNodeHandler(LabelProvider<StereotypePackageElement, StereotypePackageElement, StereotypeClassElement, StereotypeMethodElement, Object> labels, BiConsumer<Node, Object> customHandler) { |
| 55 | + this.labels = labels; |
| 56 | + this.root = new Node(null); |
| 57 | + this.customHandler = customHandler; |
| 58 | + this.current = root; |
| 59 | + } |
| 60 | + |
| 61 | + /* |
| 62 | + * (non-Javadoc) |
| 63 | + * @see org.jmolecules.stereotype.tooling.NodeHandler#handleApplication(java.lang.Object) |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public void handleApplication(StereotypePackageElement application) { |
| 67 | + this.root |
| 68 | + .withAttribute(TEXT, labels.getApplicationLabel(application)) |
| 69 | + .withAttribute(ICON, "fa-application"); |
| 70 | + } |
| 71 | + |
| 72 | + /* |
| 73 | + * (non-Javadoc) |
| 74 | + * @see org.jmolecules.stereotype.tooling.NodeHandler#handlePackage(java.lang.Object, org.jmolecules.stereotype.tooling.NodeContext) |
| 75 | + */ |
| 76 | + @Override |
| 77 | + public void handlePackage(StereotypePackageElement pkg, NodeContext context) { |
| 78 | + |
| 79 | + addChild(node -> node |
| 80 | + .withAttribute(ICON, "fa-package") |
| 81 | + .withAttribute(TEXT, labels.getPackageLabel(pkg))); |
| 82 | + } |
| 83 | + |
| 84 | + /* |
| 85 | + * (non-Javadoc) |
| 86 | + * @see org.jmolecules.stereotype.tooling.NodeHandler#handleStereotype(org.jmolecules.stereotype.api.Stereotype, org.jmolecules.stereotype.tooling.NestingLevel) |
| 87 | + */ |
| 88 | + @Override |
| 89 | + public void handleStereotype(Stereotype stereotype, NodeContext context) { |
| 90 | + |
| 91 | + addChild(node -> node.withAttribute(ICON, "fa-stereotype") |
| 92 | + .withAttribute(TEXT, labels.getSterotypeLabel(stereotype))); |
| 93 | + } |
| 94 | + |
| 95 | + /* |
| 96 | + * (non-Javadoc) |
| 97 | + * @see org.jmolecules.stereotype.tooling.NodeHandler#handleType(java.lang.Object, org.jmolecules.stereotype.tooling.NestingLevel) |
| 98 | + */ |
| 99 | + @Override |
| 100 | + public void handleType(StereotypeClassElement type, NodeContext context) { |
| 101 | + addChild(node -> node |
| 102 | + .withAttribute(TEXT, labels.getTypeLabel(type)) |
| 103 | + .withAttribute("location", new Location("sampleUri", new Range())) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + public Node createNested() { |
| 108 | + return new Node(this.current); |
| 109 | + } |
| 110 | + |
| 111 | + /* |
| 112 | + * (non-Javadoc) |
| 113 | + * @see org.jmolecules.stereotype.tooling.NodeHandler#handleMethod(java.lang.Object, org.jmolecules.stereotype.tooling.NestingLevel, boolean) |
| 114 | + */ |
| 115 | + @Override |
| 116 | + public void handleMethod(StereotypeMethodElement method, MethodNodeContext<StereotypeClassElement> context) { |
| 117 | + addChildFoo(node -> node.withAttribute("title", labels.getMethodLabel(method, context.getContextualType()))); |
| 118 | + } |
| 119 | + |
| 120 | + /* |
| 121 | + * (non-Javadoc) |
| 122 | + * @see org.jmolecules.stereotype.tooling.NodeHandler#handleCustom(java.lang.Object, org.jmolecules.stereotype.tooling.NodeContext) |
| 123 | + */ |
| 124 | + @Override |
| 125 | + public void handleCustom(Object custom, NodeContext context) { |
| 126 | + addChild(node -> customHandler.accept(node, custom)); |
| 127 | + } |
| 128 | + |
| 129 | + /* |
| 130 | + * (non-Javadoc) |
| 131 | + * @see org.jmolecules.stereotype.tooling.NodeHandler#postGroup() |
| 132 | + */ |
| 133 | + @Override |
| 134 | + public void postGroup() { |
| 135 | + this.current = this.current.parent; |
| 136 | + } |
| 137 | + |
| 138 | + private void addChild(Consumer<Node> consumer) { |
| 139 | + this.current = addChildFoo(consumer); |
| 140 | + } |
| 141 | + |
| 142 | + private Node addChildFoo(Consumer<Node> consumer) { |
| 143 | + |
| 144 | + var node = new Node(this.current); |
| 145 | + consumer.accept(node); |
| 146 | + |
| 147 | + this.current.children.add(node); |
| 148 | + |
| 149 | + return node; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String toString() { |
| 154 | + Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| 155 | + return gson.toJson(root); |
| 156 | + } |
| 157 | + |
| 158 | + Node getRoot() { |
| 159 | + return root; |
| 160 | + } |
| 161 | + |
| 162 | + static class Node { |
| 163 | + |
| 164 | + transient final Node parent; |
| 165 | + final Map<String, Object> attributes; |
| 166 | + final List<Node> children; |
| 167 | + |
| 168 | + Node(Node parent) { |
| 169 | + this.parent = parent; |
| 170 | + this.attributes = new LinkedHashMap<>(); |
| 171 | + this.children = new ArrayList<>(); |
| 172 | + } |
| 173 | + |
| 174 | + public Node withAttribute(String key, Object value) { |
| 175 | + this.attributes.put(key, value); |
| 176 | + return this; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments