001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.encoder;
015
016import java.nio.charset.Charset;
017
018import ch.qos.logback.core.CoreConstants;
019
020public class DummyEncoder<E> extends EncoderBase<E> {
021
022    public static final String DUMMY = "dummy" + CoreConstants.LINE_SEPARATOR;
023    String val = DUMMY;
024    String fileHeader;
025    String fileFooter;
026    Charset charset;
027
028    public Charset getCharset() {
029        return charset;
030    }
031
032    public void setCharset(Charset charset) {
033        this.charset = charset;
034    }
035
036    public DummyEncoder() {
037    }
038
039    public DummyEncoder(String val) {
040        this.val = val;
041    }
042
043    public byte[] encode(E event)  {
044        return encodeString(val);
045    }
046
047    byte[] encodeString(String s) {
048        if (charset == null) {
049            return s.getBytes();
050        } else {
051            return s.getBytes(charset);
052        }
053    }
054
055    private void appendIfNotNull(StringBuilder sb, String s) {
056        if (s != null) {
057            sb.append(s);
058        }
059    }
060
061    byte[] header() {
062        StringBuilder sb = new StringBuilder();
063        appendIfNotNull(sb, fileHeader);
064        if (sb.length() > 0) {
065            // If at least one of file header or presentation header were not
066            // null, then append a line separator.
067            // This should be useful in most cases and should not hurt.
068            sb.append(CoreConstants.LINE_SEPARATOR);
069        }
070        return encodeString(sb.toString());
071    }
072
073    public byte[] headerBytes() {
074        return header();
075    }
076
077    public byte[] footerBytes()  {
078        if (fileFooter == null) {
079            return null;
080        }
081        return encodeString(fileFooter);
082    }
083
084    public String getFileHeader() {
085        return fileHeader;
086    }
087
088    public void setFileHeader(String fileHeader) {
089        this.fileHeader = fileHeader;
090    }
091
092    public String getFileFooter() {
093        return fileFooter;
094    }
095
096    public void setFileFooter(String fileFooter) {
097        this.fileFooter = fileFooter;
098    }
099
100}