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