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.printer.SourcePrinter; 27 import com.github.javaparser.TokenTypes; 28 import com.github.javaparser.utils.Utils; 29 30 import static com.github.javaparser.TokenTypes.isEndOfLineToken; 31 import static com.github.javaparser.TokenTypes.isSpaceOrTab; 32 33 public class CsmToken implements CsmElement { 34 private final int tokenType; 35 private String content; 36 private TokenContentCalculator tokenContentCalculator; 37 38 public interface TokenContentCalculator { calculate(Node node)39 String calculate(Node node); 40 } 41 getTokenType()42 public int getTokenType() { 43 return tokenType; 44 } 45 getContent(Node node)46 public String getContent(Node node) { 47 if (tokenContentCalculator != null) { 48 return tokenContentCalculator.calculate(node); 49 } 50 return content; 51 } 52 CsmToken(int tokenType)53 public CsmToken(int tokenType) { 54 this.tokenType = tokenType; 55 this.content = GeneratedJavaParserConstants.tokenImage[tokenType]; 56 if (content.startsWith("\"")) { 57 content = content.substring(1, content.length() - 1); 58 } 59 if (isEndOfLineToken(tokenType)) { 60 content = Utils.EOL; 61 } else if (isSpaceOrTab(tokenType)) { 62 content = " "; 63 } 64 } 65 CsmToken(int tokenType, String content)66 public CsmToken(int tokenType, String content) { 67 this.tokenType = tokenType; 68 this.content = content; 69 } 70 CsmToken(int tokenType, TokenContentCalculator tokenContentCalculator)71 public CsmToken(int tokenType, TokenContentCalculator tokenContentCalculator) { 72 this.tokenType = tokenType; 73 this.tokenContentCalculator = tokenContentCalculator; 74 } 75 76 @Override prettyPrint(Node node, SourcePrinter printer)77 public void prettyPrint(Node node, SourcePrinter printer) { 78 if (isEndOfLineToken(tokenType)) { 79 printer.println(); 80 } else { 81 printer.print(getContent(node)); 82 } 83 } 84 85 @Override toString()86 public String toString() { 87 return "token(" + content + ")"; 88 } 89 90 @Override equals(Object o)91 public boolean equals(Object o) { 92 if (this == o) return true; 93 if (o == null || getClass() != o.getClass()) return false; 94 95 CsmToken csmToken = (CsmToken) o; 96 97 if (tokenType != csmToken.tokenType) return false; 98 if (content != null ? !content.equals(csmToken.content) : csmToken.content != null) return false; 99 return tokenContentCalculator != null ? tokenContentCalculator.equals(csmToken.tokenContentCalculator) : csmToken.tokenContentCalculator == null; 100 } 101 102 @Override hashCode()103 public int hashCode() { 104 int result = tokenType; 105 result = 31 * result + (content != null ? content.hashCode() : 0); 106 result = 31 * result + (tokenContentCalculator != null ? tokenContentCalculator.hashCode() : 0); 107 return result; 108 } 109 isWhiteSpace()110 public boolean isWhiteSpace() { 111 return TokenTypes.isWhitespace(tokenType); 112 } 113 isNewLine()114 public boolean isNewLine() { 115 return TokenTypes.isEndOfLineToken(tokenType); 116 } 117 } 118