View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.pattern;
    19
    20
    21  /**
    22
    23     <p>PatternConverter is an abstract class that provides the
    24     formatting functionality that derived classes need.
    25
    26     <p>Conversion specifiers in a conversion patterns are parsed to
    27     individual PatternConverters. Each of which is responsible for
    28     converting an object in a converter specific manner.
    29
    30     @author <a href="mailto:cakalijp@Maritz.com">James P. Cakalic</a>
    31     @author Ceki G&uuml;lc&uuml;
    32     @author Chris Nokes
    33     @author Curt Arnold
    34
    35   */
    36  public abstract class PatternConverter {
    37    /**
    38     * Converter name.
    39     */
    40    private final String name;
    41
    42    /**
    43     * Converter style name.
    44     */
    45    private final String style;
    46
    47    /**
    48     * Create a new pattern converter.
    49     * @param name name for pattern converter.
    50     * @param style CSS style for formatted output.
    51     */
    52    protected PatternConverter(final String name, final String style) {
    53      this.name = name;
    54      this.style = style;
    55    }
    56
    57    /**
    58     * Formats an object into a string buffer.
    59     * @param obj event to format, may not be null.
    60     * @param toAppendTo string buffer to which the formatted event will be appended.  May not be null.
    61     */
    62    public abstract void format(final Object obj, final StringBuffer toAppendTo);
    63
    64    /**
    65     * This method returns the name of the conversion pattern.
    66     *
    67     * The name can be useful to certain Layouts such as HTMLLayout.
    68     *
    69     * @return        the name of the conversion pattern
    70     */
    71    public final String getName() {
    72      return name;
    73    }
    74
    75    /**
    76     * This method returns the CSS style class that should be applied to
    77     * the LoggingEvent passed as parameter, which can be null.
    78     *
    79     * This information is currently used only by HTMLLayout.
    80     *
    81     * @param e null values are accepted
    82     * @return  the name of the conversion pattern
    83     */
    84    public String getStyleClass(Object e) {
    85      return style;
    86    }
    87  }