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  package org.apache.log4j.builders.layout;
18  
19  import static org.apache.log4j.builders.BuilderManager.CATEGORY;
20  import static org.apache.log4j.xml.XmlConfiguration.PARAM_TAG;
21  import static org.apache.log4j.xml.XmlConfiguration.forEachElement;
22  
23  import java.util.Properties;
24  import java.util.concurrent.atomic.AtomicBoolean;
25  import java.util.concurrent.atomic.AtomicReference;
26  
27  import org.apache.log4j.Layout;
28  import org.apache.log4j.bridge.LayoutWrapper;
29  import org.apache.log4j.builders.AbstractBuilder;
30  import org.apache.log4j.config.PropertiesConfiguration;
31  import org.apache.log4j.xml.XmlConfiguration;
32  import org.apache.logging.log4j.Logger;
33  import org.apache.logging.log4j.core.config.plugins.Plugin;
34  import org.apache.logging.log4j.core.layout.HtmlLayout;
35  import org.apache.logging.log4j.status.StatusLogger;
36  import org.w3c.dom.Element;
37  
38  /**
39   * Build a Pattern Layout
40   */
41  @Plugin(name = "org.apache.log4j.HTMLLayout", category = CATEGORY)
42  public class HtmlLayoutBuilder extends AbstractBuilder implements LayoutBuilder {
43  
44      private static final Logger LOGGER = StatusLogger.getLogger();
45  
46      private static final String TITLE = "Title";
47      private static final String LOCATION_INFO = "LocationInfo";
48  
49      public HtmlLayoutBuilder() {
50      }
51  
52      public HtmlLayoutBuilder(String prefix, Properties props) {
53          super(prefix, props);
54      }
55  
56  
57      @Override
58      public Layout parseLayout(Element layoutElement, XmlConfiguration config) {
59          final AtomicReference<String> title = new AtomicReference<>();
60          final AtomicBoolean locationInfo = new AtomicBoolean();
61          forEachElement(layoutElement.getElementsByTagName("param"), currentElement -> {
62              if (currentElement.getTagName().equals(PARAM_TAG)) {
63                  if (TITLE.equalsIgnoreCase(currentElement.getAttribute("name"))) {
64                      title.set(currentElement.getAttribute("value"));
65                  } else if (LOCATION_INFO.equalsIgnoreCase(currentElement.getAttribute("name"))) {
66                      locationInfo.set(Boolean.parseBoolean(currentElement.getAttribute("value")));
67                  }
68              }
69          });
70          return createLayout(title.get(), locationInfo.get());
71      }
72  
73      @Override
74      public Layout parseLayout(PropertiesConfiguration config) {
75          String title = getProperty(TITLE);
76          boolean locationInfo = getBooleanProperty(LOCATION_INFO);
77          return createLayout(title, locationInfo);
78      }
79  
80      private Layout createLayout(String title, boolean locationInfo) {
81          return new LayoutWrapper(HtmlLayout.newBuilder()
82                  .withTitle(title)
83                  .withLocationInfo(locationInfo)
84                  .build());
85      }
86  }