1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | |
22 | package com.github.javaparser.printer; |
23 | |
24 | import com.github.javaparser.ast.Node; |
25 | import com.github.javaparser.ast.NodeList; |
26 | import com.github.javaparser.metamodel.NodeMetaModel; |
27 | import com.github.javaparser.metamodel.PropertyMetaModel; |
28 | |
29 | import java.util.List; |
30 | |
31 | import static com.github.javaparser.utils.Utils.assertNotNull; |
32 | import static java.util.stream.Collectors.toList; |
33 | |
34 | /** |
35 | * Outputs an XML file containing the AST meant for inspecting it. |
36 | */ |
37 | public class XmlPrinter { |
38 | private final boolean outputNodeType; |
39 | |
40 | public XmlPrinter(boolean outputNodeType) { |
41 | this.outputNodeType = outputNodeType; |
42 | } |
43 | |
44 | public String output(Node node) { |
45 | StringBuilder output = new StringBuilder(); |
46 | output(node, "root", 0, output); |
47 | return output.toString(); |
48 | } |
49 | |
50 | public void output(Node node, String name, int level, StringBuilder builder) { |
51 | assertNotNull(node); |
52 | NodeMetaModel metaModel = node.getMetaModel(); |
53 | List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels(); |
54 | List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute).filter(PropertyMetaModel::isSingular).collect(toList()); |
55 | List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode).filter(PropertyMetaModel::isSingular).collect(toList()); |
56 | List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList).collect(toList()); |
57 | |
58 | builder.append("<").append(name); |
59 | if (outputNodeType) { |
60 | builder.append(attribute("type", metaModel.getTypeName())); |
61 | } |
62 | |
63 | for (PropertyMetaModel attributeMetaModel : attributes) { |
64 | builder.append(attribute(attributeMetaModel.getName(), attributeMetaModel.getValue(node).toString())); |
65 | } |
66 | builder.append(">"); |
67 | |
68 | for (PropertyMetaModel subNodeMetaModel : subNodes) { |
69 | Node value = (Node) subNodeMetaModel.getValue(node); |
70 | if (value != null) { |
71 | output(value, subNodeMetaModel.getName(), level + 1, builder); |
72 | } |
73 | } |
74 | |
75 | for (PropertyMetaModel subListMetaModel : subLists) { |
76 | NodeList<? extends Node> subList = (NodeList<? extends Node>) subListMetaModel.getValue(node); |
77 | if (subList != null && !subList.isEmpty()) { |
78 | String listName = subListMetaModel.getName(); |
79 | builder.append("<").append(listName).append(">"); |
80 | String singular = listName.substring(0, listName.length() - 1); |
81 | for (Node subListNode : subList) { |
82 | output(subListNode, singular, level + 1, builder); |
83 | } |
84 | builder.append(close(listName)); |
85 | } |
86 | } |
87 | builder.append(close(name)); |
88 | } |
89 | |
90 | private static String close(String name) { |
91 | return "</" + name + ">"; |
92 | } |
93 | |
94 | private static String attribute(String name, String value) { |
95 | return " " + name + "='" + value + "'"; |
96 | } |
97 | |
98 | public static void print(Node node) { |
99 | System.out.println(new XmlPrinter(true).output(node)); |
100 | } |
101 | } |
102 | |
103 |
Members