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.concretesyntaxmodel; 23 24 import com.github.javaparser.ast.Node; 25 import com.github.javaparser.ast.NodeList; 26 import com.github.javaparser.ast.observer.ObservableProperty; 27 import com.github.javaparser.printer.ConcreteSyntaxModel; 28 import com.github.javaparser.printer.SourcePrinter; 29 30 import java.util.Collection; 31 import java.util.Iterator; 32 33 public 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 property, CsmElement 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 property, CsmElement separatorPre, CsmElement separatorPost, CsmElement preceeding, CsmElement 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 node, SourcePrinter printer) { 78 if (property.isAboutNodes()) { 79 NodeList nodeList = property.getValueAsMultipleReference(node); 80 if (nodeList == null) { 81 return; 82 } 83 if (!nodeList.isEmpty() && preceeding != null) { 84 preceeding.prettyPrint(node, printer); 85 } 86 for (int i = 0; i < nodeList.size(); i++) { 87 if (separatorPre != null && i != 0) { 88 separatorPre.prettyPrint(node, printer); 89 } 90 ConcreteSyntaxModel.genericPrettyPrint(nodeList.get(i), printer); 91 if (separatorPost != null && i != (nodeList.size() - 1)) { 92 separatorPost.prettyPrint(node, printer); 93 } 94 } 95 if (!nodeList.isEmpty() && following != null) { 96 following.prettyPrint(node, printer); 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(node, printer); 105 } 106 for (Iterator it = values.iterator(); it.hasNext(); ) { 107 if (separatorPre != null && it.hasNext()) { 108 separatorPre.prettyPrint(node, printer); 109 } 110 printer.print(PrintingHelper.printToString(it.next())); 111 if (separatorPost != null && it.hasNext()) { 112 separatorPost.prettyPrint(node, printer); 113 } 114 } 115 if (!values.isEmpty() && following != null) { 116 following.prettyPrint(node, printer); 117 } 118 } 119 } 120 } 121