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.logging.log4j.taglib;
018
019import java.io.IOException;
020import java.util.Enumeration;
021
022import javax.servlet.jsp.JspException;
023import javax.servlet.jsp.PageContext;
024import javax.servlet.jsp.tagext.Tag;
025import javax.servlet.jsp.tagext.TagSupport;
026
027/**
028 * This class implements the {@code <log:dump>} tag.
029 *
030 * @since 2.0
031 */
032public class DumpTag extends TagSupport {
033    private static final long serialVersionUID = 1L;
034
035    private int scope;
036
037    public DumpTag() {
038        super();
039        init();
040    }
041
042    @Override
043    public void release() {
044        super.release();
045        this.init();
046    }
047
048    private void init() {
049        this.scope = PageContext.PAGE_SCOPE;
050    }
051
052    public void setScope(final String scope) {
053        this.scope = TagUtils.getScope(scope);
054    }
055
056    @Override
057    public int doEndTag() throws JspException {
058        try {
059            final Enumeration<String> names = this.pageContext.getAttributeNamesInScope(this.scope);
060            this.pageContext.getOut().write("<dl>");
061            while (names != null && names.hasMoreElements()) {
062                final String name = names.nextElement();
063                final Object value = this.pageContext.getAttribute(name, this.scope);
064
065                this.pageContext.getOut().write("<dt><code>" + name + "</code></dt>");
066                this.pageContext.getOut().write("<dd><code>" + value + "</code></dd>");
067            }
068            this.pageContext.getOut().write("</dl>");
069        } catch (final IOException e) {
070            throw new JspException("Could not write scope contents. Cause:  " + e.toString(), e);
071        }
072
073        return Tag.EVAL_PAGE;
074    }
075}