View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache license, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License. You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the license for the specific language governing permissions and
    15   * limitations under the license.
    16   */
    17  package org.apache.logging.log4j.taglib;
    18
    19  import java.io.IOException;
    20  import java.util.Enumeration;
    21
    22  import javax.servlet.jsp.JspException;
    23  import javax.servlet.jsp.PageContext;
    24  import javax.servlet.jsp.tagext.Tag;
    25  import javax.servlet.jsp.tagext.TagSupport;
    26
    27  /**
    28   * This class implements the {@code <log:dump>} tag.
    29   *
    30   * @since 2.0
    31   */
    32  public class DumpTag extends TagSupport {
    33      private static final long serialVersionUID = 1L;
    34
    35      private int scope;
    36
    37      public DumpTag() {
    38          super();
    39          init();
    40      }
    41
    42      @Override
    43      public void release() {
    44          super.release();
    45          this.init();
    46      }
    47
    48      private void init() {
    49          this.scope = PageContext.PAGE_SCOPE;
    50      }
    51
    52      public void setScope(final String scope) {
    53          this.scope = TagUtils.getScope(scope);
    54      }
    55
    56      @Override
    57      public int doEndTag() throws JspException {
    58          try {
    59              final Enumeration<String> names = this.pageContext.getAttributeNamesInScope(this.scope);
    60              this.pageContext.getOut().write("<dl>");
    61              while (names != null && names.hasMoreElements()) {
    62                  final String name = names.nextElement();
    63                  final Object value = this.pageContext.getAttribute(name, this.scope);
    64
    65                  this.pageContext.getOut().write("<dt><code>" + name + "</code></dt>");
    66                  this.pageContext.getOut().write("<dd><code>" + value + "</code></dd>");
    67              }
    68              this.pageContext.getOut().write("</dl>");
    69          } catch (final IOException e) {
    70              throw new JspException("Could not write scope contents. Cause:  " + e.toString(), e);
    71          }
    72
    73          return Tag.EVAL_PAGE;
    74      }
    75  }