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 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
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 }