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  import org.apache.log4j.spi.LoggingEvent;
    21
    22  import java.util.ArrayList;
    23  import java.util.Iterator;
    24  import java.util.List;
    25  import java.util.Map;
    26
    27
    28  /**
    29   * The class implements the pre log4j 1.3 org.apache.log4j.helpers.PatternConverter
    30   * contract by delegating to the log4j 1.3 pattern implementation.
    31   *
    32   *
    33   * @author Curt Arnold
    34   *
    35   */
    36  public final class BridgePatternConverter
    37    extends org.apache.log4j.helpers.PatternConverter {
    38    /**
    39     * Pattern converters.
    40     */
    41    private LoggingEventPatternConverter[] patternConverters;
    42
    43    /**
    44     * Field widths and alignment corresponding to pattern converters.
    45     */
    46    private FormattingInfo[] patternFields;
    47
    48    /**
    49     * Does pattern process exceptions.
    50     */
    51    private boolean handlesExceptions;
    52
    53    /**
    54     * Create a new instance.
    55     * @param pattern pattern, may not be null.
    56     */
    57    public BridgePatternConverter(
    58      final String pattern) {
    59      next = null;
    60      handlesExceptions = false;
    61
    62      List converters = new ArrayList();
    63      List fields = new ArrayList();
    64      Map converterRegistry = null;
    65
    66      PatternParser.parse(
    67        pattern, converters, fields, converterRegistry,
    68        PatternParser.getPatternLayoutRules());
    69
    70      patternConverters = new LoggingEventPatternConverter[converters.size()];
    71      patternFields = new FormattingInfo[converters.size()];
    72
    73      int i = 0;
    74      Iterator converterIter = converters.iterator();
    75      Iterator fieldIter = fields.iterator();
    76
    77      while (converterIter.hasNext()) {
    78        Object converter = converterIter.next();
    79
    80        if (converter instanceof LoggingEventPatternConverter) {
    81          patternConverters[i] = (LoggingEventPatternConverter) converter;
    82          handlesExceptions |= patternConverters[i].handlesThrowable();
    83        } else {
    84          patternConverters[i] =
    85            new org.apache.log4j.pattern.LiteralPatternConverter("");
    86        }
    87
    88        if (fieldIter.hasNext()) {
    89          patternFields[i] = (FormattingInfo) fieldIter.next();
    90        } else {
    91          patternFields[i] = FormattingInfo.getDefault();
    92        }
    93
    94        i++;
    95      }
    96    }
    97
    98    /**
    99     * {@inheritDoc}
    100    */
    101   protected String convert(final LoggingEvent event) {
    102     //
    103     //  code should be unreachable.
    104     //
    105     StringBuffer sbuf = new StringBuffer();
    106     format(sbuf, event);
    107
    108     return sbuf.toString();
    109   }
    110
    111   /**
    112      Format event to string buffer.
    113      @param sbuf string buffer to receive formatted event, may not be null.
    114      @param e event to format, may not be null.
    115    */
    116   public void format(final StringBuffer sbuf, final LoggingEvent e) {
    117     for (int i = 0; i < patternConverters.length; i++) {
    118       int startField = sbuf.length();
    119       patternConverters[i].format(e, sbuf);
    120       patternFields[i].format(startField, sbuf);
    121     }
    122   }
    123
    124   /**
    125    * Will return false if any of the conversion specifiers in the pattern
    126    * handles {@link Exception Exceptions}.
    127    * @return true if the pattern formats any information from exceptions.
    128    */
    129   public boolean ignoresThrowable() {
    130     return !handlesExceptions;
    131   }
    132 }