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