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