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.chainsaw.layout;
    19
    20  import java.io.BufferedReader;
    21  import java.io.InputStreamReader;
    22  import java.net.URL;
    23
    24  import org.apache.log4j.LogManager;
    25  import org.apache.log4j.PatternLayout;
    26
    27
    28  /**
    29   * Factory class to load and cache Layout information from resources.
    30   *
    31   * @author Paul Smith &lt;psmith@apache.org&gt;
    32   */
    33  public class DefaultLayoutFactory {
    34    private volatile static String defaultPatternLayout = null;
    35
    36    private DefaultLayoutFactory() {
    37    }
    38
    39    public static String getDefaultPatternLayout() {
    40        return getPatternLayout("org/apache/log4j/chainsaw/layout/DefaultDetailLayout.html");
    41    }
    42    public static String getFullPatternLayout() {
    43        return getPatternLayout("org/apache/log4j/chainsaw/layout/FullDetailLayout.html");
    44    }
    45
    46    private static String getPatternLayout(String fileNamePath) {
    47        StringBuffer content = new StringBuffer();
    48        URL defaultLayoutURL =
    49          DefaultLayoutFactory.class.getClassLoader().getResource(fileNamePath);
    50
    51        if (defaultLayoutURL == null) {
    52          LogManager.getLogger(DefaultLayoutFactory.class).warn(
    53            "Could not locate the default Layout for Event Details and Tooltips");
    54        } else {
    55          try {
    56            BufferedReader reader = null;
    57
    58            try {
    59              reader =
    60                new BufferedReader(
    61                  new InputStreamReader(defaultLayoutURL.openStream()));
    62
    63              String line = "";
    64
    65              while ((line = reader.readLine()) != null) {
    66                content.append(line).append("\n");
    67              }
    68            } finally {
    69              if (reader != null) {
    70                reader.close();
    71              }
    72            }
    73          } catch (Exception e) {
    74            content = new StringBuffer(PatternLayout.TTCC_CONVERSION_PATTERN);
    75          }
    76          String trimmedContent = content.toString().trim();
    77          //the default docs contain the apache license header, strip that out before displaying
    78          String startComment = "<!--";
    79          String endComment = "-->";
    80          if (trimmedContent.startsWith(startComment)) {
    81              int endIndex = trimmedContent.indexOf(endComment);
    82              if (endIndex > -1) {
    83                  trimmedContent = trimmedContent.substring(endIndex + endComment.length()).trim();
    84              }
    85          }
    86          defaultPatternLayout = trimmedContent;
    87        }
    88
    89      return defaultPatternLayout;
    90    }
    91  }