001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.pattern;
019
020import org.apache.log4j.spi.LoggingEvent;
021
022import java.util.ArrayList;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Map;
026
027
028/**
029 * The class implements the pre log4j 1.3 org.apache.log4j.helpers.PatternConverter
030 * contract by delegating to the log4j 1.3 pattern implementation.
031 *
032 *
033 * @author Curt Arnold
034 *
035 */
036public final class BridgePatternConverter
037  extends org.apache.log4j.helpers.PatternConverter {
038  /**
039   * Pattern converters.
040   */
041  private LoggingEventPatternConverter[] patternConverters;
042
043  /**
044   * Field widths and alignment corresponding to pattern converters.
045   */
046  private FormattingInfo[] patternFields;
047
048  /**
049   * Does pattern process exceptions.
050   */
051  private boolean handlesExceptions;
052
053  /**
054   * Create a new instance.
055   * @param pattern pattern, may not be null.
056   */
057  public BridgePatternConverter(
058    final String pattern) {
059    next = null;
060    handlesExceptions = false;
061
062    List converters = new ArrayList();
063    List fields = new ArrayList();
064    Map converterRegistry = null;
065
066    PatternParser.parse(
067      pattern, converters, fields, converterRegistry,
068      PatternParser.getPatternLayoutRules());
069
070    patternConverters = new LoggingEventPatternConverter[converters.size()];
071    patternFields = new FormattingInfo[converters.size()];
072
073    int i = 0;
074    Iterator converterIter = converters.iterator();
075    Iterator fieldIter = fields.iterator();
076
077    while (converterIter.hasNext()) {
078      Object converter = converterIter.next();
079
080      if (converter instanceof LoggingEventPatternConverter) {
081        patternConverters[i] = (LoggingEventPatternConverter) converter;
082        handlesExceptions |= patternConverters[i].handlesThrowable();
083      } else {
084        patternConverters[i] =
085          new org.apache.log4j.pattern.LiteralPatternConverter("");
086      }
087
088      if (fieldIter.hasNext()) {
089        patternFields[i] = (FormattingInfo) fieldIter.next();
090      } else {
091        patternFields[i] = FormattingInfo.getDefault();
092      }
093
094      i++;
095    }
096  }
097
098  /**
099   * {@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}