JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/printer/YamlPrinter.java
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
22package com.github.javaparser.printer;
23
24import static com.github.javaparser.utils.Utils.assertNotNull;
25import static java.util.stream.Collectors.toList;
26
27import java.util.List;
28
29import com.github.javaparser.ast.Node;
30import com.github.javaparser.ast.NodeList;
31import com.github.javaparser.metamodel.NodeMetaModel;
32import com.github.javaparser.metamodel.PropertyMetaModel;
33
34/**
35 * Outputs a YAML file containing the AST meant for inspecting it.
36 */
37public class YamlPrinter {
38
39    private static final int NUM_SPACES_FOR_INDENT = 4;
40    private final boolean outputNodeType;
41
42    public YamlPrinter(boolean outputNodeType) {
43        this.outputNodeType = outputNodeType;
44    }
45
46    public String output(Node node) {
47        StringBuilder output = new StringBuilder();
48        output.append("---");
49        output(node"root"0output);
50        output.append(System.lineSeparator() + "...");
51        return output.toString();
52    }
53
54    public void output(Node nodeString nameint levelStringBuilder builder) {
55        assertNotNull(node);
56        NodeMetaModel metaModel = node.getMetaModel();
57        List<PropertyMetaModelallPropertyMetaModels = metaModel.getAllPropertyMetaModels();
58        List<PropertyMetaModelattributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute)
59                .filter(PropertyMetaModel::isSingular).collect(toList());
60        List<PropertyMetaModelsubNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode)
61                .filter(PropertyMetaModel::isSingular).collect(toList());
62        List<PropertyMetaModelsubLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList)
63                .collect(toList());
64
65        if (outputNodeType)
66            builder.append(System.lineSeparator() + indent(level) + name + "(Type=" + metaModel.getTypeName() + "): ");
67        else
68            builder.append(System.lineSeparator() + indent(level) + name + ": ");
69
70        level++;
71        for (PropertyMetaModel a : attributes) {
72            builder.append(System.lineSeparator() + indent(level) + a.getName() + ": " + escapeValue(a.getValue(node).toString()));
73        }
74
75        for (PropertyMetaModel sn : subNodes) {
76            Node nd = (Nodesn.getValue(node);
77            if (nd != null)
78                output(ndsn.getName(), levelbuilder);
79        }
80
81        for (PropertyMetaModel sl : subLists) {
82            NodeList<? extends Nodenl = (NodeList<? extends Node>) sl.getValue(node);
83            if (nl != null && nl.isNonEmpty()) {
84                builder.append(System.lineSeparator() + indent(level) + sl.getName() + ": ");
85                String slName = sl.getName();
86                slName = slName.endsWith("s") ? slName.substring(0sl.getName().length() - 1) : slName;
87                for (Node nd : nl)
88                    output(nd"- " + slNamelevel + 1builder);
89            }
90        }
91    }
92
93    private String indent(int level) {
94        StringBuilder sb = new StringBuilder();
95        for (int i = 0i < leveli++)
96            for (int j = 0j < NUM_SPACES_FOR_INDENTj++)
97                sb.append(" ");
98        return sb.toString();
99    }
100
101    private String escapeValue(String value) {
102        return "\"" + value
103                .replace("\\""\\\\")
104                .replaceAll("\"""\\\\\"")
105                .replace("\n""\\n")
106                .replace("\r""\\r")
107                .replace("\f""\\f")
108                .replace("\b""\\b")
109                .replace("\t""\\t") + "\"";
110    }
111
112    public static void print(Node node) {
113        System.out.println(new YamlPrinter(true).output(node));
114    }
115}
116
MembersX
YamlPrinter:output:Block:attributes
YamlPrinter:escapeValue
YamlPrinter:output:Block:output
YamlPrinter:output:Block:subLists
YamlPrinter:print
YamlPrinter:output:Block:Block:Block:slName
YamlPrinter:output
YamlPrinter:indent
YamlPrinter:YamlPrinter
YamlPrinter:output:Block:subNodes
YamlPrinter:NUM_SPACES_FOR_INDENT
YamlPrinter:output:Block:Block:nl
YamlPrinter:output:Block:allPropertyMetaModels
YamlPrinter:indent:Block:sb
YamlPrinter:output:Block:metaModel
YamlPrinter:output:Block:Block:nd
YamlPrinter:outputNodeType
Members
X