1 /*
2  * Copyright 2008-2019 by Emeric Vernat
3  *
4  *     This file is part of Java Melody.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.bull.javamelody.internal.web.html;
19
20 import java.io.IOException;
21 import java.io.Writer;
22 import java.util.List;
23
24 import net.bull.javamelody.internal.model.JndiBinding;
25
26 /**
27  * Partie du rapport html pour l'arbre JNDI.
28  * @author Emeric Vernat
29  */

30 class HtmlJndiTreeReport extends HtmlAbstractReport {
31     private final List<JndiBinding> jndiBindings;
32     private final String path;
33
34     HtmlJndiTreeReport(List<JndiBinding> jndiBindings, String path, Writer writer) {
35         super(writer);
36         assert jndiBindings != null;
37
38         this.jndiBindings = jndiBindings;
39         this.path = JndiBinding.normalizePath(path);
40     }
41
42     @Override
43     void toHtml() throws IOException {
44         writeLinks();
45         writeln("<br/>");
46
47         final String title;
48         if (path.isEmpty()) {
49             title = getString("Arbre_JNDI");
50         } else {
51             title = getFormattedString("Arbre_JNDI_pour_contexte", htmlEncode(path));
52         }
53         writeTitle("jndi.png", title);
54         writeTable();
55     }
56
57     private void writeTable() throws IOException {
58         final HtmlTable table = new HtmlTable();
59         table.beginTable(getString("Arbre_JNDI"));
60         write("<th>#Nom#</th><th>#Type#</th><th>#Value#</th>");
61         for (final JndiBinding binding : jndiBindings) {
62             table.nextRow();
63             writeBinding(binding);
64         }
65         table.endTable();
66     }
67
68     private void writeBinding(JndiBinding binding) throws IOException {
69         write("<td>");
70         final String name = binding.getName();
71         final String className = binding.getClassName();
72         final String contextPath = binding.getContextPath();
73         final String value = binding.getValue();
74         if (contextPath != null) {
75             writeDirectly("<a href=\"?part=jndi&amp;path=" + urlEncode(contextPath) + "\">");
76             writeDirectly("<img width='16' height='16' src='?resource=folder.png' alt='"
77                     + urlEncode(name) + "' />&nbsp;");
78             writeDirectly(htmlEncode(name));
79             writeDirectly("</a>");
80         } else {
81             writeDirectly(htmlEncode(name));
82         }
83         write("</td>");
84         write("<td>");
85         writeDirectly(className != null ? htmlEncode(className) : "&nbsp;");
86         write("</td>");
87         write("<td>");
88         writeDirectly(value != null ? htmlEncodeButNotSpace(value) : "&nbsp;");
89         write("</td>");
90     }
91
92     private void writeLinks() throws IOException {
93         writeln("<div class='noPrint'>");
94         writeln("<a class='back' href=''><img src='?resource=action_back.png' alt='#Retour#'/> #Retour#</a>");
95         writeln("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
96         writeDirectly("<a href='?#systeminfo'>");
97         writeln("<img src='?resource=action_home.png' alt='#Page_principale#'/> #Page_principale#</a>");
98         writeln("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
99         writeln("<a href='?part=jndi&amp;path=" + htmlEncode(path)
100                 + "'><img src='?resource=action_refresh.png' alt='#Actualiser#'/> #Actualiser#</a>");
101         if (isPdfEnabled()) {
102             writeln("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
103             write("<a href='?part=jndi&amp;path=" + htmlEncode(path)
104                     + "&amp;format=pdf' title='#afficher_PDF#'>");
105             write("<img src='?resource=pdf.png' alt='#PDF#'/> #PDF#</a>");
106         }
107         writeln("</div>");
108     }
109 }
110