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  
26  import org.apache.log4j.Layout;
27  import org.apache.log4j.bridge.LayoutWrapper;
28  import org.apache.log4j.builders.AbstractBuilder;
29  import org.apache.log4j.config.PropertiesConfiguration;
30  import org.apache.log4j.xml.XmlConfiguration;
31  import org.apache.logging.log4j.Logger;
32  import org.apache.logging.log4j.core.config.plugins.Plugin;
33  import org.apache.logging.log4j.core.layout.XmlLayout;
34  import org.apache.logging.log4j.status.StatusLogger;
35  import org.w3c.dom.Element;
36  
37  /**
38   * Build an XML Layout
39   */
40  @Plugin(name = "org.apache.log4j.xml.XMLLayout", category = CATEGORY)
41  public class XmlLayoutBuilder extends AbstractBuilder implements LayoutBuilder {
42  
43      private static final Logger LOGGER = StatusLogger.getLogger();
44  
45      private static final String LOCATION_INFO = "LocationInfo";
46      private static final String PROPERTIES = "Properties";
47  
48      public XmlLayoutBuilder() {
49      }
50  
51      public XmlLayoutBuilder(String prefix, Properties props) {
52          super(prefix, props);
53      }
54  
55  
56      @Override
57      public Layout parseLayout(Element layoutElement, XmlConfiguration config) {
58          final AtomicBoolean properties = new AtomicBoolean();
59          final AtomicBoolean locationInfo = new AtomicBoolean();
60          forEachElement(layoutElement.getElementsByTagName(PARAM_TAG), currentElement -> {
61              if (PROPERTIES.equalsIgnoreCase(currentElement.getAttribute("name"))) {
62                  properties.set(Boolean.parseBoolean(currentElement.getAttribute("value")));
63              } else if (LOCATION_INFO.equalsIgnoreCase(currentElement.getAttribute("name"))) {
64                  locationInfo.set(Boolean.parseBoolean(currentElement.getAttribute("value")));
65              }
66          });
67          return createLayout(properties.get(), locationInfo.get());
68      }
69  
70      @Override
71      public Layout parseLayout(PropertiesConfiguration config) {
72          boolean properties = getBooleanProperty(PROPERTIES);
73          boolean locationInfo = getBooleanProperty(LOCATION_INFO);
74          return createLayout(properties, locationInfo);
75      }
76  
77      private Layout createLayout(boolean properties, boolean locationInfo) {
78          return new LayoutWrapper(XmlLayout.newBuilder()
79                  .setLocationInfo(locationInfo)
80                  .setProperties(properties)
81                  .build());
82      }
83  }