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;
025import java.util.concurrent.atomic.AtomicReference;
026
027import org.apache.log4j.Layout;
028import org.apache.log4j.bridge.LayoutWrapper;
029import org.apache.log4j.builders.AbstractBuilder;
030import org.apache.log4j.config.PropertiesConfiguration;
031import org.apache.log4j.xml.XmlConfiguration;
032import org.apache.logging.log4j.Logger;
033import org.apache.logging.log4j.core.config.plugins.Plugin;
034import org.apache.logging.log4j.core.layout.HtmlLayout;
035import org.apache.logging.log4j.status.StatusLogger;
036import org.w3c.dom.Element;
037
038/**
039 * Build a Pattern Layout
040 */
041@Plugin(name = "org.apache.log4j.HTMLLayout", category = CATEGORY)
042public class HtmlLayoutBuilder extends AbstractBuilder implements LayoutBuilder {
043
044    private static final Logger LOGGER = StatusLogger.getLogger();
045
046    private static final String TITLE = "Title";
047    private static final String LOCATION_INFO = "LocationInfo";
048
049    public HtmlLayoutBuilder() {
050    }
051
052    public HtmlLayoutBuilder(String prefix, Properties props) {
053        super(prefix, props);
054    }
055
056
057    @Override
058    public Layout parseLayout(Element layoutElement, XmlConfiguration config) {
059        final AtomicReference<String> title = new AtomicReference<>();
060        final AtomicBoolean locationInfo = new AtomicBoolean();
061        forEachElement(layoutElement.getElementsByTagName("param"), currentElement -> {
062            if (currentElement.getTagName().equals(PARAM_TAG)) {
063                if (TITLE.equalsIgnoreCase(currentElement.getAttribute("name"))) {
064                    title.set(currentElement.getAttribute("value"));
065                } else if (LOCATION_INFO.equalsIgnoreCase(currentElement.getAttribute("name"))) {
066                    locationInfo.set(Boolean.parseBoolean(currentElement.getAttribute("value")));
067                }
068            }
069        });
070        return createLayout(title.get(), locationInfo.get());
071    }
072
073    @Override
074    public Layout parseLayout(PropertiesConfiguration config) {
075        String title = getProperty(TITLE);
076        boolean locationInfo = getBooleanProperty(LOCATION_INFO);
077        return createLayout(title, locationInfo);
078    }
079
080    private Layout createLayout(String title, boolean locationInfo) {
081        return new LayoutWrapper(HtmlLayout.newBuilder()
082                .withTitle(title)
083                .withLocationInfo(locationInfo)
084                .build());
085    }
086}