JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/printer/concretesyntaxmodel/CsmList.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.concretesyntaxmodel;
23
24import com.github.javaparser.ast.Node;
25import com.github.javaparser.ast.NodeList;
26import com.github.javaparser.ast.observer.ObservableProperty;
27import com.github.javaparser.printer.ConcreteSyntaxModel;
28import com.github.javaparser.printer.SourcePrinter;
29
30import java.util.Collection;
31import java.util.Iterator;
32
33public class CsmList implements CsmElement {
34    private final ObservableProperty property;
35    private final CsmElement separatorPost;
36    private final CsmElement separatorPre;
37    private final CsmElement preceeding;
38    private final CsmElement following;
39
40    public ObservableProperty getProperty() {
41        return property;
42    }
43
44    public CsmElement getSeparatorPost() {
45        return separatorPost;
46    }
47
48    public CsmElement getSeparatorPre() {
49        return separatorPre;
50    }
51
52    public CsmElement getPreceeding() {
53        return preceeding;
54    }
55
56    public CsmElement getFollowing() {
57        return following;
58    }
59
60    public CsmList(ObservableProperty propertyCsmElement separator) {
61        this(property, new CsmNone(), separator, new CsmNone(), new CsmNone());
62    }
63
64    public CsmList(ObservableProperty property) {
65        this(property, new CsmNone(), new CsmNone(), new CsmNone(), new CsmNone());
66    }
67
68    public CsmList(ObservableProperty propertyCsmElement separatorPreCsmElement separatorPostCsmElement preceedingCsmElement following) {
69        this.property = property;
70        this.separatorPre = separatorPre;
71        this.separatorPost = separatorPost;
72        this.preceeding = preceeding;
73        this.following = following;
74    }
75
76    @Override
77    public void prettyPrint(Node nodeSourcePrinter printer) {
78        if (property.isAboutNodes()) {
79            NodeList<? extends NodenodeList = property.getValueAsMultipleReference(node);
80            if (nodeList == null) {
81                return;
82            }
83            if (!nodeList.isEmpty() && preceeding != null) {
84                preceeding.prettyPrint(nodeprinter);
85            }
86            for (int i = 0i < nodeList.size(); i++) {
87                if (separatorPre != null && i != 0) {
88                    separatorPre.prettyPrint(nodeprinter);
89                }
90                ConcreteSyntaxModel.genericPrettyPrint(nodeList.get(i), printer);
91                if (separatorPost != null && i != (nodeList.size() - 1)) {
92                    separatorPost.prettyPrint(nodeprinter);
93                }
94            }
95            if (!nodeList.isEmpty() && following != null) {
96                following.prettyPrint(nodeprinter);
97            }
98        } else {
99            Collection<?> values = property.getValueAsCollection(node);
100            if (values == null) {
101                return;
102            }
103            if (!values.isEmpty() && preceeding != null) {
104                preceeding.prettyPrint(nodeprinter);
105            }
106            for (Iterator<?> it = values.iterator(); it.hasNext(); ) {
107                if (separatorPre != null && it.hasNext()) {
108                    separatorPre.prettyPrint(nodeprinter);
109                }
110                printer.print(PrintingHelper.printToString(it.next()));
111                if (separatorPost != null && it.hasNext()) {
112                    separatorPost.prettyPrint(nodeprinter);
113                }
114            }
115            if (!values.isEmpty() && following != null) {
116                following.prettyPrint(nodeprinter);
117            }
118        }
119    }
120}
121
MembersX
CsmList:separatorPost
CsmList:getFollowing
CsmList:getPreceeding
CsmList:property
CsmList:separatorPre
CsmList:preceeding
CsmList:getProperty
CsmList:CsmList
CsmList:getSeparatorPre
CsmList:prettyPrint:Block:Block:nodeList
CsmList:getSeparatorPost
CsmList:following
CsmList:prettyPrint
CsmList:prettyPrint:Block:Block:values
Members
X