1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }