📄 prettyprinterutils.java
字号:
/*
* Copyright (c) 2003-2004, Alexander Greif
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Flow4J-Eclipse project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.orthanc.flow4j.base;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
import net.sourceforge.jrefactory.ast.SimpleNode;
import net.sourceforge.jrefactory.factory.BufferParserFactory;
import net.sourceforge.jrefactory.factory.ParserFactory;
import org.acm.seguin.awt.ExceptionPrinter;
import org.acm.seguin.pretty.PrettyPrintFile;
import org.acm.seguin.pretty.PrettyPrintVisitor;
import org.acm.seguin.pretty.PrintData;
import org.acm.seguin.util.FileSettings;
/**
* Utility class for pretty printing java source files with the
* JRefactory tool.
* @author greifa
*/
public class PrettyPrinterUtils {
/**
* Pretty prints the java source file and returns it.
* @param source the java source as String
* @return the formatted source
* @throws IOException if streams cannot be closed
*/
public static String prettyPrintSource(String source) throws IOException {
PrettyPrintFile ppf = new PrettyPrintFile();
ParserFactory factory =
new BufferParserFactory(source);
ppf.setParserFactory(factory);
SimpleNode root =
factory.getAbstractSyntaxTree(true, ExceptionPrinter.getInstance());
StringWriter stringWriter = new StringWriter();
BufferedWriter writer = new BufferedWriter(stringWriter);
if (root != null) {
//prettySettings = FileSettings.getRefactoryPrettySettings();
//prettySettings.setReloadNow(true);
// Create the visitor
PrettyPrintVisitor printer = new PrettyPrintVisitor();
// Create the appropriate print data
PrintData data = new PrintData(writer);
if (root instanceof ASTCompilationUnit) {
printer.visit((ASTCompilationUnit) root, data);
} else {
printer.visit(root, data);
}
data.close(); // Flush the output stream and close it
writer.close();
}
return stringWriter.toString();
}
/**
* Loads the pretty printer settings from the file and returns them.
* @param settingsFile where the settings are stored
* @throws IOException
*/
static public FileSettings loadPrettySettings(File settingsFile) throws IOException {
FileSettings prettySettings = new FileSettings(settingsFile);
FileSettings.setSettingsRoot(settingsFile.getParentFile().getParentFile());
return prettySettings;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -