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  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.Tag;
24  import javax.servlet.jsp.tagext.TagSupport;
25  
26  /**
27   * This class implements the {@code <log:dump>} tag.
28   *
29   * @since 2.0
30   */
31  public class DumpTag extends TagSupport {
32      private static final long serialVersionUID = 1L;
33  
34      private int scope;
35  
36      public DumpTag() {
37          super();
38          init();
39      }
40  
41      @Override
42      public void release() {
43          super.release();
44          this.init();
45      }
46  
47      private void init() {
48          this.scope = PageContext.PAGE_SCOPE;
49      }
50  
51      public void setScope(final String scope) {
52          this.scope = TagUtils.getScope(scope);
53      }
54  
55      @Override
56      public int doEndTag() throws JspException {
57          try {
58              final Enumeration<String> names = this.pageContext.getAttributeNamesInScope(this.scope);
59              this.pageContext.getOut().write("<dl>");
60              while (names != null && names.hasMoreElements()) {
61                  final String name = names.nextElement();
62                  final Object value = this.pageContext.getAttribute(name, this.scope);
63  
64                  this.pageContext.getOut().write("<dt><code>" + name + "</code></dt>");
65                  this.pageContext.getOut().write("<dd><code>" + value + "</code></dd>");
66              }
67              this.pageContext.getOut().write("</dl>");
68          } catch (final IOException e) {
69              throw new JspException("Could not write scope contents. Cause:  " + e.toString(), e);
70          }
71  
72          return Tag.EVAL_PAGE;
73      }
74  }