Skip to content

Commit

Permalink
feat: PrinterTokenWriterDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Sep 28, 2017
1 parent 3686000 commit 573cf67
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions src/main/java/spoon/reflect/visitor/PrinterTokenWriterDelegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* Copyright (C) 2006-2017 INRIA and contributors
* Spoon - http://spoon.gforge.inria.fr/
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.reflect.visitor;

import spoon.SpoonException;
import spoon.reflect.code.CtComment;

/**
* {@link PrinterTokenWriter} implementation, which simply sends all tokens to next {@link PrinterTokenWriter}
* Inherit your own implementation from this class to manipulates print tokens - e.g. to change formatting of printed output
*/
public class PrinterTokenWriterDelegate implements PrinterTokenWriter {

private PrinterTokenWriter next;

PrinterTokenWriterDelegate() {
}

PrinterTokenWriterDelegate(PrinterTokenWriter next) {
setNext(next);
}

@Override
public PrinterHelper getPrinterHelper() {
return next.getPrinterHelper();
}

public PrinterTokenWriter getNext() {
return next;
}

public void setNext(PrinterTokenWriter next) {
if (next == null) {
throw new SpoonException("Next PrinterTokenWriter must not be null");
}
this.next = next;
}

@Override
public void reset() {
next.reset();
}

@Override
public PrinterTokenWriterDelegate writeWhitespace(String token) {
onToken();
next.writeWhitespace(token);
return this;
}

@Override
public PrinterTokenWriterDelegate writeSeparator(String token) {
onToken();
next.writeSeparator(token);
return this;
}

@Override
public PrinterTokenWriterDelegate writeOperator(String token) {
onToken();
next.writeOperator(token);
return this;
}

@Override
public PrinterTokenWriterDelegate writeLiteral(String token) {
onToken();
next.writeLiteral(token);
return this;
}

@Override
public PrinterTokenWriterDelegate writeKeyword(String token) {
onToken();
next.writeKeyword(token);
return this;
}

@Override
public PrinterTokenWriterDelegate writeIdentifier(String token) {
onToken();
next.writeIdentifier(token);
return this;
}

@Override
public PrinterTokenWriterDelegate writeCodeSnippet(String token) {
onToken();
next.writeCodeSnippet(token);
return this;
}

@Override
public PrinterTokenWriterDelegate writeComment(CtComment comment) {
onToken();
next.writeComment(comment);
return this;
}

@Override
public PrinterTokenWriterDelegate writeln() {
onToken();
next.writeln();
return this;
}

@Override
public PrinterTokenWriterDelegate writeTabs() {
onToken();
next.writeTabs();
return this;
}

@Override
public PrinterTokenWriterDelegate incTab() {
onToken();
next.incTab();
return this;
}

@Override
public PrinterTokenWriterDelegate decTab() {
onToken();
next.decTab();
return this;
}
/**
* Called at the beginning of each token handler
*/
protected void onToken() {
}
}

0 comments on commit 573cf67

Please sign in to comment.