View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2023, 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.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              // If at least one of file header or presentation header were not
67              // null, then append a line separator.
68              // This should be useful in most cases and should not hurt.
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 }