1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.monitoring.jmx;
18
19 /**
20 * This class creates a HtmlAdapter for an MBeanServer.
21 *
22 * @svnLink $Revision: 3879 $;$Date: 2009-08-04 15:13:46 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/jmx/src/main/java/ch/elca/el4j/services/monitoring/jmx/JmxHtmlFormatter.java $
23 *
24 * @author Rashid Waraich (RWA)
25 */
26 public class JmxHtmlFormatter {
27 /**
28 * Hide default constructor.
29 */
30 protected JmxHtmlFormatter() { }
31
32 /**
33 * This method creates an Html from a two dimentional String array (first
34 * dimention=row, second dimention=colomn).
35 *
36 * @param cells
37 * The cells of the table.
38 * @return The HTML code of the table.
39 */
40 public static String getHtmlTable(String[][] cells) {
41 String result = "";
42
43 // write headings
44 result = result.concat("<table border=\"1\"><tr>");
45 for (int i = 0; i < cells[0].length; i++) {
46 result = result.concat("<th>" + cells[0][i] + "</th>");
47 }
48 result = result.concat("</tr>");
49
50 // write table content
51 for (int i = 1; i < cells.length; i++) {
52 result = result.concat("<tr>");
53 for (int j = 0; j < cells[i].length; j++) {
54 result = result.concat("<td>" + cells[i][j] + "</td>");
55 }
56 result = result.concat("</tr>");
57 }
58 result = result.concat("</table>");
59
60 return result;
61 }
62
63 /**
64 * Create an XML tag for a log4j root element (root Logger) form the level.
65 *
66 * @param level
67 * The level of the root element.
68 * @return An HTML embeddable representation of the root XML element.
69 */
70 public static String getXMLLog4jRootTag(String level) {
71 String result = "";
72
73 result = openTagPre(result);
74 result = concatTab(result);
75 result = result.concat("<root>");
76 result = concatLineBreak(result);
77
78 result = concatdoubleTab(result);
79 result = result.concat("<level value=\"" + level + "\"/>");
80 result = concatLineBreak(result);
81
82 result = concatTab(result);
83 result = result.concat("</root>");
84 result = closeTagPre(result);
85
86 return convertTags(result);
87 }
88
89 /**
90 * This method creates an XML representation of a Logger level change
91 * (suitable for log4j.xml).
92 *
93 * @see Comment of method 'convertTags'.
94 * @param category
95 * The category of the logger.
96 * @param level
97 * The log-level of the logger.
98 * @return An HTML embeddable version of the XML tag.
99 */
100 public static String getXmlLog4jConfigString(
101 String category, String level) {
102
103 String result = "";
104
105 result = openTagPre(result);
106 result = concatTab(result);
107 result = result.concat("<logger name=\"" + category + "\">");
108 result = concatLineBreak(result);
109
110 result = concatdoubleTab(result);
111 result = result.concat("<level value=\"" + level + "\"/>");
112 result = concatLineBreak(result);
113
114 result = concatTab(result);
115 result = result.concat("</logger>");
116 result = closeTagPre(result);
117
118 return convertTags(result);
119 }
120
121 /**
122 * See comments for method 'convertTags'.
123 *
124 * @param input
125 * See comments for method 'convertTags'.
126 * @return See comments for method 'convertTags'.
127 */
128 private static String concatLineBreak(String input) {
129 return input.concat("#br");
130 }
131
132 /**
133 * See comments for method 'convertTags'.
134 *
135 * @param input
136 * See comments for method 'convertTags'.
137 * @return See comments for method 'convertTags'.
138 */
139 private static String concatTab(String input) {
140 return input.concat("#tab1");
141 }
142
143 /**
144 * See comments for method 'convertTags'.
145 *
146 * @param input
147 * See comments for method 'convertTags'.
148 * @return See comments for method 'convertTags'.
149 */
150 private static String concatdoubleTab(String input) {
151 return input.concat("#tab2");
152 }
153
154 /**
155 * See comments for method 'convertTags'.
156 *
157 * @param input
158 * See comments for method 'convertTags'.
159 * @return See comments for method 'convertTags'.
160 */
161 private static String openTagPre(String input) {
162 return input.concat("#pre_open");
163 }
164
165 /**
166 * See comments for method 'convertTags'.
167 *
168 * @param input
169 * See comments for method 'convertTags'.
170 * @return See comments for method 'convertTags'.
171 */
172 private static String closeTagPre(String input) {
173 return input.concat("#pre_close");
174 }
175
176 /**
177 * The 'getXmlLog4jConfigString' should return XML, which is embeddable in
178 * HTML. For this reason, the XML tag symbols '<' and '>' are converted to
179 * '<' resp. '>'. As these two symbols are also used in HTML tags,
180 * they are enconded through special methods (concatLineBreak,
181 * concatTab,...). In this method they are again converted back to the
182 * original tag.
183 *
184 * @param input
185 * Input String from getXmlLog4jConfigString.
186 * @return HTML embeddable XML.
187 */
188 private static String convertTags(String input) {
189 String output = input;
190 output = output.replaceAll("<", "<");
191 output = output.replaceAll(">", ">");
192
193 output = output.replaceAll("#br", "<br>");
194 output = output.replaceAll("#tab1", "	");
195 output = output.replaceAll("#tab2", "		");
196 output = output.replaceAll("#pre_open", "<pre>");
197 output = output.replaceAll("#pre_close", "</pre>");
198 return output;
199 }
200 }