0
|
1 package nabble.naml.compiler;
|
|
2
|
|
3 import java.io.StringWriter;
|
|
4 import nabble.model.Init;
|
|
5
|
|
6
|
|
7 class BlockWrapper {
|
|
8 private static final int blockWriterLimit = Init.get("blockWriterLimit",10 * 1024 * 1024);
|
|
9
|
|
10 private final Chunk block;
|
|
11 private final RunState runState;
|
|
12
|
|
13 BlockWrapper(Chunk block,RunState runState) {
|
|
14 this.block = block;
|
|
15 this.runState = runState;
|
|
16 }
|
|
17
|
|
18 private static class BlockWriter extends TemplatePrintWriter {
|
|
19 private final StringWriter sw;
|
|
20 private boolean isNull = false;
|
|
21
|
|
22 BlockWriter(StringWriter sw) {
|
|
23 super(sw);
|
|
24 this.sw = sw;
|
|
25 }
|
|
26
|
|
27 public void print(Object obj) {
|
|
28 if( obj == null && sw.getBuffer().length() == 0 && !isNull ) {
|
|
29 isNull = true;
|
|
30 return;
|
|
31 }
|
|
32 super.print(obj);
|
|
33 checkSize();
|
|
34 }
|
|
35
|
|
36 public void print(String s) {
|
|
37 if( s == null && sw.getBuffer().length() == 0 && !isNull ) {
|
|
38 isNull = true;
|
|
39 return;
|
|
40 }
|
|
41 super.print(s);
|
|
42 checkSize();
|
|
43 }
|
|
44
|
|
45 private void checkSize() {
|
|
46 if( sw.getBuffer().length() > blockWriterLimit )
|
|
47 throw new RuntimeException("BlockWriter too big");
|
|
48 }
|
|
49
|
|
50 String value() {
|
|
51 if( isNull ) {
|
|
52 if( sw.getBuffer().length() > 0 )
|
|
53 throw new NullPointerException("null written to stream");
|
|
54 return null;
|
|
55 }
|
|
56 return sw.toString();
|
|
57 }
|
|
58 }
|
|
59
|
|
60 public final String toString() {
|
|
61 BlockWriter out = new BlockWriter(new StringWriter());
|
|
62 printTo(out);
|
|
63 out.close();
|
|
64 return out.value();
|
|
65 }
|
|
66
|
|
67 void printTo(IPrintWriter out) {
|
|
68 block.run(out,runState);
|
|
69 }
|
|
70 }
|