use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class StringEscapeUtils method unescapeJava.
/**
* Unescapes any Java literals found in the <code>String</code>.
* For example, it will turn a sequence of <code>'\'</code> and
* <code>'n'</code> into a newline character, unless the <code>'\'</code>
* is preceded by another <code>'\'</code>.
*
* @param str the <code>String</code> to unescape, may be null
* @return a new unescaped <code>String</code>, <code>null</code> if null string input
*/
public static String unescapeJava(String str) {
if (str == null) {
return null;
}
try {
Writer writer = new StringBuilderWriter(str.length());
unescapeJava(writer, str);
return writer.toString();
} catch (IOException ioe) {
// this should never ever happen while writing to a StringBuilderWriter
throw new RuntimeException(ioe);
}
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class EncodingGroovyMethods method encodeHex.
/**
* Produces a Writable that writes the hex encoding of the byte[]. Calling
* toString() on this Writable returns the hex encoding as a String. The hex
* encoding includes two characters for each byte and all letters are lower case.
*
* @param data byte array to be encoded
* @return object which will write the hex encoding of the byte array
* @see Integer#toHexString(int)
*/
public static Writable encodeHex(final byte[] data) {
return new Writable() {
@Override
public Writer writeTo(Writer out) throws IOException {
for (byte datum : data) {
// convert byte into unsigned hex string
String hexString = Integer.toHexString(datum & 0xFF);
// add leading zero if the length of the string is one
if (hexString.length() < 2) {
out.write("0");
}
// write hex string to writer
out.write(hexString);
}
return out;
}
@Override
public String toString() {
Writer buffer = new StringBuilderWriter();
try {
writeTo(buffer);
} catch (IOException e) {
throw new StringWriterIOException(e);
}
return buffer.toString();
}
};
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class JavacJavaCompiler method compile.
@Override
public void compile(List<String> files, CompilationUnit cu) {
List<String> javacParameters = makeParameters(cu.getClassLoader());
StringBuilderWriter javacOutput = new StringBuilderWriter();
int javacReturnValue = 0;
try {
try {
boolean successful = doCompileWithSystemJavaCompiler(cu, files, javacParameters, javacOutput);
if (!successful) {
javacReturnValue = 1;
}
} catch (IllegalArgumentException e) {
// any of the options are invalid
javacReturnValue = 2;
cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
} catch (IOException e) {
javacReturnValue = 1;
cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
}
} catch (Exception e) {
cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu));
}
if (javacReturnValue != 0) {
switch(javacReturnValue) {
case 1:
addJavacError("Compile error during compilation with javac.", cu, javacOutput);
break;
case 2:
addJavacError("Invalid commandline usage for javac.", cu, javacOutput);
break;
default:
addJavacError("unexpected return value by javac.", cu, javacOutput);
break;
}
} else {
// print warnings if any
System.out.print(javacOutput);
}
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class XmlTemplateEngine method createTemplate.
@Override
public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException {
Node root;
try {
root = xmlParser.parse(reader);
} catch (SAXException e) {
throw new RuntimeException("Parsing XML source failed.", e);
}
if (root == null) {
throw new IOException("Parsing XML source failed: root node is null.");
}
StringBuilderWriter writer = new StringBuilderWriter(1024);
writer.write("/* Generated by XmlTemplateEngine */\n");
new GspPrinter(new PrintWriter(writer), indentation).print(root);
Script script;
try {
script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter.incrementAndGet() + ".groovy");
} catch (Exception e) {
throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
}
return new XmlTemplate(script);
}
use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.
the class BaseTemplate method stringOf.
public String stringOf(Closure cl) throws IOException {
Writer old = out;
Writer stringWriter = new StringBuilderWriter(32);
out = stringWriter;
Object result = cl.call();
if (result != null && result != this) {
stringWriter.append(result.toString());
}
out = old;
return stringWriter.toString();
}
Aggregations