1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.testUtil;
15
16 import java.nio.charset.Charset;
17
18 import ch.qos.logback.core.CoreConstants;
19 import ch.qos.logback.core.encoder.EncoderBase;
20
21 public class DummyEncoder<E> extends EncoderBase<E> {
22
23 public static final String DUMMY = "dummy" + CoreConstants.LINE_SEPARATOR;
24 String val = DUMMY;
25 String fileHeader;
26 String fileFooter;
27 Charset charset;
28
29 public Charset getCharset() {
30 return charset;
31 }
32
33 public void setCharset(Charset charset) {
34 this.charset = charset;
35 }
36
37 public DummyEncoder() {
38 }
39
40 public DummyEncoder(String val) {
41 this.val = val;
42 }
43
44 public byte[] encode(E event) {
45 return encodeString(val);
46 }
47
48 byte[] encodeString(String s) {
49 if (charset == null) {
50 return s.getBytes();
51 } else {
52 return s.getBytes(charset);
53 }
54 }
55
56 private void appendIfNotNull(StringBuilder sb, String s) {
57 if (s != null) {
58 sb.append(s);
59 }
60 }
61
62 byte[] header() {
63 StringBuilder sb = new StringBuilder();
64 appendIfNotNull(sb, fileHeader);
65 if (sb.length() > 0) {
66
67
68
69 sb.append(CoreConstants.LINE_SEPARATOR);
70 }
71 return encodeString(sb.toString());
72 }
73
74 public byte[] headerBytes() {
75 return header();
76 }
77
78 public byte[] footerBytes() {
79 if (fileFooter == null) {
80 return null;
81 }
82 return encodeString(fileFooter);
83 }
84
85 public String getFileHeader() {
86 return fileHeader;
87 }
88
89 public void setFileHeader(String fileHeader) {
90 this.fileHeader = fileHeader;
91 }
92
93 public String getFileFooter() {
94 return fileFooter;
95 }
96
97 public void setFileFooter(String fileFooter) {
98 this.fileFooter = fileFooter;
99 }
100
101 }