Home | History | Annotate | Download | only in printer
      1 /*
      2  * Copyright (C) 2007-2010 Jlio Vilmar Gesser.
      3  * Copyright (C) 2011, 2013-2016 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 static com.github.javaparser.utils.Utils.assertNotNull;
     25 import static java.util.stream.Collectors.toList;
     26 
     27 import java.util.List;
     28 
     29 import com.github.javaparser.ast.Node;
     30 import com.github.javaparser.ast.NodeList;
     31 import com.github.javaparser.metamodel.NodeMetaModel;
     32 import com.github.javaparser.metamodel.PropertyMetaModel;
     33 
     34 /**
     35  * Outputs a YAML file containing the AST meant for inspecting it.
     36  */
     37 public 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", 0, output);
     50         output.append(System.lineSeparator() + "...");
     51         return output.toString();
     52     }
     53 
     54     public void output(Node node, String name, int level, StringBuilder builder) {
     55         assertNotNull(node);
     56         NodeMetaModel metaModel = node.getMetaModel();
     57         List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels();
     58         List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute)
     59                 .filter(PropertyMetaModel::isSingular).collect(toList());
     60         List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode)
     61                 .filter(PropertyMetaModel::isSingular).collect(toList());
     62         List<PropertyMetaModel> subLists = 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() + ": \""
     73                     + a.getValue(node).toString() + "\"");
     74         }
     75 
     76         for (PropertyMetaModel sn : subNodes) {
     77             Node nd = (Node) sn.getValue(node);
     78             if (nd != null)
     79                 output(nd, sn.getName(), level, builder);
     80         }
     81 
     82         for (PropertyMetaModel sl : subLists) {
     83             NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node);
     84             if (nl != null && nl.isNonEmpty()) {
     85                 builder.append(System.lineSeparator() + indent(level) + sl.getName() + ": ");
     86                 String slName = sl.getName();
     87                 slName = slName.endsWith("s") ? slName.substring(0, sl.getName().length() - 1) : slName;
     88                 for (Node nd : nl)
     89                     output(nd, "- " + slName, level + 1, builder);
     90             }
     91         }
     92     }
     93 
     94     private String indent(int level) {
     95         StringBuilder sb = new StringBuilder();
     96         for (int i = 0; i < level; i++)
     97             for (int j = 0; j < NUM_SPACES_FOR_INDENT; j++)
     98                 sb.append(" ");
     99         return sb.toString();
    100     }
    101 }
    102