001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.log4j.builders.layout;
018
019import static org.apache.log4j.builders.BuilderManager.CATEGORY;
020import static org.apache.log4j.xml.XmlConfiguration.PARAM_TAG;
021import static org.apache.log4j.xml.XmlConfiguration.forEachElement;
022
023import java.util.Properties;
024import java.util.concurrent.atomic.AtomicBoolean;
025
026import org.apache.log4j.Layout;
027import org.apache.log4j.bridge.LayoutWrapper;
028import org.apache.log4j.builders.AbstractBuilder;
029import org.apache.log4j.config.PropertiesConfiguration;
030import org.apache.log4j.xml.XmlConfiguration;
031import org.apache.logging.log4j.Logger;
032import org.apache.logging.log4j.core.config.plugins.Plugin;
033import org.apache.logging.log4j.core.layout.XmlLayout;
034import org.apache.logging.log4j.status.StatusLogger;
035import org.w3c.dom.Element;
036
037/**
038 * Build an XML Layout
039 */
040@Plugin(name = "org.apache.log4j.xml.XMLLayout", category = CATEGORY)
041public class XmlLayoutBuilder extends AbstractBuilder implements LayoutBuilder {
042
043    private static final Logger LOGGER = StatusLogger.getLogger();
044
045    private static final String LOCATION_INFO = "LocationInfo";
046    private static final String PROPERTIES = "Properties";
047
048    public XmlLayoutBuilder() {
049    }
050
051    public XmlLayoutBuilder(String prefix, Properties props) {
052        super(prefix, props);
053    }
054
055
056    @Override
057    public Layout parseLayout(Element layoutElement, XmlConfiguration config) {
058        final AtomicBoolean properties = new AtomicBoolean();
059        final AtomicBoolean locationInfo = new AtomicBoolean();
060        forEachElement(layoutElement.getElementsByTagName(PARAM_TAG), currentElement -> {
061            if (PROPERTIES.equalsIgnoreCase(currentElement.getAttribute("name"))) {
062                properties.set(Boolean.parseBoolean(currentElement.getAttribute("value")));
063            } else if (LOCATION_INFO.equalsIgnoreCase(currentElement.getAttribute("name"))) {
064                locationInfo.set(Boolean.parseBoolean(currentElement.getAttribute("value")));
065            }
066        });
067        return createLayout(properties.get(), locationInfo.get());
068    }
069
070    @Override
071    public Layout parseLayout(PropertiesConfiguration config) {
072        boolean properties = getBooleanProperty(PROPERTIES);
073        boolean locationInfo = getBooleanProperty(LOCATION_INFO);
074        return createLayout(properties, locationInfo);
075    }
076
077    private Layout createLayout(boolean properties, boolean locationInfo) {
078        return new LayoutWrapper(XmlLayout.newBuilder()
079                .setLocationInfo(locationInfo)
080                .setProperties(properties)
081                .build());
082    }
083}