1 /* 2 * Copyright (C) 2007-2010 Júlio 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.GeneratedJavaParserConstants; 25 import com.github.javaparser.ast.Node; 26 import com.github.javaparser.ast.observer.ObservableProperty; 27 import com.github.javaparser.printer.SourcePrinter; 28 29 import java.util.Arrays; 30 import java.util.List; 31 32 import static com.github.javaparser.TokenTypes.*; 33 import static com.github.javaparser.utils.Utils.EOL; 34 35 public interface CsmElement { 36 prettyPrint(Node node, SourcePrinter printer)37 void prettyPrint(Node node, SourcePrinter printer); 38 child(ObservableProperty property)39 static CsmElement child(ObservableProperty property) { 40 return new CsmSingleReference(property); 41 } 42 attribute(ObservableProperty property)43 static CsmElement attribute(ObservableProperty property) { 44 return new CsmAttribute(property); 45 } 46 sequence(CsmElement... elements)47 static CsmElement sequence(CsmElement... elements) { 48 return new CsmSequence(Arrays.asList(elements)); 49 } 50 string(int tokenType, String content)51 static CsmElement string(int tokenType, String content) { 52 return new CsmToken(tokenType, content); 53 } 54 string(int tokenType)55 static CsmElement string(int tokenType) { 56 return new CsmToken(tokenType); 57 } 58 stringToken(ObservableProperty property)59 static CsmElement stringToken(ObservableProperty property) { 60 return new CsmString(property); 61 } 62 charToken(ObservableProperty property)63 static CsmElement charToken(ObservableProperty property) { 64 return new CsmChar(property); 65 } 66 token(int tokenType)67 static CsmElement token(int tokenType) { 68 return new CsmToken(tokenType); 69 } 70 token(int tokenType, CsmToken.TokenContentCalculator tokenContentCalculator)71 static CsmElement token(int tokenType, CsmToken.TokenContentCalculator tokenContentCalculator) { 72 return new CsmToken(tokenType, tokenContentCalculator); 73 } 74 conditional(ObservableProperty property, CsmConditional.Condition condition, CsmElement thenElement)75 static CsmElement conditional(ObservableProperty property, CsmConditional.Condition condition, CsmElement thenElement) { 76 return new CsmConditional(property, condition, thenElement); 77 } 78 conditional(ObservableProperty property, CsmConditional.Condition condition, CsmElement thenElement, CsmElement elseElement)79 static CsmElement conditional(ObservableProperty property, CsmConditional.Condition condition, CsmElement thenElement, CsmElement elseElement) { 80 return new CsmConditional(property, condition, thenElement, elseElement); 81 } 82 conditional(List<ObservableProperty> properties, CsmConditional.Condition condition, CsmElement thenElement, CsmElement elseElement)83 static CsmElement conditional(List<ObservableProperty> properties, CsmConditional.Condition condition, CsmElement thenElement, CsmElement elseElement) { 84 return new CsmConditional(properties, condition, thenElement, elseElement); 85 } 86 space()87 static CsmElement space() { 88 return new CsmToken(spaceTokenKind(), " "); 89 } 90 semicolon()91 static CsmElement semicolon() { 92 return new CsmToken(GeneratedJavaParserConstants.SEMICOLON); 93 } 94 comment()95 static CsmElement comment() { return new CsmComment(); } 96 newline()97 static CsmElement newline() { 98 return new CsmToken(eolTokenKind(), EOL); 99 } 100 none()101 static CsmElement none() { 102 return new CsmNone(); 103 } 104 comma()105 static CsmElement comma() { 106 return new CsmToken(GeneratedJavaParserConstants.COMMA); 107 } 108 list(ObservableProperty property)109 static CsmElement list(ObservableProperty property) { 110 return new CsmList(property); 111 } 112 list(ObservableProperty property, CsmElement separator)113 static CsmElement list(ObservableProperty property, CsmElement separator) { 114 return new CsmList(property, CsmElement.none(), separator, new CsmNone(), new CsmNone()); 115 } 116 list(ObservableProperty property, CsmElement separator, CsmElement preceeding, CsmElement following)117 static CsmElement list(ObservableProperty property, CsmElement separator, CsmElement preceeding, CsmElement following) { 118 return new CsmList(property, none(), separator, preceeding, following); 119 } 120 list(ObservableProperty property, CsmElement separatorPre, CsmElement separatorPost, CsmElement preceeding, CsmElement following)121 static CsmElement list(ObservableProperty property, CsmElement separatorPre, CsmElement separatorPost, CsmElement preceeding, CsmElement following) { 122 return new CsmList(property, separatorPre, separatorPost, preceeding, following); 123 } 124 orphanCommentsEnding()125 static CsmElement orphanCommentsEnding() { 126 return new CsmOrphanCommentsEnding(); 127 } 128 orphanCommentsBeforeThis()129 static CsmElement orphanCommentsBeforeThis() { 130 // FIXME 131 return new CsmNone(); 132 } 133 indent()134 static CsmElement indent() { 135 return new CsmIndent(); 136 } 137 unindent()138 static CsmElement unindent() { 139 return new CsmUnindent(); 140 } 141 block(CsmElement content)142 static CsmElement block(CsmElement content) { 143 return sequence(token(GeneratedJavaParserConstants.LBRACE), indent(), content, unindent(), token(GeneratedJavaParserConstants.RBRACE)); 144 } 145 } 146