本文整理汇总了Java中groovy.lang.Writable类的典型用法代码示例。如果您正苦于以下问题:Java Writable类的具体用法?Java Writable怎么用?Java Writable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Writable类属于groovy.lang包,在下文中一共展示了Writable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateScript
import groovy.lang.Writable; //导入依赖的package包/类
public static String generateScript(GroovyMap groovyMap, String templateName) throws GFacException {
URL templateUrl = ApplicationSettings.loadFile(templateName);
if (templateUrl == null) {
String error = "Template file '" + templateName + "' not found";
throw new GFacException(error);
}
File template = new File(templateUrl.getPath());
TemplateEngine engine = new GStringTemplateEngine();
Writable make;
try {
make = engine.createTemplate(template).make(groovyMap);
} catch (Exception e) {
throw new GFacException("Error while generating script using groovy map", e);
}
return make.toString();
}
示例2: renderDocument
import groovy.lang.Writable; //导入依赖的package包/类
@Override
public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException {
try {
Template template = findTemplate(templateName);
Writable writable = template.make(wrap(model));
writable.writeTo(writer);
} catch (Exception e) {
throw new RenderingException(e);
}
}
示例3: renderDocument
import groovy.lang.Writable; //导入依赖的package包/类
@Override
public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException {
try {
Template template = templateEngine.createTemplateByPath(templateName);
Map<String, Object> wrappedModel = wrap(model);
Writable writable = template.make(wrappedModel);
writable.writeTo(writer);
} catch (Exception e) {
throw new RenderingException(e);
}
}
示例4: template
import groovy.lang.Writable; //导入依赖的package包/类
public static String template(TemplateEngine engine, String name,
Map<String, ?> model) throws IOException, CompilationFailedException,
ClassNotFoundException {
Writable writable = getTemplate(engine, name).make(model);
StringWriter result = new StringWriter();
writable.writeTo(result);
return result.toString();
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:9,代码来源:GroovyTemplate.java
示例5: generateModuleCodeFromTemplate
import groovy.lang.Writable; //导入依赖的package包/类
public static String generateModuleCodeFromTemplate(String moduleName, String codeTemplate) {
try {
Map bindings = new HashMap();
bindings.put("moduleName", moduleName);
SimpleTemplateEngine simpleTemplateEngine = new SimpleTemplateEngine();
Template template = simpleTemplateEngine.createTemplate(codeTemplate);
Writable writable = template.make(bindings);
String finalScript = writable.toString();
return finalScript;
} catch (Exception ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
return null;
}
示例6: fillTemplate
import groovy.lang.Writable; //导入依赖的package包/类
public static String fillTemplate(String templateString, Map bindings) throws RuntimeException {
try {
SimpleTemplateEngine simpleTemplateEngine = new SimpleTemplateEngine();
Template template = simpleTemplateEngine.createTemplate(templateString);
Writable writable = template.make(bindings);
String finalScript = writable.toString();
return finalScript;
} catch (CompilationFailedException | ClassNotFoundException | IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
示例7: call
import groovy.lang.Writable; //导入依赖的package包/类
/**
* Writes the given Writable as the value of the given attribute name
*
* @param name The attribute name
* @param json The writable value
* @throws IOException
*/
public void call(String name, Writable json) throws IOException {
writeName(name);
verifyValue();
if(json instanceof GString) {
writer.write(generator.toJson(json.toString()));
}
else {
json.writeTo(writer);
}
}
示例8: make
import groovy.lang.Writable; //导入依赖的package包/类
public Writable make(final Map map) {
return new Writable() {
/**
* Write the template document with the set binding applied to the writer.
*
* @see groovy.lang.Writable#writeTo(java.io.Writer)
*/
public Writer writeTo(Writer writer) {
Binding binding;
if (map == null)
binding = new Binding();
else
binding = new Binding(map);
Script scriptObject = InvokerHelper.createScript(script.getClass(), binding);
PrintWriter pw = new PrintWriter(writer);
scriptObject.setProperty("out", pw);
scriptObject.run();
pw.flush();
return writer;
}
/**
* Convert the template and binding into a result String.
*
* @see java.lang.Object#toString()
*/
public String toString() {
Writer sw = new StringBuilderWriter();
writeTo(sw);
return sw.toString();
}
};
}
示例9: asString
import groovy.lang.Writable; //导入依赖的package包/类
private static String asString(GPathResult node) {
// little bit of hackery to avoid Groovy dependency in this file
try {
Object builder = ((Class) Class.forName("groovy.xml.StreamingMarkupBuilder")).newInstance();
InvokerHelper.setProperty(builder, "encoding", "UTF-8");
Writable w = (Writable) InvokerHelper.invokeMethod(builder, "bindNode", node);
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + w.toString();
} catch (Exception e) {
return "Couldn't convert node to string because: " + e.getMessage();
}
}
示例10: writeTo
import groovy.lang.Writable; //导入依赖的package包/类
public Writer writeTo(final Writer out) throws IOException {
if (this.replacementNodeStack.empty()) {
for (Object child : this.children) {
if (child instanceof Writable) {
((Writable) child).writeTo(out);
} else {
out.write(child.toString());
}
}
return out;
} else {
return ((Writable) this.replacementNodeStack.peek()).writeTo(out);
}
}
示例11: encodeHex
import groovy.lang.Writable; //导入依赖的package包/类
/**
* 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() {
public Writer writeTo(Writer out) throws IOException {
for (int i = 0; i < data.length; i++) {
// convert byte into unsigned hex string
String hexString = Integer.toHexString(data[i] & 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;
}
public String toString() {
Writer buffer = new StringBuilderWriter();
try {
writeTo(buffer);
} catch (IOException e) {
throw new StringWriterIOException(e);
}
return buffer.toString();
}
};
}
示例12: filterLine
import groovy.lang.Writable; //导入依赖的package包/类
/**
* Filter the lines from this Reader, and return a Writable which can be
* used to stream the filtered lines to a destination. The closure should
* return <code>true</code> if the line should be passed to the writer.
*
* @param reader this reader
* @param closure a closure used for filtering
* @return a Writable which will use the closure to filter each line
* from the reader when the Writable#writeTo(Writer) is called.
* @since 1.0
*/
public static Writable filterLine(Reader reader, @ClosureParams(value=SimpleType.class, options="java.lang.String") final Closure closure) {
final BufferedReader br = new BufferedReader(reader);
return new Writable() {
public Writer writeTo(Writer out) throws IOException {
BufferedWriter bw = new BufferedWriter(out);
String line;
BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
while ((line = br.readLine()) != null) {
if (bcw.call(line)) {
bw.write(line);
bw.newLine();
}
}
bw.flush();
return out;
}
public String toString() {
Writer buffer = new StringBuilderWriter();
try {
writeTo(buffer);
} catch (IOException e) {
throw new StringWriterIOException(e);
}
return buffer.toString();
}
};
}
示例13: write
import groovy.lang.Writable; //导入依赖的package包/类
/**
* Writes an object to a Writer using Groovy's default representation for the object.
*/
public static void write(Writer out, Object object) throws IOException {
if (object instanceof String) {
out.write((String) object);
} else if (object instanceof Object[]) {
out.write(toArrayString((Object[]) object));
} else if (object instanceof Map) {
out.write(toMapString((Map) object));
} else if (object instanceof Collection) {
out.write(toListString((Collection) object));
} else if (object instanceof Writable) {
Writable writable = (Writable) object;
writable.writeTo(out);
} else if (object instanceof InputStream || object instanceof Reader) {
// Copy stream to stream
Reader reader;
if (object instanceof InputStream) {
reader = new InputStreamReader((InputStream) object);
} else {
reader = (Reader) object;
}
char[] chars = new char[8192];
int i;
while ((i = reader.read(chars)) != -1) {
out.write(chars, 0, i);
}
reader.close();
} else {
out.write(toString(object));
}
}
示例14: append
import groovy.lang.Writable; //导入依赖的package包/类
/**
* Appends an object to an Appendable using Groovy's default representation for the object.
*/
public static void append(Appendable out, Object object) throws IOException {
if (object instanceof String) {
out.append((String) object);
} else if (object instanceof Object[]) {
out.append(toArrayString((Object[]) object));
} else if (object instanceof Map) {
out.append(toMapString((Map) object));
} else if (object instanceof Collection) {
out.append(toListString((Collection) object));
} else if (object instanceof Writable) {
Writable writable = (Writable) object;
Writer stringWriter = new StringBuilderWriter();
writable.writeTo(stringWriter);
out.append(stringWriter.toString());
} else if (object instanceof InputStream || object instanceof Reader) {
// Copy stream to stream
Reader reader;
if (object instanceof InputStream) {
reader = new InputStreamReader((InputStream) object);
} else {
reader = (Reader) object;
}
char[] chars = new char[8192];
int i;
while ((i = reader.read(chars)) != -1) {
for (int j = 0; j < i; j++) {
out.append(chars[j]);
}
}
reader.close();
} else {
out.append(toString(object));
}
}
示例15: evaluate
import groovy.lang.Writable; //导入依赖的package包/类
@Override
public void evaluate(Writer output, Map<String, Object> binding) throws GrafikonException {
if (templateGString == null) {
initialize();
}
Writable result = templateGString.make(binding);
try {
result.writeTo(output);
output.flush();
} catch (IOException e) {
throw new GrafikonException("Error writing output.", e);
}
}