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