Search in sources :

Example 1 with NodeMetaModel

use of com.github.javaparser.metamodel.NodeMetaModel in project javaparser by javaparser.

the class DotPrinter method output.

public void output(Node node, String parentNodeName, String name, StringBuilder builder) {
    assertNotNull(node);
    NodeMetaModel metaModel = node.getMetaModel();
    List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels();
    List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList).collect(toList());
    String ndName = nextNodeName();
    if (outputNodeType)
        builder.append(System.lineSeparator() + ndName + " [label=\"" + name + " (" + metaModel.getTypeName() + ")\"];");
    else
        builder.append(System.lineSeparator() + ndName + " [label=\"" + name + "\"];");
    if (parentNodeName != null)
        builder.append(System.lineSeparator() + parentNodeName + " -> " + ndName + ";");
    for (PropertyMetaModel a : attributes) {
        String attrName = nextNodeName();
        builder.append(System.lineSeparator() + attrName + " [label=\"" + a.getName() + "='" + a.getValue(node).toString() + "'\"];");
        builder.append(System.lineSeparator() + ndName + " -> " + attrName + ";");
    }
    for (PropertyMetaModel sn : subNodes) {
        Node nd = (Node) sn.getValue(node);
        if (nd != null)
            output(nd, ndName, sn.getName(), builder);
    }
    for (PropertyMetaModel sl : subLists) {
        NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node);
        if (nl != null && nl.isNonEmpty()) {
            String ndLstName = nextNodeName();
            builder.append(System.lineSeparator() + ndLstName + " [label=\"" + sl.getName() + "\"];");
            builder.append(System.lineSeparator() + ndName + " -> " + ndLstName + ";");
            String slName = sl.getName().substring(0, sl.getName().length() - 1);
            for (Node nd : nl) output(nd, ndLstName, slName, builder);
        }
    }
}
Also used : Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) NodeMetaModel(com.github.javaparser.metamodel.NodeMetaModel) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 2 with NodeMetaModel

use of com.github.javaparser.metamodel.NodeMetaModel in project javaparser by javaparser.

the class JsonPrinter method output.

public String output(Node node, String name, int level) {
    assertNotNull(node);
    NodeMetaModel metaModel = node.getMetaModel();
    List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels();
    List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList).collect(toList());
    final List<String> content = new ArrayList<>();
    if (outputNodeType) {
        content.add(q("type") + ":" + q(metaModel.getTypeName()));
    }
    for (PropertyMetaModel attributeMetaModel : attributes) {
        content.add(q(attributeMetaModel.getName()) + ":" + q(attributeMetaModel.getValue(node).toString()));
    }
    for (PropertyMetaModel subNodeMetaModel : subNodes) {
        Node value = (Node) subNodeMetaModel.getValue(node);
        if (value != null) {
            content.add(output(value, subNodeMetaModel.getName(), level + 1));
        }
    }
    for (PropertyMetaModel subListMetaModel : subLists) {
        NodeList<? extends Node> subList = (NodeList<? extends Node>) subListMetaModel.getValue(node);
        if (subList != null && !subList.isEmpty()) {
            final List<String> listContent = new ArrayList<>();
            for (Node subListNode : subList) {
                listContent.add(output(subListNode, null, level + 1));
            }
            content.add(listContent.stream().collect(Collectors.joining(",", q(subListMetaModel.getName()) + ":[", "]")));
        }
    }
    if (name == null) {
        return content.stream().collect(Collectors.joining(",", "{", "}"));
    }
    return content.stream().collect(Collectors.joining(",", q(name) + ":{", "}"));
}
Also used : Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) ArrayList(java.util.ArrayList) NodeMetaModel(com.github.javaparser.metamodel.NodeMetaModel) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 3 with NodeMetaModel

use of com.github.javaparser.metamodel.NodeMetaModel in project javaparser by javaparser.

the class XmlPrinter method output.

public void output(Node node, String name, int level, StringBuilder builder) {
    assertNotNull(node);
    NodeMetaModel metaModel = node.getMetaModel();
    List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels();
    List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList).collect(toList());
    builder.append("<").append(name);
    if (outputNodeType) {
        builder.append(attribute("type", metaModel.getTypeName()));
    }
    for (PropertyMetaModel attributeMetaModel : attributes) {
        builder.append(attribute(attributeMetaModel.getName(), attributeMetaModel.getValue(node).toString()));
    }
    builder.append(">");
    for (PropertyMetaModel subNodeMetaModel : subNodes) {
        Node value = (Node) subNodeMetaModel.getValue(node);
        if (value != null) {
            output(value, subNodeMetaModel.getName(), level + 1, builder);
        }
    }
    for (PropertyMetaModel subListMetaModel : subLists) {
        NodeList<? extends Node> subList = (NodeList<? extends Node>) subListMetaModel.getValue(node);
        if (subList != null && !subList.isEmpty()) {
            String listName = subListMetaModel.getName();
            builder.append("<").append(listName).append(">");
            String singular = listName.substring(0, listName.length() - 1);
            for (Node subListNode : subList) {
                output(subListNode, singular, level + 1, builder);
            }
            builder.append(close(listName));
        }
    }
    builder.append(close(name));
}
Also used : Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) NodeMetaModel(com.github.javaparser.metamodel.NodeMetaModel) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 4 with NodeMetaModel

use of com.github.javaparser.metamodel.NodeMetaModel in project javaparser by javaparser.

the class YamlPrinter method output.

public void output(Node node, String name, int level, StringBuilder builder) {
    assertNotNull(node);
    NodeMetaModel metaModel = node.getMetaModel();
    List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels();
    List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList).collect(toList());
    if (outputNodeType)
        builder.append(System.lineSeparator() + indent(level) + name + "(Type=" + metaModel.getTypeName() + "): ");
    else
        builder.append(System.lineSeparator() + indent(level) + name + ": ");
    level++;
    for (PropertyMetaModel a : attributes) {
        builder.append(System.lineSeparator() + indent(level) + a.getName() + ": \"" + a.getValue(node).toString() + "\"");
    }
    for (PropertyMetaModel sn : subNodes) {
        Node nd = (Node) sn.getValue(node);
        if (nd != null)
            output(nd, sn.getName(), level, builder);
    }
    for (PropertyMetaModel sl : subLists) {
        NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node);
        if (nl != null && nl.isNonEmpty()) {
            builder.append(System.lineSeparator() + indent(level) + sl.getName() + ": ");
            String slName = sl.getName();
            slName = slName.endsWith("s") ? slName.substring(0, sl.getName().length() - 1) : slName;
            for (Node nd : nl) output(nd, "- " + slName, level + 1, builder);
        }
    }
}
Also used : Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) NodeMetaModel(com.github.javaparser.metamodel.NodeMetaModel) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Aggregations

Node (com.github.javaparser.ast.Node)4 NodeList (com.github.javaparser.ast.NodeList)4 NodeMetaModel (com.github.javaparser.metamodel.NodeMetaModel)4 PropertyMetaModel (com.github.javaparser.metamodel.PropertyMetaModel)4 ArrayList (java.util.ArrayList)1