View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.encoder;
15  
16  import java.nio.charset.Charset;
17  
18  import ch.qos.logback.core.CoreConstants;
19  
20  public class DummyEncoder<E> extends EncoderBase<E> {
21  
22      public static final String DUMMY = "dummy" + CoreConstants.LINE_SEPARATOR;
23      String val = DUMMY;
24      String fileHeader;
25      String fileFooter;
26      Charset charset;
27  
28      public Charset getCharset() {
29          return charset;
30      }
31  
32      public void setCharset(Charset charset) {
33          this.charset = charset;
34      }
35  
36      public DummyEncoder() {
37      }
38  
39      public DummyEncoder(String val) {
40          this.val = val;
41      }
42  
43      public byte[] encode(E event) {
44          return encodeString(val);
45      }
46  
47      byte[] encodeString(String s) {
48          if (charset == null) {
49              return s.getBytes();
50          } else {
51              return s.getBytes(charset);
52          }
53      }
54  
55      private void appendIfNotNull(StringBuilder sb, String s) {
56          if (s != null) {
57              sb.append(s);
58          }
59      }
60  
61      byte[] header() {
62          StringBuilder sb = new StringBuilder();
63          appendIfNotNull(sb, fileHeader);
64          if (sb.length() > 0) {
65              // If at least one of file header or presentation header were not
66              // null, then append a line separator.
67              // This should be useful in most cases and should not hurt.
68              sb.append(CoreConstants.LINE_SEPARATOR);
69          }
70          return encodeString(sb.toString());
71      }
72  
73      public byte[] headerBytes() {
74          return header();
75      }
76  
77      public byte[] footerBytes() {
78          if (fileFooter == null) {
79              return null;
80          }
81          return encodeString(fileFooter);
82      }
83  
84      public String getFileHeader() {
85          return fileHeader;
86      }
87  
88      public void setFileHeader(String fileHeader) {
89          this.fileHeader = fileHeader;
90      }
91  
92      public String getFileFooter() {
93          return fileFooter;
94      }
95  
96      public void setFileFooter(String fileFooter) {
97          this.fileFooter = fileFooter;
98      }
99  
100 }