use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class XmlUtil method serialize.
/**
* Return a pretty String version of the Element.
*
* @param element the Element to serialize
* @return the pretty String representation of the Element
*/
public static String serialize(Element element) {
Writer sw = new StringBuilderWriter();
serialize(new DOMSource(element), sw);
return sw.toString();
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class XmlUtil method asString.
private static String asString(Node node) {
Writer sw = new StringBuilderWriter();
PrintWriter pw = new PrintWriter(sw);
XmlNodePrinter nodePrinter = new XmlNodePrinter(pw);
nodePrinter.setPreserveWhitespace(true);
nodePrinter.print(node);
return sw.toString();
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class Groovyc method runCompiler.
private void runCompiler(String[] commandLine) {
// hand crank it so we can add our own compiler configuration
try {
FileSystemCompiler.CompilationOptions options = new FileSystemCompiler.CompilationOptions();
CommandLine parser = FileSystemCompiler.configureParser(options);
parser.parseArgs(commandLine);
configuration = options.toCompilerConfiguration();
configuration.setScriptExtensions(getScriptExtensions());
String tmpExtension = getScriptExtension();
if (tmpExtension.startsWith("*."))
tmpExtension = tmpExtension.substring(1);
configuration.setDefaultScriptExtension(tmpExtension);
if (targetBytecode != null) {
configuration.setTargetBytecode(targetBytecode);
}
// Load the file name list
String[] fileNames = options.generateFileNames();
boolean fileNameErrors = (fileNames == null || !FileSystemCompiler.validateFiles(fileNames));
if (!fileNameErrors) {
try (GroovyClassLoader loader = buildClassLoaderFor()) {
FileSystemCompiler.doCompilation(configuration, makeCompileUnit(loader), fileNames, forceLookupUnnamedFiles);
}
}
} catch (Exception e) {
Throwable t = e;
if (e.getClass() == RuntimeException.class && e.getCause() != null) {
// unwrap to the real exception
t = e.getCause();
}
Writer writer = new StringBuilderWriter();
new ErrorReporter(t, false).write(new PrintWriter(writer));
String message = writer.toString();
taskSuccess = false;
if (errorProperty != null) {
getProject().setNewProperty(errorProperty, "true");
}
if (failOnError) {
log.error(message);
throw new BuildException("Compilation Failed", t, getLocation());
} else {
log.error(message);
}
}
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class Groovy method processError.
private void processError(Exception e) {
Writer writer = new StringBuilderWriter();
new ErrorReporter(e, false).write(new PrintWriter(writer));
String message = writer.toString();
throw new BuildException("Script Failed: " + message, e, getLocation());
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class EncodingGroovyMethods method encodeBase64.
private static Writable encodeBase64(final byte[] data, final boolean chunked, final boolean urlSafe, final boolean pad) {
return new Writable() {
@Override
public Writer writeTo(final Writer writer) throws IOException {
int charCount = 0;
final int dLimit = (data.length / 3) * 3;
final char[] table = urlSafe ? T_TABLE_URLSAFE : T_TABLE;
for (int dIndex = 0; dIndex != dLimit; dIndex += 3) {
int d = ((data[dIndex] & 0XFF) << 16) | ((data[dIndex + 1] & 0XFF) << 8) | (data[dIndex + 2] & 0XFF);
writer.write(table[d >> 18]);
writer.write(table[(d >> 12) & 0X3F]);
writer.write(table[(d >> 6) & 0X3F]);
writer.write(table[d & 0X3F]);
if (chunked && ++charCount == 19) {
writer.write(CHUNK_SEPARATOR);
charCount = 0;
}
}
if (dLimit != data.length) {
int d = (data[dLimit] & 0XFF) << 16;
if (dLimit + 1 != data.length) {
d |= (data[dLimit + 1] & 0XFF) << 8;
}
writer.write(table[d >> 18]);
writer.write(table[(d >> 12) & 0X3F]);
if (pad) {
writer.write((dLimit + 1 < data.length) ? table[(d >> 6) & 0X3F] : '=');
writer.write('=');
} else {
if (dLimit + 1 < data.length) {
writer.write(table[(d >> 6) & 0X3F]);
}
}
if (chunked && charCount != 0) {
writer.write(CHUNK_SEPARATOR);
}
}
return writer;
}
@Override
public String toString() {
Writer buffer = new StringBuilderWriter();
try {
writeTo(buffer);
} catch (IOException e) {
throw new StringWriterIOException(e);
}
return buffer.toString();
}
};
}
Aggregations